Skip to content

Commit 57490e0

Browse files
committed
GPU Metal: implement the device kernel timers
With --debug 1 and deviceTimers (the default), the kernel timers expect the backend to report the kernel's GPU time, and Metal reported none, so all kernel times read 0. Give the timed kernel a command buffer of its own and report its GPUEndTime - GPUStartTime. The times are only set once the completion is processed, so wait for it rather than for the stream's event. pbpb30 on M1 Max: 1012 ms of kernel time per event, vs 1218 ms on the CPU.
1 parent 6826099 commit 57490e0

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
if (kEvents.evList) {
3030
StreamWaitForEvents(kExec.stream, kEvents.evList, kEvents.nEvents);
3131
}
32+
// Device timers: the kernel gets a command buffer of its own, whose GPU time is the kernel's
33+
const bool timed = GetProcessingSettings().deviceTimers && GetProcessingSettings().debugLevel > 0;
34+
if (timed && mInternals->streamHasWork[kExec.stream]) {
35+
CommitStream(kExec.stream);
36+
}
3237

3338
id<MTLComputePipelineState> pso = mInternals->pipelines[num];
3439
id<MTLComputeCommandEncoder> computeEncoder = [mInternals->commandBuffers[kExec.stream] computeCommandEncoder];
@@ -57,9 +62,19 @@
5762
[computeEncoder dispatchThreadgroups:MTLSizeMake(kExec.nBlocks, 1, 1)
5863
threadsPerThreadgroup:MTLSizeMake(threadGroupSize, 1, 1)];
5964
metalEndEncoder(mInternals, computeEncoder, kExec.stream);
65+
id<MTLCommandBuffer> cb = [mInternals->commandBuffers[kExec.stream] retain];
6066
if (kEvents.ev) {
6167
RecordMarker(kEvents.ev, kExec.stream);
6268
}
69+
if (timed) {
70+
if (mInternals->commandBuffers[kExec.stream] == cb) {
71+
CommitStream(kExec.stream);
72+
}
73+
DrainStream(kExec.stream);
74+
[cb waitUntilCompleted]; // the times are only set once the completion is processed
75+
_xyz.t = cb.GPUEndTime - cb.GPUStartTime;
76+
}
77+
[cb release];
6378
}
6479

6580
template <class T, int32_t I>

0 commit comments

Comments
 (0)