|
NEP 2.7.0
NetCDF Extension Pack
|
Demonstrates how chunk shape selection affects NetCDF-4 I/O performance. More...
#include <netcdf.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/time.h>Macros | |
| #define | NZ 100 |
| #define | NY 36 |
| #define | NX 72 |
| #define | CHUNK_Z_TIME 1 |
| #define | CHUNK_Y_TIME 36 |
| #define | CHUNK_X_TIME 72 |
| #define | CHUNK_Z_COL 100 |
| #define | CHUNK_Y_COL 1 |
| #define | CHUNK_X_COL 1 |
| #define | ERRCODE 2 |
| #define | ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} |
Functions | |
| static double | get_time (void) |
| Return current wall-clock time in seconds. | |
| static int | write_file (const char *path, size_t chunk_z, size_t chunk_y, size_t chunk_x) |
| Create a NZ×NY×NX temperature file with the given chunk shape. | |
| static int | time_access_time_slab (const char *path, const char *shape_label) |
| Time the "time slab" access pattern: read all NZ horizontal fields. | |
| static int | time_access_column (const char *path, const char *shape_label) |
| Time the "column profile" access pattern: read all NY×NX point time series. | |
| int | main (void) |
| Main entry point. | |
Demonstrates how chunk shape selection affects NetCDF-4 I/O performance.
Chunk shape is one of the most impactful performance decisions when creating a NetCDF-4 file. The optimal shape depends entirely on how the data will be accessed: a layout tuned for one access pattern is often worst-case for another.
This program creates a meteorologically realistic 3D temperature dataset (100 time steps × 36 latitudes × 72 longitudes) with two different chunk shapes and measures read performance across two contrasting access patterns:
Chunk shapes tested:
Access patterns measured:
Output: CSV printed to stdout with columns: chunk_shape, access_pattern, elapsed_s, MB_per_s
The 2×2 contrast (two shapes × two patterns) demonstrates that a 10–100× performance difference is common between well-matched and mismatched configurations.
Plotting the output:
Learning Objectives:
Key Concepts:
Prerequisites:
Related Examples:
Key API functions:
| #define CHUNK_X_COL 1 |
| #define CHUNK_X_TIME 72 |
| #define CHUNK_Y_COL 1 |
| #define CHUNK_Y_TIME 36 |
| #define CHUNK_Z_COL 100 |
| #define CHUNK_Z_TIME 1 |
| #define ERR | ( | e | ) | {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);} |
Error handling macro.
| #define ERRCODE 2 |
Error exit code.
| #define NX 72 |
Longitude dimension size
| #define NY 36 |
Latitude dimension size
| #define NZ 100 |
Time dimension size
|
static |
Return current wall-clock time in seconds.
| int main | ( | void | ) |
Main entry point.
Creates two NetCDF files with contrasting chunk shapes, runs two access patterns on each, and prints CSV results to stdout.
|
static |
Time the "column profile" access pattern: read all NY×NX point time series.
Issues NY×NX nc_get_vara_float() calls, each requesting one complete [NZ, 1, 1] column. With column-optimized chunks this loads one chunk per call; with time-optimized chunks each call must touch NZ chunks.
| path | Path to the NetCDF file to read. |
| shape_label | Short string identifying the chunk shape (for CSV output). |
|
static |
Time the "time slab" access pattern: read all NZ horizontal fields.
Issues NZ separate nc_get_vara_float() calls, each requesting one complete [1, NY, NX] slab. With time-optimized chunks this loads one chunk per call; with column-optimized chunks each call must load NY×NX chunks.
| path | Path to the NetCDF file to read. |
| shape_label | Short string identifying the chunk shape (for CSV output). |
|
static |
Create a NZ×NY×NX temperature file with the given chunk shape.
For time-optimized chunks (1×NY×NX) the file is written one time slab at a time, which aligns with the chunk boundaries. For column-optimized chunks (NZ×1×1) the file is written one column at a time, which aligns with those chunk boundaries and avoids the extreme slowness of writing column chunks via horizontal slabs.
| path | Path to the NetCDF file to create (overwritten if it exists). |
| chunk_z | Chunk size along the time dimension. |
| chunk_y | Chunk size along the latitude dimension. |
| chunk_x | Chunk size along the longitude dimension. |