Skip to main content


File View Examples

Example 1: We write contiguous data into a contiguous block defined by a file view. We give each process a different file view so that together, the processes lay out a series of blocks in the file, one block per process.

#define N 100
MPI_Datatype arraytype;
MPI_Offset disp;

disp = rank*sizeof(int)*N; etype = MPI_INT;
MPI_Type_contiguous(N, MPI_INT, &arraytype);
MPI_Type_commit(&arraytype);

MPI_File_open(    MPI_COMM_WORLD, "/pfs/datafile", 
                  MPI_MODE_CREATE | MPI_MODE_RDWR, 
                  MPI_INFO_NULL, &fh);
MPI_File_set_view(fh, disp, etype, arraytype, 
                  "native", MPI_INFO_NULL);
MPI_File_write(fh, buf, N, etype, MPI_STATUS_IGNORE);


Example 2: We write contiguous data into two separate blocks defined by a file view. Each block is a contiguous type in memory, but the pair of blocks is a vector type in the file view. We again use displacements to lay out a series of blocks in the file, one block per process, in a repeating fashion.

int buf[NW*2];
   MPI_File_open(MPI_COMM_WORLD, "/data2", 
                 MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
/* want to see 2 blocks of NW ints, NW*npes apart */
   MPI_Type_vector(2, NW, NW*npes, MPI_INT, &fileblk);
   MPI_Type_commit(                         &fileblk);
   disp = (MPI_Offset)rank*NW*sizeof(int);
   MPI_File_set_view(fh, disp, MPI_INT, fileblk, 
                     "native", MPI_INFO_NULL);

/* processor writes 2 'ablk', each with NW ints */
   MPI_Type_contiguous(NW,   MPI_INT, &ablk);
   MPI_Type_commit(&ablk);
   MPI_File_write(fh, (void *)buf, 2, ablk, &status);

  • The data type in memory is just a contiguous set of NW ints.
  • The file view is of type fileblk, displaced by rank*NW*sizeof(int).
  • Two units of the data type are written into the file whose view has been set.