OAOzden Akinci
Writing / MPI, Parallel I/O

From one file per process to one shared file: a practical path to MPI-IO

Output is where many well-scaled MPI codes quietly stop scaling. The solver runs in parallel on hundreds of nodes, then every checkpoint funnels through one process, or each rank writes its own file and the job leaves forty thousand small files on the parallel file system. MPI-IO, part of the standard since MPI-2, solves both problems, and the basic version needs only a few lines.

This post follows the parallel I/O part of a lecture I gave on MPI-2: the same four steps, with the final version tested on Open MPI 4.1.

1. Funnel everything through rank 0

The simplest approach: every process sends its data to rank 0, and rank 0 writes the file with ordinary C I/O.

if (rank != 0) {
    MPI_Send(buf, N, MPI_INT, 0, 99, MPI_COMM_WORLD);
} else {
    FILE *f = fopen("testfile", "w");
    fwrite(buf, sizeof(int), N, f);
    for (int src = 1; src < size; src++) {
        MPI_Recv(buf, N, MPI_INT, src, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        fwrite(buf, sizeof(int), N, f);
    }
    fclose(f);
}

You get one file in the right order, and you only need one process that can do I/O. But the write speed is limited to what a single process can push, and rank 0 needs memory for incoming data while every other rank waits. This is fine for small logs and summaries, but not for checkpoints.

2. One file per process

Each rank writes testfile.<rank> with fopen and fwrite. Now the writes do run in parallel, and nothing has to be sent between processes. The cost appears later:

  • The result is a directory of fragments that must be joined before another tool can use it.
  • Restarting usually requires exactly the same number of processes.
  • At scale, creating thousands of files at once puts heavy load on the file system’s metadata servers. On Lustre or similar systems this can slow down the whole machine, not only your job.

Doing the same thing with MPI_File_open(MPI_COMM_SELF, ...) and MPI_File_write is a useful first step into the MPI-IO API, but it has the same drawbacks.

3. One shared file, every rank writes its own block

The goal is a single file that looks exactly like the output of step 1, written in parallel as in step 2. Every process opens the same file on MPI_COMM_WORLD and writes its block at its own offset:

MPI_File fh;
MPI_File_open(MPI_COMM_WORLD, "data.bin", MPI_MODE_CREATE | MPI_MODE_WRONLY,
              MPI_INFO_NULL, &fh);
MPI_Offset offset = (MPI_Offset)rank * N * sizeof(int);
MPI_File_write_at_all(fh, offset, buf, N, MPI_INT, MPI_STATUS_IGNORE);
MPI_File_close(&fh);

Two details matter here:

  • Use MPI_Offset for offsets. It is 64-bit. Computing rank * N * sizeof(int) in a plain int silently overflows once the file passes 2 GB.
  • Prefer the collective _all variants. MPI_File_write_at lets each rank write on its own. MPI_File_write_at_all is called by all ranks together, and that lets the library merge many small requests into a few large, well-aligned writes (“collective buffering”). For small blocks per process, this is usually where the speed-up comes from.

The lecture version used MPI_File_set_view with a displacement per rank followed by MPI_File_write. That is equivalent for contiguous blocks, and file views become essential once each rank owns a non-contiguous part of the file, for example a sub-block of a 3D array, described with an MPI derived datatype.

4. Read it back with any number of processes

Because the file has no trace of how many processes wrote it, a restart or a post-processing step can use a different process count. Each reader takes a nearly equal slice:

MPI_Offset bytes;
MPI_File_get_size(fh, &bytes);
MPI_Offset total = bytes / sizeof(int);
MPI_Offset first = total * rank / size;
MPI_Offset last  = total * (rank + 1) / size;
MPI_File_read_at_all(fh, first * sizeof(int), buf, (int)(last - first),
                     MPI_INT, MPI_STATUS_IGNORE);

Written by 4 processes, read by 3:

4 ranks wrote 1000 ints each
rank 0 read values 0..1332: correct
rank 1 read values 1333..2665: correct
rank 2 read values 2666..3999: correct

On a real parallel file system

  • Striping. On Lustre, a single shared file only gets parallel bandwidth if it is spread over several storage targets. Set the striping on the output directory (for example with lfs setstripe), or pass the reserved MPI-IO hints striping_factor and striping_unit through an MPI_Info object when the file is created.
  • Alignment. Blocks that start on stripe boundaries avoid two processes writing into the same stripe, which forces locking.
  • Libraries. For structured scientific data, parallel HDF5 or NetCDF are built on MPI-IO and add self-describing metadata. The principles above still decide how fast they run.

Summary

  • Rank-0 funnelling is fine for small output, not for checkpoints.
  • File-per-process scales until the metadata servers do not, and ties restarts to the process count.
  • One shared file written with MPI_File_write_at_all is a few lines of code and works with any number of readers.

Code: shared_file.c. Run mpirun -np 4 ./shared_file write data.bin, then mpirun -np 3 ./shared_file read data.bin.

All writing