Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.     MPI_Init(&argc, &argv);
  8.    
  9.     int rank, size;
  10.    
  11.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  12.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  13.  
  14.     if( rank )
  15.     {
  16.         char buf[] = "Hello!";
  17.         MPI_Send(buf, sizeof(buf), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  18.     } else {
  19.         cout << "Process 0 started" << endl;
  20.         for(int i(1); i<size; ++i)
  21.         {
  22.             MPI_Status s;
  23.             MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &s);
  24.             int count;
  25.             MPI_Get_count(&s, MPI_CHAR, &count);
  26.  
  27.             char *buf = new char[count];
  28.             MPI_Recv(buf, count, MPI_CHAR,
  29.                      MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &s);
  30.             cout << "Message from process " << s.MPI_SOURCE << ": "
  31.                  << buf << endl;
  32.             delete[] buf;
  33.         }
  34.         cout << "Done." << endl;
  35.     }
  36.  
  37.     MPI_Finalize();
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement