title: MPI serial main function
body: <p>This is a pretty basic MPI question, but I can't wrap my head around it. I have a main function that calls another function that uses MPI. I want the main function to execute in serial, and the other function to execute in parallel. My code is like this:</p>
  
  <pre><code>int main (int argc, char *argv[])
  {
      //some serial code goes here
      parallel_function(arg1, arg2);
      //some more serial code goes here
  }
  
  void parallel_function(int arg1, int arg2)
  {
      //init MPI and do some stuff in parallel
      MPI_Init(NULL, NULL);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;p);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;my_rank);
      //now do some parallel stuff
      //....
      //finalize to end MPI??
      MPI_Finalize();
  }
  </code></pre>
  
  <p>My code runs fine and gets the expected output, but the issue is that the main function is also being run in separate processes and so the serial code executes more than once. I don't know how it's running multiple times, because I haven't even called MPI_Init yet (if I printf in main before I call parallel_function, I see multiple printf's)</p>
  
  <p>How can I stop my program running in parallel after I'm done?</p>
  
  <p>Thanks for any responses!</p>