/* nearly the simplest possible program to time message-passing: process 0 sends a message, process 1 receives it and sends it back, process 0 receives the result and prints timing info. */ #include #include #include "mpi.h" /* MPI header file */ int main(int argc, char *argv[]) { int nprocs; int myid; int buff[2]; int bufflen = 2; int mytag = 0; int other_id; MPI_Status status; double start_time, end_time; /* initialize for MPI */ if (MPI_Init(&argc, &argv) != MPI_SUCCESS) { printf("MPI initialization error\n"); exit(EXIT_FAILURE); } /* get number of processes */ (void) MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (nprocs != 2) { printf("number of processes must be 2\n"); exit(EXIT_FAILURE); } /* get this process's number (ranges from 0 to nprocs - 1) */ (void) MPI_Comm_rank(MPI_COMM_WORLD, &myid); if (myid == 0) { /* in process 0: send message to process 1 and wait for echo */ buff[0] = 10; buff[1] = 20; other_id = 1; start_time = MPI_Wtime(); if (MPI_Send(buff, bufflen, MPI_INTEGER, other_id, mytag, MPI_COMM_WORLD) != MPI_SUCCESS) { printf("error in send in process 0\n"); exit(EXIT_FAILURE); } if (MPI_Recv(buff, bufflen, MPI_INTEGER, other_id, mytag, MPI_COMM_WORLD, &status) != MPI_SUCCESS) { printf("error in receive in process 0\n"); exit(EXIT_FAILURE); } end_time = MPI_Wtime(); printf("starting, ending times %g %g\n", start_time, end_time); printf("difference %g\n", end_time - start_time); } else { /* in process 1: receive message from process 0 and send back */ other_id = 0; if (MPI_Recv(buff, bufflen, MPI_INTEGER, other_id, mytag, MPI_COMM_WORLD, &status) != MPI_SUCCESS) { printf("error in receive in process 1\n"); exit(EXIT_FAILURE); } if (MPI_Send(buff, bufflen, MPI_INTEGER, other_id, mytag, MPI_COMM_WORLD) != MPI_SUCCESS) { printf("error in send in process 1\n"); exit(EXIT_FAILURE); } } /* clean up for MPI */ (void) MPI_Finalize(); return EXIT_SUCCESS; }