[mpich-discuss] need for MPI_Barrier in code using romio

shankha shankhabanerjee at gmail.com
Thu Jul 1 15:17:57 CDT 2010


Hi,
I have the following piece of code which writes a array ( buf ) on to
the file and tries to read it back from a different offset.
Is a MPI_Barrier call ( line no : 36 ) necessary after we have written
the array to the file and want to read it back. Logically it seems yes
because
unless the previous process has written on to the file one would read
erroneous values.

I tested my code with and without the Barrier. It seems to work fine
in both cases.
My question is do we need the Barrier call at line no  : 36

   1 #include <stdio.h>
  2 #include <mpi.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <assert.h>
  6 #include <stdbool.h>
  7
  8 #define BUF_SIZE 3
  9
 10
 11
 12 static void
 13 print_read_buf ( int myrank, int *read_buf, int buf_size );
 14
 15   int
 16 main ( int argc , char **argv )
 17 {
 18   int myrank , size , i = 0;
 19   MPI_File thefile;
 20   int buf [ BUF_SIZE ];
 21   MPI_Init(&argc, &argv);
 22   MPI_Comm_rank ( MPI_COMM_WORLD, &myrank );
 23   MPI_Comm_size ( MPI_COMM_WORLD, &size );
 24
 25   i = 0;
 26   while ( i < BUF_SIZE )
 27     buf [ i++ ] = myrank;
 28
 29   MPI_File_open ( MPI_COMM_WORLD, "testfile", MPI_MODE_CREATE |
MPI_MODE_RDWR,
 30       MPI_INFO_NULL, &thefile );
 31   MPI_File_set_view ( thefile, (( myrank + 4 ) % size ) * BUF_SIZE
*  sizeof ( int ), MPI_INT,
 32       MPI_INT, "native", MPI_INFO_NULL );
 33   MPI_File_write ( thefile, buf, BUF_SIZE, MPI_INT, MPI_STATUS_IGNORE );
 34   MPI_File_set_view ( thefile,  myrank * BUF_SIZE *  sizeof ( int
) , MPI_INT,
 35       MPI_INT, "native", MPI_INFO_NULL );

 36   MPI_Barrier ( MPI_COMM_WORLD );

 37   MPI_File_read_all ( thefile , buf, BUF_SIZE , MPI_INT,
MPI_STATUS_IGNORE );
 38   if ( myrank == 7 )
 39     print_read_buf ( myrank, buf , BUF_SIZE );
 40   MPI_File_close ( &thefile );
 41   MPI_Finalize ();
 42   return 0;
 43 }
 44
 45 static void
 46 print_read_buf ( int myrank, int *read_buf, int buf_size )
 47 {
 48   int i = 0;
 49   printf ( " READ BUF : MYRANK = %d \n", myrank );
 50   while ( i < buf_size )
 51     printf ( " %d ", read_buf [ i++ ] );
 52   printf ( "\n" );
 53 }
 54

Thanks
Shankha


More information about the mpich-discuss mailing list