/* Problem 3.6.3 in PPMPI * * by John Weatherwax (wax@alumn.mit.edu) * * To explore the MPI programming environment I changed * (with corresponding results): * 1) dest to 1 in all sending process (invoking an incorrect MPI_Recv) * -Hangs waiting forever ... MPI_Recv never completes * 2) Incorrect string length (removed +1 from MPI_Send call) * -Still worked...(removing more elements) * -Prints until print process gets "\0" (depends on contents of memory) * 3) Incorrect MPI data type in MPI_Send call * -CHAR->INT: code crashed (larger data type) * 4) Incorrect recieve size (tried 10) * -code crashed * 5) Incorrect MPI data type in recieve * -CHAR->INT worked (change to DOUBLE) * -worked these are larger data types * 6) Incorrect tag field in MPI recieve * -program hangs * * 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" main(int argc, char* argv[]) { int my_rank; /* rank of process */ int p; /* number of processes */ int source; /* rank of sender */ int dest; /* rank of receiver */ int tag = 0; /* tag for messages */ char message[100]; /* storage for message */ MPI_Status status; /* return status for */ /* receive */ /* Start up MPI */ MPI_Init(&argc, &argv); /* Find out process rank */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* Find out number of processes */ MPI_Comm_size(MPI_COMM_WORLD, &p); if (my_rank != 0) { /* Create message */ sprintf(message, "Greetings from process %d!", my_rank); dest = 0; /* Use strlen+1 so that '\0' gets transmitted */ MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else { /* my_rank == 0 */ for (source = 1; source < p; source++) { MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status); printf("%s\n", message); } } /* Shut down MPI */ MPI_Finalize(); } /* main */