Files
Jungfraujoch/tests/CUDAMemHelpersTest.cpp
T
leonarski_f 6dfe065365
Build Packages / Create release (push) Successful in 16s
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 8m27s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 9m15s
Build Packages / build:viewer-tgz:cpu (push) Successful in 10m11s
Build Packages / build:viewer-tgz:cuda (push) Successful in 12m6s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 15m44s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 16m1s
Build Packages / build:windows:nocuda (push) Successful in 17m29s
Build Packages / build:windows:cuda (push) Successful in 19m58s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 24m7s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 19m8s
Build Packages / build:rugnux:windows (push) Successful in 10m58s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 20m46s
Build Packages / Generate python client (push) Successful in 53s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 20m13s
Build Packages / Build documentation (push) Successful in 1m36s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 19m57s
Build Packages / build:rpm (rocky8) (push) Successful in 18m7s
Build Packages / build:rpm (rocky9) (push) Successful in 18m54s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 19m32s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 17m30s
Build Packages / Unit tests (push) Successful in 1h39m2s
v1.0.0-rc.172 (#82)
* Fixed `jfjoch_broker` cancelling every data collection with a CUDA "out of memory" error after long operation: GPU memory no longer leaks with each collection.
* Rugnux scales a rotation sweep until the per-frame scales settle instead of for a fixed three rounds, and says so when they did not - merged intensities, and the space group, resolution cut and frame rejection read off them, change accordingly; `--scaling-iterations` is now the cap on that loop (default 100).
* Rugnux places every frame of a marCCD, SMV or miniCBF series at the spindle angle its own header states, so a series with missing frames, or with angles written modulo 360, is no longer read at the wrong geometry or refused.
* Every rotation run writes two diagnostic files beside its reflections: `<prefix>_detector.jpg`, the detector projection with the pixel mask and the detected beam-stop shadow drawn on it, and `<prefix>_plot.txt`, one row per image.

Reviewed-on: #82
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-09-22 06:48:37 +02:00

104 lines
3.7 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 <future>
#include <latch>
#include <set>
#include <thread>
#include "../image_analysis/indexing/CUDAMemHelpers.h"
// The broker starts fresh worker threads for every data collection. A stream per thread ever started
// is a device-memory leak that ends a long-running broker, so threads that follow one another have to
// end up on the same stream.
TEST_CASE("CudaAllocationStream_ReusedByLaterThreads", "[CUDAMemHelpers]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
std::set<cudaStream_t> seen;
for (int i = 0; i < 100; i++)
std::thread([&seen] {
CudaDevicePtr<uint8_t> buffer(1 << 16);
seen.insert(cuda_allocation_stream());
}).join();
CHECK(seen.size() == 1);
CHECK(*seen.begin() != nullptr);
}
TEST_CASE("CudaAllocationStream_DistinctForConcurrentThreads", "[CUDAMemHelpers]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
// Neither thread may exit - and hand its stream back - before the other has taken its own.
std::latch both_have_one(2);
auto take = [&both_have_one] {
const cudaStream_t stream = cuda_allocation_stream();
both_have_one.count_down();
both_have_one.wait();
return stream;
};
auto a = std::async(std::launch::async, take);
auto b = std::async(std::launch::async, take);
const cudaStream_t stream_a = a.get();
const cudaStream_t stream_b = b.get();
CHECK(stream_a != nullptr);
CHECK(stream_b != nullptr);
CHECK(stream_a != stream_b);
}
// The shadow finder builds its GPU accumulator on a throw-away thread and frees it from another one
// long after. The free is ordered on the allocating thread's stream, so that stream has to outlive
// the thread - including while a later thread has borrowed it.
TEST_CASE("CudaDevicePtr_FreedAfterAllocatingThreadExited", "[CUDAMemHelpers]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
auto buffer = std::async(std::launch::async, [] {
return std::make_unique<CudaDevicePtr<uint8_t>>(1 << 20);
}).get();
std::thread([] { CudaDevicePtr<uint8_t> other(1 << 16); }).join();
REQUIRE(cudaMemset(buffer->get(), 0, 1 << 20) == cudaSuccess);
buffer.reset();
CHECK(cudaDeviceSynchronize() == cudaSuccess);
CHECK(cudaGetLastError() == cudaSuccess);
}
// A pool that cannot serve the request is not an error: the buffer comes from cudaMalloc instead. It
// must then not stay behind as the thread's last error, or the cudaGetLastError() after the next
// kernel launch throws "out of memory" over work that went fine.
TEST_CASE("CudaDevicePtr_HandledPoolFailureLeavesNoError", "[CUDAMemHelpers]") {
if (get_gpu_count() == 0)
SKIP("No CUDA GPU present");
int device = 0;
REQUIRE(cudaGetDevice(&device) == cudaSuccess);
cudaMemPoolProps props{};
props.allocType = cudaMemAllocationTypePinned;
props.location.type = cudaMemLocationTypeDevice;
props.location.id = device;
props.maxSize = 4 << 20;
cudaMemPool_t previous = nullptr, capped = nullptr;
REQUIRE(cudaDeviceGetMemPool(&previous, device) == cudaSuccess);
REQUIRE(cudaMemPoolCreate(&capped, &props) == cudaSuccess);
REQUIRE(cudaDeviceSetMemPool(device, capped) == cudaSuccess);
{
CudaDevicePtr<uint8_t> buffer(64 << 20);
CHECK(buffer.get() != nullptr);
CHECK(cudaGetLastError() == cudaSuccess);
}
REQUIRE(cudaDeviceSetMemPool(device, previous) == cudaSuccess);
cudaMemPoolDestroy(capped);
}
#endif