// // nearly the simplest possible message-passing program: // // process 0 sends a message, and process 1 receives and prints it. // #include #include // has exit(), etc. #include "mpi++.h" // MPI header file int main(int argc, char *argv[]) { // initialize for MPI (should come before any other calls to // MPI routines) MPI::Init(argc, argv); // get number of processes int nprocs = MPI::COMM_WORLD.Get_size(); // get this process's number (ranges from 0 to nprocs - 1) int myid = MPI::COMM_WORLD.Get_rank(); const int bufflen = 2; int buff[bufflen]; int mytag = 0; if (myid == 0) { // in process 0: send message to process 1 buff[0] = 10; buff[1] = 20; int dest = 1; MPI::COMM_WORLD.Send(buff, bufflen, MPI::INTEGER, dest, mytag); cout << "process 0 sent " << buff[0] << " " << buff[1] << endl; } else { // in process 1: receive message from process 0 and print MPI::Status status; int source = 0; MPI::COMM_WORLD.Recv(buff, bufflen, MPI::INTEGER, source, mytag, status); cout << "process 1 received " << buff[0] << " " << buff[1] << endl; } // clean up for MPI MPI::Finalize(); return EXIT_SUCCESS; }