0

Is there a standard way to synchronise MPI shared file pointers so that MPI_File_get_position_shared returns the same value on all processes?

For example the following program:

#include <mpi.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    MPI_File fh;
    MPI_File_open(MPI_COMM_WORLD, "foo", MPI_MODE_RDWR | MPI_MODE_CREATE, MPI_INFO_NULL, &fh);

    char* buf;
    buf = "hello";
    MPI_Status status;
    sleep(rank);
    MPI_File_write_shared(fh, buf, strlen(buf), MPI_CHAR, &status);    

    MPI_File_sync(fh); 

    MPI_Offset offset;
    MPI_File_get_position_shared(fh, &offset);

    printf("rank: %i, offset: %i\n", rank, (int) offset);

    MPI_File_close(&fh);

    MPI_Finalize();
}

will print

rank: 0, offset: 5
rank: 1, offset: 10
rank: 2, offset: 15
rank: 3, offset: 20

It does work if I replace MPI_File_sync(fh) with MPI_Barrier(MPI_COMM_WORLD): is that guaranteed to work correctly?

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50

1 Answers1

0

You can use the below function to synchronise the shared file pointer.

MPI_File_seek_shared( fh, OFFSET, MPI_SEEK_SET );

Syntax:

int MPI_File_seek_shared(
  MPI_File mpi_fh,
  MPI_Offset offset,
  int whence
);

MPI_FILE_SEEK_SHARED updates the shared file pointer according to whence, which has the following possible values:

MPI_SEEK_SET: the pointer is set to offset

MPI_SEEK_CUR: the pointer is set to the current pointer position plus offset

MPI_SEEK_END: the pointer is set to the end of file plus offset

Reference

j23
  • 3,139
  • 1
  • 6
  • 13