/* test of communication methods in MPI */ #include "mpi.h" #include #include int main(int argc, char **argv) { char buff[10]; // Buffer char *recv_buff; MPI_Status status; int nbytes; int numprocs; int myid; int i; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if(myid == 0) { cout << "We have " << numprocs << " processors" << endl; MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); // Allocate memory to receive data MPI_Get_count(&status, MPI_CHAR, &nbytes); cout << "Received " << nbytes << " bytes from proc " << status.MPI_SOURCE << " with tag " << status.MPI_TAG << endl; if ( nbytes != MPI_UNDEFINED ) recv_buff = (char *) malloc( nbytes ); MPI_Recv(recv_buff, nbytes, MPI_CHAR, status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD, NULL); cout << ">>>>> "; for ( i = 0; i < nbytes; i++ ) cout << recv_buff[i]; cout << endl; } else if ( myid == 1 ) { strcpy(buff, "Hello World"); MPI_Send(&buff, 4, MPI_CHAR, 0, 0, MPI_COMM_WORLD); } MPI_Finalize(); }