Files
Jungfraujoch/tests/BSLZ4DecoderGPUTest.cpp
T
leonarski_fandClaude Opus 5 6e4c0ce202
Build Packages / build:viewer-tgz:cpu (push) Successful in 20m32s
Build Packages / build:viewer-tgz:cuda (push) Successful in 20m40s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 22m24s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 23m8s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 27m31s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 27m38s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 29m7s
Build Packages / XDS test (durin plugin) (push) Successful in 11m12s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 22m49s
Build Packages / build:rpm (rocky9) (push) Successful in 22m51s
Build Packages / Generate python client (push) Successful in 40s
Build Packages / Build documentation (push) Successful in 1m22s
Build Packages / Create release (push) Skipped
Build Packages / DIALS test (push) Successful in 20m21s
Build Packages / build:rpm (rocky8) (push) Successful in 27m26s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 20m59s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 25m52s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 9m41s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m41s
Build Packages / Unit tests (push) Successful in 1h17m41s
Build Packages / build:windows:nocuda (push) Successful in 13m24s
Build Packages / build:windows:cuda (push) Successful in 17m0s
image_preprocessing: decode bitshuffle+LZ4 on the GPU
The pipeline decompressed each image on the host and uploaded the result. On
an 18 Mpx rotation dataset that made the host-to-device copy the bottleneck of
the whole per-image loop: nsys puts the copies at 78% of the loop against 39%
for every kernel combined - 3600 transfers of 72.4 MB - and they ran at only
12.5 GB/s of an available 27-28 because the host-side decompression was itself
saturating host memory bandwidth. The GPU was mostly waiting.

So the compressed chunk goes across instead, about 4 MB rather than 72 MB, and
is decoded on the device. That removes the transfer and the host decompression
that was throttling it, in one change. Measured on an idle machine, a run goes
from 45.11 s to 24.97 s - 1.81x - with the merged output unchanged.

THE APPROACH IS JON WRIGHT'S (ESRF): "Experiences with GPU decompression for
bitshuffle + LZ4 data", HDF5 User Group 2021, and github.com/jonwright/
bslz4decoders. The kernels here are ours, but the idea and the demonstration
that it is worth doing are his. Cited in docs/ACKNOWLEDGEMENT.md and in the new
section 0 of docs/CPU_DATA_ANALYSIS.md.

Two kernels mirror the CPU decoder. LZ4 runs one WARP per bitshuffle block:
every lane parses the same sequence stream (a broadcast read, no divergence)
and the literal and match copies are split across the 32 lanes so the stores
coalesce; an overlapping match is treated as a pattern of period offset sourced
from bytes that already precede the write position, which keeps it parallel
rather than a serial byte loop. One thread per block instead measured 13x
slower. The bitshuffle inverse then un-transposes each byte-plane through
shared memory and interleaves the planes back into elements.

Only BSHUF_LZ4 is decoded on the device. The zstd variants have no device
decoder, and neither has an uncompressed or float image; Supports() returns
false for those and the caller decompresses on the host exactly as before. The
fallback is explicit, so a format we cannot decode on the device is a slower
path and never a wrong answer.

Tests hold the device decoder against the CPU one byte for byte, on data from
the production compressor, for every element size the detectors emit -
including the 8-bit DECTRIS modes, which take bitshuf_decode_block's separate
elem_size == 1 branch - plus a many-block frame, the formats it must decline,
and malformed containers, which must throw rather than run off a buffer.

Battery: 37 crystals, no failures, identical to the host-decode run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 00:16:36 +02:00

160 lines
7.3 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/CUDAWrapper.h"
#ifdef JFJOCH_USE_CUDA
#include <random>
#include <cstring>
#include <limits>
#include "../image_analysis/image_preprocessing/BSLZ4DecoderGPU.h"
#include "../compression/JFJochCompressor.h"
#include "../compression/JFJochDecompress.h"
// The GPU decoder must agree with the CPU one BYTE FOR BYTE, on data produced by our own compressor,
// for every element size the detectors emit - including the 8-bit DECTRIS modes, which take a
// different branch in bitshuf_decode_block (bit un-transpose only, no byte interleave).
//
// Images are built to exercise what the LZ4 format actually does on detector data: long runs of a
// repeated byte (offset == 1 matches, the overlapping-match path), isolated bright pixels
// (literals), and a noisy region (short matches at assorted offsets). A uniformly random image
// would be almost incompressible and would never reach the match code at all.
namespace {
template <class T>
std::vector<T> MakeDetectorLikeImage(size_t npixels, uint32_t seed) {
std::mt19937 rng(seed);
std::vector<T> img(npixels, 0); // sparse background: long zero runs
// A band of low-level noise, so matches are short and offsets vary.
for (size_t i = npixels / 4; i < npixels / 2; i++)
img[i] = static_cast<T>(rng() % 7);
// Bright, isolated spots - these become literals.
for (size_t s = 0; s < 64; s++) {
const size_t c = rng() % npixels;
for (size_t d = 0; d < 9 && c + d < npixels; d++)
img[c + d] = static_cast<T>(std::numeric_limits<T>::max() / (2 + (d % 3)));
}
// A run of one repeated non-zero value, the classic offset==1 match.
for (size_t i = npixels * 3 / 4; i < npixels * 3 / 4 + 5000 && i < npixels; i++)
img[i] = static_cast<T>(42);
return img;
}
template <class T>
void RoundTrip(CompressedImageMode mode, size_t width, size_t height, uint32_t seed) {
const size_t npixels = width * height;
const auto original = MakeDetectorLikeImage<T>(npixels, seed);
// Compress with the production compressor, so the container is exactly what the pipeline reads.
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
const std::vector<uint8_t> compressed = compressor.Compress(original);
REQUIRE(!compressed.empty());
const CompressedImage image(compressed.data(), compressed.size(), width, height, mode,
CompressionAlgorithm::BSHUF_LZ4);
REQUIRE(BSLZ4DecoderGPU::Supports(image));
REQUIRE(image.GetUncompressedSize() == npixels * sizeof(T));
// CPU reference: the same call the host path makes.
std::vector<uint8_t> cpu_buffer;
const uint8_t *cpu_out = image.GetUncompressedPtr(cpu_buffer);
REQUIRE(std::memcmp(cpu_out, original.data(), npixels * sizeof(T)) == 0);
auto stream = std::make_shared<CudaStream>();
BSLZ4DecoderGPU decoder(npixels * sizeof(uint32_t), stream);
CudaDevicePtr<uint8_t> gpu_out(npixels * sizeof(T));
decoder.Decode(image, gpu_out.get());
REQUIRE(cudaStreamSynchronize(*stream) == cudaSuccess);
std::vector<T> gpu_result(npixels);
REQUIRE(cudaMemcpy(gpu_result.data(), gpu_out.get(), npixels * sizeof(T),
cudaMemcpyDeviceToHost) == cudaSuccess);
REQUIRE(std::memcmp(gpu_result.data(), original.data(), npixels * sizeof(T)) == 0);
}
} // namespace
TEST_CASE("BSLZ4DecoderGPU_MatchesCPU_AllElementSizes", "[BSLZ4DecoderGPU]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
// Sizes chosen so the last block is partial and the leftover tail (the elements bitshuffle
// leaves uncompressed because they do not fill a multiple of 8) is non-empty on some of them.
RoundTrip<uint8_t>(CompressedImageMode::Uint8, 1030, 517, 1);
RoundTrip<int8_t>(CompressedImageMode::Int8, 1030, 517, 2);
RoundTrip<uint16_t>(CompressedImageMode::Uint16, 1030, 517, 3);
RoundTrip<int16_t>(CompressedImageMode::Int16, 1030, 517, 4);
RoundTrip<uint32_t>(CompressedImageMode::Uint32, 1030, 517, 5);
RoundTrip<int32_t>(CompressedImageMode::Int32, 1030, 517, 6);
}
TEST_CASE("BSLZ4DecoderGPU_MatchesCPU_LargeFrame", "[BSLZ4DecoderGPU]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
// Many blocks, so the per-block descriptor scan and the one-warp-per-block launch are exercised
// at a realistic scale rather than on a handful of blocks.
RoundTrip<uint32_t>(CompressedImageMode::Uint32, 2068, 2162, 7);
}
// A decoder that cannot handle an image must SAY so rather than produce something wrong: the caller
// relies on Supports() to decide whether the host route is needed.
TEST_CASE("BSLZ4DecoderGPU_DeclinesWhatItCannotDecode", "[BSLZ4DecoderGPU]") {
std::vector<uint8_t> dummy(1024, 0);
const size_t w = 16, h = 16;
CHECK_FALSE(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Uint32,
CompressionAlgorithm::BSHUF_ZSTD)));
CHECK_FALSE(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Uint32,
CompressionAlgorithm::BSHUF_ZSTD_RLE)));
CHECK_FALSE(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Uint32,
CompressionAlgorithm::BSHUF_ZSTD_RLE_HUFF)));
CHECK_FALSE(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Uint32,
CompressionAlgorithm::NO_COMPRESSION)));
CHECK_FALSE(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Float32,
CompressionAlgorithm::BSHUF_LZ4)));
CHECK(BSLZ4DecoderGPU::Supports(
CompressedImage(dummy.data(), dummy.size(), w, h, CompressedImageMode::Uint32,
CompressionAlgorithm::BSHUF_LZ4)));
}
// A malformed container comes off the network or off disk, so it must throw rather than run off
// the end of a buffer on the device.
TEST_CASE("BSLZ4DecoderGPU_RejectsMalformed", "[BSLZ4DecoderGPU]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
const size_t width = 128, height = 128, npixels = width * height;
const auto original = MakeDetectorLikeImage<uint32_t>(npixels, 11);
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
const std::vector<uint8_t> compressed = compressor.Compress(original);
auto stream = std::make_shared<CudaStream>();
BSLZ4DecoderGPU decoder(npixels * sizeof(uint32_t), stream);
CudaDevicePtr<uint8_t> gpu_out(npixels * sizeof(uint32_t));
// Truncated mid-stream: the block header promises more than is there.
const CompressedImage truncated(compressed.data(), compressed.size() / 2, width, height,
CompressedImageMode::Uint32, CompressionAlgorithm::BSHUF_LZ4);
CHECK_THROWS(decoder.Decode(truncated, gpu_out.get()));
// Shorter than the 12-byte container header.
const CompressedImage stub(compressed.data(), 8, width, height,
CompressedImageMode::Uint32, CompressionAlgorithm::BSHUF_LZ4);
CHECK_THROWS(decoder.Decode(stub, gpu_out.get()));
}
#endif