#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/times.h> #include <unistd.h> #include "mpi.h" struct timeval tv; struct timezone tz; double wallclocktime(){ static int startflag=1; static double tsecs0, tsecs1, dt; (void) gettimeofday(&tv,&tz); tsecs1 = tv.tv_sec + tv.tv_usec*1.0e-6; return tsecs1; } int main(int argc, char **argv){ char name[BUFSIZ]; int length,procnum,numprocs,size,i=0,n; MPI_Status *recv_status; void *buf; int mark=123; double start,stop,timetot; double bandwith; double nbytetot; MPI_Init(&argc, &argv); /* Find out this host name */ MPI_Get_processor_name(name, &length); /* Find out this processor number */ MPI_Comm_rank(MPI_COMM_WORLD, &procnum); /* Find out the number of processors */ MPI_Comm_size(MPI_COMM_WORLD, &numprocs); size=atoi(argv[1]); n=atoi(argv[2]); buf=malloc(size); if(buf==NULL) printf("Malloc error!!\n"); if((procnum % 2)==0){ MPI_Barrier (MPI_COMM_WORLD); for(i=0;i<n;i++){ MPI_Recv(buf,size, MPI_BYTE,(procnum+1),mark, MPI_COMM_WORLD,recv_status); MPI_Send(buf,size,MPI_BYTE,(procnum+1),mark, MPI_COMM_WORLD); } }else{ MPI_Barrier (MPI_COMM_WORLD); start=wallclocktime(); for(i=0;i<n;i++){ MPI_Send(buf,size,MPI_BYTE,(procnum-1),mark, MPI_COMM_WORLD); MPI_Recv(buf,size, MPI_BYTE,(procnum-1),mark, MPI_COMM_WORLD,recv_status); } stop=wallclocktime(); timetot=(stop-start); nbytetot=( ( (double)size/1024.0 )*n ); bandwith=(nbytetot/timetot); fprintf(stderr,"%d packsize: %d byte, timetot: %.3f sec, nbytetot: %.3f Kbyte, bw: %.3f Kbyte/sec,ping: %d\n", procnum,size,timetot,nbytetot,bandwith,n ); } MPI_Finalize(); }