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

Store a real meteorological field as NC_BFLOAT16 in a nxCDF file. More...

#include <netcdf.h>
#include <nep.h>
#include "nxcdf/nxcdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "bfloat16_conv.h"

Macros

#define NY   768
#define NX   1536
#define NVALS   ((size_t)NY * NX)
#define ERRCODE   2
#define ERR(e)

Functions

static void get_att_text (int ncid, int varid, const char *name, char **text, size_t *len)
 Read a null-padded text attribute into a malloc'ed buffer.
static int write_file (const char *path, nc_type xtype, const void *data, const double *grid_yt, const double *grid_xt, const char *long_name, size_t long_name_len, const char *units, size_t units_len)
 Write one field and the coordinate variables to a nxCDF file.
int main (int argc, char **argv)
 Main entry point.

Detailed Description

Store a real meteorological field as NC_BFLOAT16 in a nxCDF file.

bfloat16 (brain floating point) is a 16-bit floating-point format with 1 sign bit, 8 exponent bits and 7 mantissa bits. It has the same range as float32, about 2-3 significant decimal digits of precision, and takes half the storage. It suits data where a relative error of up to 2^-8 (about 0.4%) is acceptable.

This program reads the tmp2m (2 m temperature) field and its coordinate variables from the GDAS sample file test/data/netCDF-4/gdas_sfc_sample.nc, writes it twice to nxCDF files - once as NC_FLOAT and once as NC_BFLOAT16 - then reads the bfloat16 file back, checks the bits round-tripped exactly, and reports file sizes and the maximum conversion error.

Usage:

HDF5_PLUGIN_PATH=<dir with one zstd plugin> \
NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> \
./bfloat16 gdas_sfc_sample.nc

The input file is zstd-compressed, so HDF5_PLUGIN_PATH must point to a directory holding the zstd HDF5 plugin. The nxCDF backend is loaded through the .ncrc UDF9 autoload mechanism, so NCRCENV_RC and NETCDF_RC must name the NEP build directory that contains the generated .ncrc.

Learning Objectives:

  • Create a nxCDF file with nc_create() and NC_NXCDF
  • Define an NC_BFLOAT16 variable
  • Read and write bfloat16 data with the nc_*_bfloat16() functions
  • Read NC_BFLOAT16 data as float with nc_get_vara_float()
  • Understand the precision and size trade-offs of bfloat16

Key Concepts:

  • Raw bit patterns: NC_BFLOAT16 data is exchanged with the library as raw uint16_t bit patterns
  • Conversion by NEP: the nc_*_bfloat16() functions convert between bfloat16 and float/double variables in the library; float_to_bfloat16() stays as the reference conversion and is used here as a cross-check; the typed float/double calls convert through HDF5 instead
  • Native mode only: NC_BFLOAT16 is only available in nxCDF native mode
  • No fill value: bfloat16 variables are defined without a fill value

Key API functions:

  • nc_create() Create a nxCDF file with NC_NXCDF
  • nc_def_var() Define a variable with NC_BFLOAT16
  • nc_get_var_bfloat16() Read a float variable as bfloat16 (NEP converts)
  • nc_put_vara_bfloat16() Write bfloat16 bits to an NC_BFLOAT16 variable
  • nc_get_vara_bfloat16() Read bfloat16 bits from an NC_BFLOAT16 variable
  • nc_get_vara_float() Read bfloat16 data converted to float

Related Examples:

  • bfloat16_perf.c - Benchmark of float32 vs bfloat16 size and speed
  • compression.c - NetCDF-4 compression tutorial
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

◆ 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.

Function Documentation

◆ get_att_text()

void get_att_text ( int ncid,
int varid,
const char * name,
char ** text,
size_t * len )
static

Read a null-padded text attribute into a malloc'ed buffer.

Parameters
ncidNetCDF file ID.
varidVariable ID.
nameAttribute name.
textReturned buffer (caller frees); NULL if absent.
lenReturned attribute length; 0 if absent.

◆ main()

int main ( int argc,
char ** argv )

Main entry point.

Reads tmp2m and its coordinate variables from the GDAS sample, writes float_tmp2m.nc (NC_FLOAT) and bfloat16_tmp2m.nc (NC_BFLOAT16), verifies the bfloat16 bits round-trip exactly, and prints file sizes and conversion errors.

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

◆ write_file()

int write_file ( const char * path,
nc_type xtype,
const void * data,
const double * grid_yt,
const double * grid_xt,
const char * long_name,
size_t long_name_len,
const char * units,
size_t units_len )
static

Write one field and the coordinate variables to a nxCDF file.

Creates path with nc_create(NC_NXCDF | NC_CLOBBER), defines fixed dims time = 1, grid_yt = NY and grid_xt = NX, the NC_DOUBLE coordinate variables grid_yt and grid_xt, and the variable tmp2m(time, grid_yt, grid_xt) of type xtype with the long_name and units attributes copied from the input. The variable gets no fill value.

Parameters
pathOutput file path.
xtypeVariable type (NC_FLOAT or NC_BFLOAT16).
dataData buffer (float or uint16_t, NVALS elements).
grid_ytY coordinates (NY doubles).
grid_xtX coordinates (NX doubles).
long_namelong_name attribute text.
long_name_lenLength of long_name.
unitsunits attribute text.
units_lenLength of units.
Returns
0 on success, exits on NetCDF error.