From e7fe261aa1b446c43761801a342f62649ff196e6 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:39:12 +0100 Subject: [PATCH] GPU Metal: make the double pipeline work, with the slave's offsets into the shared buffers With --PROCdoublePipeline 1, the slave instance shares the master's mem_host / mem_gpu buffers, but its mHostMemoryBase / mDeviceMemoryBase are the end of the master's permanent memory. The backend computed the buffer offsets of GPUMemCpy, WriteToConstantMemory (metalResolve) and GPUMemClean16 from the instance's bases, so every slave copy and every clean landed that much too low, on the master's memory: the slave's scratch was never cleared (its clusters accumulated over the time frames, 19009 -> 33527 in sim5pp), and its cleans zeroed the master's permanent memory. On pbpb30, plain dev crashed in MergeBorderTracks<3>, and with only the copies fixed every slave time frame was wrong (3.8 M of 4416506 clusters, 0 tracks) and the run hung in DrainStream. --benchmark sync-gpu-wall, which forces the double pipeline, could not run on Metal. The offsets are now taken from the buffers, as the CPU-visible addresses already were, and the slave takes the master's core count as in CUDA (it ran its kernels with the grids for one core). The slave keeps sharing the constant buffer, the queues and the events' backend state with the master, as in CUDA: the pipeline worker runs one chain at a time. pbpb30 and sim5pp, --runs 10 in the double pipeline with debugDumpFinal: all 5 time frames of each instance have the cluster set of the single pipeline (identical sorted dump) and match its tracks as closely as single-pipeline runs match each other (pbpb30 99.53 to 99.66% against 99.53 to 99.58%, sim5pp 100%); rc 0, no errors, no hang. The single pipeline is unchanged (same check, CPU library identical). On an M1 Max, sync-gpu-wall on pbpb30 gives 1095.9 +- 15.0 ms per TF against 1091.3 +- 15.7 for the single pipeline (5 interleaved rounds): the double pipeline works but gains nothing on this small time frame. --- .../Base/metal/GPUReconstructionMetal.mm | 23 +++++++++++-------- ...PUReconstructionMetalKernelsSpecialize.inc | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm b/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm index 1e6d164e267be..eaa053585e06a 100644 --- a/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm +++ b/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm @@ -203,6 +203,7 @@ uint32_t metalGPUCoreCount() } else { auto* master = dynamic_cast(mMaster); mWarpSize = master->mWarpSize; + mMultiprocessorCount = master->mMultiprocessorCount; mMaxBackendThreads = master->mMaxBackendThreads; mDeviceName = master->mDeviceName; mDeviceConstantMem = master->mDeviceConstantMem; @@ -282,21 +283,25 @@ uint32_t metalGPUCoreCount() { // Where a pointer lives as far as the backend is concerned. mDeviceMemoryBase is // a GPU address, so the CPU-visible address of owned memory has to be taken from -// the buffer rather than from the pointer itself. +// the buffer rather than from the pointer itself. The offsets are into the buffers, +// not from the instance's bases: a slave's bases start after the master's +// permanent memory, but it shares the master's buffers. struct MetalMemRef { id buffer; // nil for memory the backend does not own size_t offset; char* cpu; }; -MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const void* p, const void* hostBase, size_t hostSize, const void* devBase, size_t devSize) +MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const void* p) { - if (p >= hostBase && p < ((const char*)hostBase + hostSize)) { - const size_t o = (const char*)p - (const char*)hostBase; + const char* hostBase = (const char*)internals->mem_host.contents; + const char* devBase = (const char*)(uintptr_t)internals->mem_gpu.gpuAddress; + if (p >= hostBase && p < hostBase + internals->mem_host.allocatedSize) { + const size_t o = (const char*)p - hostBase; return {internals->mem_host, o, (char*)internals->mem_host.contents + o}; } - if (p >= devBase && p < ((const char*)devBase + devSize)) { - const size_t o = (const char*)p - (const char*)devBase; + if (p >= devBase && p < devBase + internals->mem_gpu.allocatedSize) { + const size_t o = (const char*)p - devBase; return {internals->mem_gpu, o, (char*)internals->mem_gpu.contents + o}; } return {nil, 0, (char*)p}; @@ -320,8 +325,8 @@ MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const if (nEvents) { StreamWaitForEvents(realStream, evList, nEvents); } - const MetalMemRef s = metalResolve(mInternals, src, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); - const MetalMemRef d = metalResolve(mInternals, dst, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); + const MetalMemRef s = metalResolve(mInternals, src); + const MetalMemRef d = metalResolve(mInternals, dst); if (s.buffer == nil || d.buffer == nil) { // A blit moves data between MTLBuffers only, and the calibration objects @@ -361,7 +366,7 @@ MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const } auto realStream = stream == -1 ? 0 : stream; - const MetalMemRef s = metalResolve(mInternals, src, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); + const MetalMemRef s = metalResolve(mInternals, src); if (s.buffer == nil) { DrainStream(realStream); diff --git a/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc b/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc index 5f456a7ce06bb..26779dcc2805a 100644 --- a/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc +++ b/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc @@ -14,7 +14,8 @@ template <> inline void GPUReconstructionMetal::runKernelBackend(const krnlSetupTime& _xyz, void* const& ptr, uint64_t const& size) { - const uint64_t offset = static_cast(ptr) - static_cast(mDeviceMemoryBase); + // Into mem_gpu, not from mDeviceMemoryBase: a slave shares the buffer, but its base is the end of the master's permanent memory + const uint64_t offset = (uint64_t)(uintptr_t)ptr - mInternals->mem_gpu.gpuAddress; const uint64_t length = (size + 15ull) & ~15ull; if (_xyz.z.evList) { StreamWaitForEvents(_xyz.x.stream, _xyz.z.evList, _xyz.z.nEvents);