title: Multi-Threaded MPI Process Suddenly Terminating
body: <p>I'm writing an MPI program (Visual Studio 2k8 + MSMPI) that uses Boost::thread to spawn two threads per MPI process, and have run into a problem I'm having trouble tracking down.</p>
  
  <p>When I run the program with: <code>mpiexec -n 2 program.exe</code>, one of the processes suddenly terminates:</p>
  
  <pre><code>job aborted:
  [ranks] message
  
  [0] terminated
  
  [1] process exited without calling finalize
  
  ---- error analysis -----
  
  [1] on winblows
  program.exe ended prematurely and may have crashed. exit code 0xc0000005
  
  
  ---- error analysis -----
  </code></pre>
  
  <p>I have no idea why the first process is suddenly terminating, and can't figure out how to track down the reason.  This happens even if I put the rank zero process into an infinite loop at the end of all of it's operations... it just suddenly dies.  My main function looks like this:</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
      /* Initialize the MPI execution environment. */
      MPI_Init(0, NULL);
  
      /* Create the worker threads. */
      boost::thread masterThread(&amp;Master);
      boost::thread slaveThread(&amp;Slave);
  
      /* Wait for the local test thread to end. */
      masterThread.join();
      slaveThread.join();
  
      /* Shutdown. */
      MPI_Finalize();
      return 0;
  }
  </code></pre>
  
  <p>Where the <code>master</code> and <code>slave</code> functions do some arbitrary work before ending.  I can confirm that the master thread, at the very least, is reaching the end of it's operations.  The slave thread is always the one that isn't done before the execution gets aborted.  Using print statements, it seems like the slave thread isn't actually hitting any errors... it's happily moving along and just get's taken out in the crash.</p>
  
  <p>So, does anyone have any ideas for:<br>
  a) What could be causing this?<br>
  b) How should I go about debugging it?</p>
  
  <p>Thanks so much!</p>
  
  <p>Edit:</p>
  
  <p>Posting minimal versions of the Master/Slave functions.  Note that the goal of this program is purely for demonstration purposes... so it isn't doing anything useful.  Essentially, the master threads send a dummy payload to the slave thread of the other MPI process.</p>
  
  <pre><code>void Master()
  {   
      int  myRank;
      int  numProcs;
      MPI_Comm_size(MPI_COMM_WORLD, &amp;numProcs);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;myRank);
  
      /* Create a message with numbers 0 through 39 as the payload, addressed 
       * to this thread. */
      int *payload= new int[40];
      for(int n = 0; n &lt; 40; n++) {
          payload[n] = n;
      }
  
      if(myRank == 0) {
          MPI_Send(payload, 40, MPI_INT, 1, MPI_ANY_TAG, MPI_COMM_WORLD);
      } else {
          MPI_Send(payload, 40, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);
      }
  
      /* Free memory. */
      delete(payload);
  }
  
  void Slave()
  {
      MPI_Status status;
      int *payload= new int[40];
      MPI_Recv(payload, 40, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &amp;status);
  
      /* Free memory. */
      delete(payload);
  }
  </code></pre>