Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GPU/GPUTracking/Base/GPUReconstruction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion GPU/GPUTracking/Base/GPUReconstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; // ...
Expand Down
25 changes: 17 additions & 8 deletions GPU/GPUTracking/Base/GPUReconstructionCPU.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char[]> 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<std::decay_t<decltype(a)>>) {
return deviceProcessors ? static_cast<std::decay_t<decltype(a)>>(HostPointer(a)) : a;
Expand Down Expand Up @@ -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<char[]> 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<char[]>& 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<char[]>(sizeof(GPUConstantMem));
GPUConstantMem* mem = (GPUConstantMem*)buffer.get();
copy = std::make_unique<char[]>(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;
Expand All @@ -136,7 +145,7 @@ std::unique_ptr<char[]> GPUReconstructionCPU::DeviceProcessorOnHostView(const kr
mem->param = param();
mem->calibObjects = processors()->calibObjects;
mem->errorCodes.setMemory(static_cast<uint32_t*>(HostPointer(mem->errorCodes.getErrorPtr() - 1)));
return buffer;
return mem;
}

template <>
Expand Down
2 changes: 1 addition & 1 deletion GPU/GPUTracking/Base/GPUReconstructionCPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*>(HostPointer(static_cast<const void*>(ptr))); }
std::unique_ptr<char[]> DeviceProcessorOnHostView(const krnlSetupTime& _xyz);
GPUConstantMem* DeviceProcessorOnHostView(const krnlSetupTime& _xyz, std::unique_ptr<char[]>& 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()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down