#include#include#includetypedef enum CIRCLE_ENTRANCE {N, W, S, E} Entrance;float PROBABILITY44 = {{0.1,0.2,0.5,0.2},{0.2,0.1,0.3,0.4},{0.5,0.1,0.1,0.3},{0.3,0.4,0.2,0.1}};int offset={0,4,8,12};int Choose_Exit(const Entrance entrance_no); int main(int argc,char *argv){int circle15;int new_circle15;int arrival3;int wait_cnt3;int arrival_cnt3;int queue3;int queue_accum3;int i=0;float u=0;int iteration=0;int myid;int numprocs;MPI_Init(&argc, &argv);MPI_Comm_size(MPI_COMM_WORLD, &numprocs);MPI_Comm_rank(MPI_COMM_WORLD, &myid);for(i=0;i<=3;i++) { arrival_cnti=0; wait_cnti=0; queuei=0; queue_accumi=0; }//printf("rest zeros");for(i=0;i<=15;i++) { circlei=-1; }//printf("circlei set to -1");for(i=0;i<=3;i++) { MPI_Bcast(PROBABILITYi,4, MPI_FLOAT,0, MPI_COMM_WORLD); } int requested_iterations = 1000;int f={3,3,4,2};for(iteration=0;iteration<=requested_iterations;iteration++) { printf("iteration no = %d",iteration); //NEW CARS ARRIVE AT ENTRANCES for(i=0;i<=3;i++) { //printf("new cars arrive at entrance"); u=rand()/(float)RAND_MAX; //printf("the random value is %f",u); if(u<=1.0/(float)fi) { arrivali=1; arrival_cnti=arrival_cnti+1; //printf("inside if of arrival"); } else { arrivali=0; //printf("inside else of arrival"); } } //printf("calculation of arrival done"); //CARS INSIDE CIRCLE ADVANCE SIMULTANEOUSLY for(i=0;i<=15;i++) { const int j=(i+1)%16; //printf("i=%d,j=%d,circlei=%d",i,j,circlei); if(circlei==-1 || circlei==j) { //printf("inside if of new_circle"); new_circlej=-1; } else { //printf("inside else of new_circle"); new_circlej=circlei; } } //printf("calculation of circle done"); for(i=0;i<=15;i++) { circlei=new_circlei; } //CARS ENTER CIRCLE for(i=0;i<=3;i++) { //printf("circleoffseti=%d",circleoffseti); if(circleoffseti==-1) //THERE IS SPACE FOR CAR TO ENTER //printf("inside there is a space for car to enter"); { if(queuei>0) //CAR WAITING IN QUEUE ENTERS CIRCLE { //printf(“car waiting in queue enters circle”); queuei=queuei-1; circleoffseti=Choose_Exit(i); } else if(arrivali>0) //NEWLY ARRIVED CAR ENTERS CIRCLE { //printf(“newly arrived car enters circle”); arrivali=0; circleoffseti=Choose_Exit(i); } } //printf(“arrivali=%d”,arrivali); if(arrivali>0) //NEWLY ARRIVED CAR QUEUE UP { //printf(“newly arrived car queues up”); wait_cnti=wait_cnti+1; queuei=queuei+1; } } for(i=0;i<=3;i++) { queue_accumi=queue_accumi+queuei; } //printf("cars enter circle done"); }int tot_arrival_cnt4;int tot_wait_cnt4;int tot_queue_accum4;//int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,MPI_Op op, int root, MPI_Comm comm)//collecting total data from arrival, wait and queueMPI_Reduce(arrival_cnt, tot_arrival_cnt, 4, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);MPI_Reduce(wait_cnt, tot_wait_cnt, 4, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);MPI_Reduce(queue_accum, tot_queue_accum, 4, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);if(myid==0) { printf("results of parallel processing are"); for(i=0;i<=3;i++) { printf("total arrival count%d = %d",i,tot_arrival_cnti); printf("total wait count%d = %d",i,tot_wait_cnti); printf("total queue accumulator%d = %d",i,tot_queue_accumi); } } MPI_Finalize();return 0;} int Choose_Exit(const Entrance entrance_no) { printf("inside function choose exit"); float dist_N, dist_W, dist_S; Entrance exit_no; float RANDOM = 0.0;  RANDOM = (float)rand()/(float)RAND_MAX; dist_N = PROBABILITYentrance_no0; dist_W = dist_N + PROBABILITYentrance_no1; dist_S = dist_W + PROBABILITYentrance_no2; if(RANDOM < dist_N) {      printf("IN EXIT 'N' random = %f",RANDOM); exit_no = N; } else if(dist_N <= RANDOM && RANDOM < dist_W)  { printf("IN EXIT 'W' random = %f",RANDOM); exit_no = W; } else if(dist_W <= RANDOM && RANDOM < dist_S)  { printf("IN EXIT 'S' random = %f",RANDOM); exit_no = S; } else {  printf("IN EXIT 'E' random = %d",RANDOM); exit_no = E; } return offsetexit_no; }


I'm Erica!

Would you like to get a custom essay? How about receiving a customized one?

Check it out