OAOzden Akinci
MPI lecture notes / Chapter 7 of 7

Parallel I/O with MPI-IO

Write and read one shared file from many processes: file handles, offsets, file views, and why collective I/O matters on parallel file systems.

The options

There are three ways to get data from many processes into files:

  1. Funnel through one process. Every rank sends its data to rank 0, which writes. Simple, but limited by one process’s bandwidth and memory.
  2. One file per process. Fast to write, but leaves thousands of files, overloads the file system’s metadata servers at scale, and ties restarts to the process count.
  3. One shared file written in parallel. Every process writes its own part of the same file. This is what MPI-IO is for.

This article compares the three in more detail. This chapter focuses on the MPI-IO interface itself.

Opening and closing files

MPI_File fh;
MPI_File_open(MPI_COMM_WORLD, "data.bin", MPI_MODE_CREATE | MPI_MODE_WRONLY,
              MPI_INFO_NULL, &fh);
...
MPI_File_close(&fh);

Opening is collective over the communicator. Open with MPI_COMM_SELF if only one process should use the file. The access mode is a bitwise OR of flags such as MPI_MODE_RDONLY, MPI_MODE_WRONLY, MPI_MODE_RDWR, MPI_MODE_CREATE and MPI_MODE_EXCL.

Three ways to say where to write

Positioning Example calls When to use
Explicit offset MPI_File_write_at, MPI_File_read_at simple block layouts. The offset is passed in every call.
Individual file pointer MPI_File_seek + MPI_File_write sequential access by each process, often combined with a file view
Shared file pointer MPI_File_write_shared, MPI_File_write_ordered log-style output where order matters less than simplicity. These are slow at scale.

Offsets have the type MPI_Offset, which is 64-bit. Always compute offsets in that type. Products such as rank * N * sizeof(int) overflow a 32-bit int once the file passes 2 GB.

Independent and collective I/O

Every data access call has an independent form (MPI_File_write_at) and a collective form with an _all suffix (MPI_File_write_at_all). The collective form must be called by every process that opened the file. In return, the library sees all requests at once and can merge them into a few large, aligned writes. This technique is known as two-phase I/O or collective buffering. When each process writes small or scattered pieces, the collective version is often many times faster.

MPI_Offset offset = (MPI_Offset)rank * N * sizeof(int);
MPI_File_write_at_all(fh, offset, buf, N, MPI_INT, MPI_STATUS_IGNORE);

File views

A file view tells MPI which parts of the file a process sees, using the derived datatypes from chapter 4:

MPI_File_set_view(fh, displacement, MPI_INT, filetype, "native", MPI_INFO_NULL);
  • displacement: where the view starts, in bytes.
  • etype: the elementary unit, here MPI_INT.
  • filetype: the pattern this process owns, repeated through the file. For a 2D array distributed in blocks, a subarray type (MPI_Type_create_subarray) describing the local tile within the global array is all you need. After that, each process writes its tile with one call, and the file contains the global array in normal order.

Writing and reading with different process counts

11_shared_file.c writes a file with one process count and reads it with another:

$ mpirun -np 4 ./11_shared_file write data.bin
4 ranks wrote 1000 ints each
$ mpirun -np 3 ./11_shared_file read data.bin
rank 0 read values 0..1332: correct
rank 1 read values 1333..2665: correct
rank 2 read values 2666..3999: correct

Nothing in the file records how many processes wrote it. That is the main practical advantage over one file per process.

Hints and parallel file systems

MPI_Info objects pass hints to the I/O layer. The MPI standard reserves several keys, including striping_factor and striping_unit (how a new file is spread over storage targets) and cb_buffer_size and cb_nodes (collective buffering). On Lustre, a shared file only gets parallel bandwidth if it is striped over several targets. Set it on the directory with lfs setstripe or with these hints when you create the file.

For self-describing scientific formats, parallel HDF5 and PnetCDF are built on MPI-IO. The same rules apply to them: use collective operations, and align large writes with the file system’s stripes.

Exercises

  1. Change 11_shared_file.c to use MPI_File_set_view and MPI_File_write_all instead of explicit offsets.
  2. Write a 2D array distributed over a 2 × 2 process grid into one file, using a subarray file type, so that the file holds the global array in row-major order. Check the result with od.
  3. On a parallel file system, time the independent and the collective version for 1 KB, 1 MB and 64 MB per process.