/**************************************************************************** * MPI Bandwidth Test - C Version * FILE: mpi_bandwidth.c * DESCRIPTION: * This code conducts timing tests on messages sent between two processes. * It begins by asking the user to supply the following parameters: * -starting message size (J) * -finish message size (K) * -message increment size (I) * -number of round trips per iteration (N) * It then sends/receives N roundtrips of incrementally sized messages * from start size J to finish size K by increment I. * AUTHOR: Blaise Barney 12/21/95 * LAST REVISED: ******************************************************************************/ #include "mpi.h" #include #include #include #define MAXSIZE 5000000 #define task0 0 #define task1 1 #define tag0 0 #define tag1 1 int numtasks,rank,n,i,rndtrps,mbytes,startsize,finishsize,incr,ok; float tbytes,ttime; char c[MAXSIZE],host0[35], host1[35]; struct timeval tv1, tv2; MPI_Status Stat; int main(argc,argv) int argc; char *argv[]; { void Startup(); MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /**************************** task 0 ***********************************/ if (rank == task0) { Startup(); MPI_Send(&ok, 1, MPI_INT, task1, tag0, MPI_COMM_WORLD); if (!ok) { MPI_Finalize(); exit(0); } MPI_Send(&startsize, 1, MPI_INT, task1, tag0, MPI_COMM_WORLD); MPI_Send(&finishsize, 1, MPI_INT, task1, tag0, MPI_COMM_WORLD); MPI_Send(&incr, 1, MPI_INT, task1, tag0, MPI_COMM_WORLD); MPI_Send(&rndtrps, 1, MPI_INT, task1, tag0, MPI_COMM_WORLD); for (n=startsize; n<=finishsize; n=n+incr) { ttime = 0.0; mbytes = sizeof(char) * n; for (i=1; i<=rndtrps; i++){ gettimeofday(&tv1, (struct timeval*)0); /* before time */ MPI_Send(&c, n, MPI_CHAR, task1, tag0, MPI_COMM_WORLD); MPI_Recv(&c, n, MPI_CHAR, task1, tag1, MPI_COMM_WORLD, &Stat); gettimeofday(&tv2, (struct timeval*)0); /* after time */ ttime = ttime + ((tv2.tv_sec - tv1.tv_sec) * 1000000) + tv2.tv_usec - tv1.tv_usec; } tbytes = (float)mbytes * 2.0 * (float)rndtrps; printf("%9d %16d\n",mbytes,(int)(tbytes/(ttime/1000000.0))); } } /* end of task 0 */ /**************************** task 1 ************************************/ if (rank == task1) { gethostname(host1,&n); MPI_Send(&host1, 35, MPI_CHAR, task0, tag1, MPI_COMM_WORLD); MPI_Recv(&ok, 1, MPI_INT, task0, tag0, MPI_COMM_WORLD, &Stat); if (!ok) { MPI_Finalize(); exit(0); } MPI_Recv(&startsize, 1, MPI_INT, task0, tag0, MPI_COMM_WORLD, &Stat); MPI_Recv(&finishsize, 1, MPI_INT, task0, tag0, MPI_COMM_WORLD, &Stat); MPI_Recv(&incr, 1, MPI_INT, task0, tag0, MPI_COMM_WORLD, &Stat); MPI_Recv(&rndtrps, 1, MPI_INT, task0, tag0, MPI_COMM_WORLD, &Stat); for (n=startsize; n<=finishsize; n=n+incr) { for (i=1; i<=rndtrps; i++){ MPI_Recv(&c, n, MPI_CHAR, task0, tag0, MPI_COMM_WORLD, &Stat); MPI_Send(&c, n, MPI_CHAR, task0, tag1, MPI_COMM_WORLD); } } } /* end of task 1 */ MPI_Finalize(); } /* end of main */ /*****************************************************************************/ void Startup() { /*****************************************************************************/ gethostname(host0,&n); MPI_Recv(&host1, 35, MPI_CHAR, task1, tag1, MPI_COMM_WORLD, &Stat); for (i=0; i MAXSIZE) || (finishsize > MAXSIZE) || (startsize > finishsize) || (incr > finishsize) || (rndtrps < 1)) { printf("ERROR: Input error(s) encountered! Quitting. Please try again\n"); ok = 0; } if (ok) { printf("\n****** MPI Bandwidth Test ******\n"); printf("start size= %8d\n",startsize); printf("finish size= %8d\n",finishsize); printf("increment size= %8d\n",incr); printf("roundtrips/incr= %8d\n",rndtrps); printf("Using nodes: %s %s\n\n",host0,host1); printf("Message Size Bandwidth (bytes/sec)\n"); } }