Sample C code:
#include <stdio.h>
#include <mpi.h>
#define MAX_INTS 65536
#define TEST_RUNS 100
int blip [MAX_INTS];
void main (int argc, char **argv)
{
int
rank, size;
/* of MPI_COMM_WORLD */
MPI_Status status;
/* For MPI_Recv */
int
i, j;
/* loop variables */
double
start, end;
/* for MPI_Wtime() */
double
tot_sec;
/* for MPI_Wtime() */
int
num_ints;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
fprintf (stderr, "[%2d] Init\n", rank);
tot_sec = 0.0;
for (j = 0; j < 3; j++)
{
if ((rank % 2) == 0)
{
fprintf (stderr, "[%2d] receiving/sending\n", rank);
MPI_Recv
(blip, MAX_INTS, MPI_INT, rank+1, 1,
MPI_COMM_WORLD, &status);
fprintf (stderr, "[%2d] received/sending\n", rank);
MPI_Send (blip, MAX_INTS, MPI_INT, rank+1, 1,
MPI_COMM_WORLD);
fprintf
(stderr, "[%2d] received/sent\n", rank);
}
else
{
fprintf (stderr, "[%2d] sending/receiving\n", rank);
MPI_Send
(blip, MAX_INTS, MPI_INT, rank-1, 1,
MPI_COMM_WORLD);
fprintf (stderr, "[%2d] sent/receiving\n", rank);
MPI_Recv (blip, MAX_INTS, MPI_INT, rank-1, 1,
MPI_COMM_WORLD, &status);
fprintf
(stderr, "[%2d] sent/received\n", rank);
}
}
for (num_ints = 8; num_ints <= MAX_INTS; num_ints
*= 2) {
for (i = 0; i < num_ints;
i++)
{
blip [i] = (((i * size) + rank) << 16) + (num_ints / 1024);
}
/*
* get starting time
*/
start = MPI_Wtime();
for (j = 0; j < TEST_RUNS;
j++)
{
if ((rank % 2) == 0)
/* I receive first */
{
MPI_Recv (blip, num_ints, MPI_INT, rank+1, 1,
MPI_COMM_WORLD, &status);
MPI_Send (blip, num_ints, MPI_INT, rank+1, 1,
MPI_COMM_WORLD);
}
else
/* I send first */
{
MPI_Send (blip, num_ints, MPI_INT, rank-1, 1,
MPI_COMM_WORLD);
MPI_Recv (blip, num_ints, MPI_INT, rank-1, 1,
MPI_COMM_WORLD, &status);
}
}
/*
* get final time
*/
end = MPI_Wtime();
if ((rank % 2) == 1) {
tot_sec = (end - start)
/ 2.0;
printf ("[%2d] %10d %6.3f
MB/sec\n", rank, num_ints * sizeof(int),
(float) ((float) num_ints / (256.0 * 1024.0)) /
((float) tot_sec / (float) TEST_RUNS));
fflush (stdout);
}
}
MPI_Finalize();
fprintf(stderr, "[%2d] done\n", rank );
}