Sample C code:
int main(argc,argv)
int argc;
char *argv[];
{
int reps;
/* number of samples per test */
struct timeval tv1, tv2; /* for timing
*/
int dt1, dt2;
/* time for one iter */
int at1, at2;
/* accum. time */
int n;
char *inmsg, *outmsg;
/* Buffer containing message */
int type;
int numtasks, taskid;
int rc, dest, source;
MPI_Status status;
rc = MPI_Init(&argc,&argv);
rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
if (rc != MPI_SUCCESS)
printf ("error initializing MPI and
obtaining task ID information\n");
else
printf ("task ID = %d\n", taskid);
if (numtasks != 2)
{
fprintf(stderr, "Only using 2 tasks...continuing\n");
}
at1 = 0;
inmsg = (char *) malloc(MESSAGE_SIZE);
outmsg = (char *) malloc(MESSAGE_SIZE);
type = 1;
reps = NUMBER_REPS;
if (taskid == 0)
{
/* round-trip timing test */
printf("Doing round trip test, minimal
message size, %d reps.\n",reps);
dest = 1;
source = 1;
for (n = 1; n <= reps; n++)
{
gettimeofday(&tv1,
(struct timeval*)0);
/* before time */
/* send message to
worker - message type set to 1. */
/* If return code
indicates error quit */
rc = MPI_Send(&outmsg,
MESSAGE_SIZE, MPI_BYTE, dest, type, MPI_COMM_WORLD);
if (rc != MPI_SUCCESS)
{
fprintf(stderr, "Send error in processor 1\n");
exit(1);
}
/* Now wait to receive
the echo reply from the worker */
/* If return code
indicates error quit */
rc = MPI_Recv(&inmsg,
MESSAGE_SIZE, MPI_BYTE, source, type, MPI_COMM_WORLD, &status);
if (rc != MPI_SUCCESS)
{
fprintf(stderr, "Recieve error in processor 0\n");
exit(1);
}
gettimeofday(&tv2, (struct timeval*)0); /* after time */
/* calculate round
trip time and print */
dt1 = (tv2.tv_sec
- tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
printf("round trip#
%2d uSec = %8d\n", n, dt1);
at1 += dt1;
}
printf("\n*** Round Trip Avg uSec =
%d\n", at1 / reps);
} else if (taskid == 1)
{
dest = 0;
source = 0;
for (n = 1; n <= reps; n++)
{
rc = MPI_Recv(&inmsg,
MESSAGE_SIZE, MPI_BYTE, source, type, MPI_COMM_WORLD, &status);
if (rc != MPI_SUCCESS)
{
fprintf(stderr, "Recieve error in processor 0\n");
exit(1);
}
rc = MPI_Send(&outmsg,
MESSAGE_SIZE, MPI_BYTE, dest, type, MPI_COMM_WORLD);
if (rc != MPI_SUCCESS)
{
fprintf(stderr, "Send error in processor 1\n");
exit(1);
}
}
}
MPI_Finalize();
exit(0);
}