]> Simple MPI performance measurement program example

Simple MPI performance measurement program example


If Cut/Copy and Paste fails, then click here for download.


#include <mpi.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main (int argc, char **argv)
{
  int size, rank, i;
  char *buf;
  double t0, t1;
  MPI_Status status;
  (void) MPI_Init (&argc, &argv);
  (void) MPI_Comm_size ((MPI_Comm) MPI_COMM_WORLD, &size);
  (void) MPI_Comm_rank ((MPI_Comm) MPI_COMM_WORLD, &rank);
  buf = malloc (100000 * sizeof (char));
  (void) memset (buf, 0, 100000 * sizeof (char));
  for (i = 1; i < 100000; i++) {
    if (rank == 0 && size > 1) {
        t0 = MPI_Wtime ();
        (void) MPI_Send (buf,i*sizeof(char),MPI_BYTE,(rank+size+1)%size,0,(MPI_Comm)MPI_COMM_WORLD);
        (void) MPI_Recv (buf,i*sizeof(char),MPI_BYTE,(rank+size-1)%size,0,(MPI_Comm)MPI_COMM_WORLD,&status);
        t1 = MPI_Wtime ();
        (void) printf ("message size = %d, norm comm time = %g\n", i, (t1-t0)/(double)size);
        }
      else if (rank != 0 && size > 1) {
        (void) MPI_Recv (buf,i*sizeof(char),MPI_BYTE,(rank+size-1)%size,0,(MPI_Comm)MPI_COMM_WORLD,&status);
        (void) MPI_Send (buf,i*sizeof(char),MPI_BYTE,(rank+size+1)%size,0,(MPI_Comm)MPI_COMM_WORLD);
        }
      }
  (void) MPI_Finalize ();
  return (0);
  }

Thu, 07 Aug 2014 00:56:57 +0200

Stephan K.H. Seidl