Skip to content

Commit 7518d76

Browse files
committed
GPU: pass the work-item indices to the helpers that lack them
MSL has no ambient work-item builtins: the thread and threadgroup positions exist only as attributes on the kernel entry point, so get_local_id() and its siblings cannot read them from a device function the way CUDA's threadIdx or OpenCL's get_local_id() can. Almost every call site is already inside a function that receives nBlocks, nThreads, iBlock and iThread, which is how the CPU backend has always worked: four of the six helpers expand to the bare iBlock and nBlocks there, so they only compile where those are in scope. Four functions have no index in scope at all and get them passed in: sortInBlock, GPUTPCCFClusterizer::buildCluster, GPUTPCCFNoiseSuppression::findMinimaAndPeaks and GPUTPCCFPeakFinder::isPeak. GPUCA_THREAD_INFO_DECL and GPUCA_THREAD_INFO_PROVIDE gate that so only Metal pays for it: both expand to nothing on every other backend, and the helpers keep reading get_local_id(0) rather than open-coding the index arithmetic.
1 parent 542ba37 commit 7518d76

12 files changed

Lines changed: 37 additions & 26 deletions

‎GPU/Common/GPUCommonAlgorithm.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class GPUCommonAlgorithm
3232
template <class T>
3333
GPUd() static void sort(T* begin, T* end);
3434
template <class T>
35-
GPUd() static void sortInBlock(T* begin, T* end);
35+
GPUd() static void sortInBlock(T* begin, T* end GPUCA_THREAD_INFO_DECL);
3636
template <class T>
3737
GPUd() static void sortDeviceDynamic(T* begin, T* end);
3838
template <class T, class S>
3939
GPUd() static void sort(T* begin, T* end, const S& comp);
4040
template <class T, class S>
41-
GPUd() static void sortInBlock(T* begin, T* end, const S& comp);
41+
GPUd() static void sortInBlock(T* begin, T* end, const S& comp GPUCA_THREAD_INFO_DECL);
4242
template <class T, class S>
4343
GPUd() static void sortDeviceDynamic(T* begin, T* end, const S& comp);
4444
#if __cplusplus >= 202002L // sortOnDevice takes an auto parameter
@@ -268,17 +268,17 @@ GPUdi() void GPUCommonAlgorithm::sort(T* begin, T* end, const S& comp)
268268
}
269269

270270
template <class T>
271-
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end)
271+
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end GPUCA_THREAD_INFO_DECL)
272272
{
273273
#ifndef GPUCA_GPUCODE
274274
GPUCommonAlgorithm::sort(begin, end);
275275
#else
276-
GPUCommonAlgorithm::sortInBlock(begin, end, [](auto&& x, auto&& y) { return x < y; });
276+
GPUCommonAlgorithm::sortInBlock(begin, end, [](auto&& x, auto&& y) { return x < y; } GPUCA_THREAD_INFO_PROVIDE);
277277
#endif
278278
}
279279

280280
template <class T, class S>
281-
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end, const S& comp)
281+
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end, const S& comp GPUCA_THREAD_INFO_DECL)
282282
{
283283
#ifndef GPUCA_GPUCODE
284284
GPUCommonAlgorithm::sort(begin, end, comp);

‎GPU/Common/GPUCommonAlgorithmThrust.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ GPUdi() void GPUCommonAlgorithm::sort(T* begin, T* end, const S& comp)
6363
}
6464
6565
template <class T>
66-
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end) // TODO: Try cub::BlockMergeSort
66+
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end GPUCA_THREAD_INFO_DECL) // TODO: Try cub::BlockMergeSort
6767
{
6868
if (get_local_id(0) == 0) {
6969
sortDeviceDynamic(begin, end);
7070
}
7171
}
7272
7373
template <class T, class S>
74-
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end, const S& comp)
74+
GPUdi() void GPUCommonAlgorithm::sortInBlock(T* begin, T* end, const S& comp GPUCA_THREAD_INFO_DECL)
7575
{
7676
if (get_local_id(0) == 0) {
7777
sortDeviceDynamic(begin, end, comp);

‎GPU/Common/GPUCommonDefAPI.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,16 @@
286286
#define get_group_id(dim) iBlock
287287
#endif
288288

289+
// A device function that uses the helpers above but has none of the indices in
290+
// scope needs them passed in on Metal, where they are ordinary parameters.
291+
// Both expand to nothing everywhere else, and go last in the parameter list.
292+
#ifdef __METAL__
293+
#define GPUCA_THREAD_INFO_DECL , int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread
294+
#define GPUCA_THREAD_INFO_PROVIDE , nBlocks, nThreads, iBlock, iThread
295+
#else
296+
#define GPUCA_THREAD_INFO_DECL
297+
#define GPUCA_THREAD_INFO_PROVIDE
298+
#endif
299+
289300
// clang-format on
290301
#endif

‎GPU/GPUTracking/DataCompression/GPUTPCCompressionKernels.cxx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,16 @@ GPUdii() void GPUTPCCompressionKernels::Thread<GPUTPCCompressionKernels::step1un
274274
static_assert(GPUCA_GET_THREAD_COUNT(GPUCA_LB_GPUTPCCompressionKernels_step1unattached) * 2 <= constants::TPC_COMP_CHUNK_SIZE);
275275
#endif
276276
#ifdef GPUCA_DETERMINISTIC_MODE
277-
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZPadTime>(clusters->clusters[iSector][iRow]));
277+
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZPadTime>(clusters->clusters[iSector][iRow]) GPUCA_THREAD_INFO_PROVIDE);
278278
#else // GPUCA_DETERMINISTIC_MODE
279279
if (param.rec.tpc.compressionSortOrder == GPUSettings::SortZPadTime) {
280-
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZPadTime>(clusters->clusters[iSector][iRow]));
280+
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZPadTime>(clusters->clusters[iSector][iRow]) GPUCA_THREAD_INFO_PROVIDE);
281281
} else if (param.rec.tpc.compressionSortOrder == GPUSettings::SortZTimePad) {
282-
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZTimePad>(clusters->clusters[iSector][iRow]));
282+
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortZTimePad>(clusters->clusters[iSector][iRow]) GPUCA_THREAD_INFO_PROVIDE);
283283
} else if (param.rec.tpc.compressionSortOrder == GPUSettings::SortPad) {
284-
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortPad>(clusters->clusters[iSector][iRow]));
284+
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortPad>(clusters->clusters[iSector][iRow]) GPUCA_THREAD_INFO_PROVIDE);
285285
} else if (param.rec.tpc.compressionSortOrder == GPUSettings::SortTime) {
286-
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortTime>(clusters->clusters[iSector][iRow]));
286+
CAAlgo::sortInBlock(sortBuffer, sortBuffer + count, GPUTPCCompressionKernels_Compare<GPUSettings::SortTime>(clusters->clusters[iSector][iRow]) GPUCA_THREAD_INFO_PROVIDE);
287287
}
288288
#endif // GPUCA_DETERMINISTIC_MODE
289289
GPUbarrier();

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFCheckPadBaseline.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ GPUd() void GPUTPCCFHIPTailConnector::Thread<0>(int32_t nBlocks, int32_t nThread
773773
} else {
774774
return t1.qMax < t2.qMax;
775775
}
776-
});
776+
} GPUCA_THREAD_INFO_PROVIDE);
777777
if (iThread > 0) {
778778
return;
779779
}

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFClusterizer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class GPUTPCCFClusterizer : public GPUKernelTemplate
5959

6060
static GPUd() void computeClustersImpl(int32_t, int32_t, int32_t, int32_t, processorType&, const CfFragment&, GPUSharedMemory&, const CfArray2D<PackedCharge>&, const CfChargePos*, const GPUSettingsRec&, MCLabelAccumulator*, uint32_t, uint32_t, uint32_t*, tpc::ClusterNative*, uint32_t*, int8_t);
6161

62-
static GPUd() void buildCluster(const GPUSettingsRec&, const CfArray2D<PackedCharge>&, CfChargePos, CfChargePos*, PackedCharge*, uint8_t*, ClusterAccumulator*, MCLabelAccumulator*);
62+
static GPUd() void buildCluster(const GPUSettingsRec&, const CfArray2D<PackedCharge>&, CfChargePos, CfChargePos*, PackedCharge*, uint8_t*, ClusterAccumulator*, MCLabelAccumulator* GPUCA_THREAD_INFO_DECL);
6363

6464
static GPUd() uint32_t sortIntoBuckets(processorType&, const tpc::ClusterNative&, uint32_t, uint32_t, uint32_t*, tpc::ClusterNative*);
6565

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFClusterizer.inc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ GPUdii() void GPUTPCCFClusterizer::computeClustersImpl(int32_t nBlocks, int32_t
4949
smem.buf,
5050
smem.innerAboveThreshold,
5151
&pc,
52-
labelAcc);
52+
labelAcc GPUCA_THREAD_INFO_PROVIDE);
5353

5454
if (idx >= clusternum) {
5555
return;
@@ -151,7 +151,7 @@ GPUdii() void GPUTPCCFClusterizer::buildCluster(
151151
PackedCharge* buf,
152152
uint8_t* innerAboveThreshold,
153153
ClusterAccumulator* myCluster,
154-
MCLabelAccumulator* labelAcc)
154+
MCLabelAccumulator* labelAcc GPUCA_THREAD_INFO_DECL)
155155
{
156156
uint16_t ll = get_local_id(0);
157157

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFNoiseSuppression.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ GPUdii() void GPUTPCCFNoiseSuppression::noiseSuppressionImpl(int32_t nBlocks, in
6060
smem.buf,
6161
&minimas,
6262
&bigger,
63-
&peaksAround);
63+
&peaksAround GPUCA_THREAD_INFO_PROVIDE);
6464

6565
peaksAround &= bigger;
6666

@@ -173,7 +173,7 @@ GPUd() void GPUTPCCFNoiseSuppression::findMinimaAndPeaks(
173173
PackedCharge* buf,
174174
uint64_t* minimas,
175175
uint64_t* bigger,
176-
uint64_t* peaks)
176+
uint64_t* peaks GPUCA_THREAD_INFO_DECL)
177177
{
178178
uint16_t ll = get_local_id(0);
179179

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFNoiseSuppression.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class GPUTPCCFNoiseSuppression : public GPUKernelTemplate
6969

7070
static GPUdi() bool keepPeak(uint64_t, uint64_t);
7171

72-
static GPUd() void findMinimaAndPeaks(const CfArray2D<PackedCharge>&, const CfArray2D<uint8_t>&, const GPUSettingsRec&, float, const CfChargePos&, CfChargePos*, PackedCharge*, uint64_t*, uint64_t*, uint64_t*);
72+
static GPUd() void findMinimaAndPeaks(const CfArray2D<PackedCharge>&, const CfArray2D<uint8_t>&, const GPUSettingsRec&, float, const CfChargePos&, CfChargePos*, PackedCharge*, uint64_t*, uint64_t*, uint64_t* GPUCA_THREAD_INFO_DECL);
7373
};
7474

7575
} // namespace o2::gpu

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFPeakFinder.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ GPUdii() bool GPUTPCCFPeakFinder::isPeak(
3838
const CfArray2D<PackedCharge>& chargeMap,
3939
const GPUSettingsRec& calib,
4040
CfChargePos* posBcast,
41-
PackedCharge* buf)
41+
PackedCharge* buf GPUCA_THREAD_INFO_DECL)
4242
{
4343
uint16_t ll = get_local_id(0);
4444

@@ -111,7 +111,7 @@ GPUd() void GPUTPCCFPeakFinder::findPeaksImpl(int32_t nBlocks, int32_t nThreads,
111111
bool hasLostBaseline = pos.valid() ? padHasLostBaseline[pos.gpad] : true;
112112
charge = hasLostBaseline ? 0.f : charge;
113113

114-
uint8_t peak = isPeak(smem, charge, pos, SCRATCH_PAD_SEARCH_N, chargeMap, calib, smem.posBcast, smem.buf);
114+
uint8_t peak = isPeak(smem, charge, pos, SCRATCH_PAD_SEARCH_N, chargeMap, calib, smem.posBcast, smem.buf GPUCA_THREAD_INFO_PROVIDE);
115115

116116
// Exit early if dummy. See comment above.
117117
bool iamDummy = (idx >= digitnum);

0 commit comments

Comments
 (0)