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.cpp (679B)


      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     if (0 == rank) {
     12         MPI_Send(&rank, 1, MPI_INT, 1, 98, MPI_COMM_WORLD);
     13         MPI_Recv(&recv, 1, MPI_INT, 1, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
     14     } else {
     15         MPI_Send(&rank, 1, MPI_INT, 0, 99, MPI_COMM_WORLD);
     16         MPI_Recv(&recv, 1, MPI_INT, 0, 98, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
     17     }
     18     std::cout << "Rank " << rank << " got ID " << recv << " from other rank\n";
     19 
     20     MPI_Finalize();
     21     return 0;
     22 }