/* pingpong.c -- pingpong program * * Send a message from all processes with rank != 0 to process 0. * Process 0 prints the messages received. * * Input: none. * Output: contents of messages received by process 0. * * See Chapter 3, pp. 41 & ff in PPMPI. */ #include #include #include "mpi.h" int main(int argc, char* argv[]) { int my_rank; /* rank of process */ int numprocesses; /* number of processes */ int tag = 0; /* tag for messages */ int x = 0, count=0; int startTime = 0; int endTime = 0; int numTimes; char message[100]; /* storage for message */ MPI_Status status; /* return status for */ /* receive */ /* Start up MPI */ MPI_Init(&argc, &argv); if (argc < 2) numTimes = 100; else numTimes = atoi(argv[1]); printf("Numtimes=%d\n",numTimes); strcpy(message, "Hello World"); /* Find out process rank */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* Find out number of processes */ MPI_Comm_size(MPI_COMM_WORLD, &numprocesses); if (my_rank == 0) { startTime = MPI_Wtime(); printf("Start time=%d\n", startTime); for(x=0; x < numTimes; x++) { count++; // printf("%d Sending %d...", my_rank, count); MPI_Send(&count, 1, MPI_INT, 1, tag, MPI_COMM_WORLD); MPI_Send(message, strlen(message)+1, MPI_CHAR, 1, tag, MPI_COMM_WORLD); MPI_Recv(&count, 1, MPI_INT, 1, tag, MPI_COMM_WORLD, &status); MPI_Recv(message, strlen(message)+1, MPI_CHAR, 1, tag, MPI_COMM_WORLD, &status); // printf("%d Receiving %d\n", my_rank, count); } endTime = MPI_Wtime(); printf("Endtime=%d, totaltime=%d\n", endTime, endTime-startTime); } else if (my_rank == 1) { for(x=0; x < numTimes; x++) { MPI_Recv(&count, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); MPI_Recv(message, strlen(message)+1, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status); // printf("%d receiving %s, %d\n", my_rank, message, count); count++; MPI_Send(&count, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); MPI_Send(message, strlen(message)+1, MPI_CHAR, 0, tag, MPI_COMM_WORLD); // printf("%d sending %s, %d\n", my_rank, message, count); } } MPI_Finalize(); return 0; }