Hardware Reference
In-Depth Information
File
P 0
P 1
P 2
P
n−1
(a)
1. MPI_File fh;
2. MPI_Status status;
3. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
4. MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
5. bufsize = FILESIZE / nprocs;
6. nints = bufsize / sizeof(int);
7. MPI_File_open(MPI_COMM_WORLD, "/pfs/datafile",
MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
8. MPI_File_seek(fh, rank * bufsize, MPI_SEEK_SET);
9. MPI_File_read(fh, buf, nints, MPI_INT, &status);
10. MPI_File_close(&fh);
(b)
FIGURE 13.2: (a) A simple example in which each process of a parallel pro-
gram needs to access a separate portion of a common file. (b) Sample MPI-IO
code used to perform the I/O.
is a collective function: all processes in the communicator passed as the first
argument must call the function.
After the file is successfully opened, each process can independently access
and read its portion of data from the file. For this purpose, each process seeks
to the right offset in the file by calling MPIFileseek . Then, each process
reads the amount of data it needs by calling MPIFileread . Each process
reads the number of integers, nints , into the buffer, buf , in its local mem-
ory. The status object is the same as in an MPIRecv function; it can be
used to query the amount of data actually read. Finally, all processes call
MPIFileclose to close the file.
13.2.1 Three Ways of File Access
MPI supports three ways of specifying the location from which data is
accessed in a file. The first method is by using individual file pointers, as in
the above example. In this case, each process maintains its own file pointer,
independent of other processes. The file pointer can be moved to a specific
offset in the file by calling MPIFileseek . The following call to MPIFileread
or MPIFilewrite will access data starting from the current location of the
individual file pointer. The file pointer will be incremented by the amount of
data read or written by the read/write call.
An alternate way is by using explicit offsets. No seek function is needed
in this case. Instead, the user calls the functions MPIFilereadat or
MPIFilewriteat . These functions take the file offset as an argument. The
individual file pointer is not affected by reads or writes using these functions.
 
Search WWH ::




Custom Search