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

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.
 

Detailed Description

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:

./chunking > results.csv
# Import results.csv into any spreadsheet or Python/R plotting tool

Learning Objectives:

Key Concepts:

Prerequisites:

Related Examples:

Key API functions:

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.

Macro Definition Documentation

◆ CHUNK_X_COL

#define CHUNK_X_COL   1

◆ CHUNK_X_TIME

#define CHUNK_X_TIME   72

◆ CHUNK_Y_COL

#define CHUNK_Y_COL   1

◆ CHUNK_Y_TIME

#define CHUNK_Y_TIME   36

◆ CHUNK_Z_COL

#define CHUNK_Z_COL   100

◆ CHUNK_Z_TIME

#define CHUNK_Z_TIME   1

◆ ERR

#define ERR (   e)    {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

Error handling macro.

◆ ERRCODE

#define ERRCODE   2

Error exit code.

◆ NX

#define NX   72

Longitude dimension size

◆ NY

#define NY   36

Latitude dimension size

◆ NZ

#define NZ   100

Time dimension size

Function Documentation

◆ get_time()

static double get_time ( void  )
static

Return current wall-clock time in seconds.

Returns
Seconds since the epoch as a double.

◆ main()

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.

Returns
0 on success, 1 on any error.

◆ time_access_column()

static int time_access_column ( const char *  path,
const char *  shape_label 
)
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.

Parameters
pathPath to the NetCDF file to read.
shape_labelShort string identifying the chunk shape (for CSV output).
Returns
0 on success, 1 on error.

◆ time_access_time_slab()

static int time_access_time_slab ( const char *  path,
const char *  shape_label 
)
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.

Parameters
pathPath to the NetCDF file to read.
shape_labelShort string identifying the chunk shape (for CSV output).
Returns
0 on success, 1 on error.

◆ write_file()

static int write_file ( const char *  path,
size_t  chunk_z,
size_t  chunk_y,
size_t  chunk_x 
)
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.

Parameters
pathPath to the NetCDF file to create (overwritten if it exists).
chunk_zChunk size along the time dimension.
chunk_yChunk size along the latitude dimension.
chunk_xChunk size along the longitude dimension.
Returns
0 on success, 1 on error.