From e71b158a57d52701022f5923621da16de3348554 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:59:49 +0100 Subject: [PATCH] GPU: scan the pad baseline in time segments where the backend asks for it The new scan is in the shared TPC cluster finder code and is selected per backend, at compile time, by the new parameter CF_CHECK_PAD_BASELINE_SEGMENTED, which is 1 only for Metal. CUDA, HIP and OpenCL keep the existing scan, since its speed there could not be measured here. GPUTPCCFCheckPadBaseline runs one block of 576 threads per row, and without the HIP tail filter only the up to 138 threads that own a pad do work, each walking all time bins of its pad. Without the filter the baseline needs, per pad, only the number of time bins with charge, the longest run of them and the largest charge, and these combine over consecutive segments of time. So with the new parameter all four threads per pad scan a quarter of the time bins each, straight from the charge map, and the quarters are joined at the end. The block size, its static_assert, the grid and the noisy-pad flags are unchanged; with the HIP tail filter on, the existing scan runs. On an M1 Max the kernel takes 1.6 ms per call instead of 3.7 (median in a Metal System Trace), and the 30-collision Pb-Pb time frame 1088 ms per event instead of 1182 (3 interleaved rounds of 10 runs, load average 4 to 6). The clusters are byte-identical to the CPU's, also with noisy-pad thresholds tightened until 6 to 24% of the clusters are dropped, and the CPU backend's own output is unchanged. --- .../Definitions/Parameters/GPUParameters.csv | 1 + .../GPUTPCCFCheckPadBaseline.cxx | 65 +++++++++++++++++++ .../GPUTPCCFCheckPadBaseline.h | 12 +++- GPU/GPUTracking/kernels.cmake | 1 + 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/GPU/GPUTracking/Definitions/Parameters/GPUParameters.csv b/GPU/GPUTracking/Definitions/Parameters/GPUParameters.csv index 87336277eadbd..82c9200f60ad2 100644 --- a/GPU/GPUTracking/Definitions/Parameters/GPUParameters.csv +++ b/GPU/GPUTracking/Definitions/Parameters/GPUParameters.csv @@ -122,4 +122,5 @@ MERGER_INTERPOLATION_ERROR_TYPE,"""CAMath::half_t""","""float""",,,,,,,,,,,,,,,, COMP_GATHER_KERNEL,4,0,4,4,,,,,,4,4,4,4,,4,,4,4, COMP_GATHER_MODE,3,0,3,3,,,,,,3,3,3,3,,3,,3,3, CF_SCAN_WORKGROUP_SIZE,512,0,,,,,,,,224,,992,448,,1024,,,448, +CF_CHECK_PAD_BASELINE_SEGMENTED,0,0,,,,,,,,,,,,,,,,,1 MERGER_SPLIT_LOOP_INTERPOLATION,,,,,,,,,,,,,,,1,,,1, diff --git a/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.cxx b/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.cxx index dabd79c5601bd..bf91d8e3805fc 100644 --- a/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.cxx +++ b/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.cxx @@ -239,6 +239,12 @@ GPUd() void GPUTPCCFCheckPadBaseline::CheckBaselineGPU(int32_t nBlocks, int32_t const CfFragment& frag = clusterer.mPmemory->frag; const bool hipFilterOn = clusterer.Param().rec.tpc.hipTailFilter; + if constexpr (GPUCA_PAR_CF_CHECK_PAD_BASELINE_SEGMENTED) { + if (!hipFilterOn) { + CheckBaselineGPUSegmented(iBlock, iThread, smem, clusterer); + return; + } + } const Charge hipTailThreshold = clusterer.Param().rec.tpc.hipTailFilterThreshold; const Charge hipTailFilterAlpha = clusterer.Param().rec.tpc.hipTailFilterAlpha; CfArray2D chargeMap(reinterpret_cast(clusterer.mPchargeMap)); @@ -407,6 +413,65 @@ GPUd() void GPUTPCCFCheckPadBaseline::CheckBaselineGPU(int32_t nBlocks, int32_t #endif } +// Without the HIP tail filter, a pad only needs the number of time bins with charge, the longest run of +// them and the largest charge. These combine over consecutive time segments, so the NThreads / MaxNPadsPerRow +// threads of a pad each scan one segment straight from the charge map, instead of one thread scanning all time +// bins of the pad, and the segments are joined at the end. The result is the same as that of the scan in time order. +GPUd() void GPUTPCCFCheckPadBaseline::CheckBaselineGPUSegmented(int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer) +{ +#if defined(GPUCA_GPUCODE) && !defined(GPUCA_DEVICE_SINGLE_THREAD) + constexpr int32_t NSegments = NThreads / MaxNPadsPerRow; + constexpr GPUTPCGeometry geo; + const CfFragment& frag = clusterer.mPmemory->frag; + CfArray2D chargeMap(reinterpret_cast(clusterer.mPchargeMap)); + const auto iRow = iBlock; + const int16_t nPads = geo.NPads(iRow); + const CfChargePos basePos{(Row)iRow, 0, 0}; + const int16_t pad = iThread % MaxNPadsPerRow; + const int32_t segment = iThread / MaxNPadsPerRow; + const int32_t segmentLength = (frag.length + NSegments - 1) / NSegments; + const int32_t tStart = CAMath::Min(segment * segmentLength, frag.length); + const int32_t tEnd = CAMath::Min(tStart + segmentLength, frag.length); + + if (segment < NSegments) { + int32_t totalCharges = 0, consecCharges = 0, maxConsecCharges = 0, firstConsecCharges = 0; + bool leading = true; + Charge maxCharge = 0; + if (pad < nPads) { + for (int32_t t = tStart; t < tEnd; t++) { + const Charge q = chargeMap[basePos.delta({pad, (TPCFragmentTime)t})].unpack(); + totalCharges += q > 0; + consecCharges = q > 0 ? consecCharges + 1 : 0; + maxConsecCharges = CAMath::Max(consecCharges, maxConsecCharges); + maxCharge = CAMath::Max(q, maxCharge); + leading &= q > 0; + firstConsecCharges += leading; + } + } + smem.segments.totalCharges[iThread] = totalCharges; + smem.segments.maxConsecCharges[iThread] = maxConsecCharges; + smem.segments.firstConsecCharges[iThread] = firstConsecCharges; + smem.segments.lastConsecCharges[iThread] = consecCharges; + smem.segments.maxCharge[iThread] = maxCharge; + } + GPUbarrier(); + + if (segment == 0 && pad < nPads) { + int32_t totalCharges = 0, maxConsecCharges = 0, consecCharges = 0; + Charge maxCharge = 0; + for (int32_t i = 0; i < NSegments; i++) { + const int32_t j = i * MaxNPadsPerRow + pad; + const int32_t length = CAMath::Min(CAMath::Min(i * segmentLength, frag.length) + segmentLength, frag.length) - CAMath::Min(i * segmentLength, frag.length); + totalCharges += smem.segments.totalCharges[j]; + maxConsecCharges = CAMath::Max(maxConsecCharges, CAMath::Max(smem.segments.maxConsecCharges[j], consecCharges + smem.segments.firstConsecCharges[j])); + consecCharges = smem.segments.firstConsecCharges[j] == length ? consecCharges + length : smem.segments.lastConsecCharges[j]; + maxCharge = CAMath::Max(smem.segments.maxCharge[j], maxCharge); + } + updatePadBaseline(basePos.gpad + pad, clusterer, totalCharges, maxConsecCharges, maxCharge); + } +#endif +} + GPUd() void GPUTPCCFCheckPadBaseline::CheckBaselineCPU(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer) { #ifndef GPUCA_GPUCODE diff --git a/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.h b/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.h index 0609868dd3b2c..4b925544fbdd0 100644 --- a/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.h +++ b/GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.h @@ -95,7 +95,16 @@ class GPUTPCCFCheckPadBaseline : public GPUKernelTemplate }; struct GPUSharedMemory : public GPUKernelTemplate::GPUSharedMemoryScan64 { - tpccf::Charge charges[NumOfCachedTBs][MaxNPadsPerRow]; + union { + tpccf::Charge charges[NumOfCachedTBs][MaxNPadsPerRow]; + struct { // Per-thread results of the segmented scan, see CheckBaselineGPUSegmented + int32_t totalCharges[NThreads]; + int32_t maxConsecCharges[NThreads]; + int32_t firstConsecCharges[NThreads]; + int32_t lastConsecCharges[NThreads]; + tpccf::Charge maxCharge[NThreads]; + } segments; + }; HipTailRange tails[MaxNPadsPerRow]; uint8_t tailsClosedPad[MaxNPadsPerRow]; HipTailRange tailsClosed[MaxNPadsPerRow]; @@ -139,6 +148,7 @@ class GPUTPCCFCheckPadBaseline : public GPUKernelTemplate private: GPUd() static void CheckBaselineGPU(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer); + GPUd() static void CheckBaselineGPUSegmented(int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer); GPUd() static void CheckBaselineCPU(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUSharedMemory& smem, processorType& clusterer); GPUd() static void updatePadBaseline(int32_t pad, const GPUTPCClusterFinder&, int32_t totalCharges, int32_t consecCharges, tpccf::Charge maxCharge); diff --git a/GPU/GPUTracking/kernels.cmake b/GPU/GPUTracking/kernels.cmake index 0e95c09a3ef78..aeadd5b905eb0 100644 --- a/GPU/GPUTracking/kernels.cmake +++ b/GPU/GPUTracking/kernels.cmake @@ -156,6 +156,7 @@ o2_gpu_kernel_add_parameter(NEIGHBOURS_FINDER_MAX_NNEIGHUP # Number o SORT_STARTHITS # Sort start hits to improve cache locality during tracklet construction [0/1] TPC_MERGER_REBUILD_CONSTRAINED_ROW_MEMORY # Allocate the track rebuild memory only for the row range a track can reach [0/1] CF_SCAN_WORKGROUP_SIZE # Work group size to use in clusterizer scan operation + CF_CHECK_PAD_BASELINE_SEGMENTED # Scan the pad baseline in time segments split between the threads of a pad [0/1] AMD_EUS_PER_CU) # Number of SIMD units per Compute Unit (only for AMD GPUs) o2_gpu_kernel_add_string_parameter(DEDX_STORAGE_TYPE # Data type to use for intermediate storage of dEdx truncated mean inputs