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

Benchmark NC_FLOAT vs NC_BFLOAT16 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 "bfloat16_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   "bfloat16_perf_tmp.nc"
#define ERRCODE   2
#define ERR(e)

Enumerations

enum  cfg_type_t { CFG_FLOAT , CFG_BF16_USER , CFG_BF16_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 *bdata, 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_BFLOAT16 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), (bfloat16, contiguous), (bfloat16_hdf5, contiguous), (float, deflate level 1 with shuffle), (bfloat16, deflate level 1 with shuffle) and (bfloat16_hdf5, 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 bfloat16 rows convert float32 to bfloat16 in user code and exchange raw uint16_t bits through nc_put_vara_bfloat16() and nc_get_vara_bfloat16(). The bfloat16_internal rows define the same NC_BFLOAT16 variable but write the float data with nc_put_vara_float() and read with nc_get_vara_float(). Internal conversion is now permanent, so NEP performs the conversion; historical runs labeled bfloat16_hdf5 used the HDF5 engine. wconv_s and rconv_s are 0. For each typed-conversion row a #-prefixed line is printed to stdout (not to the CSV) with the number of stored values that differ from round-to-nearest-even.

Write time runs from just before the float32 to bfloat16 conversion (bfloat16 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 bfloat16. 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-bfloat16 conversion time (part of write_s) and rconv_s the minimum bfloat16-to-float32 conversion time (part of read_s); both are 0 for float and bfloat16_hdf5 rows. Each row is also printed to stdout.

Typical workflow:

./bfloat16_perf gdas_sfc_sample.nc bfloat16_results.csv
python3 plot_bfloat16.py bfloat16_results.csv bfloat16

Learning Objectives:

  • Measure the file-size benefit of bfloat16 storage
  • Compare contiguous and shuffle+deflate storage for both types
  • Quantify the maximum conversion error of bfloat16

Key Concepts:

  • Raw bit patterns: bfloat16 data is written and read as uint16_t through nc_put_vara_bfloat16()/nc_get_vara_bfloat16(), or converted inside HDF5 by the typed float calls
  • Minimum of 10 runs: reduces timing noise
  • Gates: every contiguous bfloat16 file must be smaller than its float file, and the maximum relative error must not exceed 2^-8

Related Examples:

  • bfloat16.c - Tutorial that stores tmp2m as NC_BFLOAT16
  • 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   "bfloat16_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_BF16_USER 

NC_BFLOAT16 variable, conversion in user code.

CFG_BF16_TYPED 

NC_BFLOAT16 variable, conversion inside HDF5.

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/bfloat16/bfloat16_hdf5 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 * bdata,
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_BF16_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_BF16_USER use nc_put_vara_float()/nc_put_vara_bfloat16() and check the raw bits; CFG_BF16_TYPED uses the typed float calls and checks the read floats against 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_BF16_USER or CFG_BF16_TYPED).
deflateNon-zero for shuffle+deflate level 1, 0 for contiguous.
rowResult row to fill in.
bdataScratch buffer of NVALS uint16_t.
rdataScratch buffer of at least NCOPIES*NVALS*4 bytes.
backScratch buffer of NCOPIES*NVALS floats for the bfloat16 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.