cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

mpi_p2p_nonblocking.cpp (807B)


      1 #include <mpi.h>
      2 #include <iostream>
      3 
      4 // exchange rank IDs between 2 processes
      5 // DISCLAIMER: this code contains a bug
      6 int main(int argc, char* argv[])
      7 {
      8     int rank, recv;
      9     MPI_Init(&argc, &argv);
     10     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
     11     MPI_Request request;
     12     if (0 == rank) {
     13         MPI_Irecv(&recv, 1, MPI_INT, 1, 99, MPI_COMM_WORLD, &request);
     14         MPI_Send(&rank, 1, MPI_INT, 1, 98, MPI_COMM_WORLD);
     15     } else {
     16         MPI_Irecv(&recv, 1, MPI_INT, 0, 98, MPI_COMM_WORLD, &request);
     17         MPI_Send(&rank, 1, MPI_INT, 0, 99, MPI_COMM_WORLD);
     18     }
     19     // The following line is required to fix the bug:
     20     // MPI_Wait(&request, MPI_STATUS_IGNORE); // synchronization
     21     std::cout << "Rank " << rank << " got ID " << recv << " from other rank\n";
     22 
     23     MPI_Finalize();
     24     return 0;
     25 }