NEP 2.7.0
NetCDF Extension Pack
Loading...
Searching...
No Matches
Macros | Functions
square16_par.c File Reference

Demonstrates parallel NetCDF-4 I/O with MPI using a 2x2 rank decomposition. More...

#include <netcdf.h>
#include <netcdf_par.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

Macros

#define NDIMS   2
 
#define QUAD_SIZE   8
 
#define DIM_SIZE   16
 
#define ERR(e)
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Demonstrates parallel NetCDF-4 I/O with MPI using a 2x2 rank decomposition.

This example shows how to use NetCDF-4's parallel I/O capabilities with MPI to write and read a shared dataset concurrently from multiple processes. 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:

Key Concepts:

Rank Layout (2×2 grid):

col 0-7 col 8-15
+----------+----------+
| rank 0 | rank 2 | rows 0-7
+----------+----------+
| rank 1 | rank 3 | rows 8-15
+----------+----------+

Prerequisites:

Related Examples:

Compilation:

mpicc -o square16_par square16_par.c -lnetcdf

Usage:

mpirun -n 4 ./square16_par

Expected Output:

Parallel I/O write and read-back successful. File: square16_par.nc
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

Macro Definition Documentation

◆ DIM_SIZE

#define DIM_SIZE   16

◆ ERR

#define ERR (   e)
Value:
do { \
fprintf(stderr, "Error at line %d: %s\n", __LINE__, nc_strerror(e)); \
MPI_Abort(MPI_COMM_WORLD, 1); \
} while(0)

◆ NDIMS

#define NDIMS   2

◆ QUAD_SIZE

#define QUAD_SIZE   8

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)