title: MPI Send and receive questions
body: <p>I have questions about MPI send and receive operations. </p>
  
  <p>Suppose, we have 2 MPI threads that try to send message to each other. Following are three code snippets doing that:</p>
  
  <p>First (Blocking 'send' and 'receive'):</p>
  
  <pre><code>...
  int data = ...;
  ...
  MPI_Send( &amp;data, sizeof( int ), MPI_INT, 
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD );
  
  MPI_Status status; 
  MPI_Recv( &amp;data, sizeof( int ), MPI_INT,
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &amp;status );
  ...
  </code></pre>
  
  <p>Second  (Non-blocking 'send' but blocking 'receive'):</p>
  
  <pre><code>...
  int data = ...;
  ...
  MPI_Request request;
  MPI_Isend( &amp;data, sizeof( int ), MPI_INT, 
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &amp;request);
  
  MPI_Status status; 
  MPI_Recv( &amp;data, sizeof( int ), MPI_INT,
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &amp;status );
  // Synchronize sender &amp; receiver
  MPI_Wait( &amp;request, &amp;status);
  ...
  </code></pre>
  
  <p>Third (Non-blocking 'receive' with blocking 'send'):</p>
  
  <pre><code>...
  int data = ...;
  ...
  MPI_Request request;
  MPI_Irecv( &amp;data, sizeof( int ), MPI_INT,
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &amp;request );
  
  MPI_Send( &amp;data, sizeof( int ), MPI_INT, 
          (my_id == 0)?1:0, 0, MPI_COMM_WORLD);
  
  MPI_Status status; 
  
  // Synchronize sender &amp; receiver
  MPI_Wait( &amp;request, &amp;status);
  ...
  </code></pre>
  
  <p>I guess there are potential problems with above three code but I want your opinion. So, I have the following questions:</p>
  
  <ol>
  <li><p>What are the (potential) problems (if any) with 3 codes given above?</p></li>
  <li><p>Which of the above three code are valid/correct considering MPI standard so that it can work with all MPI implementations? </p></li>
  <li><p>What is the best way (if not one of above 3 please write it) to do that?</p></li>
  <li><p>In the third code, what if we change the order of MPI_Irecv and MPI_Send call?</p></li>
  </ol>
  
  <p>PS: By the way, I have tried executing them using Scali MPI and all of them worked!</p>