NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
nxCDF float16 Storage

nxCDF supports two 16-bit floating-point atomic types: NC_FLOAT16 (IEEE 754 binary16) and NC_BFLOAT16 (brain floating point). Both store 2 bytes per value on disk, but they split the bits differently and suit different data.

Property NC_FLOAT16 NC_BFLOAT16
nc_type value 17 (include/nep.h) 18 (include/nep.h)
Layout 1 sign, 5 exponent, 10 mantissa bits 1 sign, 8 exponent, 7 mantissa bits
Range up to 65504 float32 range, up to ~3.39e38
Precision ~3 significant decimal digits ~2-3 significant decimal digits
File type H5T_IEEE_F16LE H5T_FLOAT_BFLOAT16LE
Memory type raw uint16_t (untyped calls), or float/double (typed calls) the same

Even smaller floats exist — NC_FLOAT8_E4M3, NC_FLOAT8_E5M2, NC_FLOAT6_E2M3, NC_FLOAT6_E3M2 and NC_FLOAT4_E2M1 are stored as raw uint8_t patterns; see the type table in nxcdf.md.

Internal conversion engine

nxCDF uses the private, NEP-owned nxcdf_convert_type() conversion engine, selected as the permanent path in Sprint 1H; the temporary NEP_INTERNAL_CONVERT option and NEP_HAS_INTERNAL_CONVERT marker were removed, and passing the option now fails at configure time. Standard float/double calls on NC_BFLOAT16 use NEP round-to-nearest-even (449 becomes 448), double narrows once, and finite overflow produces signed infinity. Supported bfloat16 attributes use the same rules. Since Sprint 1J (v4.3.0), IEEE float16 uses the same engine: float/double calls on NC_FLOAT16 convert with NEP round-to-nearest-even, single rounding from double, signed infinity on finite overflow, and the former H5Tconvert path is gone. Values at or above 65520 (the halfway point past 65504, the largest binary16) overflow to infinity, as do the boundary roundings that reach it; finite overflow returns NC_ERANGE after all values are processed. Raw-bit I/O and the independent nc_*_bfloat16/nc_*_float16 functions do not change. The engine reports NC_ERANGE after completing out-of-range variable/attribute conversions and quantized writes. The warning does not undo writes, attribute replacement or dimension growth. Precision loss, fractional truncation and floating underflow are not range errors; nonfinite-to-integer conversion is.

The committed OFF/ON benchmark artifacts in examples/nxcdf/conversion_*.csv record the Sprint 1H comparison that selected this engine. See nxcdf.md for the conversion engine's scope.

Rules

  • Native mode only. Small floating-point types are rejected with NC_ENOTNC4 in NC_NETCDF4_MODEL and NC_CLASSIC_MODEL mode (NXCDF_check_atomic_type in src/nxcdf/nxcdf_meta.c).
  • Coordinate variables allowed. Small floating-point types (including FP8/FP6/FP4) may be used as coordinate variables in native mode — the v4.0.0 Sprint 9 ban was lifted in v4.3.0 Sprint 1L1 (issue #380). The dimension-scale dataset carries the variable's small-float dtype, so ordering/comparison of non-IEEE encodings is the caller's concern, and tools that don't know the encodings will show raw bit patterns.
  • Raw bits or float/double. Untyped calls exchange raw bit patterns. For NC_FLOAT16 and NC_BFLOAT16, the typed float and double calls convert through the internal engine. Reads are exact.
  • Write rounding. The internal engine rounds both types to nearest, ties to even. For bfloat16, finite input past the largest bfloat16 becomes signed infinity; for float16, input at or above 65520 (or below -65520) becomes ±infinity. NaN stays a quiet NaN carrying the top bits of its payload (a float32 quiet NaN becomes 0x7e00 for float16). Finite overflow returns NC_ERANGE after all values are processed (issue #381). The nc_*_bfloat16 and nc_*_float16 functions convert in NEP with the same rounding (see below), and bfloat16_conv.h/float16_conv.h provide the same conversion in user code.
  • Other typed calls are rejected. Integer, string and other typed calls return NC_EBADTYPE; the _text calls return NC_ECHAR.
  • FP8/FP6/FP4: untyped calls only. All typed calls are rejected, because HDF5 2.1.1 converts these types incorrectly (for example, E2M1 6.0 is stored as infinity). See HDF5 2.1.1 "Known Problems".
  • No fill value. _FillValue attributes on NC_BFLOAT16 variables are rejected, and a fill value set with nc_def_var_fill is not recovered on reopen (issue #377). Define small-float variables without a fill value.
  • nxCDF itself requires HDF5 2.1.1, so both types are available whenever nxCDF is built. (The per-type minimums are HDF5 1.14 for NC_FLOAT16 and HDF5 2.1.1 for NC_BFLOAT16.)

Why bfloat16 for the GDAS sample

bfloat16 keeps the full float32 exponent range and pays for it with a 7-bit mantissa, so it suits data where a relative error of up to 2^-8 (about 0.4%) is acceptable. IEEE binary16 makes the opposite trade: 3 more mantissa bits (for example, values 0.25 K apart in 256-512 K instead of 2 K), but a maximum of 65504 and a smallest normal value of about 6.1e-5, below which precision drops. The non-fill values in this sample (tmp2m 218.976-316.82 K, prate_ave 0-0.0109397 kg/m**2/s, soilw1 0.02-1, land 0-2) all fit within binary16's maximum, but the input's declared fill value 9.99e+20 and any small prate_ave values below 6.1e-5 do not fit binary16 well, while bfloat16 represents both. (No point in this sample actually equals the fill value.) In absolute terms the maximum errors on the four GDAS fields are 1 K for tmp2m (bfloat16 values are 1 K apart in 128-256 K and 2 K apart in 256-512 K, so tmp2m is stored to the nearest 1-2 K), 3.0e-05 kg/m**2/s for prate_ave, 9.8e-04 (fraction) for soilw1, and 0 for land, whose three small integer values are exactly representable.

bfloat16 I/O functions

libnep provides eight bfloat16 memory-type functions that work on any file the netCDF API can open, not just nxCDF:

int nc_put_var_bfloat16(int ncid, int varid, const nc_bfloat16_t *op)
Write a whole variable from bfloat16 values.
Definition nep_bfloat16.c:388
int nc_put_var1_bfloat16(int ncid, int varid, const size_t *indexp, const nc_bfloat16_t *op)
Write one value to a variable as bfloat16.
Definition nep_bfloat16.c:339
int nc_get_var_bfloat16(int ncid, int varid, nc_bfloat16_t *ip)
Read a whole variable as bfloat16.
Definition nep_bfloat16.c:363
int nc_get_var1_bfloat16(int ncid, int varid, const size_t *indexp, nc_bfloat16_t *ip)
Read one value from a variable as bfloat16.
Definition nep_bfloat16.c:316
int nc_put_vars_bfloat16(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, const nc_bfloat16_t *op)
Write a strided hyperslab of a variable from bfloat16 values.
Definition nep_bfloat16.c:272
int nc_put_vara_bfloat16(int ncid, int varid, const size_t *startp, const size_t *countp, const nc_bfloat16_t *op)
Write a hyperslab of a variable from bfloat16 values.
Definition nep_bfloat16.c:302
int nc_get_vars_bfloat16(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, nc_bfloat16_t *ip)
Read a strided hyperslab of a variable as bfloat16.
Definition nep_bfloat16.c:258
int nc_get_vara_bfloat16(int ncid, int varid, const size_t *startp, const size_t *countp, nc_bfloat16_t *ip)
Read a hyperslab of a variable as bfloat16.
Definition nep_bfloat16.c:288

The buffer type is nc_bfloat16_t: the compiler's __bf16 where available (NEP_HAVE_NATIVE_BFLOAT16 = 1), otherwise uint16_t. Both are 2 bytes holding the bfloat16 bit pattern, so raw-bits code casts the pointer — (nc_bfloat16_t *)(void *)bits — rather than assigning values.

What the functions do depends on the variable's type:

Variable type Behavior
NC_BFLOAT16 Raw bit patterns, no conversion
NC_FLOAT, NC_DOUBLE NEP converts: reads round to nearest, ties to even (single rounding from double); writes widen exactly
NC_CHAR NC_ECHAR
anything else (incl. NC_FLOAT16) NC_EBADTYPE

On a get, a finite float or double whose rounding overflows is stored as ±inf, every value is still converted, and the call returns NC_ERANGE. Infinity and NaN are not range errors, and puts never overflow because widening is exact.

/* Read a float variable as bfloat16, converted by NEP. */
ret = nc_get_vara_bfloat16(ncid, varid, start, count, btmp);
if (ret == NC_ERANGE)
fprintf(stderr, "some values overflowed to bfloat16 infinity\n");
#define NVALS
Definition bfloat16.c:82
uint16_t nc_bfloat16_t
Definition nep.h:49

float16 I/O functions

libnep provides the same eight functions for IEEE binary16, added in Sprint 1J (v4.3.0):

int nc_get_var_float16(int ncid, int varid, nc_float16_t *ip)
Read a whole variable as IEEE 754 float16.
int nc_put_vars_float16(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, const nc_float16_t *op)
Write a strided hyperslab of a variable from IEEE 754 float16 values.
int nc_get_var1_float16(int ncid, int varid, const size_t *indexp, nc_float16_t *ip)
Read one value from a variable as IEEE 754 float16.
int nc_put_var_float16(int ncid, int varid, const nc_float16_t *op)
Write a whole variable from IEEE 754 float16 values.
int nc_put_vara_float16(int ncid, int varid, const size_t *startp, const size_t *countp, const nc_float16_t *op)
Write a hyperslab of a variable from IEEE 754 float16 values.
int nc_get_vara_float16(int ncid, int varid, const size_t *startp, const size_t *countp, nc_float16_t *ip)
Read a hyperslab of a variable as IEEE 754 float16.
int nc_put_var1_float16(int ncid, int varid, const size_t *indexp, const nc_float16_t *op)
Write one value to a variable as IEEE 754 float16.
int nc_get_vars_float16(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, nc_float16_t *ip)
Read a strided hyperslab of a variable as IEEE 754 float16.

The buffer type is nc_float16_t: the compiler's _Float16 where available (NEP_HAVE_NATIVE_FLOAT16 = 1), otherwise uint16_t. Both are 2 bytes holding the binary16 bit pattern, so raw-bits code casts the pointer — (nc_float16_t *)(void *)bits — rather than assigning values.

Variable type Behavior
NC_FLOAT16 Raw bit patterns, no conversion
NC_FLOAT, NC_DOUBLE NEP converts: reads round to nearest, ties to even (single rounding from double); writes widen exactly
NC_CHAR NC_ECHAR
anything else (incl. NC_BFLOAT16) NC_EBADTYPE

On a get, a finite float or double at or above 65520 in magnitude is stored as ±inf, every value is still converted, and the call returns NC_ERANGE. Infinity and NaN are not range errors, and puts never overflow because widening is exact. Values below 6.1e-5 in magnitude become subnormals or zero without a range warning.

Example: examples/nxcdf/bfloat16.c

bfloat16.c reads the tmp2m field and its grid_yt/grid_xt coordinate variables from the GDAS sample — twice: as NC_FLOAT for reference and as bfloat16 through nc_get_var_bfloat16, so NEP does the conversion — writes two nxCDF files — float_tmp2m.nc (NC_FLOAT) and bfloat16_tmp2m.nc (NC_BFLOAT16) — then reopens the bfloat16 file, verifies the bits round-tripped exactly, and reports file sizes and the maximum conversion error.

Conversion (bfloat16_conv.h)

float32 to bfloat16 rounds to nearest, ties to even; NaN stays a quiet NaN and values past the maximum overflow to infinity. bfloat16 to float32 is an exact 16-bit left shift. The nc_*_bfloat16 functions use the same rounding; the program cross-checks nc_get_var_bfloat16 against float_to_bfloat16 on every point:

static inline uint16_t
{
uint32_t u;
memcpy(&u, &f, sizeof(u));
if ((u & 0x7fffffffu) > 0x7f800000u)
return (uint16_t)((u >> 16) | 0x0040u);
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
}
static inline float
bfloat16_to_float(uint16_t b)
{
uint32_t u = (uint32_t)b << 16;
float f;
memcpy(&f, &u, sizeof(f));
return f;
}
static float bfloat16_to_float(uint16_t b)
Convert a bfloat16 bit pattern to float32 (exact).
Definition bfloat16_conv.h:53
static uint16_t float_to_bfloat16(float f)
Convert a float32 to a bfloat16 bit pattern, round to nearest, ties to even.
Definition bfloat16_conv.h:36

Writing the bfloat16 file

The file is created with NC_NXCDF, the variable is defined with NC_BFLOAT16 and no fill value, and the raw uint16_t bits are written with nc_put_vara_bfloat16 (the NC_FLOAT file is written with nc_put_vara_float):

if ((ret = nc_create(path, NC_NXCDF | NC_CLOBBER, &ncid))) ERR(ret);
...
if ((ret = nc_def_var(ncid, "tmp2m", xtype, 3, dimids, &varid))) ERR(ret);
if ((ret = nc_def_var_fill(ncid, varid, NC_NOFILL, NULL))) ERR(ret);
...
/* nc_put_vara_bfloat16 stores the raw bits on an NC_BFLOAT16
* variable; nc_put_vara_float would also work, but HDF5 rounds
* ties away from zero, while float_to_bfloat16() and the NEP
* functions round ties to even. */
if ((ret = nc_put_vara_bfloat16(ncid, varid, start, count,
(nc_bfloat16_t *)(void *)data))) ERR(ret);
#define ERR(e)
Definition dicom_read.c:25
#define NC_NXCDF
Definition nxcdf.h:35

Reopening and checking

The program reopens bfloat16_tmp2m.nc with NC_NXCDF | NC_NOWRITE, checks nc_inq_vartype returns NC_BFLOAT16, reads the bits with nc_get_vara_bfloat16 into a uint16_t buffer, memcmps them against the written bits, and computes the maximum absolute and relative error over the non-fill points. As a cross-check it also reads the same hyperslab with nc_get_vara_float into a float buffer and verifies that every value's bits equal the stored uint16_t pattern shifted left 16 bits — HDF5's bfloat16-to-float conversion is exact:

if ((ret = nc_get_vara_bfloat16(ncid, varid, start, count,
(nc_bfloat16_t *)(void *)bdata_in))) ERR(ret);
if ((ret = nc_get_vara_float(ncid, varid, start, count, typed_in))) ERR(ret);

Building and running

cmake -S . -B build -DNEP_ENABLE_NXCDF=ON -DNEP_BUILD_EXAMPLES=ON \
-DNEP_ZSTD_PLUGIN_DIR=$HOME/plugins/zstd-only \
-DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build -j
cd build/examples/nxcdf
HDF5_PLUGIN_PATH=$HOME/plugins/zstd-only \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./bfloat16 ../../../test/data/netCDF-4/gdas_sfc_sample.nc

-DNEP_ZSTD_PLUGIN_DIR points ctest at a directory holding exactly one HDF5 zstd plugin, needed to read the compressed sample; NCRCENV_RC/NETCDF_RC point netcdf-c at the build-tree .ncrc that autoloads the nxCDF backend. The RelWithDebInfo build type (-O2) is what the benchmark results below were measured with; the benchmark programs print a warning when built without optimization. Real output on the reference machine:

tmp2m: 1179648 values
float32 file: 4743920 bytes
bfloat16 file: 2384624 bytes (50.3% of float32)
max abs error: 1 K
max rel error: 0.00389105
typed nc_get_vara_float: 0 mismatches
nc_get_var_bfloat16 vs float_to_bfloat16: 0 mismatches

h5dump -H bfloat16_tmp2m.nc shows DATATYPE H5T_FLOAT_BFLOAT16LE for tmp2m.

Benchmark: bfloat16_perf and plot_bfloat16.py

bfloat16_perf.c runs all four GDAS fields (tmp2m, prate_ave, soilw1, land) through six configurations — float/bfloat16/bfloat16_hdf5 x contiguous/deflate1 (shuffle + deflate level 1, chunks of 1x768x1536) — with 10 repetitions each, keeping the minimum times. Each file holds 10 copies of the field as time steps (NCOPIES), written and read one step at a time, so the fixed cost of creating, opening and closing a file is spread over more data. The bfloat16 rows convert in user code and use the untyped calls; the bfloat16_hdf5 rows define the same NC_BFLOAT16 variable but write with nc_put_vara_float and read with nc_get_vara_float, so HDF5 does the conversion inside the timed I/O and wconv_s/rconv_s are 0. For each bfloat16_hdf5 row a #-prefixed line is printed to stdout (not to the CSV) counting, for one copy, the stored values that differ from round-to-nearest-even, because HDF5 rounds bfloat16 ties away from zero:

# tmp2m contiguous: 9 of 1179648 values differ from round-to-nearest-even
# tmp2m deflate1: 9 of 1179648 values differ from round-to-nearest-even
# prate_ave contiguous: 8 of 1179648 values differ from round-to-nearest-even
# prate_ave deflate1: 8 of 1179648 values differ from round-to-nearest-even
# soilw1 contiguous: 3 of 1179648 values differ from round-to-nearest-even
# soilw1 deflate1: 3 of 1179648 values differ from round-to-nearest-even
# land contiguous: 0 of 1179648 values differ from round-to-nearest-even
# land deflate1: 0 of 1179648 values differ from round-to-nearest-even

Write time runs from just before conversion (bfloat16 only) through nc_close and an fsync() of the file; before each read the file is dropped from the page cache with posix_fadvise(POSIX_FADV_DONTNEED) (untimed), and read time runs from nc_open through nc_close plus the conversion back to float. Both times cover all 10 copies. Both helpers are in examples/perf_disk.h, so the times are disk-bound. The bfloat16 rows are checked bit-exact and the bfloat16_hdf5 rows are checked against the stored raw bits; the program fails if any contiguous bfloat16 file is not smaller than its float file, or if any relative error exceeds 2^-8.

The CSV written has 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. ratio_vs_float is the matching float row's file_bytes divided by this row's; throughput is measured in MB/s of float32 data (10 x 4718592 bytes) for every row. wconv_s/rconv_s are the minimum float32-to-bfloat16 and bfloat16-to-float32 conversion times — parts of write_s/read_s; 0 for float and bfloat16_hdf5 rows.)

HDF5_PLUGIN_PATH=$HOME/plugins/zstd-only \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./bfloat16_perf ../../../test/data/netCDF-4/gdas_sfc_sample.nc bfloat16_results.csv
python3 ../../../examples/nxcdf/plot_bfloat16.py bfloat16_results.csv bfloat16

Results

Measured on the machine recorded in examples/nxcdf/reference_machine.txt; the full CSV is examples/nxcdf/reference_results.csv. File sizes are in bytes, for files holding 10 copies of the field.

Field float contiguous bfloat16 contiguous bfloat16 (HDF5) contiguous float deflate1 bfloat16 deflate1 bfloat16 (HDF5) deflate1 max rel err max rel err (HDF5)
tmp2m 47192259 23599299 23599299 25022289 1977709 1979129 0.00389105 0.00389105
prate_ave 47192259 23599299 23599299 33543449 13157179 13157179 0.00389081 0.00389081
soilw1 47192259 23599299 23599299 8538049 3021869 3021879 0.00388914 0.00388914
land 47192259 23599299 23599299 758369 646119 646119 0 0
nxCDF file size: float32 vs bfloat16 (4 configurations)
nxCDF disk write/read throughput (MB/s of float32 data): float32 vs bfloat16 (4 configurations)
bfloat16 conversion: NEP vs HDF5, disk write/read throughput (all 6 configurations)

What the numbers show:

  • Every contiguous bfloat16 file is 23599299 bytes, 50.0% of its float file (ratio 2.00) — essentially the 2:1 element-size ratio plus fixed metadata. The bfloat16 (HDF5) files are byte-identical in size contiguous, and within a few bytes deflate1.
  • With shuffle+deflate level 1, bfloat16 compresses much better than float32, because rounding collapses the number of distinct values the filters see. The four fields contain 858874 (tmp2m), 954856 (prate_ave), 202809 (soilw1) and 3 (land) distinct float32 values; after bfloat16 rounding only 68, 6114, 594 and 3 distinct values remain. Compressed sizes drop accordingly: 1977709 vs 25022289 bytes for tmp2m (12.7x smaller), 13157179 vs 33543449 for prate_ave (2.5x), and 3021869 vs 8538049 for soilw1 (2.8x). land already has only 3 distinct values, so rounding changes nothing and the compressed files are close in size (646119 vs 758369 bytes, 1.17x).
  • The maximum relative error is 0.00389 across all fields, just under the 2^-8 = 0.00390625 round-to-nearest bound; land is exact because its three values are representable in bfloat16. The HDF5-converted rows show the same maximum errors — ties away from zero stays within the same bound.
  • With -O2 the in-memory conversion is cheap, and the disk time is halved, so bfloat16 total times are well below the float times. For tmp2m contiguous, write_s is 0.082047 s, of which wconv_s is 0.012499 s, against 0.166828 s for float; read_s is 0.051379 s, of which rconv_s is 0.015501 s, against 0.089417 s for float. With deflate1 the savings grow with the compression gain: tmp2m bfloat16 write is 0.156095 s versus 0.807080 s for float, so even including conversion the total is 5.2x faster.
  • HDF5 conversion is much slower than the user-code conversion on this data. For tmp2m contiguous, bfloat16_hdf5 write is 1.180582 s and read 0.865629 s, against 0.082047 s and 0.051379 s for user conversion — HDF5's generic software conversion inside H5Dwrite/H5Dread costs far more than the disk I/O. Stored file sizes are nearly identical (23599299 bytes contiguous for both paths; 1979129 vs 1977709 bytes deflate1 for tmp2m, 3021879 vs 3021869 for soilw1), and only a handful of stored values differ from round-to-nearest-even: 9 of 1179648 per copy for tmp2m, 8 for prate_ave, 3 for soilw1 and 0 for land.

Benchmark: bfloat16_io_perf

bfloat16_io_perf.c times pure I/O with no conversion, using the committed bfloat16 copy of the GDAS sample test/data/NXCDF/gdas_sfc_sample_bfloat16.nc (see test/data/NXCDF/README.md for how it is made). It checks that the sample's stored bits are exactly float_to_bfloat16() of the float sample, then writes and reads 10 copies of each field (as time steps) as float32 and as bfloat16 bits, contiguous and deflate1, 10 repetitions each, minimum kept. Timing is disk-bound in the same way as bfloat16_perf (fsync() in the write time, page cache dropped before each read):

HDF5_PLUGIN_PATH=$HOME/plugins/zstd-only \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./bfloat16_io_perf ../../../test/data/netCDF-4/gdas_sfc_sample.nc \
../../../test/data/NXCDF/gdas_sfc_sample_bfloat16.nc bfloat16_io_results.csv
python3 ../../../examples/nxcdf/plot_bfloat16_io.py bfloat16_io_results.csv bfloat16

The CSV written has 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; sizes and times cover all copies, and write_MBps/read_MBps are MB/s of float32 data for every row. ratio_vs_float is the matching float row's file_bytes divided by this row's; write_speedup/read_speedup are the matching float row's times divided by this row's — 1.000 on the float rows.) Results are discussed in float16.md, and the reference CSV is examples/nxcdf/reference_io_results.csv.

Example: examples/nxcdf/float16.c

float16.c is the binary16 version of the bfloat16 tutorial. It reads tmp2m and its grid_yt/grid_xt coordinate variables from the GDAS sample — twice: as NC_FLOAT for reference and as float16 through nc_get_var_float16, so NEP does the conversion — writes float_tmp2m.nc (NC_FLOAT) and float16_tmp2m.nc (NC_FLOAT16), reopens the float16 file, verifies the bits round-tripped exactly, and reports file sizes and the maximum conversion error. The conversion helpers live in float16_conv.h, with the same algorithms as the internal engine and nep_float16.c.

Real output on the reference machine:

tmp2m: 1179648 values
float32 file: 4743920 bytes
float16 file: 2384624 bytes (50.3% of float32)
max abs error: 0.125 K
max rel error: 0.000487924
typed nc_get_vara_float: 0 mismatches
nc_get_var_float16 vs float_to_float16: 0 mismatches

The maximum relative error is just under the 2^-11 = 0.000488281 round-to-nearest bound for binary16. h5dump -H float16_tmp2m.nc shows DATATYPE H5T_IEEE_F16LE for tmp2m.

Benchmark: float16_perf and plot_float16.py

float16_perf.c runs the same four GDAS fields through six configurations — float/float16/float16_typed x contiguous/deflate1 — with the same 10-repetition, disk-bound, min-time methodology as bfloat16_perf. The float16 rows convert in user code with float_to_float16()/float16_to_float() and use nc_put_vara_float16/nc_get_vara_float16; the float16_typed rows define the same NC_FLOAT16 variable but write with nc_put_vara_float and read with nc_get_vara_float, so the internal engine converts inside the timed I/O. Both paths use NEP round-to-nearest-even, so there is no differing-rounding report: the typed path's stored bits are checked against float_to_float16() and must match exactly.

The error gate is a combined tolerance: a non-fill point fails only if its relative error exceeds 2^-11 and its absolute error exceeds 2^-25 (half the smallest binary16 subnormal). The absolute-error leg keeps prate_ave honest — values below the 6.1e-5 minimum normal become subnormals or zero, which is precision loss the format cannot avoid, not a conversion bug — while the relative leg still bounds every normal-range value.

HDF5_PLUGIN_PATH=$HOME/plugins/zstd-only \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./float16_perf ../../../test/data/netCDF-4/gdas_sfc_sample.nc float16_results.csv
python3 ../../../examples/nxcdf/plot_float16.py float16_results.csv float16

Results

Measured on the machine recorded in examples/nxcdf/reference_machine.txt; the full CSV is examples/nxcdf/reference_float16_results.csv. File sizes are in bytes, for files holding 10 copies of the field.

Field float contiguous float16 contiguous float16 (typed) contiguous float deflate1 float16 deflate1 float16 (typed) deflate1 max rel err
tmp2m 47192259 23599299 23599299 25022289 5493699 5493699 0.000487924
prate_ave 47192259 23599299 23599299 33543449 9277849 9277849 1*
soilw1 47192259 23599299 23599299 8538049 3892069 3892069 0.000486854
land 47192259 23599299 23599299 758369 402889 402889 0

*prate_ave's maximum relative error of 1 comes from values below binary16's smallest normal that round to a subnormal or zero; its maximum absolute error is only 3.4e-06 kg/m**2/s.

nxCDF file size: float32 vs float16 (4 configurations)
nxCDF disk write/read throughput (MB/s of float32 data): float32 vs float16 (4 configurations)
float16 conversion: user code vs internal engine, disk write/read throughput (all 6 configurations)

What the numbers show:

  • Every contiguous float16 file is 23599299 bytes, 50.0% of its float file, identical to bfloat16 — both are 2-byte types plus the same metadata.
  • With shuffle+deflate level 1, float16 compresses less well than bfloat16 because the three extra mantissa bits leave more distinct values, but still much better than float32: tmp2m 5493699 bytes (4.6x smaller than float, against bfloat16's 12.7x), prate_ave 9277849 (3.6x), soilw1 3892069 (2.2x), land 402889 (1.9x).
  • The maximum relative error is 0.00049 on tmp2m and soilw1, just under the 2^-11 bound; land is exact. prate_ave's relative error reaches 1 only at subnormal-underflow points (absolute error bounded by 3.4e-06), which is the expected binary16 trade-off for that field.
  • The typed path costs only a little more than user-code conversion — for tmp2m contiguous, typed write is 0.110 s against 0.089 s for user code (0.027 s of which is wconv_s), and typed read is 0.057 s against 0.055 s — because the internal engine is a compiled loop like the user code, not HDF5's generic software conversion. The extra cost over user code is the engine's extra full-buffer pass on reads (issue #386) and the in-engine conversion on writes.

Benchmark: float16_io_perf

float16_io_perf.c times pure I/O with no conversion, using the committed float16 copy of the GDAS sample test/data/NXCDF/gdas_sfc_sample_float16.nc (see test/data/NXCDF/README.md). It checks that the sample's stored bits are exactly float_to_float16() of the float sample, then writes and reads 10 copies of each field as float32 and as float16 bits, contiguous and deflate1, 10 repetitions each, minimum kept. Results are in examples/nxcdf/reference_float16_io_results.csv:

nxCDF disk write/read throughput (MB/s of float32 data): float32 vs data already in float16
  • Contiguous reads are 1.88-1.94x faster for float16 (984-1031 MB/s of float32 data against 508-531 MB/s), the same disk-bound 2x ceiling as bfloat16.
  • Contiguous writes are 1.8-2.9x faster (604-784 against 269-418 MB/s).
  • With deflate1 the gains track the smaller compression ratios: writes 2.2-3.3x and reads 1.9-3.1x faster than float32 (tmp2m 616 against 281 MB/s to read; prate_ave 519 against 165 MB/s).