#include #include #include #include #include "mpi.h" #define DEFAULT_LENGTH 256 #define TAG_ID 1 void Usage(const char *program) { printf("This program forces MPI_Send to accumulate unexpected receives\n" "Parameters:\n" " -l to specify size of the send buffers; default to %u\n" " -help to show this screen\n", DEFAULT_LENGTH); printf("Note:\n" "the job is required to run on 3 nodes. One node will send to the second\n" "and the second will send to the third node. The third node does not match\n" "the sends with receives nor does the second node.\n" "More details are offered in the README file attached to this application's\n" "directory.\n"); } int main(int argc, char **argv) { char *sbuf; int i, length = DEFAULT_LENGTH; int myRank, nProcs, err; if(MPI_SUCCESS != MPI_Init(&argc, &argv)) { printf("MPI_Init failed\n"); return -1; } for(i = 1; i < argc; i ++) { if(strcmp("-l", argv[i]) == 0) { if (i + 1 < argc) { length = atoi(argv[i + 1]); if(length <= 0) { length = DEFAULT_LENGTH; } } continue; } if(!strcmp("-help", argv[i]) || !strcmp("--help", argv[i]) || !strcmp("/?", argv[i]) || !strcmp("-h", argv[i])) { Usage(argv[0]); return 0; } } MPI_Comm_size(MPI_COMM_WORLD, &nProcs); if(nProcs != 3) { printf("The job size must be 3\n"); MPI_Abort(MPI_COMM_WORLD, -1); } MPI_Comm_rank(MPI_COMM_WORLD, &myRank); sbuf = (char *) malloc(length); if(sbuf == NULL) { printf("Failed to allocate %d bytes\n", length); MPI_Abort(MPI_COMM_WORLD, -1); } switch(myRank) { case 0: /*Rank 0 sends to rank 1*/ case 1: /*Rank 1 sends to rank 2*/ while(1) { err = MPI_Send(sbuf, length, MPI_BYTE, myRank + 1, TAG_ID, MPI_COMM_WORLD); if(err != MPI_SUCCESS) { printf("MPI_Send failed on rank %d with error code %d = %x\n", myRank, err, err); MPI_Abort(MPI_COMM_WORLD, -1); } } break; case 2: /*Rank 2 does not do anything. This will make rank 1 block on Send and acumulate unexpected receives.*/ while(1) { Sleep(2); } break; } free(sbuf); MPI_Finalize(); return 0; }