title: How to handle IO etc outide MPI portion
body: <p>Writing a sorting program with MPI. It is probably the best to have the code that handles IO outside the MPI scope, e.g. reading in data file before sorting, writing out sorted data into a file after sorting. </p>
  
  <p>So in my main function I did the input before MPI_Init and output after MPI_Finalize. However it does not seem to work the way I wanted. Because I was trying to print out a line of "*" before MPI_Init and guess what, it does it n_procs times instead of just once. What is the best way to handle IO in a MPI code?</p>
  
  <pre><code>int main()
  {
     read in data;
     cout &lt;&lt; "************************";
     MPI_Init();
     MPI_Comm_rank(MPI_COMM_WORLD, &amp;my_rank);
     MPI_Comm_rank(MPI_COMM_WORLD, &amp;nproc);
     if(rank == 0)
     {
        mergesort_parallel; // recursively
     }
     else
     {
        MPI_Recv subarray from parent;
        mergesort_parallel(subarray);
        MPI_Send subarray after sorting to parent;
        MPI_Finalize();
        return 0;
     }
     MPI_Finalize();
  
     output sorted data to file;
  }
  </code></pre>