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

Hyperslab variable I/O and memtype checks for fixed-size nxCDF atomic types. More...

#include "nxcdf/nxcdf.h"
#include "config.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "nxcdf_internal.h"
#include "nclist.h"

Functions

hid_t NXCDF_memory_type (nc_type type, int *closep)
 Map a netCDF atomic type to the HDF5 datatype describing the caller's memory buffer.
static int is_small_float (nc_type xtype)
 Return non-zero if xtype is a nxCDF small floating-point type.
static int check_memtype (nc_type xtype, nc_type memtype)
 Decide whether a typed I/O call is allowed on a variable.
int NXCDF_var_par_access (int ncid, int varid, int par_access)
 Select a variable's session-only parallel transfer mode.
static int var_io (int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, void *data, nc_type memtype, int writing)
 Common hyperslab I/O behind the nc_get/put_vara and nc_get/put_vars dispatch entries.
int NXCDF_get_vara (int ncid, int varid, const size_t *start, const size_t *count, void *value, nc_type memtype)
 Read a hyperslab — the nc_get_vara_* dispatch entry.
int NXCDF_put_vara (int ncid, int varid, const size_t *start, const size_t *count, const void *value, nc_type memtype)
 Write a hyperslab — the nc_put_vara_* dispatch entry.
int NXCDF_get_vars (int ncid, int varid, const size_t *start, const size_t *count, const ptrdiff_t *stride, void *value, nc_type memtype)
 Read a strided hyperslab — the nc_get_vars_* dispatch entry.
int NXCDF_put_vars (int ncid, int varid, const size_t *start, const size_t *count, const ptrdiff_t *stride, const void *value, nc_type memtype)
 Write a strided hyperslab — the nc_put_vars_* dispatch entry.
int NXCDF_get_varm (int ncid, int varid, const size_t *s, const size_t *c, const ptrdiff_t *st, const ptrdiff_t *im, void *v, nc_type t)
 Mapped hyperslab read (nc_get_varm_*); not implemented.
int NXCDF_put_varm (int ncid, int varid, const size_t *s, const size_t *c, const ptrdiff_t *st, const ptrdiff_t *im, const void *v, nc_type t)
 Mapped hyperslab write (nc_put_varm_*); not implemented.

Detailed Description

Hyperslab variable I/O and memtype checks for fixed-size nxCDF atomic types.

Author
Edward Hartnett
Date
2026-08-29

Function Documentation

◆ check_memtype()

int check_memtype ( nc_type xtype,
nc_type memtype )
static

Decide whether a typed I/O call is allowed on a variable.

netCDF lets users read/write a variable with a buffer of a different (compatible) type — "the memory type" — and the library converts. nxCDF's rules:

  • Untyped or same-type calls always pass (raw bit patterns, no conversion).
  • Standard types keep netcdf-c behavior: anything goes, the internal engine converts.
  • NC_FLOAT16/NC_BFLOAT16 additionally accept NC_FLOAT and NC_DOUBLE buffers, converted by the internal engine.
  • The FP8/FP6/FP4 types accept only their own type, because HDF5 2.1.1 converts them incorrectly (an HDF5 Known Problem).
  • Text (NC_CHAR) calls on small floats return NC_ECHAR, matching netcdf-c's char/non-char convention.
Parameters
xtypeThe variable's type in the file.
memtypeThe memory type of the I/O call.
Returns
NC_NOERR The memtype is allowed.
NC_ECHAR A text call on a small floating-point variable.
NC_EBADTYPE Any other memtype not allowed for xtype.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ is_small_float()

int is_small_float ( nc_type xtype)
static

Return non-zero if xtype is a nxCDF small floating-point type.

Small floats are NC_FLOAT16, NC_BFLOAT16, and the HDF5 2.x sub-16-bit types NC_FLOAT8_E4M3/E5M2, NC_FLOAT6_E2M3/E3M2 and NC_FLOAT4_E2M1. They get special memtype rules in check_memtype().

Parameters
xtypeThe netCDF type.
Returns
1 for a small floating-point type, 0 otherwise.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ NXCDF_get_vara()

int NXCDF_get_vara ( int ncid,
int varid,
const size_t * start,
const size_t * count,
void * value,
nc_type memtype )

Read a hyperslab — the nc_get_vara_* dispatch entry.

Parameters
ncidNetCDF file identifier.
varidVariable id.
startHyperslab start indices.
countHyperslab counts.
valueDestination buffer.
memtypeNetCDF atomic memory type.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_get_varm()

int NXCDF_get_varm ( int ncid,
int varid,
const size_t * s,
const size_t * c,
const ptrdiff_t * st,
const ptrdiff_t * im,
void * v,
nc_type t )

Mapped hyperslab read (nc_get_varm_*); not implemented.

varm calls add a memory-layout mapping to the strided selection. The feature is deprecated upstream and deliberately unimplemented here; every call returns NC_ENOTBUILT.

Parameters
ncidNetCDF file identifier.
varidVariable id.
sHyperslab start indices.
cHyperslab counts.
stHyperslab strides.
imMemory mapping offsets.
vDestination buffer.
tNetCDF atomic memory type.
Returns
NC_ENOTBUILT always.

◆ NXCDF_get_vars()

int NXCDF_get_vars ( int ncid,
int varid,
const size_t * start,
const size_t * count,
const ptrdiff_t * stride,
void * value,
nc_type memtype )

Read a strided hyperslab — the nc_get_vars_* dispatch entry.

Parameters
ncidNetCDF file identifier.
varidVariable id.
startHyperslab start indices.
countHyperslab counts.
strideHyperslab strides.
valueDestination buffer.
memtypeNetCDF atomic memory type.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_memory_type()

hid_t NXCDF_memory_type ( nc_type type,
int * closep )

Map a netCDF atomic type to the HDF5 datatype describing the caller's memory buffer.

H5Dread/H5Dwrite take a "memory datatype" describing the layout of the buffer in RAM — this is what lets HDF5 convert on the fly (for example nc_get_vara_float on an NC_SHORT variable). Most netCDF types map to a predefined H5T_NATIVE_* constant that must not be closed; types with no native constant (NC_CHAR, small floats, strings, references) are produced by H5Tcopy of a base type, and the caller owns the copy — closep says which.

NC_BFLOAT16 copies little-endian bfloat16 and then adopts the machine's uint16 byte order, because HDF5 2.1.1 has no native-order bfloat16 constant. NC_CHAR is a fixed-length 1-byte string; NC_STRING is a variable-length string.

Parameters
typeNetCDF atomic type (the nc_type of the memory buffer).
closepSet nonzero when the returned type must be closed with H5Tclose(); may be NULL.
Returns
HDF5 memory datatype, or negative on error.

◆ NXCDF_put_vara()

int NXCDF_put_vara ( int ncid,
int varid,
const size_t * start,
const size_t * count,
const void * value,
nc_type memtype )

Write a hyperslab — the nc_put_vara_* dispatch entry.

Parameters
ncidNetCDF file identifier.
varidVariable id.
startHyperslab start indices.
countHyperslab counts.
valueSource buffer.
memtypeNetCDF atomic memory type.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_put_varm()

int NXCDF_put_varm ( int ncid,
int varid,
const size_t * s,
const size_t * c,
const ptrdiff_t * st,
const ptrdiff_t * im,
const void * v,
nc_type t )

Mapped hyperslab write (nc_put_varm_*); not implemented.

See NXCDF_get_varm(): always returns NC_ENOTBUILT.

Parameters
ncidNetCDF file identifier.
varidVariable id.
sHyperslab start indices.
cHyperslab counts.
stHyperslab strides.
imMemory mapping offsets.
vSource buffer.
tNetCDF atomic memory type.
Returns
NC_ENOTBUILT always.

◆ NXCDF_put_vars()

int NXCDF_put_vars ( int ncid,
int varid,
const size_t * start,
const size_t * count,
const ptrdiff_t * stride,
const void * value,
nc_type memtype )

Write a strided hyperslab — the nc_put_vars_* dispatch entry.

Parameters
ncidNetCDF file identifier.
varidVariable id.
startHyperslab start indices.
countHyperslab counts.
strideHyperslab strides.
valueSource buffer.
memtypeNetCDF atomic memory type.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_var_par_access()

int NXCDF_var_par_access ( int ncid,
int varid,
int par_access )

Select a variable's session-only parallel transfer mode.

All ranks must select the same mode and follow the same collective call sequence. Filtered variables cannot use independent transfers. This does not change storage or enter define mode, and works on read-only sessions.

Parameters
ncidFile or group identifier.
varidGroup-local variable identifier.
par_accessNC_COLLECTIVE or NC_INDEPENDENT.
Returns
NC_NOERR on success, NC_ENOPAR for a serial session/build, NC_EINVAL for an invalid mode or independent filtered access, or the identifier lookup error.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ var_io()

int var_io ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
const ptrdiff_t * stridep,
void * data,
nc_type memtype,
int writing )
static

Common hyperslab I/O behind the nc_get/put_vara and nc_get/put_vars dispatch entries.

A hyperslab is the rectangular (possibly strided) region of a variable an I/O call touches. This function resolves the variable's HDF5 dataset, validates the memtype against check_memtype(), bounds-checks the selection (extending unlimited dimensions on writes past the current extent), performs any type conversion or quantization through the internal engine nxcdf_convert_type(), and runs H5Dread/H5Dwrite with a matching memory dataspace.

When a conversion overflows (NC_ERANGE semantics, issue #381), the I/O still completes — the engine records the range error and the function returns NC_ERANGE after the data lands.

Parallel sessions use an owned MPI transfer list. Collective empty/fill-only selections still enter HDF5 with none-selected spaces. Unlimited writes negotiate extents across the file communicator; independent growth fails before mutation. The caller supplies matching collective order and valid rank-local arguments. Dynamic/reference transfers remain serial-only.

Author
Edward Hartnett, Intelligent Data Design, Inc.
Parameters
ncidNetCDF file identifier.
varidVariable identifier.
startpHyperslab start indices.
countpHyperslab counts.
stridepHyperslab strides, or NULL for contiguous.
dataData buffer.
memtypeNetCDF atomic memory type (NC_NAT resolves to the variable's own type for the untyped nc_get_var/nc_put_var, var1 and vars calls).
writingNonzero for a write, zero for a read.
Returns
NC_NOERR on success, NC_ERANGE when data landed but a conversion overflowed, or another NetCDF error code.