// // nearly the simplest possible program to time message-passing: // // process 0 sends a message, process 1 receives it and sends it // back, process 0 receives the result and prints timing info. // #include #include // has exit(), etc. #include // MPI header file // a short function to print a message, clean up, and exit void errorExit(const char msg[]) { cerr << msg; MPI::Finalize(); exit(EXIT_FAILURE); } // main program 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(); if (nprocs != 2) errorExit("number of processes must be 2\n"); // 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; MPI::Status status; if (myid == 0) { // in process 0: send message to process 1 and wait for echo buff[0] = 10; buff[1] = 20; int other_id = 1; double start_time = MPI::Wtime(); MPI::COMM_WORLD.Send(buff, bufflen, MPI::INT, other_id, mytag); MPI::COMM_WORLD.Recv(buff, bufflen, MPI::INT, other_id, mytag, status); double end_time = MPI::Wtime(); cout << "starting, ending times " << start_time << " " << end_time << endl; cout << "difference " << end_time - start_time << endl; } else { // in process 1: receive message from process 0 and send back int other_id = 0; MPI::COMM_WORLD.Recv(buff, bufflen, MPI::INT, other_id, mytag, status); MPI::COMM_WORLD.Send(buff, bufflen, MPI::INT, other_id, mytag); } // clean up for MPI MPI::Finalize(); return EXIT_SUCCESS; }