#include #include #include "mpi.h" /* This program tests ping-pong performance between nprocs/2 pairs of processeces (nprocs must be even): process 2k sends/recvs 'msgcount' messages of length 'msgsize' to 2k+1 */ int main(int argc, char *argv[]) { int i, msgsize, msgcount, myrank, nprocs; char *buffer; MPI_Status status; double start_time, end_time; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (nprocs%2) { fprintf(stderr, "Error: This program requires an even number of processes.\n"); MPI_Abort(MPI_COMM_WORLD, 1); exit(1); } MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (argc!=3) { fprintf(stderr, "Usage: %s msgsize msgcount\n", argv[0]); MPI_Abort(MPI_COMM_WORLD, 2); exit(2); } msgsize=atoi(argv[1]); msgcount=atoi(argv[2]); #ifdef DEBUG fprintf(stderr, "Proc %d: size=%d, count=%d\n", myrank, msgsize, msgcount); #endif /* Allocate a buffer of size msgsize */ if ( (buffer=malloc(msgsize)) == NULL ) { fprintf(stderr, "Cannot allocate buffer!\n"); MPI_Abort(MPI_COMM_WORLD, 3); exit(3); } /* Synchronization */ MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); start_time=MPI_Wtime(); if ( myrank < nprocs/2 ) { /* receive from myrank-1 */ for (i=0; i