Skip to content

Commit 6d52586

Browse files
committed
GPU Metal: let more groups of the tracklet constructor fit on a core
The tracklet constructor follows each tracklet row by row through a chain of dependent reads: hit data, links and three spline evaluations of the transformation per row, which take about 60% of its time. On an M1 Max it is bound by their latency, not by bandwidth, address translation or ALU (ALU utilization 16%, LLC limiter 22%, MMU limiter 5%, compute occupancy 10%), so it gets faster with more threads in flight, and two limits kept them few: the 12 KB of rows that each group copies to threadgroup memory, and registers allocated for groups of 128 threads. On Metal the rows are now read from device memory, and the pipeline is compiled as for 640 threads per group while it still runs groups of 128, as the minimum blocks of __launch_bounds__ do. Either change alone gains nothing. Together the kernel takes 14% less time on a quarter of a 30 kHz Pb-Pb time frame: 421 -> 362 ms (--debug 1, three interleaved rounds), with the same number of tracks. Compiling for 512 to 768 threads is within 4%, 1024 is slower; groups of 64 or 192 threads are slower than 128. The sim5pp cluster dump is byte-identical to the CPU, and pbpb30 in deterministic mode gives 22,183 tracks. Other backends compile the existing code unchanged.
1 parent 4ef5f46 commit 6d52586

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernels.mm‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@
102102
const uint32_t tunedThreads = this->template getKernelProperties<T, I>().nThreads;
103103

104104
// Like __launch_bounds__: without it the register allocation may leave room
105-
// for fewer threads than the kernel is tuned for.
105+
// for fewer threads than the kernel is tuned for. The tracklet constructor
106+
// runs in groups of 128 threads but gets the registers of a group of 640, as
107+
// the minimum blocks of __launch_bounds__ would do, so that more groups fit
108+
// on a core to hide the latency of its row-by-row reads.
106109
MTLComputePipelineDescriptor* desc = [MTLComputePipelineDescriptor new];
107110
desc.computeFunction = krnl;
108-
desc.maxTotalThreadsPerThreadgroup = tunedThreads;
111+
desc.maxTotalThreadsPerThreadgroup = std::is_same_v<T, GPUTPCTrackletConstructor> ? 640 : tunedThreads;
109112
NSError* error = nil;
110113
id<MTLComputePipelineState> pso = [mInternals->device newComputePipelineStateWithDescriptor:desc options:MTLPipelineOptionNone reflection:nil error:&error];
111114
[desc release];

‎GPU/GPUTracking/SectorTracker/GPUTPCTrackletConstructor.cxx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929

3030
using namespace o2::gpu;
3131

32+
// Metal reads the rows from device memory: their 12 KB of threadgroup memory
33+
// limit how many groups fit on a core, and with more groups the GPU hides
34+
// more of the latency of the row-by-row hit and transformation reads.
35+
#ifdef __METAL__
36+
#define GPUCA_TRACKLET_CONSTRUCTOR_ROW(row, iRow, s) const GPUglobalref() GPUTPCRow& GPUrestrict() row = tracker.Row(iRow)
37+
#else
38+
#define GPUCA_TRACKLET_CONSTRUCTOR_ROW(row, iRow, s) GPUCA_MAKE_SHARED_REF(GPUTPCRow, row, tracker.Row(iRow), s.mRows[iRow])
39+
#endif
40+
3241
GPUdii() void GPUTPCTrackletConstructor::InitTracklet(GPUTPCTrackParam& GPUrestrict() tParam)
3342
{
3443
// Initialize Tracklet Parameters using default values
@@ -96,7 +105,7 @@ GPUd() void GPUTPCTrackletConstructor::StoreTracklet(int32_t /*nBlocks*/, int32_
96105
calink ih = rowHits[iRow];
97106
tracker.TrackletRowHits()[hitout + (iRow - r.mFirstRow)] = ih;
98107
if (ih != CALINK_INVAL && ih != CALINK_DEAD_CHANNEL) {
99-
GPUCA_MAKE_SHARED_REF(GPUTPCRow, row, tracker.Row(iRow), s.mRows[iRow]);
108+
GPUCA_TRACKLET_CONSTRUCTOR_ROW(row, iRow, s);
100109
tracker.MaximizeHitWeight(row, ih, w);
101110
}
102111
}
@@ -106,7 +115,7 @@ template <class T>
106115
GPUdic(2, 1) void GPUTPCTrackletConstructor::UpdateTracklet(int32_t /*nBlocks*/, int32_t /*nThreads*/, int32_t /*iBlock*/, int32_t /*iThread*/, GPUsharedref() T& s, GPUTPCThreadMemory& GPUrestrict() r, GPUconstantref() GPUTPCTracker& GPUrestrict() tracker, GPUTPCTrackParam& GPUrestrict() tParam, int32_t iRow, calink& rowHit, calink* rowHits)
107116
{
108117
// reconstruction of tracklets, tracklets update step
109-
GPUCA_MAKE_SHARED_REF(GPUTPCRow, row, tracker.Row(iRow), s.mRows[iRow]);
118+
GPUCA_TRACKLET_CONSTRUCTOR_ROW(row, iRow, s);
110119

111120
float y0 = row.Grid().YMin();
112121
float stepY = row.HstepY();
@@ -482,7 +491,9 @@ GPUdii() void GPUTPCTrackletConstructor::Thread(int32_t nBlocks, int32_t nThread
482491
if (get_local_id(0) == 0) {
483492
sMem.mNStartHits = *tracker.NStartHits();
484493
}
494+
#ifndef __METAL__
485495
GPUCA_SHARED_CACHE(&sMem.mRows[0], tracker.TrackingDataRows(), GPUTPCGeometry::NROWS * sizeof(GPUTPCRow));
496+
#endif
486497
GPUbarrier();
487498

488499
GPUTPCThreadMemory rMem;
@@ -509,3 +520,5 @@ GPUd() int32_t GPUTPCTrackletConstructor::GPUTPCTrackletConstructorExtrapolation
509520
}
510521
return (rMem.mNHits);
511522
}
523+
524+
#undef GPUCA_TRACKLET_CONSTRUCTOR_ROW

‎GPU/GPUTracking/SectorTracker/GPUTPCTrackletConstructor.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ class GPUTPCTrackletConstructor : public GPUKernelTemplate
6262
};
6363

6464
struct GPUSharedMemory {
65+
#ifndef __METAL__ // see GPUCA_TRACKLET_CONSTRUCTOR_ROW
6566
GPUCA_SHARED_STORAGE(GPUTPCRow mRows[GPUTPCGeometry::NROWS]); // rows
66-
uint32_t mNStartHits; // Total number of start hits
67+
#endif
68+
uint32_t mNStartHits; // Total number of start hits
6769

6870
#ifdef GPUCA_TRACKLET_CONSTRUCTOR_DO_PROFILE
6971
int32_t fMaxSync; // temporary shared variable during profile creation

0 commit comments

Comments
 (0)