Parallel NetCDF-4 I/O with MPI using a 2x2 rank decomposition (Fortran)
Fortran equivalent of square16_par.c, showing how to use NetCDF-4 parallel I/O with MPI from Fortran 90. Four MPI ranks cooperate to write a single 16x16 integer array, each contributing an 8x8 quadrant filled with its own rank number.
The program demonstrates the fundamental parallel I/O pattern: open a file collectively, define the full dataset shape once, then have each rank write only its local portion using hyperslab selections (start/count). After writing, it performs a parallel read-back to verify correctness.
Learning Objectives:
- Open/create NetCDF files in parallel with nf90_create_par() and nf90_open_par()
- Use nf90_var_par_access() to enable collective I/O mode (NF90_COLLECTIVE)
- Compute per-rank hyperslab offsets for a 2D domain decomposition (1-based indexing)
- Write and read variable subsets with nf90_put_var()/nf90_get_var() via start/count
- Verify parallel write correctness with a coordinated read-back
Key Concepts:
- Collective I/O: All ranks participate in every I/O call (NF90_COLLECTIVE), which allows the MPI-IO layer to optimize access patterns
- Independent I/O: Alternative mode (NF90_INDEPENDENT) where each rank calls I/O functions independently; less common for performance-critical code
- Domain Decomposition: Dividing a global array among MPI ranks; here a simple 2x2 block decomposition maps rank → quadrant
- Hyperslab: A rectangular sub-region selected by start() and count() passed to nf90_put_var / nf90_get_var; Fortran uses 1-based indexing
- MPI_INFO_NULL: Passed when no MPI-IO hints are needed; production code may supply hints (e.g., striping) via MPI_Info objects for better performance
- Column-major storage: Fortran arrays are stored column-major, so the innermost dimension varies fastest; dimension ordering is the reverse of C
Rank Layout (2×2 grid):
col 0-7 col 8-15
+----------+----------+
| rank 0 | rank 1 | rows 0-7
+----------+----------+
| rank 2 | rank 3 | rows 8-15
+----------+----------+
Prerequisites:
- f_simple_nc4.f90 - NetCDF-4 format basics
- square16_par.c - C equivalent
- An MPI installation and NetCDF-C/Fortran libraries built with parallel support
Related Examples:
Compilation:
program f_square16_par
Definition f_square16_par.f90:85
Usage:
Expected Output:
- Note
- Companion code for "The NetCDF Developer's Handbook: The Authoritative Guide to Writing
High-Performance Programs for Scientific Data Management, Second Edition" (https://www.amazon.com/dp/B0H7Q1Z75L)
- Author
- Edward Hartnett, Intelligent Data Design, Inc.
- Date
- June 3, 2026