Question

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.

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?

int main()
{
   read in data;
   cout << "************************";
   MPI_Init();
   MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
   MPI_Comm_rank(MPI_COMM_WORLD, &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;
}
Was it helpful?

Solution

The processes are created by mpiexec/mpirun and exist before MPI_Init() is called. Therefore it prints the *** lines the number of times equal to the process count. I suggest go for using standard I/O routines like fopen(), fread() etc. inside the code for the root process. i.e.

If(myrank == 0)
{
  read file into buffer of root ; //Master I/O.
}
else
{
other code ; 
}
MPI_Finalize();
return 0; 
}

Further, place MPI_Finalize(), return 0 outside both If and else conditions. If you want to read a portion of the file into individual buffers of a process in parallel then go for MPI I/O i.e. use I/O functions provided by MPI like MPI_File_open(), MPI_File_set_view() etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top