Skip to content

Commit e9adec7

Browse files
committed
GPU TPC clusterizer: decode the dense-link ZS channels after the link headers on Metal
GPUTPCCFDecodeZSDenseLink decodes a page with one warp, walking it timebin by timebin and link by link. For every link header, the warp also collected the channels with data, a thread per channel with three warp scans, inside a serial chain of dependent loads that is the latency limit of the kernel. On Metal the header loop now only walks the headers, and thread 0 stores each link's level-2 bitmask and the offset of its channel bitmask in shared memory (+64 bytes). The channels are then collected with one thread per byte of a link's channel bitmask: one warp scan per 32 bytes instead of about three per link. Same samples in the same order, same pointers into the page, including into the next one, same error paths. On an M1 Max the decode takes 23 to 39% less time per launch in normal multi-stream runs (pbpb30 4.9 -> 3.7 ms, 50 kHz TF0 6.7 -> 4.2 ms), about 1 to 2% of the full time frame. The cluster dumps are byte-identical: sim5pp against the CPU, 30 kHz (1 of 4) and 50 kHz TF0 before against after. pbpb30 in deterministic mode gives 22,183 tracks as before, and a 36 against 36 pass comparison at 50 kHz shows no difference in the track count. It is Metal-only for now, since it was measured only there: CUDA, HIP, OpenCL and the CPU compile the existing code unchanged. The chain it shortens is there on every backend, so it could be enabled for them once timed.
1 parent 5dffc90 commit e9adec7

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFDecodeZS.cxx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ GPUd() int16_t GPUTPCCFDecodeZSDenseLink::DecodeTB(
670670
int32_t timeBin = (linkBC + (uint64_t)(raw::RDHUtils::getHeartBeatOrbit(*rawDataHeader) - ctx.firstHBF) * o2::constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN;
671671

672672
int16_t nSamplesInTB = 0;
673+
#ifdef __METAL__
674+
const uint8_t* const tbStart = ctx.page;
675+
#endif
673676

674677
// Read timebin link headers
675678
for (uint8_t iLink = 0; iLink < nLinksInTimebin; iLink++) {
@@ -687,6 +690,15 @@ GPUd() int16_t GPUTPCCFDecodeZSDenseLink::DecodeTB(
687690
MAYBE_PAGE_OVERFLOW(ctx.page);
688691
}
689692

693+
#ifdef __METAL__ // Channels collected after this loop, see below. Measured on Metal only, not yet timed on CUDA and HIP
694+
if (ctx.iThread == 0) {
695+
smem.linkBitmaskL2[iLink] = bitmaskL2;
696+
smem.linkBitmaskOffset[iLink] = ctx.page - tbStart;
697+
}
698+
699+
ConsumeBytes(ctx.page, CAMath::Popcount(bitmaskL2));
700+
MAYBE_PAGE_OVERFLOW(ctx.page);
701+
#else
690702
int32_t nBytesBitmask = CAMath::Popcount(bitmaskL2);
691703

692704
for (int32_t chan = ctx.iThread; chan < CAMath::nextMultipleOf<NTHREADS>(80); chan += NTHREADS) {
@@ -713,9 +725,39 @@ GPUd() int16_t GPUTPCCFDecodeZSDenseLink::DecodeTB(
713725
if (ctx.iThread == 0) {
714726
smem.samplesPerLinkEnd[iLink] = nSamplesInTB;
715727
}
728+
#endif
716729

717730
} // for (uint8_t iLink = 0; iLink < nLinksInTimebin; iLink++)
718731

732+
#ifdef __METAL__
733+
GPUbarrierWarp(); // Ensure the link headers are in shared memory before reading them
734+
735+
// Collect the channels with data, one byte of a link's channel bitmask per thread. A thread per
736+
// channel would need 3 warp-wide scans for each link, in a chain that is long enough as it is.
737+
for (int32_t slot = ctx.iThread; slot < CAMath::nextMultipleOf<NTHREADS>(nLinksInTimebin * 10); slot += NTHREADS) {
738+
const int32_t iLink = slot / 10;
739+
const int32_t chanL2Idx = slot % 10;
740+
uint8_t bitmask = 0;
741+
if (iLink < nLinksInTimebin) {
742+
const uint16_t bitmaskL2 = smem.linkBitmaskL2[iLink];
743+
if (TEST_BIT(bitmaskL2, chanL2Idx)) {
744+
const int32_t chanByteOffset = CAMath::Popcount(bitmaskL2) - 1 - CAMath::Popcount(bitmaskL2 >> (chanL2Idx + 1));
745+
bitmask = PEEK_OVERFLOW(tbStart + smem.linkBitmaskOffset[iLink], chanByteOffset);
746+
}
747+
}
748+
const int32_t nSamplesThread = CAMath::Popcount(bitmask);
749+
const int32_t threadSampleEnd = warp_scan_inclusive_add(nSamplesThread);
750+
int32_t sampleOffset = nSamplesInTB + threadSampleEnd - nSamplesThread;
751+
for (uint32_t bits = bitmask; bits; bits &= bits - 1) {
752+
smem.rawFECChannels[sampleOffset++] = chanL2Idx * 8 + CAMath::Ctz(bits);
753+
}
754+
if (chanL2Idx == 9 && iLink < nLinksInTimebin) {
755+
smem.samplesPerLinkEnd[iLink] = nSamplesInTB + threadSampleEnd;
756+
}
757+
nSamplesInTB += warp_broadcast(threadSampleEnd, NTHREADS - 1);
758+
}
759+
#endif
760+
719761
GPUbarrierWarp(); // Ensure all writes to shared memory are finished, before reading it
720762

721763
if (nSamplesInTB > nSamplesLeftInPage) {

‎GPU/GPUTracking/TPCClusterFinder/GPUTPCCFDecodeZS.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class GPUTPCCFDecodeZSDenseLink : public GPUTPCCFDecodeZSLinkBase
165165
// GPUCA_SHARED_STORAGE(uint32_t ZSPage[o2::tpc::TPCZSHDR::TPC_ZS_PAGE_SIZE / sizeof(uint32_t)]);
166166
uint16_t samplesPerLinkEnd[MaxNLinksPerTimebin]; // Offset from end of TB link header to first sample not in this link
167167
uint8_t linkIds[MaxNLinksPerTimebin];
168+
#ifdef __METAL__
169+
uint16_t linkBitmaskL2[MaxNLinksPerTimebin];
170+
uint16_t linkBitmaskOffset[MaxNLinksPerTimebin]; // Offset of the link's channel bitmask from the timebin header
171+
#endif
168172
uint8_t rawFECChannels[MaxNLinksPerTimebin * 80];
169173
};
170174

0 commit comments

Comments
 (0)