ClusterFinderCUDA: no stale CUDA error after a failed call
Build on RHEL8 / build (push) Successful in 4m13s
Build on RHEL9 / build (push) Successful in 4m40s
Run tests using data on local RHEL8 / build (push) Successful in 3m48s

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.
This commit is contained in:
kferjaoui committed 2026-11-12 14:14:09 +01:00
1 parent 673ff6c823
commit 399e8b7381
7 files changed
+113 -6

No files matched your search

+3 -2
View File
@@ -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;
}
+3 -2
View File
@@ -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;
}
+5
View File
@@ -363,6 +363,11 @@ struct FixedWindow {
(static_cast<unsigned>(lp.ncols) + BLOCK_X - 1) / BLOCK_X,
(static_cast<unsigned>(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<ClusterType, FrameType, BLOCK_X,
BLOCK_Y>
<<<grid, block, shmem_bytes, stream>>>(
+23 -2
View File
@@ -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
+18
View File
@@ -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);
}
+47
View File
@@ -4,6 +4,7 @@
#include "aare/cf/clusterfinder_algo.cuh"
#include <algorithm>
#include <random>
#include <stdexcept>
namespace aare::test_cuda {
namespace {
@@ -171,6 +172,52 @@ ManualVsDriver<Cluster<int32_t, N, N>> 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<int32_t, 3, 3>;
using Algo = cuda::FixedWindow<C, uint16_t>;
using Finder = cuda::ClusterFinderCUDA<Algo>;
const float nSigma = 5.0f;
const size_t max_clusters = 4096;
const size_t npx = static_cast<size_t>(nrows) * ncols;
Synth s = make_synth(nrows, ncols, n_ped, n_frames);
const Shape<2> shape{static_cast<ssize_t>(nrows),
static_cast<ssize_t>(ncols)};
const NDView<uint16_t, 3> sig(
s.sig.data(), {static_cast<ssize_t>(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<uint16_t, 2>(s.ped.data() + f * npx, shape));
auto res = cf.find_clusters_batched(sig, 0);
std::vector<std::vector<C>> 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<Cluster<int32_t, 3, 3>>
run_fixed_window_3x3(int nrows, int ncols, int n_ped, int n_frames) {
return run<3>(nrows, ncols, n_ped, n_frames);
+14
View File
@@ -21,4 +21,18 @@ run_fixed_window_3x3(int nrows, int ncols, int n_ped, int n_frames);
ManualVsDriver<Cluster<int32_t, 9, 9>>
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<std::vector<Cluster<int32_t, 3, 3>>> before; // clean run
std::vector<std::vector<Cluster<int32_t, 3, 3>>> 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