From 399e8b7381de939fa057ae11b36a67a3ff0b59fd Mon Sep 17 00:00:00 2001 From: kferjaoui Date: Thu, 12 Nov 2026 14:14:09 +0100 Subject: [PATCH] ClusterFinderCUDA: no stale CUDA error after a failed call Clear the last CUDA error before the kernel launch, and forget the old input buffer before registering a new one, so a failed registration can't make the destructor unpin it twice. --- include/aare/cf/ClusterFinderCUDA.hpp | 5 ++- include/aare/cf/ClusterFinderCUDA_graph.hpp | 5 ++- include/aare/cf/clusterfinder_algo.cuh | 5 +++ python/tests/test_ClusterFinderCUDA.py | 25 ++++++++++- src/cf/ClusterFinderCUDA.test.cpp | 18 ++++++++ src/cf/ClusterFinderCUDA_testimpl.cu | 47 +++++++++++++++++++++ src/cf/ClusterFinderCUDA_testimpl.hpp | 14 ++++++ 7 files changed, 113 insertions(+), 6 deletions(-) diff --git a/include/aare/cf/ClusterFinderCUDA.hpp b/include/aare/cf/ClusterFinderCUDA.hpp index b3352221..a3815783 100644 --- a/include/aare/cf/ClusterFinderCUDA.hpp +++ b/include/aare/cf/ClusterFinderCUDA.hpp @@ -536,8 +536,9 @@ class ClusterFinderCUDA { * up. */ void register_input_buffer(void *ptr, size_t bytes) { - if (m_registered_input) - CUDA_CHECK(cudaHostUnregister(m_registered_input)); + // Forget the old buffer before pinning the new one: if that fails, + // the destructor must not unpin the old buffer a second time. + unregister_input_buffer(); CUDA_CHECK(cudaHostRegister(ptr, bytes, cudaHostRegisterDefault)); m_registered_input = ptr; } diff --git a/include/aare/cf/ClusterFinderCUDA_graph.hpp b/include/aare/cf/ClusterFinderCUDA_graph.hpp index 86625d98..4db74bb7 100644 --- a/include/aare/cf/ClusterFinderCUDA_graph.hpp +++ b/include/aare/cf/ClusterFinderCUDA_graph.hpp @@ -261,8 +261,9 @@ class ClusterFinderCUDAGraph { * up. */ void register_input_buffer(void *ptr, size_t bytes) { - if (m_registered_input) - CUDA_CHECK(cudaHostUnregister(m_registered_input)); + // Forget the old buffer before pinning the new one: if that fails, + // the destructor must not unpin the old buffer a second time. + unregister_input_buffer(); CUDA_CHECK(cudaHostRegister(ptr, bytes, cudaHostRegisterDefault)); m_registered_input = ptr; } diff --git a/include/aare/cf/clusterfinder_algo.cuh b/include/aare/cf/clusterfinder_algo.cuh index 01d684d7..6c1dd637 100644 --- a/include/aare/cf/clusterfinder_algo.cuh +++ b/include/aare/cf/clusterfinder_algo.cuh @@ -363,6 +363,11 @@ struct FixedWindow { (static_cast(lp.ncols) + BLOCK_X - 1) / BLOCK_X, (static_cast(lp.nrows) + BLOCK_Y - 1) / BLOCK_Y); + // Clear CUDA's per-thread last error first: one left by a call whose + // result nobody checked (the destructors ignore theirs) would + // otherwise be reported as this launch's error, after the batch's + // earlier frames were queued. + cudaGetLastError(); detail::find_clusters_in_single_frame <<>>( diff --git a/python/tests/test_ClusterFinderCUDA.py b/python/tests/test_ClusterFinderCUDA.py index defd8e0a..70632349 100644 --- a/python/tests/test_ClusterFinderCUDA.py +++ b/python/tests/test_ClusterFinderCUDA.py @@ -13,8 +13,7 @@ BATCH = 4 HIT = (16, 16) -@pytest.fixture -def cf(): +def make_cf(): cf = ClusterFinderCUDA(SHAPE, n_streams=2, min_pedestal_samples=10) rng = np.random.default_rng(42) for _ in range(20): @@ -23,6 +22,11 @@ def cf(): return cf +@pytest.fixture +def cf(): + return make_cf() + + @pytest.fixture def frames(): rng = np.random.default_rng(7) @@ -78,3 +82,20 @@ def test_views_batched_iter_pipelines_all_chunks(cf, frames): chunk=BATCH): seen.append((v.first_frame, v.n_frames)) assert seen == [(10, BATCH), (10 + BATCH, BATCH), (10 + 2 * BATCH, BATCH)] + + +def counts(cf, frames): + with cf.collect_view(cf.submit_batch(frames)) as v: + return [v.count(i) for i in range(v.n_frames)] + + +def test_failed_call_does_not_fail_the_next_find(frames): + # CUDA refuses to register one array twice. The error it keeps until read + # must not make a later find raise after its work was queued: a retry + # would process the frames twice. + expected = counts(make_cf(), frames) + a, a2 = make_cf(), make_cf() + a.register_input_buffer(frames) + with pytest.raises(RuntimeError, match="already mapped"): + a2.register_input_buffer(frames) + assert counts(make_cf(), frames) == expected diff --git a/src/cf/ClusterFinderCUDA.test.cpp b/src/cf/ClusterFinderCUDA.test.cpp index 94f99bca..3d8940b8 100644 --- a/src/cf/ClusterFinderCUDA.test.cpp +++ b/src/cf/ClusterFinderCUDA.test.cpp @@ -48,3 +48,21 @@ TEST_CASE("FixedWindow::launch on caller-owned buffers matches the driver", SECTION("3x3") { check(test_cuda::run_fixed_window_3x3(61, 97, 1000, 20)); } SECTION("9x9") { check(test_cuda::run_fixed_window_9x9(61, 97, 1000, 20)); } } + +// CUDA keeps the error of a failed call until cudaGetLastError() reads it. +// Left set, the next find's launch check reported it after the batch's earlier +// frames had been queued, so a retry processed them twice. +TEST_CASE("ClusterFinderCUDA find is not failed by an earlier CUDA error", + "[cuda]") { + const auto r = test_cuda::find_after_failed_registration(61, 97, 1000, 8); + REQUIRE(r.registration_threw); + REQUIRE_FALSE(r.find_threw); + REQUIRE(r.before.size() == r.after.size()); + size_t total = 0; + for (size_t f = 0; f < r.before.size(); ++f) { + INFO("frame " << f); + REQUIRE(identical(r.before[f], r.after[f])); + total += r.before[f].size(); + } + REQUIRE(total > 0); +} diff --git a/src/cf/ClusterFinderCUDA_testimpl.cu b/src/cf/ClusterFinderCUDA_testimpl.cu index d8307e75..57a04480 100644 --- a/src/cf/ClusterFinderCUDA_testimpl.cu +++ b/src/cf/ClusterFinderCUDA_testimpl.cu @@ -4,6 +4,7 @@ #include "aare/cf/clusterfinder_algo.cuh" #include #include +#include namespace aare::test_cuda { namespace { @@ -171,6 +172,52 @@ ManualVsDriver> run(int nrows, int ncols, int n_ped, } // namespace +FailedRegistration find_after_failed_registration(int nrows, int ncols, + int n_ped, int n_frames) { + using C = Cluster; + using Algo = cuda::FixedWindow; + using Finder = cuda::ClusterFinderCUDA; + const float nSigma = 5.0f; + const size_t max_clusters = 4096; + + const size_t npx = static_cast(nrows) * ncols; + Synth s = make_synth(nrows, ncols, n_ped, n_frames); + const Shape<2> shape{static_cast(nrows), + static_cast(ncols)}; + const NDView sig( + s.sig.data(), {static_cast(n_frames), shape[0], shape[1]}); + + auto find = [&] { + Finder cf(shape, nSigma, max_clusters, 1); + for (int f = 0; f < n_ped; ++f) + cf.push_pedestal_frame( + NDView(s.ped.data() + f * npx, shape)); + auto res = cf.find_clusters_batched(sig, 0); + std::vector> out(n_frames); + for (int f = 0; f < n_frames; ++f) + for (size_t i = 0; i < res[f].size(); ++i) + out[f].push_back(res[f][i]); + return out; + }; + + FailedRegistration r{false, false, find(), {}}; + Finder a(shape, nSigma, max_clusters, 1); + Finder a2(shape, nSigma, max_clusters, 1); + const size_t bytes = s.sig.size() * sizeof(uint16_t); + a.register_input_buffer(s.sig.data(), bytes); + try { + a2.register_input_buffer(s.sig.data(), bytes); + } catch (const std::runtime_error &) { + r.registration_threw = true; + } + try { + r.after = find(); + } catch (const std::runtime_error &) { + r.find_threw = true; + } + return r; +} + ManualVsDriver> run_fixed_window_3x3(int nrows, int ncols, int n_ped, int n_frames) { return run<3>(nrows, ncols, n_ped, n_frames); diff --git a/src/cf/ClusterFinderCUDA_testimpl.hpp b/src/cf/ClusterFinderCUDA_testimpl.hpp index 1a4faf82..bd9a4e3d 100644 --- a/src/cf/ClusterFinderCUDA_testimpl.hpp +++ b/src/cf/ClusterFinderCUDA_testimpl.hpp @@ -21,4 +21,18 @@ run_fixed_window_3x3(int nrows, int ncols, int n_ped, int n_frames); ManualVsDriver> run_fixed_window_9x9(int nrows, int ncols, int n_ped, int n_frames); +struct FailedRegistration { + bool registration_threw; // the second registration of one buffer + bool find_threw; // the find on a new finder afterwards + std::vector>> before; // clean run + std::vector>> after; // after it +}; + +/// Find clusters with a new finder, register one buffer in two finders (CUDA +/// refuses the second), then find again with another new finder. A CUDA error +/// left over from the failed call must not make that find throw after its +/// work was queued, and both finds must give the same clusters. +FailedRegistration find_after_failed_registration(int nrows, int ncols, + int n_ped, int n_frames); + } // namespace aare::test_cuda