Skip to content

Create H5read2 - #70

Open
dimitrivlachos wants to merge 33 commits into
mainfrom
h5read2
Open

dimitrivlachos wants to merge 33 commits into
mainfrom
h5read2

Conversation

@dimitrivlachos

@dimitrivlachos dimitrivlachos commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

fast-feedback-service reads detector frames through [h5read][h5read],
a C library written for 16-bit data that fixes the pixel width when a
consumer is compiled, assumes bitshuffle-LZ4 without looking at the
filter pipeline, and admits one thread at a time: every worker takes a
mutex to ask whether its image has arrived and again to fetch the
chunk. The fast-feedback service used it and now
DiamondLightSource/fast-feedback-service#191 replaces it with this.

This PR adds the reader to dx2. ImageSource is a pixel-only
interface: frame count, shape, pixel type, filter pipeline, whether a
frame is available yet, and a frame either decoded into the caller's
buffer or handed over as its stored chunk for a GPU to decode. It is
pixel-only because the other sources FFS puts behind it, detector
streams and CBF files, have no NeXus metadata; a source that is not
HDF5 implements the frame methods and nothing else. H5ImageSource
implements it for NeXus files, flattening a plain dataset, a virtual
dataset or the numbered data_000001..N links into one frame table,
with the pixel type read from the file and dispatched at run time, so
one binary reads both Eiger widths. Compression is read from the
pipeline rather than assumed, the per-chunk filter mask is honoured,
and the bitshuffle block size comes from the chunk's own header rather
than the legacy block_size = 0. dx2::nxmx reads the rest of the
master into the models dx2 already has, scaling each quantity by its
units attribute, which h5read ignored.

Two things make it more than a port. With Options::swmr a source
opens as soon as the master exists, before any data file has been
written, and follows files as they appear and datasets as they grow;
wait_for_frame polls with a deadline and a std::stop_token, and a
file that is not there yet is a FileNotFound, so a consumer waiting
for the writer retries on that and fails at once on a broken file. The
other is the fetch. The conda HDF5 is the thread-safe build, which
serialises every API call behind one lock, so H5Dread_chunk from
sixteen threads moves bytes no faster than from one. dx2 asks HDF5 for
the chunk's address and size under the lock and preads the bytes from
its own descriptor outside it. Reading all 1000 frames of a 1.3 GB
Eiger file from tmpfs:

threads raw, H5Dread_chunk raw, pread
1 4503 frames/s 4490
4 4277 13589
16 3892 9290

Decoding on the CPU costs the same on both paths, about 3.4 ms per
frame per thread, so FFS today sees the gain in #191's figures rather
than here; the point is that the fetch is the one stage that could not
otherwise scale as the decode moves to the GPU. dx2 holds no lock of
its own and refuses to open on an HDF5 built without one.

Changes:

  • ImageSource and H5ImageSource (include/dx2/image/): the
    interface and the HDF5 implementation, with layout discovery, SWMR
    open and refresh, and the pread path. Chunk addresses include the
    user block from HDF5 1.14.4, so the offset is added only below that.
  • PixelType, FrameView and Frame: the element type as a
    run-time value over a type list shared with h5dispatch, and a
    type-erased view with checked as<T>(), as_mdspan<T>() and
    visit(), so a GPU consumer decodes into pinned bytes and a CPU
    consumer gets an mdspan.
  • FilterPipeline and ChunkDecoder: the pipeline read back from
    the dataset, its Compression summary, and codecs for bitshuffle,
    bitshuffle-LZ4, bitshuffle-zstd and LZ4 compiled from the upstream
    bitshuffle 0.5.2 sources, since no package ships them as a library.
    Anything else falls back to H5Dread through HDF5's plugins.
  • h5link: resolves soft and external links to a file and object
    path before anything is opened, searching relative names in HDF5's
    own order, so a dangling link fails naming the file and a missing one
    can be waited for.
  • FramePrefetcher: N worker threads over any ImageSource, in
    order or any order, decoded or raw, with pooled buffers; a fresh
    36 MB allocation per frame cost 2.3 ms in kernel zero-fill.
  • nxmx: Panel, MonochromaticBeam, Scan, Goniometer and
    the pixel mask from the master, with the axis mapped from McStas to
    imgCIF as dxtbx does. Panel gains getters for image size and
    trusted range.
  • Build: LZ4 through a shipped find module, optional zstd, and
    DX2_BUILD_TESTS and DX2_INSTALL so an embedding project neither
    builds the suite nor installs dx2's files. DX2_BITSHUFFLE_SIMD_FLAGS
    selects the AVX2 kernels; the default stays portable.
    h5read_processed.hpp is split into the generic read helpers and the
    DIALS format attributes, with no change in behaviour.

Column views now follow the constness of the table, so a const table
hands out read-only views. Code that reads columns from a const table
in fast-feedback-service needs read-only view types to build against
this.
… library

The installed package never declared its dependencies and did not ship
mdspan, so find_package(DX2) could not be linked against. Embedded
consumers inherited the test suite and relied on the parent to request
position-independent code. Bitshuffle and LZ4 are added here as private
dependencies for the image reader that follows.
# Conflicts:
#	dx2/CMakeLists.txt
#	include/dx2/h5/h5dispatch.hpp
#	requirements.txt
#	tests/CMakeLists.txt
# Conflicts:
#	CMakeLists.txt
#	dx2/CMakeLists.txt
#	include/dx2/h5/h5read_processed.hpp
@dimitrivlachos dimitrivlachos self-assigned this Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant