From af97ae66116807502b8f71a37fcd0061bc1a0e1a Mon Sep 17 00:00:00 2001 From: David Rohr Date: Fri, 27 Nov 2026 12:51:23 +0100 Subject: [PATCH] GPU: Make host-sorting work with CPUDevice backend --- GPU/GPUTracking/Base/GPUReconstruction.cxx | 1 + GPU/GPUTracking/Base/GPUReconstruction.h | 2 +- GPU/GPUTracking/Base/GPUReconstructionCPU.cxx | 25 +++++++++++++------ GPU/GPUTracking/Base/GPUReconstructionCPU.h | 2 +- .../cpudevice/GPUReconstructionCPUDevice.cxx | 4 +-- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/GPU/GPUTracking/Base/GPUReconstruction.cxx b/GPU/GPUTracking/Base/GPUReconstruction.cxx index b8dc1a3a4c7ae..4a37b7fde54d5 100644 --- a/GPU/GPUTracking/Base/GPUReconstruction.cxx +++ b/GPU/GPUTracking/Base/GPUReconstruction.cxx @@ -159,6 +159,7 @@ int32_t GPUReconstruction::Init() } for (uint32_t i = 0; i < mSlaves.size(); i++) { mSlaves[i]->mDeviceMemoryBase = mDeviceMemoryPermanent; + mSlaves[i]->mDeviceMemoryHostView = mDeviceMemoryHostView ? (char*)mDeviceMemoryHostView + ptrDiff(mSlaves[i]->mDeviceMemoryBase, mDeviceMemoryBase) : nullptr; mSlaves[i]->mHostMemoryBase = mHostMemoryPermanent; mSlaves[i]->mDeviceMemorySize = mDeviceMemorySize - ptrDiff(mSlaves[i]->mDeviceMemoryBase, mDeviceMemoryBase); mSlaves[i]->mHostMemorySize = mHostMemorySize - ptrDiff(mSlaves[i]->mHostMemoryBase, mHostMemoryBase); diff --git a/GPU/GPUTracking/Base/GPUReconstruction.h b/GPU/GPUTracking/Base/GPUReconstruction.h index 0cd6e554298e6..ce341b7cf50a5 100644 --- a/GPU/GPUTracking/Base/GPUReconstruction.h +++ b/GPU/GPUTracking/Base/GPUReconstruction.h @@ -353,7 +353,7 @@ class GPUReconstruction size_t mHostMemoryUSSMax = 0; // Maximum USS of the process seen by QueryHostMemoryUSS std::string mHostMemoryUSSMaxWhere; // and where it was seen void* mDeviceMemoryBase = nullptr; // Same for device ... - void* mDeviceMemoryHostView = nullptr; // Host address of the device memory, if shared with the host at another address + void* mDeviceMemoryHostView = nullptr; // Host address of the device memory. nullptr: not accessible from host. == mDeviceMemoryBase: unified address space. Otherwise: device memory mapped at this address. void* mDeviceMemoryPermanent = nullptr; // ... void* mDeviceMemoryPool = nullptr; // ... void* mDeviceMemoryPoolEnd = nullptr; // ... diff --git a/GPU/GPUTracking/Base/GPUReconstructionCPU.cxx b/GPU/GPUTracking/Base/GPUReconstructionCPU.cxx index 2bae3638f2f67..1a4be02e66b5a 100644 --- a/GPU/GPUTracking/Base/GPUReconstructionCPU.cxx +++ b/GPU/GPUTracking/Base/GPUReconstructionCPU.cxx @@ -65,8 +65,9 @@ inline void GPUReconstructionCPU::runKernelBackend(const krnlSetupTime& _xyz, co throw std::runtime_error("Cannot run device kernel on host with nThreads != 1"); } int32_t nThreads = getNKernelHostThreads(false); - const auto deviceProcessors = IsGPU() && x.device == krnlDeviceType::HostOnDeviceMemory ? DeviceProcessorOnHostView(_xyz) : nullptr; - GPUConstantMem& mem = deviceProcessors ? *(GPUConstantMem*)deviceProcessors.get() : *mHostConstantMem; + std::unique_ptr deviceProcessorsCopy; + GPUConstantMem* const deviceProcessors = IsGPU() && x.device == krnlDeviceType::HostOnDeviceMemory ? DeviceProcessorOnHostView(_xyz, deviceProcessorsCopy) : nullptr; + GPUConstantMem& mem = deviceProcessors ? *deviceProcessors : *mHostConstantMem; auto arg = [&](const auto& a) -> decltype(auto) { if constexpr (std::is_pointer_v>) { return deviceProcessors ? static_cast>(HostPointer(a)) : a; @@ -107,17 +108,25 @@ const void* GPUReconstructionCPU::HostPointer(const void* ptr) const // A kernel run with krnlDeviceType::HostOnDeviceMemory, on a GPU sharing its memory with the host // (UNIFIED_MEM_SORT_ON_HOST), runs the CPU code on the device processors, once the work before it in -// the stream is done: on a copy of them pointing at the host view of the device memory, with the -// host's parameters and calibration objects. The copy is their constant memory. -std::unique_ptr GPUReconstructionCPU::DeviceProcessorOnHostView(const krnlSetupTime& _xyz) +// the stream is done. If the whole device address space is valid on the host (see mDeviceMemoryHostView), +// directly on the constant memory of the device. Otherwise on a copy of the device processors pointing +// at the host view of the device memory, with the host's parameters and calibration objects. The copy +// is their constant memory. +GPUConstantMem* GPUReconstructionCPU::DeviceProcessorOnHostView(const krnlSetupTime& _xyz, std::unique_ptr& copy) { + if (mDeviceMemoryHostView == nullptr) { + throw std::runtime_error("Cannot run kernel on the host on device memory (UNIFIED_MEM_SORT_ON_HOST / UNIFIED_MEM_MERGE_BORDERS_ON_HOST), the backend does not share its memory with the host"); + } if (_xyz.z.evList) { StreamWaitForEvents(_xyz.x.stream, _xyz.z.evList, _xyz.z.nEvents); } SynchronizeStream(_xyz.x.stream); + if (mDeviceMemoryHostView == mDeviceMemoryBase) { + return mDeviceConstantMem; + } static_assert(alignof(GPUConstantMem) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__); - auto buffer = std::make_unique(sizeof(GPUConstantMem)); - GPUConstantMem* mem = (GPUConstantMem*)buffer.get(); + copy = std::make_unique(sizeof(GPUConstantMem)); + GPUConstantMem* mem = (GPUConstantMem*)copy.get(); memcpy((void*)mem, (const void*)mProcessorsShadow, sizeof(GPUConstantMem)); auto onHost = [&](const GPUProcessor* p) { const size_t offset = (const char*)p - (const char*)mProcessorsShadow; @@ -136,7 +145,7 @@ std::unique_ptr GPUReconstructionCPU::DeviceProcessorOnHostView(const kr mem->param = param(); mem->calibObjects = processors()->calibObjects; mem->errorCodes.setMemory(static_cast(HostPointer(mem->errorCodes.getErrorPtr() - 1))); - return buffer; + return mem; } template <> diff --git a/GPU/GPUTracking/Base/GPUReconstructionCPU.h b/GPU/GPUTracking/Base/GPUReconstructionCPU.h index bfab7dd69e39b..0873ce6c30542 100644 --- a/GPU/GPUTracking/Base/GPUReconstructionCPU.h +++ b/GPU/GPUTracking/Base/GPUReconstructionCPU.h @@ -73,7 +73,7 @@ class GPUReconstructionCPU : public GPUReconstructionProcessing::KernelInterface virtual void ReleaseEvent(deviceEvent ev) {} const void* HostPointer(const void* ptr) const; void* HostPointer(void* ptr) const { return const_cast(HostPointer(static_cast(ptr))); } - std::unique_ptr DeviceProcessorOnHostView(const krnlSetupTime& _xyz); + GPUConstantMem* DeviceProcessorOnHostView(const krnlSetupTime& _xyz, std::unique_ptr& copy); size_t TransferMemoryResourceToGPU(GPUMemoryResource* res, int32_t stream = -1, deviceEvent* ev = nullptr, deviceEvent* evList = nullptr, int32_t nEvents = 1) { return TransferMemoryInternal(res, stream, ev, evList, nEvents, true, res->Ptr(), res->PtrDevice()); } size_t TransferMemoryResourceToHost(GPUMemoryResource* res, int32_t stream = -1, deviceEvent* ev = nullptr, deviceEvent* evList = nullptr, int32_t nEvents = 1) { return TransferMemoryInternal(res, stream, ev, evList, nEvents, false, res->PtrDevice(), res->Ptr()); } diff --git a/GPU/GPUTracking/Base/cpudevice/GPUReconstructionCPUDevice.cxx b/GPU/GPUTracking/Base/cpudevice/GPUReconstructionCPUDevice.cxx index 9e2b82db67a9e..d91964f36fb5f 100644 --- a/GPU/GPUTracking/Base/cpudevice/GPUReconstructionCPUDevice.cxx +++ b/GPU/GPUTracking/Base/cpudevice/GPUReconstructionCPUDevice.cxx @@ -101,7 +101,7 @@ int32_t GPUReconstructionCPUDevice::InitDevice_Runtime() GPUError("CPUDevice memory allocation error (trying %ld bytes device / %ld bytes host memory)", (int64_t)mDeviceMemorySize, (int64_t)mHostMemorySize); return 1; } - mDeviceMemoryBase = mInternals->deviceMemory; + mDeviceMemoryBase = mDeviceMemoryHostView = mInternals->deviceMemory; mHostMemoryBase = mInternals->hostMemory; mDeviceConstantMem = (GPUConstantMem*)mInternals->constantMemory; if (GetProcessingSettings().debugLevel >= 1) { @@ -158,7 +158,7 @@ int32_t GPUReconstructionCPUDevice::ExitDevice_Runtime() mInternals->deviceMemory = mInternals->hostMemory = mInternals->constantMemory = nullptr; GPUInfo("CPUDevice Uninitialized"); } - mDeviceMemoryBase = nullptr; + mDeviceMemoryBase = mDeviceMemoryHostView = nullptr; mHostMemoryBase = nullptr; mDeviceConstantMem = nullptr;