#include #include #include #include #include #include "MPI1.h" using namespace std; int num_procs, my_proc; //These let you know how many processes there are, and what your specific process number is in this execution. MPI_Request r; MPI_Status status; int main (int argc, char **argv) { MPI_Init (&argc,&argv); //This initializes MPI MPI_Comm_size (MPI_COMM_WORLD,&num_procs); //gets me the total number of processors MPI_Comm_rank (MPI_COMM_WORLD,&my_proc); //gets me my process number if (num_procs != 2) { if (my_proc==0) cout << "Error: You must run with exactly two processors!" << endl; } else { if (my_proc==0) { cout << "Enter a string: "; string str; getline (cin,str); cout << my_proc << " is sending: \"" << str << "\"." << endl; const char *A = str.c_str(); //A is a pointer to the char array held in str MPI_Isend (A,strlen(A)+1,MPI_CHAR,1,42,MPI_COMM_WORLD,&r); //non-blocking send command. The message will not send until the receiveding process //is ready to receive it...but this process will not have to wait around until the other process is ready //Parameters: Pointer to the A. Size of message, the type of data (I always use MPI_CHAR), the receiving process, //the message tag, the next parameters is always MPI_COMM_WORLD, and the parameter will hold return //status information from this method call } else { char A[100]; MPI_Recv (A, sizeof A,MPI_CHAR,0,42,MPI_COMM_WORLD,&status); /* This is a "blocking receive" command. It will wait until it receives a message. Parameters: address of buffer, size of buffer, data type, who I'm receiving from, the message tag, MPI_COMM_WORLD, and status information returned by the message call. char A[100]; sizeof A yields 100. char *A = new char[100]; sizeof A yields 4. */ string str (A); cout << "Process " << my_proc << " received the following transmission: \"" << str << "\"." << endl; } } MPI_Finalize (); //clean everything up return 0; }