Search code examples
cmpimessage

MPI_Send and MPI_Recv, measuring transfer time of a 1Mb message


I am attempting to send a message of size 1Mb using MPI_Send and MPI_Recv and to measure how long it takes to send that message. Here is my c code.

#include <stdio.h>
#include <mpi.h>
#include <assert.h>
#include <sys/time.h>

int main(int argc,char *argv[])
{

   int rank,p;
   struct timeval t1,t2;

   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
   MPI_Comm_size(MPI_COMM_WORLD,&p);

   printf("my rank=%d\n",rank);
   printf("Rank=%d: number of processes =%d\n",rank,p);

   assert(p>=2);


if(rank==0) {
            int x[255] = { 0 };
            int dest = 7;
            int i = 0;
            while (i<254)
            {
                    x[i] = 255;
                    i++;
            }
            gettimeofday(&t1,NULL);
            MPI_Send(&x[0],255,MPI_INT,dest,1,MPI_COMM_WORLD);
            gettimeofday(&t2,NULL);
            int tSend = (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000;

            printf("Rank=%d: sent message %d to rank %d; Send time %d millisec\n", rank,*x,dest,tSend);
} else
if (rank==7) {
            int y[255]={0};
            MPI_Status status;
            gettimeofday(&t1,NULL);
            MPI_Recv(&y[0],255,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
            gettimeofday(&t2,NULL);
            int tRecv = (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000;
            printf("Rank=%d: received message %d from rank %d; Recv time %d millisec\n",rank,*y,status.MPI_SOURCE,tRecv);
}

MPI_Finalize();
}

This code compiles and runs just fine, but it always says that it completes the send and receive in 0 milliseconds, which isn't possible. I'm guessing that my syntax in sending the array is wrong so I'm just sending 4 bytes or something, but I can't figure it out.

Any help would be appreciated!


Solution

  • Maybe a better way to measure the time is to measure it in microseconds

    (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec
    

    and see if you get any values.