Merge unified-nvtx into rc175: NVTX range per timed section and console step; kernels read host buffers in place on unified-memory GPUs (discrete cards unchanged)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	common/CUDAWrapper.cu
This commit is contained in:
leonarski_f committed 2026-10-14 22:13:14 +02:00
commit 8eacce02f7
15 files changed
+297 -30

No files matched your search

+3
View File
@@ -43,6 +43,9 @@ ADD_LIBRARY(JFJochLogger STATIC
)
TARGET_LINK_LIBRARIES(JFJochLogger PUBLIC spdlog::spdlog JFJochVersion)
IF (JFJOCH_CUDA_AVAILABLE)
TARGET_LINK_LIBRARIES(JFJochLogger PRIVATE CUDA::nvtx3) # header-only NVTX v3
ENDIF()
ADD_LIBRARY(JFJochZMQ STATIC ZMQWrappers.cpp ZMQWrappers.h)
+4
View File
@@ -109,6 +109,10 @@ bool get_gpu_free_memory(size_t &, size_t &) {
void set_gpu(int32_t dev_id) {}
bool gpu_direct_host_read() {
return false;
}
void pin_gpu() {}
void pin_gpu(int32_t dev_id) {}
+23
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <string>
#include <thread>
@@ -99,6 +100,28 @@ bool get_gpu_free_memory(size_t &free_bytes, size_t &total_bytes) {
return true;
}
bool gpu_direct_host_read() {
// Pageable memory access through the host's page tables is the hardware-coherent case (ATS over
// NVLink-C2C); without the second attribute the access is HMM-emulated, page faults and migration,
// and slower than a copy.
static const std::vector<bool> direct = [] {
const bool force = std::getenv("JFJOCH_FORCE_DIRECT_HOST_READ") != nullptr;
std::vector<bool> ret(get_gpu_count(), force);
for (int d = 0; d < static_cast<int>(ret.size()); d++) {
int pageable = 0, host_page_tables = 0;
cudaDeviceGetAttribute(&pageable, cudaDevAttrPageableMemoryAccess, d);
cudaDeviceGetAttribute(&host_page_tables, cudaDevAttrPageableMemoryAccessUsesHostPageTables, d);
if (pageable && host_page_tables)
ret[d] = true;
}
return ret;
}();
int device = 0;
if (cudaGetDevice(&device) != cudaSuccess || device >= static_cast<int>(direct.size()))
return false;
return direct[device];
}
void set_gpu(int32_t dev_id) {
auto dev_count = get_gpu_count();
+8
View File
@@ -39,6 +39,14 @@ bool get_gpu_free_memory(size_t &free_bytes, size_t &total_bytes);
void set_gpu(int32_t dev_id);
// Whether kernels on the current device read ordinary (pageable) host memory directly and at memory
// speed, so a host buffer can be handed to a kernel instead of being copied to the card first: true on
// a unified-memory system where the GPU walks the CPU's page tables (DGX Spark GB10, Grace Hopper GH200,
// GB200), false on a discrete card. Asked of the driver once per device. JFJOCH_FORCE_DIRECT_HOST_READ
// in the environment makes it true on every device - for testing the direct path on a discrete card,
// where only page-locked host memory is then readable. false without CUDA.
bool gpu_direct_host_read();
// Pin the calling thread to the next GPU in round-robin order, using a process-wide counter
// (counter++ % get_gpu_count()). Call once per thread; no thread id needed. No-op when no GPU
// is visible. Honours CUDA_VISIBLE_DEVICES via get_gpu_count().
+18
View File
@@ -9,6 +9,24 @@
#include "Logger.h"
#include "GitInfo.h"
#ifdef JFJOCH_USE_CUDA
#include <nvtx3/nvToolsExt.h>
uint64_t NvtxRangeStart(const std::string &name) {
return nvtxRangeStartA(name.c_str());
}
void NvtxRangeEnd(uint64_t id) {
nvtxRangeEnd(id);
}
#else
uint64_t NvtxRangeStart(const std::string &) {
return 0;
}
void NvtxRangeEnd(uint64_t) {}
#endif
namespace {
spdlog::sink_ptr routed_sink; // set once, at start-up, before any thread logs
}
+10 -1
View File
@@ -70,13 +70,21 @@ public:
void ReplayInto(Logger &to) const;
};
// An NVTX range named `name`, so a timed section shows up by name in an Nsight Systems trace. A start/end
// pair, not a push/pop, so a range may end on another thread and ranges on parallel threads may overlap.
// Without a profiler attached both calls return at once; without CUDA they do nothing.
uint64_t NvtxRangeStart(const std::string &name);
void NvtxRangeEnd(uint64_t id);
// Times one section of a run: logs "TIMING <section> <seconds>" at debug level when the section ends -
// at Stop(), or when the mark goes out of scope, whichever comes first. rugnux keeps debug lines off the
// terminal and in <prefix>_log.txt; tools/rugnux_timing.py turns them into a table.
// terminal and in <prefix>_log.txt; tools/rugnux_timing.py turns them into a table. The section is also
// an NVTX range of the same name.
class TimingMark {
Logger &logger;
std::string section;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
uint64_t nvtx_range = NvtxRangeStart(section);
bool stopped = false;
public:
TimingMark(Logger &in_logger, std::string in_section)
@@ -88,6 +96,7 @@ public:
if (stopped)
return;
stopped = true;
NvtxRangeEnd(nvtx_range);
logger.Debug("TIMING {} {:.3f}", section,
std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count());
}
@@ -7,6 +7,7 @@
#include "BSLZ4DecoderGPU.h"
#include "BSLZ4DecodeWarp.h"
#include "../../common/CUDAWrapper.h"
#include "../../common/JFJochException.h"
#include "../../compression/JFJochDecompress.h" // BSHUF_BLOCKED_MULT and the container layout
@@ -118,6 +119,7 @@ bool BSLZ4DecoderGPU::Supports(const CompressedImage &image) {
BSLZ4DecoderGPU::BSLZ4DecoderGPU(size_t in_max_uncompressed_bytes, std::shared_ptr<CudaStream> in_stream)
: stream(std::move(in_stream)),
direct_host_read(gpu_direct_host_read()),
max_uncompressed_bytes(in_max_uncompressed_bytes) {
gpu_status = CudaDevicePtr<uint32_t>(1);
host_status = CudaHostPtr<uint32_t>(1);
@@ -211,7 +213,8 @@ BSLZ4ShuffledImage BSLZ4DecoderGPU::PrepareChunk(const CompressedImage &image) {
if (nblocks_needed > (clen - 12) / MIN_BLOCK_BYTES_ON_WIRE)
throw JFJochException(JFJochExceptionCategory::Compression, "bslz4 chunk too short for the blocks its header implies");
EnsureCompressedCapacity(clen);
if (!direct_host_read)
EnsureCompressedCapacity(clen);
EnsureBlockCapacity(nblocks_needed);
size_t nblk = 0, off = 12, out_off = 0;
@@ -240,8 +243,14 @@ BSLZ4ShuffledImage BSLZ4DecoderGPU::PrepareChunk(const CompressedImage &image) {
host_status.get()[0] = 0;
cuda_err(cudaMemcpyAsync(gpu_status.get(), host_status.get(), sizeof(uint32_t),
cudaMemcpyHostToDevice, *stream));
memcpy(host_compressed.get(), src, clen);
cuda_err(cudaMemcpyAsync(gpu_compressed.get(), host_compressed.get(), clen, cudaMemcpyHostToDevice, *stream));
// Where the card reads host memory directly, the kernels read the reader's own buffer, which the
// caller keeps until it has synchronised the stream.
const uint8_t *compressed = src;
if (!direct_host_read) {
memcpy(host_compressed.get(), src, clen);
cuda_err(cudaMemcpyAsync(gpu_compressed.get(), host_compressed.get(), clen, cudaMemcpyHostToDevice, *stream));
compressed = gpu_compressed.get();
}
const int nb = static_cast<int>(nblk);
if (nb > 0)
@@ -249,7 +258,7 @@ BSLZ4ShuffledImage BSLZ4DecoderGPU::PrepareChunk(const CompressedImage &image) {
cudaMemcpyHostToDevice, *stream));
BSLZ4ShuffledImage ret;
ret.compressed = gpu_compressed.get();
ret.compressed = compressed;
ret.desc = gpu_desc.get();
ret.status = gpu_status.get();
ret.nblocks = nb;
@@ -257,7 +266,7 @@ BSLZ4ShuffledImage BSLZ4DecoderGPU::PrepareChunk(const CompressedImage &image) {
ret.block_bytes = block_bytes;
ret.tail_elems = static_cast<uint32_t>(leftover_bytes / elem_size);
ret.tail_elem0 = static_cast<uint32_t>(out_off / elem_size);
ret.tail_src = leftover_bytes > 0 ? gpu_compressed.get() + off : nullptr;
ret.tail_src = leftover_bytes > 0 ? compressed + off : nullptr;
return ret;
}
@@ -306,11 +315,12 @@ void BSLZ4DecoderGPU::Decode(const CompressedImage &image, uint8_t *gpu_out) {
cuda_err(cudaGetLastError());
}
// The verbatim tail is already on the device inside the uploaded chunk.
// The verbatim tail is already on the device inside the uploaded chunk - or in host memory the card
// reads directly, hence cudaMemcpyDefault.
if (s.tail_elems > 0)
cuda_err(cudaMemcpyAsync(gpu_out + static_cast<size_t>(s.tail_elem0) * s.elem_size, s.tail_src,
static_cast<size_t>(s.tail_elems) * s.elem_size,
cudaMemcpyDeviceToDevice, *stream));
cudaMemcpyDefault, *stream));
}
void BSLZ4DecoderGPU::ThrowIfDecodeFailed() {
@@ -66,6 +66,9 @@ class BSLZ4DecoderGPU {
// uploads pageable memory through small staging buffers of its own, a piece at a time - at a few
// GB/s on a host whose memory is far from the card. Copied here, the upload is one DMA at the link rate.
CudaHostPtr<uint8_t> host_compressed;
// The card reads the chunk where the reader left it (gpu_direct_host_read()); neither buffer above
// is then used.
bool direct_host_read = false;
CudaDevicePtr<uint8_t> gpu_shuffled; // LZ4 output, still bitshuffled
CudaDevicePtr<BSLZ4BlockDesc> gpu_desc;
CudaHostPtr<BSLZ4BlockDesc> host_desc; // pinned, so the descriptor upload is truly async
@@ -7,6 +7,7 @@
#include "ImagePreprocessorGPU.h"
#include "BSLZ4DecodeWarp.h"
#include "../../common/CUDAWrapper.h"
#include "../../common/JFJochException.h"
namespace {
@@ -14,6 +15,12 @@ namespace {
if (val != cudaSuccess)
throw JFJochException(JFJochExceptionCategory::GPUCUDAError, cudaGetErrorString(val));
}
// A kernel reads pixels of type T, so a host buffer it reads in place must be aligned to T. A
// decompressed buffer always is; an image inside a received message need not be.
bool aligned_to(const void *ptr, size_t bytes) {
return reinterpret_cast<uintptr_t>(ptr) % bytes == 0;
}
}
template<class T>
@@ -338,6 +345,7 @@ ImagePreprocessorGPU::ImagePreprocessorGPU(const DiffractionExperiment &experime
threads = 128;
blocks = 4 * prop.multiProcessorCount;
direct_host_read = gpu_direct_host_read();
}
float ImagePreprocessorGPU::GetLastDecompressionTime_s() const {
@@ -359,7 +367,7 @@ ImagePreprocessorGPU::~ImagePreprocessorGPU() {
}
void ImagePreprocessorGPU::PinInputRegion(const void *ptr, size_t bytes) {
if (ptr == pinned_input && bytes == pinned_input_bytes)
if (direct_host_read || (ptr == pinned_input && bytes == pinned_input_bytes))
return;
if (pinned_input)
cuda_err(cudaHostUnregister(const_cast<void *>(pinned_input)));
@@ -385,7 +393,8 @@ void ImagePreprocessorGPU::PinInputBuffer(std::vector<uint8_t> &buffer, size_t s
// Unregister before the resize, which can move the buffer.
input_reg.unregister();
buffer.resize(size);
input_reg.rebind(buffer);
if (!direct_host_read)
input_reg.rebind(buffer);
}
ImageStatistics ImagePreprocessorGPU::Analyze(ImagePreprocessorBuffer &processed_image, const uint8_t *image_ptr,
@@ -527,6 +536,11 @@ ImageStatistics ImagePreprocessorGPU::Analyze(ImagePreprocessorBuffer &processed
const uint8_t *input,
T err_value,
T sat_value) {
// Read in place where the card reads host memory directly; the input outlives this call, which
// waits for the stream.
if (direct_host_read && aligned_to(input, sizeof(T)))
return AnalyzeOnDevice<T>(processed_image, input, err_value, sat_value);
// Allocated here rather than in the constructor: only the host-upload path needs it, and the
// device-decode path - which is what BSHUF_LZ4 images take - never touches it. Overshoot to
// 4 bytes per pixel so a 1- or 2-byte image fits the same buffer.
@@ -537,14 +551,11 @@ ImageStatistics ImagePreprocessorGPU::Analyze(ImagePreprocessorBuffer &processed
// every blocking stream in the process, which serialised all workers behind whichever one was
// uploading. The stream is synchronised at the end of this function, so the ordering is unchanged.
cuda_err(cudaMemcpyAsync(gpu_decompressed_image, input, npixels * sizeof(T), cudaMemcpyHostToDevice, *stream));
return AnalyzeOnDevice<T>(processed_image, err_value, sat_value);
return AnalyzeOnDevice<T>(processed_image, gpu_decompressed_image.get(), err_value, sat_value);
}
ImageStatistics ImagePreprocessorGPU::AnalyzeStoredInt32(ImagePreprocessorBuffer &processed_image,
const CompressedImage &image) {
if (!gpu_decompressed_image.get())
gpu_decompressed_image = CudaDevicePtr<uint8_t>(npixels * sizeof(uint32_t));
if (image.GetCompressionAlgorithm() == CompressionAlgorithm::UINT16_WIDENED) {
// Uploaded as stored, two bytes a pixel, and widened by the preprocessing kernel itself: with
// int32's error marker and int32's saturation, each pixel is decided exactly as the int32 image
@@ -552,31 +563,40 @@ ImageStatistics ImagePreprocessorGPU::AnalyzeStoredInt32(ImagePreprocessorBuffer
last_decode = LastDecode::None;
if (image.GetCompressedSize() != npixels * sizeof(uint16_t))
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Mismatch in size");
// Read in place where the card reads host memory directly: nothing is page-locked, so engines
// sharing the frame do not interfere.
if (direct_host_read && aligned_to(image.GetCompressed(), sizeof(uint16_t)))
return AnalyzeOnDevice<uint16_t>(processed_image, image.GetCompressed(), INT32_MIN, INT32_MAX);
// Staged through this engine's own page-locked buffer, as the CBF and BSLZ4 decoders stage
// theirs, and not page-locked in place: the pre-scan hands one kept frame to several engines
// at once, and an engine that unregisters a frame another one is still copying from takes the
// whole context down. AnalyzeOnDevice waits for the stream, so the buffer is free again here.
if (!gpu_decompressed_image.get())
gpu_decompressed_image = CudaDevicePtr<uint8_t>(npixels * sizeof(uint32_t));
if (!host_stored.get())
host_stored = CudaHostPtr<uint8_t>(npixels * sizeof(uint16_t));
std::memcpy(host_stored.get(), image.GetCompressed(), image.GetCompressedSize());
cuda_err(cudaMemcpyAsync(gpu_decompressed_image, host_stored.get(), image.GetCompressedSize(),
cudaMemcpyHostToDevice, *stream));
return AnalyzeOnDevice<uint16_t>(processed_image, INT32_MIN, INT32_MAX);
return AnalyzeOnDevice<uint16_t>(processed_image, gpu_decompressed_image.get(), INT32_MIN, INT32_MAX);
}
if (!gpu_decompressed_image.get())
gpu_decompressed_image = CudaDevicePtr<uint8_t>(npixels * sizeof(uint32_t));
last_decode = LastDecode::CBF;
if (!cbf_decoder)
cbf_decoder = std::make_unique<CBFDecoderGPU>(stream);
cbf_decoder->Decode(image, reinterpret_cast<int32_t *>(gpu_decompressed_image.get()));
const ImageStatistics ret = AnalyzeOnDevice<int32_t>(processed_image, INT32_MIN, INT32_MAX);
const ImageStatistics ret = AnalyzeOnDevice<int32_t>(processed_image, gpu_decompressed_image.get(),
INT32_MIN, INT32_MAX);
cbf_decoder->ThrowIfDecodeFailed();
return ret;
}
// Everything after the image is on the device, shared by the host-upload and the device-decode
// entry points so the two cannot drift apart.
// Everything after the image is on the device - or in host memory the card reads directly - shared by
// the host-upload and the device-decode entry points so the two cannot drift apart.
template<class T>
ImageStatistics ImagePreprocessorGPU::AnalyzeOnDevice(ImagePreprocessorBuffer &processed_image,
ImageStatistics ImagePreprocessorGPU::AnalyzeOnDevice(ImagePreprocessorBuffer &processed_image, const void *input,
int64_t err_value, int64_t sat_value) {
if (sat_value > saturation_limit)
sat_value = saturation_limit;
@@ -584,7 +604,7 @@ ImageStatistics ImagePreprocessorGPU::AnalyzeOnDevice(ImagePreprocessorBuffer &p
cpu_stats[0] = ImageStatistics{.max_value = INT64_MIN, .min_value = INT64_MAX};
cuda_err(cudaMemcpyAsync(gpu_stats, cpu_stats.data(), sizeof(ImageStatistics), cudaMemcpyHostToDevice, *stream));
preprocess_kernel<T> <<< blocks, threads, 0, *stream >>>(
reinterpret_cast<const T *>(gpu_decompressed_image.get()),
reinterpret_cast<const T *>(input),
gpu_mask->get(),
processed_image.getGPUBuffer(),
gpu_stats,
@@ -31,6 +31,9 @@ class ImagePreprocessorGPU : public ImagePreprocessor {
size_t pinned_input_bytes = 0;
// Staging for a marCCD frame's stored 16-bit pixels (UINT16_WIDENED), allocated on first use.
CudaHostPtr<uint8_t> host_stored;
// The card reads host memory directly (gpu_direct_host_read()): images are read where they lie,
// nothing is page-locked or staged; only the CBF device decoder still uploads its input.
bool direct_host_read = false;
std::vector<int32_t> cpu_image;
@@ -46,10 +49,11 @@ class ImagePreprocessorGPU : public ImagePreprocessor {
ImageStatistics AnalyzeStoredInt32(ImagePreprocessorBuffer &processed_image, const CompressedImage &image);
template <class T> ImageStatistics Analyze(ImagePreprocessorBuffer &processed_image, const uint8_t *input, T err_value, T sat_value);
// Preprocess an image already sitting in gpu_decompressed_image (the host-upload path).
// err_value and sat_value are compared with the pixel's value, not its bits, so a narrower T can
// carry the int32 semantics of the image it widens to.
template <class T> ImageStatistics AnalyzeOnDevice(ImagePreprocessorBuffer &processed_image,
// Preprocess the image at `input`: gpu_decompressed_image on the host-upload path, or the host
// buffer itself where the card reads host memory directly. err_value and sat_value are compared
// with the pixel's value, not its bits, so a narrower T can carry the int32 semantics of the image
// it widens to.
template <class T> ImageStatistics AnalyzeOnDevice(ImagePreprocessorBuffer &processed_image, const void *input,
int64_t err_value, int64_t sat_value);
// Preprocess straight out of the bitshuffled bytes (the device-decode path). Same per-pixel
// decision and same statistics as AnalyzeOnDevice, with the un-transpose folded in.
@@ -2789,11 +2789,17 @@ void RotationScaleMergeGPU::IngestUpload(int n_frames, const Reflection *const *
CudaCheck(cudaGetLastError(), "IngestFrameKernel launch");
// Through two pinned buffers of whole frames, each filled on all threads while the other is on
// its way to the card and taken apart there.
// its way to the card and taken apart there. Where the card reads host memory directly
// (gpu_direct_host_read()), the kernel takes the pinned buffer apart in place, without the upload.
constexpr size_t STAGE_BYTES = 64 << 20;
const bool direct = gpu_direct_host_read();
const int cap = std::max(static_cast<int>(STAGE_BYTES / sizeof(Reflection)), max_count);
CudaHostPtr<Reflection> stage[2] = {CudaHostPtr<Reflection>(cap), CudaHostPtr<Reflection>(cap)};
CudaDevicePtr<Reflection> staged[2] = {d.Alloc<Reflection>(cap), d.Alloc<Reflection>(cap)};
CudaDevicePtr<Reflection> staged[2];
if (!direct) {
staged[0] = d.Alloc<Reflection>(cap);
staged[1] = d.Alloc<Reflection>(cap);
}
CudaEvent copied[2];
bool in_flight[2] = {false, false};
IngestArrays a{d.obs.get(), d.zeta.get(), d.corr.get(), d.bkg.get(), d.var_bkg.get(), d.image_number.get(),
@@ -2814,12 +2820,19 @@ void RotationScaleMergeGPU::IngestUpload(int n_frames, const Reflection *const *
std::copy(refl[o], refl[o] + count[o], dst + (d.in_src_start[o] - base));
});
if (cnt > 0) {
CudaCheck(cudaMemcpyAsync(staged[b].get(), dst, size_t(cnt) * sizeof(Reflection),
cudaMemcpyHostToDevice, d.s()), "upload reflections");
CudaCheck(cudaEventRecord(copied[b], d.s()), "upload reflections");
const Reflection *src = dst;
if (!direct) {
CudaCheck(cudaMemcpyAsync(staged[b].get(), dst, size_t(cnt) * sizeof(Reflection),
cudaMemcpyHostToDevice, d.s()), "upload reflections");
CudaCheck(cudaEventRecord(copied[b], d.s()), "upload reflections");
src = staged[b].get();
}
in_flight[b] = true;
IngestExtractKernel<<<(cnt + BLK - 1) / BLK, BLK, 0, d.s()>>>(cnt, base, staged[b].get(), a);
IngestExtractKernel<<<(cnt + BLK - 1) / BLK, BLK, 0, d.s()>>>(cnt, base, src, a);
CudaCheck(cudaGetLastError(), "IngestExtractKernel launch");
// Read in place, the buffer is free again only once the kernel has run.
if (direct)
CudaCheck(cudaEventRecord(copied[b], d.s()), "extract reflections");
}
b ^= 1;
f0 = f1;
+2
View File
@@ -204,6 +204,7 @@ void RugnuxConsole::Close(const char *how) {
}
pending_log_.push_back(std::move(log_text));
pending_timing_.push_back(fmt::format("TIMING step {} {:.3f}", step_title_, total));
NvtxRangeEnd(step_nvtx_);
warnings_held_.clear();
step_ = 0;
}
@@ -216,6 +217,7 @@ void RugnuxConsole::OnStep(const std::string &title) {
step_ = last_step_ = number;
step_title_ = title;
step_start_ = mark_ = Clock::now();
step_nvtx_ = NvtxRangeStart(fmt::format("Step {} {}", step_, title));
notes_ = 0;
if (!verbose_)
Emit(fmt::format("Step {:<2} {}", step_, title));
+1
View File
@@ -74,6 +74,7 @@ private:
int last_step_ = 0;
std::string step_title_;
Clock::time_point step_start_{};
uint64_t step_nvtx_ = 0; // the open step's NVTX range
Clock::time_point mark_{}; // the last line of the open step
int notes_ = 0;
+1
View File
@@ -129,6 +129,7 @@ ADD_EXECUTABLE(jfjoch_test
BSLZ4DecoderGPUFuzzTest.cpp
ImagePreprocessorGPUFusedTest.cpp
StoredImageDecodeGPUTest.cpp
DirectHostReadGPUTest.cpp
AdaptiveSpotFinderGPUTest.cpp
SpotExtractorGPUParityTest.cpp
CUDAMemHelpersTest.cpp
+148
View File
@@ -0,0 +1,148 @@
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/CUDAWrapper.h"
#ifdef JFJOCH_USE_CUDA
#include <cstring>
#include <random>
#include <vector>
#include "../common/PixelMask.h"
#include "../compression/JFJochCompressor.h"
#include "../image_analysis/image_preprocessing/BSLZ4DecoderGPU.h"
#include "../image_analysis/image_preprocessing/ImagePreprocessorCPU.h"
#include "../image_analysis/image_preprocessing/ImagePreprocessorGPU.h"
#include "../image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.h"
// The images the device reads straight out of host memory where gpu_direct_host_read() holds (a
// unified-memory system), against the host decoder and the CPU preprocessor. The host buffers are
// page-locked, so a discrete card can read them too: run with JFJOCH_FORCE_DIRECT_HOST_READ=1 to take
// the direct path there; without it these check the copying path on the same inputs.
namespace {
bool SameStats(const ImageStatistics &a, const ImageStatistics &b) {
return a.max_value == b.max_value && a.min_value == b.min_value
&& a.masked_pixel_count == b.masked_pixel_count
&& a.error_pixel_count == b.error_pixel_count
&& a.saturated_pixel_count == b.saturated_pixel_count;
}
std::vector<uint32_t> MakeImage(size_t npixels, uint32_t seed) {
std::mt19937 rng(seed);
std::vector<uint32_t> img(npixels, 0);
for (size_t i = npixels / 4; i < npixels / 2; i++)
img[i] = rng() % 11;
for (size_t s = 0; s < 500; s++)
img[rng() % npixels] = 30000 + rng() % 50000;
for (size_t s = 0; s < 300; s++)
img[rng() % npixels] = UINT32_MAX;
return img;
}
void CheckSameAsCPU(const DiffractionExperiment &x, const PixelMask &mask, const ImagePreprocessorBufferGPU &gpu_buf,
const ImageStatistics &gpu_stats, const uint8_t *raw, CompressedImageMode mode) {
const size_t npixels = x.GetPixelsNum();
ImagePreprocessorCPU cpu_pre(x, mask);
ImagePreprocessorBuffer cpu_buf(npixels);
const ImageStatistics cpu_stats = cpu_pre.Analyze(cpu_buf, raw, mode);
CHECK(SameStats(cpu_stats, gpu_stats));
size_t ndiff = 0;
for (size_t i = 0; i < npixels; i++)
ndiff += gpu_buf[i] != cpu_buf[i];
CHECK(ndiff == 0);
}
} // namespace
TEST_CASE("DirectHostRead_BSLZ4Decode", "[direct_host_read]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
WARN("gpu_direct_host_read() = " << gpu_direct_host_read());
// An odd pixel count, so the chunk ends in a verbatim tail that is copied rather than decoded.
const size_t width = 1001, height = 997, npixels = width * height;
const auto original = MakeImage(npixels, 3);
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
std::vector<uint8_t> compressed = compressor.Compress(original);
CudaRegisteredVector<uint8_t> reg(compressed);
const CompressedImage image(compressed.data(), compressed.size(), width, height, CompressedImageMode::Uint32,
CompressionAlgorithm::BSHUF_LZ4);
auto stream = std::make_shared<CudaStream>();
BSLZ4DecoderGPU decoder(npixels * sizeof(uint32_t), stream);
CudaDevicePtr<uint8_t> gpu_out(npixels * sizeof(uint32_t));
decoder.Decode(image, gpu_out.get());
REQUIRE(cudaStreamSynchronize(*stream) == cudaSuccess);
decoder.ThrowIfDecodeFailed();
std::vector<uint32_t> result(npixels);
REQUIRE(cudaMemcpy(result.data(), gpu_out.get(), npixels * sizeof(uint32_t), cudaMemcpyDeviceToHost)
== cudaSuccess);
CHECK(result == original);
}
TEST_CASE("DirectHostRead_Preprocessor", "[direct_host_read]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
DetectorSetup detector = DetJF4M();
detector.SaturationLimit(65535);
DiffractionExperiment x(detector);
x.DetectorDistance_mm(80).BeamX_pxl(1030).BeamY_pxl(1080);
const size_t npixels = x.GetPixelsNum();
PixelMask mask(x);
std::vector<uint32_t> user_mask(mask.GetMask().size(), 0);
for (size_t i = 0; i < npixels; i += 997) user_mask[i] = 1;
mask.LoadUserMask(x, user_mask);
auto stream = std::make_shared<CudaStream>();
ImagePreprocessorGPU gpu_pre(x, mask, stream, /*copy_image_to_host=*/true);
// The bitshuffle/LZ4 chunk, decoded and preprocessed by the fused kernel.
std::vector<uint32_t> img = MakeImage(npixels, 5);
JFJochBitShuffleCompressor compressor(CompressionAlgorithm::BSHUF_LZ4);
std::vector<uint8_t> compressed = compressor.Compress(img);
CudaRegisteredVector<uint8_t> compressed_reg(compressed);
{
const CompressedImage image(compressed.data(), compressed.size(), x.GetXPixelsNum(), x.GetYPixelsNum(),
CompressedImageMode::Uint32, CompressionAlgorithm::BSHUF_LZ4);
ImagePreprocessorBufferGPU gpu_buf(npixels);
ImageStatistics gpu_stats{};
REQUIRE(gpu_pre.AnalyzeCompressed(gpu_buf, image, gpu_stats));
CheckSameAsCPU(x, mask, gpu_buf, gpu_stats, reinterpret_cast<const uint8_t *>(img.data()),
CompressedImageMode::Uint32);
}
// An uncompressed image, as the host-upload path receives it.
{
std::vector<uint8_t> raw(reinterpret_cast<const uint8_t *>(img.data()),
reinterpret_cast<const uint8_t *>(img.data()) + npixels * sizeof(uint32_t));
CudaRegisteredVector<uint8_t> raw_reg(raw);
ImagePreprocessorBufferGPU gpu_buf(npixels);
const ImageStatistics gpu_stats = gpu_pre.Analyze(gpu_buf, raw.data(), CompressedImageMode::Uint32);
CheckSameAsCPU(x, mask, gpu_buf, gpu_stats, raw.data(), CompressedImageMode::Uint32);
}
// A marCCD frame's stored 16-bit pixels, widened by the kernel.
{
std::mt19937 rng(7);
std::vector<uint8_t> stored(npixels * sizeof(uint16_t));
for (size_t i = 0; i < npixels; i++) {
const uint16_t v = (i % 1500 == 0) ? 65535 : static_cast<uint16_t>(rng() % 70000);
std::memcpy(stored.data() + i * sizeof(uint16_t), &v, sizeof(v));
}
CudaRegisteredVector<uint8_t> stored_reg(stored);
const CompressedImage image(stored.data(), stored.size(), x.GetXPixelsNum(), x.GetYPixelsNum(),
CompressedImageMode::Int32, CompressionAlgorithm::UINT16_WIDENED);
ImagePreprocessorBufferGPU gpu_buf(npixels);
ImageStatistics gpu_stats{};
REQUIRE(gpu_pre.AnalyzeCompressed(gpu_buf, image, gpu_stats));
std::vector<uint8_t> decoded;
CheckSameAsCPU(x, mask, gpu_buf, gpu_stats, image.GetUncompressedPtr(decoded), CompressedImageMode::Int32);
}
}
#endif