CUDA: unify GPU code under aare::cuda
Build on RHEL8 / build (push) Successful in 3m55s
Build on RHEL9 / build (push) Successful in 4m8s
Run tests using data on local RHEL8 / build (push) Successful in 4m55s

- aare::device -> aare::cuda::detail; drivers, RAII helpers,
  StreamContext* moved into aare::cuda(::detail)
- Algo contract: add compute_type, assert floating point
- LaunchParams::nSigma uses compute_type
- test: use FastPedestal in the manual route
This commit is contained in:
kferjaoui committed 2026-09-28 11:47:55 +02:00
1 parent 1503331100
commit 0c3c45e653
10 files changed
+73 -49

No files matched your search

+2 -2
View File
@@ -38,8 +38,8 @@ napoleon_use_ivar = True
# CUDA execution-space and pointer qualifiers are not C++, so Sphinx's C++
# domain cannot parse declarations such as the __global__ kernels in
# aare::device. Declaring them as attributes lets the parser skip them instead
# of failing, which matters because the docs are built with -W.
# aare::cuda::detail. Declaring them as attributes lets the parser skip them
# instead of failing, which matters because the docs are built with -W.
cpp_id_attributes = [
'__global__',
'__device__',
+3 -3
View File
@@ -5,7 +5,7 @@ GPU cluster finder. Available only when aare is configured with
``-DAARE_CUDA=ON``; on a CPU-only build the class is not compiled and the
Python factory raises ``RuntimeError``.
Single frames go through :cpp:func:`aare::ClusterFinderCUDA::find_clusters`,
Single frames go through :cpp:func:`aare::cuda::ClusterFinderCUDA::find_clusters`,
which mirrors the CPU :cpp:class:`aare::ClusterFinder` interface. Throughput
comes from ``find_clusters_batched``, which distributes a batch of frames
round-robin over ``n_streams`` CUDA streams (4 by default) and overlaps H2D,
@@ -14,13 +14,13 @@ returns one ``ClusterVector`` per frame, or with ``collect_view``, which hands
back a ``BatchView`` reading clusters in place from the pinned host buffer with
no per-frame allocation or copy.
.. doxygenclass:: aare::ClusterFinderCUDA
.. doxygenclass:: aare::cuda::ClusterFinderCUDA
:members:
:undoc-members:
Device kernel
-------------
.. doxygennamespace:: aare::device
.. doxygennamespace:: aare::cuda::detail
:members:
:undoc-members:
+12 -7
View File
@@ -14,7 +14,9 @@
#include <stdexcept>
#include <vector>
namespace aare {
namespace aare::cuda {
namespace detail {
/**
* @brief One stream's device resources, owned.
@@ -61,6 +63,8 @@ template <typename Algo> class StreamContext {
typename Ped::View pedestal_view() const { return Ped::make_view(m_ped); }
};
} // namespace detail
/**
* @brief GPU cluster-finding driver: streams, batching, host staging.
*
@@ -84,15 +88,16 @@ class ClusterFinderCUDA {
using FRAME_TYPE = typename Algo::frame_type;
using Ped = typename Algo::pedestal;
using Output = typename Algo::Output;
using COMPUTE_TYPE =
typename Algo::compute_type; // match the kernel's internal precision
// i.e. detail::COMPUTE_TYPE
private:
static_assert(no_2x2_cluster<ClusterType>::value,
"ClusterFinderCUDA: both cluster dimensions must exceed 2");
static_assert(cuda::contract::require_algo<Algo>::value,
static_assert(contract::require_algo<Algo>::value,
"ClusterFinderCUDA: Algo does not satisfy the contract in "
"clusterfinder_algo.cuh");
using COMPUTE_TYPE =
device::COMPUTE_TYPE; // match the kernel's internal precision
// Two ping-pong output slots. At most 2 batches can be in flight at once:
// the caller submits batch B before collecting batch A, so A and B occupy
@@ -138,7 +143,7 @@ class ClusterFinderCUDA {
bool m_pedestal_dirty = true;
// One per stream. unique_ptr so the context can stay non-movable.
std::vector<std::unique_ptr<StreamContext<Algo>>> v_sc;
std::vector<std::unique_ptr<detail::StreamContext<Algo>>> v_sc;
float m_total_kernel_ms = 0.0f;
size_t m_frames_processed = 0;
@@ -473,7 +478,7 @@ class ClusterFinderCUDA {
// algorithm's pedestal arrays and the packed output block.
v_sc.reserve(n_streams);
for (int k = 0; k < n_streams; ++k)
v_sc.push_back(std::make_unique<StreamContext<Algo>>(
v_sc.push_back(std::make_unique<detail::StreamContext<Algo>>(
m_image_size, static_cast<uint32_t>(m_max_clusters_per_frame)));
for (int s = 0; s < NUM_SLOTS; ++s) {
@@ -962,4 +967,4 @@ class ClusterFinderCUDA {
}
};
} // namespace aare
} // namespace aare::cuda
+25 -20
View File
@@ -12,7 +12,9 @@
#include <stdexcept>
#include <vector>
namespace aare {
namespace aare::cuda {
namespace detail {
// Per-stream device resources and CUDA Graph state.
template <typename ClusterType, typename FRAME_TYPE, typename PEDESTAL_TYPE>
@@ -21,10 +23,10 @@ struct StreamContextGraph {
FRAME_TYPE *d_frame = nullptr;
// Device pedestal precision is set by DEVICE_PED_TYPE in the kernel header.
// Accumulators hold CENTERED moments of Y = X - d_pd_off (see kernel).
device::DEVICE_PED_TYPE *d_pd_mean = nullptr;
device::DEVICE_PED_TYPE *d_pd_sum = nullptr;
device::DEVICE_PED_TYPE *d_pd_sum2 = nullptr;
device::DEVICE_PED_TYPE *d_pd_off = nullptr; // frozen per-pixel baseline X0
DEVICE_PED_TYPE *d_pd_mean = nullptr;
DEVICE_PED_TYPE *d_pd_sum = nullptr;
DEVICE_PED_TYPE *d_pd_sum2 = nullptr;
DEVICE_PED_TYPE *d_pd_off = nullptr; // frozen per-pixel baseline X0
uint8_t *d_output = nullptr; // [uint32_t count | ClusterType clusters[max]]
// CUDA Graph handles — rebuilt on pedestal change or h_output_pinned resize
@@ -36,12 +38,12 @@ struct StreamContextGraph {
// Kernel argument storage for cudaKernelNodeParams.
// kargs_ptrs holds addresses into these fields; members must not move.
FRAME_TYPE *karg_d_frame = nullptr;
device::DEVICE_PED_TYPE *karg_d_pd_mean = nullptr;
device::DEVICE_PED_TYPE *karg_d_pd_sum = nullptr;
device::DEVICE_PED_TYPE *karg_d_pd_sum2 = nullptr;
device::DEVICE_PED_TYPE *karg_d_pd_off = nullptr;
DEVICE_PED_TYPE *karg_d_pd_mean = nullptr;
DEVICE_PED_TYPE *karg_d_pd_sum = nullptr;
DEVICE_PED_TYPE *karg_d_pd_sum2 = nullptr;
DEVICE_PED_TYPE *karg_d_pd_off = nullptr;
uint32_t karg_n_pd_samples = 0;
device::COMPUTE_TYPE karg_nSigma = 0.0f;
COMPUTE_TYPE karg_nSigma = 0.0f;
int32_t karg_nrows = 0; // must match the kernel signature exactly:
int32_t karg_ncols = 0; // kernelParams is untyped
ClusterType *karg_d_clusters = nullptr;
@@ -54,12 +56,14 @@ struct StreamContextGraph {
cudaMemcpy3DParms d2h_params = {};
};
} // namespace detail
template <typename ClusterType = Cluster<int32_t, 3, 3>,
typename FRAME_TYPE = uint16_t, typename PEDESTAL_TYPE = double,
typename = std::enable_if_t<no_2x2_cluster<ClusterType>::value>>
class ClusterFinderCUDAGraph {
using COMPUTE_TYPE =
device::COMPUTE_TYPE; // match the kernel's internal precision
detail::COMPUTE_TYPE; // match the kernel's internal precision
static constexpr int BLOCK_X = 16;
static constexpr int BLOCK_Y = 16;
@@ -99,9 +103,10 @@ class ClusterFinderCUDAGraph {
bool m_pedestal_dirty = true;
bool m_graphs_dirty = true; // set when pedestal or h_output_pinned changes
// Frozen per-pixel baseline X0 (~mean at t=0), captured once on first sync.
std::vector<device::DEVICE_PED_TYPE> m_offset;
std::vector<detail::DEVICE_PED_TYPE> m_offset;
using SC = StreamContextGraph<ClusterType, FRAME_TYPE, PEDESTAL_TYPE>;
using SC =
detail::StreamContextGraph<ClusterType, FRAME_TYPE, PEDESTAL_TYPE>;
std::vector<SC> v_sc;
size_t m_frames_processed = 0;
@@ -190,13 +195,13 @@ class ClusterFinderCUDAGraph {
cudaStreamCreateWithFlags(&sc.stream, cudaStreamNonBlocking));
CUDA_CHECK(cudaMalloc(&sc.d_frame, m_image_bytes));
CUDA_CHECK(cudaMalloc(
&sc.d_pd_mean, m_image_size * sizeof(device::DEVICE_PED_TYPE)));
&sc.d_pd_mean, m_image_size * sizeof(detail::DEVICE_PED_TYPE)));
CUDA_CHECK(cudaMalloc(
&sc.d_pd_sum, m_image_size * sizeof(device::DEVICE_PED_TYPE)));
&sc.d_pd_sum, m_image_size * sizeof(detail::DEVICE_PED_TYPE)));
CUDA_CHECK(cudaMalloc(
&sc.d_pd_sum2, m_image_size * sizeof(device::DEVICE_PED_TYPE)));
&sc.d_pd_sum2, m_image_size * sizeof(detail::DEVICE_PED_TYPE)));
CUDA_CHECK(cudaMalloc(
&sc.d_pd_off, m_image_size * sizeof(device::DEVICE_PED_TYPE)));
&sc.d_pd_off, m_image_size * sizeof(detail::DEVICE_PED_TYPE)));
CUDA_CHECK(cudaMalloc(&sc.d_output, m_output_bytes_per_frame));
}
}
@@ -438,7 +443,7 @@ class ClusterFinderCUDAGraph {
auto h_sum = m_pedestal.get_sum();
auto h_sum2 = m_pedestal.get_sum2();
using DPT = device::DEVICE_PED_TYPE;
using DPT = detail::DEVICE_PED_TYPE;
const double n = static_cast<double>(m_pedestal.n_samples());
// Capture the frozen per-pixel baseline X0 (≈ mean at t=0) ONCE, then
@@ -563,7 +568,7 @@ class ClusterFinderCUDAGraph {
cudaKernelNodeParams kp = {};
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
kp.func = reinterpret_cast<void *>(
device::find_clusters_in_single_frame<ClusterType, FRAME_TYPE,
detail::find_clusters_in_single_frame<ClusterType, FRAME_TYPE,
BLOCK_X, BLOCK_Y>);
kp.gridDim = grid;
kp.blockDim = block;
@@ -595,4 +600,4 @@ class ClusterFinderCUDAGraph {
}
};
} // namespace aare
} // namespace aare::cuda
+2 -2
View File
@@ -3,7 +3,7 @@
#include <cuda_runtime.h>
#include <memory>
namespace aare {
namespace aare::cuda {
template <typename T> struct CudaFreeDeleter {
void operator()(T *p) const noexcept {
@@ -52,4 +52,4 @@ inline Stream make_stream() {
return Stream{raw_ptr};
}
} // namespace aare
} // namespace aare::cuda
+13 -5
View File
@@ -43,7 +43,10 @@ namespace aare::cuda {
*
* Algo (the search algorithm, e.g. FixedWindow):
* cluster_type, frame_type, pedestal pedestal must satisfy the above
* LaunchParams per-launch algorithm scalars
* compute_type floating-point type of nSigma; the driver stores it
* LaunchParams aggregate, brace-initialised by the driver as
* {int32_t nrows, int32_t ncols, compute_type nSigma,
* uint32_t max_clusters}
* Output owning, move-only device output
* block Output Output::allocate(uint32_t max_clusters_per_frame) static
* constexpr size_t Output::clusters_offset static size_t
@@ -83,6 +86,9 @@ template <class Algo> struct require_algo {
"clusters_offset' and 'static size_t bytes_per_frame(uint32_t)': the "
"driver sizes its pinned host slots from them, before any Output "
"exists.");
static_assert(std::is_floating_point<typename Algo::compute_type>::value,
"Algo::compute_type is the nSigma threshold type: an "
"integral type compiles and truncates nSigma (3.5 -> 3).");
static constexpr bool value = true;
};
@@ -105,7 +111,7 @@ template <class Algo> struct require_algo {
* and the caller supplies it.
*/
struct CenteredRunningPedestal {
using DevicePedT = device::DEVICE_PED_TYPE;
using DevicePedT = detail::DEVICE_PED_TYPE;
struct Buffers {
DeviceBuffer<DevicePedT> mean;
@@ -282,6 +288,8 @@ struct FixedWindow {
using cluster_type = ClusterType;
using frame_type = FrameType;
using pedestal = Ped;
using compute_type = detail::COMPUTE_TYPE; // exposed here but fixed by the
// kernel (not a knob)
/// The output block one frame writes: a count word followed by the
/// clusters, in one allocation. Owns the device copy and knows how to read
@@ -336,7 +344,7 @@ struct FixedWindow {
struct LaunchParams {
int32_t nrows, ncols;
float nSigma;
compute_type nSigma;
uint32_t max_clusters;
};
@@ -349,13 +357,13 @@ struct FixedWindow {
constexpr int row_radius = ClusterType::cluster_size_y / 2;
constexpr size_t shmem_bytes = (BLOCK_X + 2 * col_radius) *
(BLOCK_Y + 2 * row_radius) *
sizeof(device::COMPUTE_TYPE);
sizeof(compute_type);
const dim3 block(BLOCK_X, BLOCK_Y);
const dim3 grid(
(static_cast<unsigned>(lp.ncols) + BLOCK_X - 1) / BLOCK_X,
(static_cast<unsigned>(lp.nrows) + BLOCK_Y - 1) / BLOCK_Y);
device::find_clusters_in_single_frame<ClusterType, FrameType, BLOCK_X,
detail::find_clusters_in_single_frame<ClusterType, FrameType, BLOCK_X,
BLOCK_Y>
<<<grid, block, shmem_bytes, stream>>>(
d_frame, pv.d_pd_mean, pv.d_pd_sum, pv.d_pd_sum2, pv.d_pd_off,
+2 -2
View File
@@ -5,7 +5,7 @@
#include <cuda_runtime.h>
#include <type_traits>
namespace aare::device {
namespace aare::cuda::detail {
/// Stencil arithmetic precision. Set both aliases to double for an f64 build.
/// float is safe here because the device pedestal stores centered moments
@@ -288,4 +288,4 @@ __global__ void find_clusters_in_single_frame(
}
}
} // namespace aare::device
} // namespace aare::cuda::detail
+2 -2
View File
@@ -29,8 +29,8 @@ void define_ClusterFinderCUDA(py::module &m, const std::string &typestr) {
using ClusterType = Cluster<T, ClusterSizeX, ClusterSizeY, CoordType>;
// The driver is templated on the algorithm; a second algorithm is a second
// instantiation registered under its own suffix (see ClusterFinder.py).
using Algo = aare::cuda::FixedWindow<ClusterType, uint16_t>;
using CF = ClusterFinderCUDA<Algo, pd_type>;
using Algo = cuda::FixedWindow<ClusterType, uint16_t>;
using CF = cuda::ClusterFinderCUDA<Algo, pd_type>;
using ContigArr =
py::array_t<uint16_t, py::array::c_style | py::array::forcecast>;
+1 -1
View File
@@ -26,7 +26,7 @@ void define_ClusterFinderCUDAGraph(py::module &m, const std::string &typestr) {
auto class_name = fmt::format("ClusterFinderCUDAGraph_{}", typestr);
using ClusterType = Cluster<T, ClusterSizeX, ClusterSizeY, CoordType>;
using CF = ClusterFinderCUDAGraph<ClusterType, uint16_t, pd_type>;
using CF = cuda::ClusterFinderCUDAGraph<ClusterType, uint16_t, pd_type>;
using ContigArr =
py::array_t<uint16_t, py::array::c_style | py::array::forcecast>;
+11 -5
View File
@@ -78,10 +78,16 @@ ManualVsDriver<Cluster<int32_t, N, N>> run(int nrows, int ncols, int n_ped,
// ---- Route 1: caller-owned buffers, nothing from the driver ----------
{
Pedestal<double> ped(nrows, ncols);
for (int f = 0; f < n_ped; ++f)
ped.push(NDView<uint16_t, 2>(
const_cast<uint16_t *>(s.ped.data() + f * npx), shape));
FastPedestal<double> ped(nrows, ncols);
for (int f = 0; f < n_ped; ++f) {
NDView<uint16_t, 2> v(
const_cast<uint16_t *>(s.ped.data() + f * npx), shape);
// Same sequence as ClusterFinderCUDA::push_pedestal_frame.
if (!ped.ready())
ped.add_init_frame(v);
else
ped.push_ema(v);
}
using Ped = typename Algo::pedestal;
const auto img = Ped::prepare_pedestal(ped);
@@ -147,7 +153,7 @@ ManualVsDriver<Cluster<int32_t, N, N>> run(int nrows, int ncols, int n_ped,
// ---- Route 2: the driver. One stream so the per-frame pedestal update
// follows the same sequence as route 1. --------------------------------
{
ClusterFinderCUDA<Algo> cf(shape, nSigma, max_clusters, 1);
cuda::ClusterFinderCUDA<Algo> cf(shape, nSigma, max_clusters, 1);
for (int f = 0; f < n_ped; ++f)
cf.push_pedestal_frame(NDView<uint16_t, 2>(
const_cast<uint16_t *>(s.ped.data() + f * npx), shape));