// // 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) if (MPI_Init(&argc, &argv) != MPI_SUCCESS) errorExit("MPI initialization error\n"); // get number of processes int nprocs; MPI_Comm_size(MPI_COMM_WORLD, &nprocs); 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_rank(MPI_COMM_WORLD, &myid); 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(); if (MPI_Send(buff, bufflen, MPI_INT, other_id, mytag, MPI_COMM_WORLD) != MPI_SUCCESS) errorExit("error in send in process 0\n"); if (MPI_Recv(buff, bufflen, MPI_INT, other_id, mytag, MPI_COMM_WORLD, &status) != MPI_SUCCESS) errorExit("error in receive in process 0\n"); 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; if (MPI_Recv(buff, bufflen, MPI_INT, other_id, mytag, MPI_COMM_WORLD, &status) != MPI_SUCCESS) errorExit("error in receive in process 1\n"); if (MPI_Send(buff, bufflen, MPI_INT, other_id, mytag, MPI_COMM_WORLD) != MPI_SUCCESS) errorExit("error in send in process 1\n"); } // clean up for MPI MPI_Finalize(); return EXIT_SUCCESS; }