/****************************************************************************/ #include #include #include #include #include "mpi.h" /****************************************************************************/ // Program 2 outline #define START_LEN 0 #define FINISH_LEN 101000 #define NUM_INCR 101 #define NUM_SAMPLES 1000 #define SLEEP_VAL 1 #define MASTER (myid == 0) /****************************************************************************/ int main(int argc, char **argv) { char *buffer; int count1, count2; int dest, order; long s_val = SLEEP_VAL; int np, myid, p, i, j, start_len, finish_len, inc; double time1, time2, work, t; FILE *out = NULL; MPI_Status mpistat; start_len = START_LEN; finish_len = FINISH_LEN; inc = (finish_len-start_len)/NUM_INCR; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&np); MPI_Comm_rank(MPI_COMM_WORLD,&myid); printf("myid = %d, np = %d\n",myid,np); fflush(stdout); if (MASTER) out = fopen("prog2.out", "w"); if ((myid % 2) == 0) { dest = myid + 1; order = 0; } else { dest = myid - 1; order = 1; } // character array for sending back and forth... buffer = (char *) malloc(sizeof(char)*finish_len); count1 = 1; // loop over pairs of processors... // increase the number of pairs by 2 each time through... for (p = 2; p <= np; p += 2) { count2 = 1; i = start_len; while (i < finish_len) { // sync everyone up... MPI_Barrier(MPI_COMM_WORLD); time1 = MPI_Wtime(); if (myid < p) { for (j = 0; j < NUM_SAMPLES; j++) { if (order == 0) { MPI_Send(buffer,i,MPI_BYTE,dest,11,MPI_COMM_WORLD); MPI_Recv(buffer,i,MPI_BYTE,dest,11,MPI_COMM_WORLD,&mpistat); } else { MPI_Recv(buffer,i,MPI_BYTE,dest,11,MPI_COMM_WORLD,&mpistat); MPI_Send(buffer,i,MPI_BYTE,dest,11,MPI_COMM_WORLD); } } } time2 = MPI_Wtime(); // wait a small amount of time to avoid interrupting // other processes sleep(s_val); // get the avg of the times across the participating procs t = time2-time1; work = t; MPI_Allreduce(&work,&t,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); t /= p; if (MASTER) { int microsec = floor(t/(2*NUM_SAMPLES)*1.0e+6); printf("size = %d bytes, t = %d microsec, %d processors\n", i, microsec, p); fprintf(out,"%d %d %d %d %d\n",i, microsec, p, count1, count2); fflush(stdout); fflush(out); } count2++; // some rule for incrementing message size with inc i += inc; } count1++; if (MASTER) { fprintf(out,"\n"); fflush(out); } } free(buffer); if (MASTER) fclose(out); MPI_Finalize(); return 0; } /****************************************************************************/