mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-11-24 01:27:54 +01:00
CUDA: load the shared-memory tile with one cooperative loop
The zero-fill pass, the interior load and the eight halo blocks were separate code paths that put all halo work on the block's edge threads. With a 16x16 block that left 2 of 32 lanes active during the side-column loads and made corner threads do 25 serialised loads at 9x9 while the rest of the block waited at the barrier. Every thread now strides over the flat padded tile, maps each cell back to a global pixel and writes either frame - pedestal or zero for cells outside the frame. One barrier instead of two, all lanes busy, and the loads coalesce along tile rows. BLOCK_X/BLOCK_Y become kernel template parameters so the tile width is constexpr and the index division is a multiply and shift; both host classes pass their existing constants. Registers per thread drop with no spills: 3x3 38 -> 34, int 5x5 48 -> 40, int 9x9 128 -> 96. Cluster positions and counts unchanged against ClusterFinderFrozen for 3x3, 5x5 and 9x9 on odd-sized frames, for both the stream and graph finders. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
f89d61640a
commit
3c2ae080a1
3 files changed
+46
-121
No files matched your search
@@ -718,7 +718,8 @@ class ClusterFinderCUDA {
|
||||
if (m_time_kernels)
|
||||
CUDA_CHECK(cudaEventRecord(
|
||||
m_kernel_start_pools[slot][frame_idx], sc.stream));
|
||||
device::find_clusters_in_single_frame<ClusterType, FRAME_TYPE>
|
||||
device::find_clusters_in_single_frame<ClusterType, FRAME_TYPE,
|
||||
BLOCK_X, BLOCK_Y>
|
||||
<<<grid, block, shmem_bytes, sc.stream>>>(
|
||||
sc.d_frame, sc.d_pd_mean, sc.d_pd_sum, sc.d_pd_sum2,
|
||||
sc.d_pd_off, n_pd_samples, m_nSigma,
|
||||
|
||||
@@ -555,7 +555,8 @@ class ClusterFinderCUDAGraph {
|
||||
cudaKernelNodeParams kp = {};
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
kp.func = reinterpret_cast<void *>(
|
||||
device::find_clusters_in_single_frame<ClusterType, FRAME_TYPE>);
|
||||
device::find_clusters_in_single_frame<ClusterType, FRAME_TYPE,
|
||||
BLOCK_X, BLOCK_Y>);
|
||||
kp.gridDim = grid;
|
||||
kp.blockDim = block;
|
||||
kp.sharedMemBytes = static_cast<unsigned int>(shmem_bytes);
|
||||
|
||||
@@ -14,8 +14,10 @@ using COMPUTE_TYPE = float;
|
||||
/// Device pedestal storage and running variance update. See COMPUTE_TYPE.
|
||||
using DEVICE_PED_TYPE = float;
|
||||
|
||||
/// BLOCK_X/BLOCK_Y must equal the launch block dims; they are template
|
||||
/// parameters so the shared-memory tile geometry is compile-time.
|
||||
template <typename ClusterType = Cluster<int32_t, 3, 3>,
|
||||
typename FRAME_TYPE = uint16_t,
|
||||
typename FRAME_TYPE = uint16_t, int BLOCK_X = 16, int BLOCK_Y = 16,
|
||||
typename = std::enable_if_t<no_2x2_cluster<ClusterType>::value>>
|
||||
__global__ void find_clusters_in_single_frame(
|
||||
const FRAME_TYPE *__restrict__ d_frame,
|
||||
@@ -43,138 +45,59 @@ __global__ void find_clusters_in_single_frame(
|
||||
// constexpr int pow2_c2 = ((CSY + 1) / 2) * ((CSX + 1) / 2);
|
||||
constexpr int pow2_c3 = CSX * CSY;
|
||||
|
||||
// Thread/pixel mapping. 32-bit signed: 64-bit integer math is emulated on
|
||||
// the GPU, and the halo guards below rely on `row_global - i >= 0`.
|
||||
auto col_global =
|
||||
static_cast<int32_t>(threadIdx.x + blockDim.x * blockIdx.x);
|
||||
auto row_global =
|
||||
static_cast<int32_t>(threadIdx.y + blockDim.y * blockIdx.y);
|
||||
int32_t global_tid = col_global + ncols * row_global;
|
||||
auto local_tid = threadIdx.x + blockDim.x * threadIdx.y;
|
||||
// Shared-memory tile: the block's pixels plus a halo of
|
||||
// col_radius/row_radius on each side, laid out row-major.
|
||||
constexpr int TILE_W = BLOCK_X + 2 * col_radius;
|
||||
constexpr int TILE_H = BLOCK_Y + 2 * row_radius;
|
||||
constexpr int TILE_SIZE = TILE_W * TILE_H;
|
||||
constexpr int N_THREADS = BLOCK_X * BLOCK_Y;
|
||||
|
||||
// ====================
|
||||
// Shared memory layout
|
||||
// ====================
|
||||
// The tile is laid out contiguously in a 1D configuration:
|
||||
// [0 ... tile_size-1] = pedestal-subtracted frame values
|
||||
//
|
||||
// Each tile has (blockDim.x + 2*col_radius) x (blockDim.y + 2*row_radius)
|
||||
// elements, with a halo of col_radius/row_radius pixels on each side.
|
||||
// Thread/pixel mapping. 32-bit signed: 64-bit integer math is emulated on
|
||||
// the GPU, and the bounds checks below rely on negative coordinates.
|
||||
const int col_global = static_cast<int>(threadIdx.x + BLOCK_X * blockIdx.x);
|
||||
const int row_global = static_cast<int>(threadIdx.y + BLOCK_Y * blockIdx.y);
|
||||
const int global_tid = col_global + ncols * row_global;
|
||||
const int local_tid = static_cast<int>(threadIdx.x + BLOCK_X * threadIdx.y);
|
||||
|
||||
// CUDA prefers raw bytes + aligned cast
|
||||
// Compile error happens when using: `extern __shared__ T sh[];`
|
||||
extern __shared__ __align__(sizeof(COMPUTE_TYPE)) unsigned char smem[];
|
||||
COMPUTE_TYPE *shmem = reinterpret_cast<COMPUTE_TYPE *>(smem);
|
||||
|
||||
// Stride includes halo on both sides
|
||||
auto shmem_stride = static_cast<int>(blockDim.x) + 2 * col_radius;
|
||||
auto tile_size =
|
||||
shmem_stride * (static_cast<int>(blockDim.y) + 2 * row_radius);
|
||||
|
||||
// Offset so that thread (0,0) maps to shared-memory position
|
||||
// (row_radius, col_radius) i.e. past the top-left halo.
|
||||
auto shmem_tid =
|
||||
(static_cast<int>(threadIdx.y) + row_radius) * shmem_stride +
|
||||
// This thread's own pixel in the tile (past the top-left halo)
|
||||
const int shmem_tid =
|
||||
(static_cast<int>(threadIdx.y) + row_radius) * TILE_W +
|
||||
(static_cast<int>(threadIdx.x) + col_radius);
|
||||
|
||||
// Cooperative zero-fill
|
||||
for (int idx = static_cast<int>(local_tid); idx < tile_size;
|
||||
idx += static_cast<int>(blockDim.x * blockDim.y)) {
|
||||
shmem[idx] = COMPUTE_TYPE{0};
|
||||
}
|
||||
__syncthreads();
|
||||
// ==========================================================
|
||||
// Cooperative tile load: interior, halo and OOB zeros in one pass
|
||||
// ==========================================================
|
||||
// Every thread strides over the flat tile, so the halo costs the same
|
||||
// per thread as the interior instead of being serialised on edge lanes.
|
||||
// Cells outside the frame are written as 0, which is what the stencil
|
||||
// expects and removes the need for a separate zero-fill + barrier.
|
||||
const int tile_row0 = static_cast<int>(BLOCK_Y * blockIdx.y) - row_radius;
|
||||
const int tile_col0 = static_cast<int>(BLOCK_X * blockIdx.x) - col_radius;
|
||||
|
||||
// OOB flag
|
||||
bool valid_pixel = col_global < ncols && row_global < nrows;
|
||||
|
||||
// ======================================================
|
||||
// Load pedestal-subtracted frame data into shared memory (MIXED PRECISION)
|
||||
// ======================================================
|
||||
|
||||
// Helper: read (frame - pedestal_mean) from global memory, or 0 if OOB.
|
||||
// gr, gc are the global row/col of the pixel to load.
|
||||
// Returns the pedestal-subtracted value.
|
||||
auto load_pixel = [&] __device__(int32_t gr, int32_t gc) -> COMPUTE_TYPE {
|
||||
int32_t gid = gc + ncols * gr;
|
||||
return static_cast<COMPUTE_TYPE>(d_frame[gid]) - d_pd_mean[gid];
|
||||
};
|
||||
|
||||
// A. Interior: every valid thread loads its own pixel
|
||||
if (valid_pixel) {
|
||||
shmem[shmem_tid] = load_pixel(row_global, col_global);
|
||||
}
|
||||
|
||||
// B. Halo regions (Boundaries)
|
||||
// B.1 Top rows of the halo
|
||||
if (threadIdx.y == 0 && valid_pixel) {
|
||||
for (int i = 1; i <= row_radius; ++i) {
|
||||
if (row_global - i >= 0)
|
||||
shmem[shmem_tid - i * shmem_stride] =
|
||||
load_pixel(row_global - i, col_global);
|
||||
}
|
||||
// Top-left corner rectangle
|
||||
if (threadIdx.x == 0) {
|
||||
for (int i = 1; i <= row_radius; ++i)
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (row_global - i >= 0 && col_global - j >= 0)
|
||||
shmem[shmem_tid - i * shmem_stride - j] =
|
||||
load_pixel(row_global - i, col_global - j);
|
||||
}
|
||||
// Top-right corner rectangle
|
||||
if (threadIdx.x == blockDim.x - 1) {
|
||||
for (int i = 1; i <= row_radius; ++i)
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (row_global - i >= 0 && col_global + j < ncols)
|
||||
shmem[shmem_tid - i * shmem_stride + j] =
|
||||
load_pixel(row_global - i, col_global + j);
|
||||
#pragma unroll
|
||||
for (int idx = local_tid; idx < TILE_SIZE; idx += N_THREADS) {
|
||||
const int tr = idx / TILE_W; // constexpr divisor: mul + shift
|
||||
const int tc = idx - tr * TILE_W;
|
||||
const int gr = tile_row0 + tr;
|
||||
const int gc = tile_col0 + tc;
|
||||
COMPUTE_TYPE v = COMPUTE_TYPE{0};
|
||||
if (gr >= 0 && gr < nrows && gc >= 0 && gc < ncols) {
|
||||
const int gid = gc + ncols * gr;
|
||||
v = static_cast<COMPUTE_TYPE>(d_frame[gid]) - d_pd_mean[gid];
|
||||
}
|
||||
shmem[idx] = v;
|
||||
}
|
||||
|
||||
// B.2 Left column of the halo
|
||||
if (threadIdx.x == 0 && valid_pixel) {
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (col_global - j >= 0)
|
||||
shmem[shmem_tid - j] = load_pixel(row_global, col_global - j);
|
||||
}
|
||||
|
||||
// B.3 Right column of the halo
|
||||
if (threadIdx.x == blockDim.x - 1 && valid_pixel) {
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (col_global + j < ncols)
|
||||
shmem[shmem_tid + j] = load_pixel(row_global, col_global + j);
|
||||
}
|
||||
|
||||
// B.4 Bottom rows of the halo
|
||||
if (threadIdx.y == blockDim.y - 1 && valid_pixel) {
|
||||
for (int i = 1; i <= row_radius; ++i) {
|
||||
if (row_global + i < nrows)
|
||||
shmem[shmem_tid + i * shmem_stride] =
|
||||
load_pixel(row_global + i, col_global);
|
||||
}
|
||||
// Bottom-left corner rectangle
|
||||
if (threadIdx.x == 0) {
|
||||
for (int i = 1; i <= row_radius; ++i)
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (row_global + i < nrows && col_global - j >= 0)
|
||||
shmem[shmem_tid + i * shmem_stride - j] =
|
||||
load_pixel(row_global + i, col_global - j);
|
||||
}
|
||||
// Bottom-right corner rectangle
|
||||
if (threadIdx.x == blockDim.x - 1) {
|
||||
for (int i = 1; i <= row_radius; ++i)
|
||||
for (int j = 1; j <= col_radius; ++j)
|
||||
if (row_global + i < nrows && col_global + j < ncols)
|
||||
shmem[shmem_tid + i * shmem_stride + j] =
|
||||
load_pixel(row_global + i, col_global + j);
|
||||
}
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
// =====================
|
||||
// Cluster-finding logic
|
||||
// =====================
|
||||
if (!valid_pixel)
|
||||
if (col_global >= ncols || row_global >= nrows)
|
||||
return;
|
||||
|
||||
// Per-pixel variance from global pedestal arrays.
|
||||
@@ -219,7 +142,7 @@ __global__ void find_clusters_in_single_frame(
|
||||
for (int ir = -row_radius; ir <= row_radius; ++ir) {
|
||||
#pragma unroll
|
||||
for (int ic = -col_radius; ic <= col_radius; ++ic) {
|
||||
COMPUTE_TYPE val = shmem[shmem_tid + ir * shmem_stride + ic];
|
||||
COMPUTE_TYPE val = shmem[shmem_tid + ir * TILE_W + ic];
|
||||
|
||||
total += val;
|
||||
max_val = val > max_val ? val : max_val;
|
||||
@@ -286,7 +209,7 @@ __global__ void find_clusters_in_single_frame(
|
||||
// pedestal via push_fast(). In this kernel, the GPU updates all pixels in a
|
||||
// frame simultaneously. So the updated pedestal will only be used starting
|
||||
// from the next frame. -> This avoids a/serialization and b/global mem I/O.
|
||||
if (!is_photon && valid_pixel) {
|
||||
if (!is_photon) {
|
||||
// Update the CENTERED moments of Y = X - X0 (X0 = frozen baseline).
|
||||
// Both sum (~n*E[Y]) and sum2 (~n*E[Y^2]) stay O(rms)-scale, so the
|
||||
// running EMA and its float rounding never touch the ~1e10 magnitudes
|
||||
@@ -322,7 +245,7 @@ __global__ void find_clusters_in_single_frame(
|
||||
for (int ir = -row_radius; ir <= row_radius; ++ir) {
|
||||
#pragma unroll
|
||||
for (int ic = -col_radius; ic <= col_radius; ++ic) {
|
||||
COMPUTE_TYPE val = shmem[shmem_tid + ir * shmem_stride + ic];
|
||||
COMPUTE_TYPE val = shmem[shmem_tid + ir * TILE_W + ic];
|
||||
if constexpr (std::is_integral_v<CT>)
|
||||
clusterData[idx] = static_cast<CT>(lround(val));
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user