From a17f3e34dc3001e27ef0167bf3c7a791c89110bf Mon Sep 17 00:00:00 2001
From: foreman
Date: Fri, 25 Jan 2019 19:18:53 -0500
Subject: [PATCH] P4 to Git Change 1736033 by kjayapra@9_HIPWS_IPCCHKIN on
2019/01/25 17:44:48
SWDEV-145570 - IPC Mem Handle Changes for HIP.
Affected files ...
... //depot/stg/opencl/drivers/opencl/api/hip/hip_internal.hpp#20 edit
... //depot/stg/opencl/drivers/opencl/api/hip/hip_memory.cpp#43 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.hpp#330 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocdevice.cpp#112 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocdevice.hpp#33 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocmemory.cpp#42 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocmemory.hpp#13 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/memory.hpp#108 edit
---
rocclr/runtime/device/device.hpp | 13 ++++++
rocclr/runtime/device/rocm/rocdevice.cpp | 53 ++++++++++++++++++++++++
rocclr/runtime/device/rocm/rocdevice.hpp | 3 ++
rocclr/runtime/device/rocm/rocmemory.cpp | 27 ++++++++++++
rocclr/runtime/device/rocm/rocmemory.hpp | 2 +
rocclr/runtime/platform/memory.hpp | 4 ++
6 files changed, 102 insertions(+)
diff --git a/rocclr/runtime/device/device.hpp b/rocclr/runtime/device/device.hpp
index c804713e25..38a90e214b 100644
--- a/rocclr/runtime/device/device.hpp
+++ b/rocclr/runtime/device/device.hpp
@@ -728,6 +728,10 @@ class Memory : public amd::HeapObject {
virtual uint64_t virtualAddress() const { return 0; }
+ virtual void IpcCreate(size_t offset, size_t* mem_size, void* handle) const {
+ ShouldNotReachHere();
+ }
+
protected:
enum Flags {
HostMemoryDirectAccess = 0x00000001, //!< GPU has direct access to the host memory
@@ -1318,6 +1322,15 @@ class Device : public RuntimeObject {
//! Checks if OCL runtime can use code object manager for compilation
bool ValidateComgr();
+ virtual amd::Memory *IpcAttach(const void* handle, size_t mem_size, unsigned int flags, void** dev_ptr) const {
+ ShouldNotReachHere();
+ return nullptr;
+ }
+
+ virtual void IpcDetach(amd::Memory& memory) const {
+ ShouldNotReachHere();
+ }
+
protected:
//! Enable the specified extension
char* getExtensionString();
diff --git a/rocclr/runtime/device/rocm/rocdevice.cpp b/rocclr/runtime/device/rocm/rocdevice.cpp
index de9b53c9c1..cd82f40dae 100644
--- a/rocclr/runtime/device/rocm/rocdevice.cpp
+++ b/rocclr/runtime/device/rocm/rocdevice.cpp
@@ -1570,6 +1570,59 @@ void Device::updateFreeMemory(size_t size, bool free) {
}
}
+amd::Memory *Device::IpcAttach(const void* handle, size_t mem_size, unsigned int flags, void** dev_ptr) const {
+ amd::Memory* amd_mem_obj = nullptr;
+ hsa_status_t hsa_status = HSA_STATUS_SUCCESS;
+
+ /* Retrieve the devPtr from the handle */
+ hsa_agent_t hsa_agent = getBackendDevice();
+ hsa_status
+ = hsa_amd_ipc_memory_attach(reinterpret_cast(handle),
+ mem_size, 1, &hsa_agent, dev_ptr);
+
+ if (hsa_status != HSA_STATUS_SUCCESS) {
+ LogError("[OCL] HSA failed to attach IPC memory");
+ return nullptr;
+ }
+
+ /* Create an amd Memory object for the pointer */
+ amd_mem_obj = new (context()) amd::Buffer(context(), flags, mem_size, *dev_ptr);
+ if (amd_mem_obj == nullptr) {
+ LogError("[OCL] failed to create a mem object!");
+ return nullptr;
+ }
+
+ if (!amd_mem_obj->create(nullptr)) {
+ LogError("[OCL] failed to create a svm hidden buffer!");
+ amd_mem_obj->release();
+ return nullptr;
+ }
+
+ return amd_mem_obj;
+}
+
+void Device::IpcDetach (amd::Memory& memory) const {
+ void* dev_ptr = nullptr;
+ hsa_status_t hsa_status = HSA_STATUS_SUCCESS;
+
+ if(memory.getSvmPtr() != nullptr) {
+ dev_ptr = memory.getSvmPtr();
+ } else if (memory.getHostMem() != nullptr) {
+ dev_ptr = memory.getHostMem();
+ } else {
+ ShouldNotReachHere();
+ }
+
+ /*Detach the memory from HSA */
+ hsa_status = hsa_amd_ipc_memory_detach(dev_ptr);
+ if (hsa_status != HSA_STATUS_SUCCESS) {
+ LogError("[OCL] HSA failed to detach memory !");
+ return;
+ }
+
+ memory.release();
+}
+
void* Device::svmAlloc(amd::Context& context, size_t size, size_t alignment, cl_svm_mem_flags flags,
void* svmPtr) const {
amd::Memory* mem = nullptr;
diff --git a/rocclr/runtime/device/rocm/rocdevice.hpp b/rocclr/runtime/device/rocm/rocdevice.hpp
index 625f59bff6..c0783184f5 100644
--- a/rocclr/runtime/device/rocm/rocdevice.hpp
+++ b/rocclr/runtime/device/rocm/rocdevice.hpp
@@ -381,6 +381,9 @@ class Device : public NullDevice {
// Update the global free memory size
void updateFreeMemory(size_t size, bool free);
+ virtual amd::Memory* IpcAttach(const void* handle, size_t mem_size, unsigned int flags, void** dev_ptr) const;
+ virtual void IpcDetach (amd::Memory& memory) const;
+
private:
static hsa_ven_amd_loader_1_00_pfn_t amd_loader_ext_table;
diff --git a/rocclr/runtime/device/rocm/rocmemory.cpp b/rocclr/runtime/device/rocm/rocmemory.cpp
index edfdea8e62..af6fdc9b8b 100644
--- a/rocclr/runtime/device/rocm/rocmemory.cpp
+++ b/rocclr/runtime/device/rocm/rocmemory.cpp
@@ -171,6 +171,33 @@ void* Memory::cpuMap(device::VirtualDevice& vDev, uint flags, uint startLayer, u
return mapTarget;
}
+void Memory::IpcCreate(size_t offset, size_t* mem_size, void* handle) const {
+
+ void* dev_ptr = nullptr;
+ hsa_status_t hsa_status = HSA_STATUS_SUCCESS;
+
+ /* Get the memory size from starting pointer */
+ *mem_size = owner()->getSize() - offset;
+
+ /* Get the starting pointer from the amd::Memory object */
+ if (owner()->getSvmPtr() != nullptr) {
+ dev_ptr = reinterpret_cast(owner()->getSvmPtr()) + offset;
+ } else if (owner()->getHostMem() != nullptr) {
+ dev_ptr = reinterpret_cast(owner()->getHostMem()) + offset;
+ } else {
+ ShouldNotReachHere();
+ }
+
+ /* Pass the pointer and memory size to retrieve the handle */
+ hsa_status = hsa_amd_ipc_memory_create(dev_ptr, *mem_size,
+ reinterpret_cast(handle));
+
+ if (hsa_status != HSA_STATUS_SUCCESS) {
+ LogError("[OCL] Failed to create memory for IPC");
+ return;
+ }
+}
+
void Memory::cpuUnmap(device::VirtualDevice& vDev) {
if (!isHostMemDirectAccess() && !IsPersistentDirectMap()) {
if (!vDev.blitMgr().writeBuffer(mapMemory_->getHostMem(), *this, amd::Coord3D(0),
diff --git a/rocclr/runtime/device/rocm/rocmemory.hpp b/rocclr/runtime/device/rocm/rocmemory.hpp
index 1a8ea42385..3b9611c1df 100644
--- a/rocclr/runtime/device/rocm/rocmemory.hpp
+++ b/rocclr/runtime/device/rocm/rocmemory.hpp
@@ -91,6 +91,8 @@ class Memory : public device::Memory {
void* PersistentHostPtr() const { return persistent_host_ptr_; }
+ virtual void IpcCreate (size_t offset, size_t* mem_size, void* handle) const;
+
protected:
bool allocateMapMemory(size_t allocationSize);
diff --git a/rocclr/runtime/platform/memory.hpp b/rocclr/runtime/platform/memory.hpp
index 5cc8eb723f..75b0254fa9 100644
--- a/rocclr/runtime/platform/memory.hpp
+++ b/rocclr/runtime/platform/memory.hpp
@@ -253,6 +253,10 @@ class Memory : public amd::RuntimeObject {
bool forceCopy = false //!< Force system memory allocation
);
+ virtual void IpcCreate (size_t offset, size_t* mem_size, void* handle) const {
+ ShouldNotReachHere();
+ }
+
// Accessors
Memory* parent() const { return parent_; }
bool isParent() const { return isParent_; }