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

Benchmark NC_FLOAT vs NC_BFLOAT16 write and read time on data already in bfloat16, with no conversion. More...

#include <netcdf.h>
#include <nep.h>
#include "nxcdf/nxcdf.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_io_perf_tmp.nc"
#define ERRCODE   2
#define ERR(e)

Functions

static double get_time (void)
 Return current wall-clock time in seconds.
static int run_config (const char *field, const void *data, int is_bf16, int deflate, row_t *row, void *rbuf)
 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 write and read time on data already in bfloat16, with no conversion.

Unlike bfloat16_perf.c, which times the float32 to bfloat16 conversion as part of the write and read, this benchmark measures pure I/O: it writes and reads the four GDAS surface fields (tmp2m, prate_ave, soilw1, land) in four configurations: (float, contiguous), (bfloat16, contiguous), (float, deflate level 1 with shuffle) and (bfloat16, deflate level 1 with shuffle). The bfloat16 data come from test/data/NXCDF/gdas_sfc_sample_bfloat16.nc, a bfloat16 copy of the GDAS sample made by make_bfloat16_sample.c, so the timed regions contain no conversion at all. 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 program first checks that the bfloat16 sample's stored bits are exactly float_to_bfloat16() of the float sample's values, fill points included, and fails if they are not.

Write time runs from nc_create() 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(). Both times cover the whole file (all NCOPIES steps). Timing uses clock_gettime(CLOCK_MONOTONIC). Every read is checked bit-for-bit against the data written.

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,write_speedup,read_speedup

copies is NCOPIES; uncompressed_bytes, file_bytes, write_s and read_s cover all copies. write_MBps and read_MBps are MB/s of float32 data (NCOPIES x 4718592 bytes) for every row, so the speeds compare the same user data. ratio_vs_float is the (float, same storage) file_bytes divided by this row's; write_speedup and read_speedup are the (float, same storage) times divided by this row's. They are 1.000 for the float rows. Each row is also printed to stdout.

Typical workflow:

./bfloat16_io_perf gdas_sfc_sample.nc gdas_sfc_sample_bfloat16.nc bfloat16_io_results.csv
python3 plot_bfloat16_io.py bfloat16_io_results.csv bfloat16

Learning Objectives:

  • Measure the I/O benefit of bfloat16 storage with no conversion
  • Compare contiguous and shuffle+deflate storage for both types
  • Check a bfloat16 file against a float32 reference

Key Concepts:

  • Data already in bfloat16, no conversion: the timed regions are pure I/O through nc_put_vara_bfloat16()/nc_get_vara_bfloat16() on raw uint16_t bits
  • Minimum of 10 runs: reduces timing noise
  • Gates: the sample bits must be float_to_bfloat16() of the float sample, and every contiguous bfloat16 file must be smaller than its float file

Related Examples:

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_io_perf_tmp.nc"

Temporary NetCDF file created for each measurement.

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, reads the float32 data from the float sample and the bfloat16 bits from the bfloat16 sample, verifies the bits are float_to_bfloat16() of the floats, runs the four configurations (float/bfloat16 x contiguous/deflate1) and appends one CSV row per configuration to the output file, printing each to stdout.

Parameters
argcArgument count; must be 4.
argvargv[1] is gdas_sfc_sample.nc, argv[2] gdas_sfc_sample_bfloat16.nc, argv[3] 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 void * data,
int is_bf16,
int deflate,
row_t * row,
void * rbuf )
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 with no fill value (nc_def_var_fill NC_NOFILL for both types, so the float and bfloat16 files match), optionally chunked 1x768x1536 with shuffle and deflate level 1, writes the data once per time step with nc_put_vara_float() or nc_put_vara_bfloat16(), closes and fsyncs the file (timed), stats it, drops it from the page cache (untimed), reopens it, reads the data back and checks the bits with memcmp(). There is no conversion in either timed region. The minimum write and read times and the last repetition's file size are returned in row.

Parameters
fieldField name (used as the variable name).
dataInput data (NVALS floats or uint16_t bfloat16 bits).
is_bf16Non-zero for an NC_BFLOAT16 variable.
deflateNon-zero for shuffle+deflate level 1.
rowResult row to fill in.
rbufScratch buffer of at least NCOPIES*NVALS*4 bytes.
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.