From e80442fdbf85bc4d4234f29e56b95d3fbc279187 Mon Sep 17 00:00:00 2001 From: Ioannis Assiouras Date: Wed, 4 Dec 2024 22:47:44 +0000 Subject: [PATCH] SWDEV-497759 - Fix memObj offset computation for hipHostRegister on Windows mgpu On Windows, hipHostRegister may add a single object in the MemObjMap that maps to memory that is allocated on different devices. This change ensures that the offset that is returned from getMemoryObject() is computed relative to the memory that is allocated on the current device. Change-Id: I5fd3af200bf6f4926fdeaea12dcb9d0154d3a843 --- hipamd/src/hip_memory.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hipamd/src/hip_memory.cpp b/hipamd/src/hip_memory.cpp index 5abf3d3397..35303c47ef 100644 --- a/hipamd/src/hip_memory.cpp +++ b/hipamd/src/hip_memory.cpp @@ -40,6 +40,20 @@ amd::Memory* getMemoryObject(const void* ptr, size_t& offset, size_t size) { memObj = (hip::getCurrentDevice()->asContext()->svmDevices()[0])->GetArenaMemObj( ptr, offset, size); } + + // On Windows, when using hipHostRegister, the map may contain a single memory object for + // multiple devices. This is because device addresses can overlap. + // The offset needs to be calculated relative to the memory of the current device. + if (IS_WINDOWS && (memObj != nullptr) && (memObj->getMemFlags() & CL_MEM_USE_HOST_PTR)) { + device::Memory* currentDevMem = + memObj->getDeviceMemory(*hip::getCurrentDevice()->devices()[0], false); + if (currentDevMem != nullptr) { + size_t currentDevOffset = reinterpret_cast(ptr) - currentDevMem->virtualAddress(); + if (currentDevOffset < memObj->getSize()) { + offset = currentDevOffset; + } + } + } return memObj; }