NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
float16_perf.c File Reference

Benchmark NC_FLOAT vs NC_FLOAT16 file size and I/O throughput on four GDAS surface fields. More...

#include <netcdf.h>
#include <nep.h>
#include "nxcdf/nxcdf.h"
#include "nep_meta.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include "float16_conv.h"
#include "perf_disk.h"

Data Structures

struct  row_t
 One benchmark row for one field/type/storage configuration. More...

Macros

#define NFIELDS   4
#define NY   768
#define NX   1536
#define NVALS   ((size_t)NY * NX)
#define NREPS   10
#define NCOPIES   10
#define TMP_FILE   "float16_perf_tmp.nc"
#define ERRCODE   2
#define ERR(e)

Enumerations

enum  cfg_type_t { CFG_FLOAT , CFG_F16_USER , CFG_F16_TYPED }

Functions

static double get_time (void)
 Return current wall-clock time in seconds.
static int run_config (const char *field, const float *fdata, cfg_type_t type, int deflate, row_t *row, uint16_t *hdata, uint16_t *rdata, float *back)
 Write and read one field in one configuration NREPS times.
int main (int argc, char **argv)
 Main entry point.

Variables

static const char * FIELDS [NFIELDS] = {"tmp2m", "prate_ave", "soilw1", "land"}

Detailed Description

Benchmark NC_FLOAT vs NC_FLOAT16 file size and I/O throughput on four GDAS surface fields.

Reads tmp2m, prate_ave, soilw1 and land from the GDAS sample file test/data/netCDF-4/gdas_sfc_sample.nc and, for each field, writes and reads a nxCDF file in six configurations: (float, contiguous), (float16, contiguous), (float16_typed, contiguous), (float, deflate level 1 with shuffle), (float16, deflate level 1 with shuffle) and (float16_typed, deflate level 1 with shuffle). Each file holds NCOPIES (10) copies of the field as time steps, written and read one step at a time, so the fixed cost of creating, opening and closing a file is spread over more data. Each configuration is repeated 10 times and the minimum times are kept.

The float16 rows convert float32 to binary16 in user code and exchange raw uint16_t bits through nc_put_vara_float16() and nc_get_vara_float16(). The float16_typed rows define the same NC_FLOAT16 variable but write the float data with nc_put_vara_float() and read with nc_get_vara_float(), so the internal conversion engine does the conversion; wconv_s and rconv_s are 0. Both paths use NEP round-to-nearest-even, so they produce identical stored bits - unlike the historical bfloat16 HDF5 rows, there is no differing-rounding report. The typed path's read is the engine's convert-out path, which (issue #386) currently takes an extra full-buffer pass over the converted data.

Write time runs from just before the float32 to binary16 conversion (float16 only) through nc_close() and an fsync() of the file (nep_perf_sync_file()), so the data has reached the storage device. Before each read the file is dropped from the page cache (nep_perf_drop_cache(), untimed), so every read comes from the device. Read time runs from nc_open() through nc_close() plus the conversion back to float for float16. Both times cover the whole file (all NCOPIES steps). Timing uses clock_gettime(CLOCK_MONOTONIC). Throughput is reported in MB/s of float32 data (NCOPIES x 4718592 bytes) for every row so the speeds compare the same user data.

Output: CSV written to the path given on the command line, with columns:

field,type,storage,copies,uncompressed_bytes,file_bytes,ratio_vs_float,write_s,read_s,write_MBps,read_MBps,max_abs_err,max_rel_err,wconv_s,rconv_s

copies is NCOPIES; uncompressed_bytes, file_bytes, write_s and read_s cover all copies. wconv_s is the minimum float32-to-binary16 conversion time (part of write_s) and rconv_s the minimum binary16-to-float32 conversion time (part of read_s); both are 0 for float and float16_typed rows. Each row is also printed to stdout.

Typical workflow:

./float16_perf gdas_sfc_sample.nc float16_results.csv
python3 plot_float16.py float16_results.csv float16

Learning Objectives:

  • Measure the file-size benefit of binary16 storage
  • Compare contiguous and shuffle+deflate storage for both types
  • Quantify the maximum conversion error of binary16, including subnormal underflow on small prate_ave values

Key Concepts:

  • Raw bit patterns: float16 data is written and read as uint16_t through nc_put_vara_float16()/nc_get_vara_float16(), or converted by the internal engine in the typed float calls
  • Minimum of 10 runs: reduces timing noise
  • Gates: every contiguous float16 file must be smaller than its float file, and every non-fill point must be within the error gate: relative error at most 2^-11, or absolute error below half the smallest binary16 subnormal (subnormal underflow)

Related Examples:

  • float16.c - Tutorial that stores tmp2m as NC_FLOAT16
  • bfloat16_perf.c - The bfloat16 version of this benchmark
  • deflate.c - Deflate/shuffle benchmark for NetCDF-4 files
Note
The program is intended for local performance profiling. It is registered as a ctest test only when NEP_ENABLE_BENCHMARKS=ON. Reference results are measured with -DCMAKE_BUILD_TYPE=RelWithDebInfo (-O2).
Author
Edward Hartnett, Intelligent Data Design, Inc.

Macro Definition Documentation

◆ ERR

#define ERR ( e)
Value:
{printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
#define ERRCODE
Definition bfloat16.c:84

◆ ERRCODE

#define ERRCODE   2

◆ NCOPIES

#define NCOPIES   10

Copies of the field written to (and read from) each file, as time steps, so the fixed cost of opening and closing a file is spread over more data.

◆ NFIELDS

#define NFIELDS   4

Number of GDAS fields benchmarked.

◆ NREPS

#define NREPS   10

Repetitions per configuration; the minimum time is kept.

◆ NVALS

#define NVALS   ((size_t)NY * NX)

Number of values in one field.

◆ NX

#define NX   1536

Length of the grid_xt dimension of the GDAS sample.

◆ NY

#define NY   768

Length of the grid_yt dimension of the GDAS sample.

◆ TMP_FILE

#define TMP_FILE   "float16_perf_tmp.nc"

Temporary NetCDF file created for each measurement.

Enumeration Type Documentation

◆ cfg_type_t

enum cfg_type_t

Which conversion path a configuration uses.

Enumerator
CFG_FLOAT 

NC_FLOAT variable, no conversion.

CFG_F16_USER 

NC_FLOAT16 variable, conversion in user code.

CFG_F16_TYPED 

NC_FLOAT16 variable, conversion by the internal engine.

Function Documentation

◆ get_time()

double get_time ( void )
static

Return current wall-clock time in seconds.

Returns
Seconds from CLOCK_MONOTONIC as a double.

◆ main()

int main ( int argc,
char ** argv )

Main entry point.

For each of the four GDAS fields, runs the six configurations (float/float16/float16_typed x contiguous/deflate1), appends one CSV row per configuration to the output file and prints it to stdout.

Parameters
argcArgument count; must be 3.
argvargv[1] is the input gdas_sfc_sample.nc path, argv[2] the output CSV path.
Returns
0 on success, 1 on gate or verification failure, 2 on usage or NetCDF error.

◆ run_config()

int run_config ( const char * field,
const float * fdata,
cfg_type_t type,
int deflate,
row_t * row,
uint16_t * hdata,
uint16_t * rdata,
float * back )
static

Write and read one field in one configuration NREPS times.

For each repetition: creates TMP_FILE with nc_create(NC_NXCDF | NC_CLOBBER), defines fixed dims time = NCOPIES, grid_yt = NY, grid_xt = NX and one variable of the field name, optionally chunked 1x768x1536 with shuffle and deflate level 1, writes the field once per time step (converting it again for each step in the CFG_F16_USER case), closes and fsyncs the file (timed), stats it, drops it from the page cache (untimed), reopens it, reads every time step back and checks it. CFG_FLOAT and CFG_F16_USER use nc_put_vara_float()/nc_put_vara_float16() and check the raw bits; CFG_F16_TYPED uses the typed float calls and checks the read floats and the stored raw bits on the last repetition. The minimum write and read times and the last repetition's file size and error metrics are returned in row.

Parameters
fieldField name (used as the variable name).
fdataInput float data (NVALS elements).
typeConversion path (CFG_FLOAT, CFG_F16_USER or CFG_F16_TYPED).
deflateNon-zero for shuffle+deflate level 1, 0 for contiguous.
rowResult row to fill in.
hdataScratch buffer of NVALS uint16_t.
rdataScratch buffer of at least NCOPIES*NVALS*4 bytes.
backScratch buffer of NCOPIES*NVALS floats for the binary16 to float32 conversion.
Returns
0 on success, 1 on verification failure, exits on NetCDF error.

Variable Documentation

◆ FIELDS

const char* FIELDS[NFIELDS] = {"tmp2m", "prate_ave", "soilw1", "land"}
static

Field names in the GDAS sample.