From 6847893e02a0b5ed9c887849b311452807717bfb Mon Sep 17 00:00:00 2001 From: Michal Mrozek Date: Wed, 4 Dec 2019 07:46:44 +0100 Subject: [PATCH] Add support for querying device from usm allocations. Change-Id: I8aad69622e3af1ebec74ee9d325340b02ca9a6b2 Signed-off-by: Michal Mrozek --- .../memory_manager/unified_memory_manager.cpp | 1 + core/memory_manager/unified_memory_manager.h | 2 + public/cl_ext_private.h | 1 + runtime/api/api.cpp | 7 +++ .../api/cl_unified_shared_memory_tests.inl | 58 +++++++++++++++++++ 5 files changed, 69 insertions(+) diff --git a/core/memory_manager/unified_memory_manager.cpp b/core/memory_manager/unified_memory_manager.cpp index f4abd78feb..2d70ddd1a5 100644 --- a/core/memory_manager/unified_memory_manager.cpp +++ b/core/memory_manager/unified_memory_manager.cpp @@ -111,6 +111,7 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(uint32_t rootDeviceIndex, allocData.size = size; allocData.memoryType = memoryProperties.memoryType; allocData.allocationFlagsProperty = memoryProperties.allocationFlags; + allocData.device = memoryProperties.device; std::unique_lock lock(mtx); this->SVMAllocs.insert(allocData); diff --git a/core/memory_manager/unified_memory_manager.h b/core/memory_manager/unified_memory_manager.h index 5d13a1d5c4..1e78ede1f7 100644 --- a/core/memory_manager/unified_memory_manager.h +++ b/core/memory_manager/unified_memory_manager.h @@ -24,6 +24,7 @@ struct SvmAllocationData { size_t size = 0; InternalMemoryType memoryType = InternalMemoryType::SVM; uint64_t allocationFlagsProperty; + void *device = nullptr; }; struct SvmMapOperation { @@ -72,6 +73,7 @@ class SVMAllocsManager { UnifiedMemoryProperties(InternalMemoryType memoryType) : memoryType(memoryType){}; InternalMemoryType memoryType = InternalMemoryType::NOT_SPECIFIED; uint64_t allocationFlags = 0; + void *device = nullptr; }; SVMAllocsManager(MemoryManager *memoryManager); diff --git a/public/cl_ext_private.h b/public/cl_ext_private.h index 7fa2ce5a65..60a8709808 100644 --- a/public/cl_ext_private.h +++ b/public/cl_ext_private.h @@ -99,6 +99,7 @@ typedef cl_uint cl_execution_info_intel; #define CL_MEM_ALLOC_TYPE_INTEL 0x419A #define CL_MEM_ALLOC_BASE_PTR_INTEL 0x419B #define CL_MEM_ALLOC_SIZE_INTEL 0x419C +#define CL_MEM_ALLOC_DEVICE_INTEL 0x419D /* cl_unified_shared_memory_type_intel */ #define CL_MEM_TYPE_UNKNOWN_INTEL 0x4196 diff --git a/runtime/api/api.cpp b/runtime/api/api.cpp index 1673d6c5f4..1e6ef9169a 100644 --- a/runtime/api/api.cpp +++ b/runtime/api/api.cpp @@ -3456,6 +3456,7 @@ void *clDeviceMemAllocINTEL( err.set(CL_INVALID_VALUE); return nullptr; } + unifiedMemoryProperties.device = device; return neoContext->getSVMAllocsManager()->createUnifiedMemoryAllocation(neoDevice->getRootDeviceIndex(), size, unifiedMemoryProperties); } @@ -3489,6 +3490,7 @@ void *clSharedMemAllocINTEL( err.set(CL_INVALID_VALUE); return nullptr; } + unifiedMemoryProperties.device = device; return neoContext->getSVMAllocsManager()->createSharedUnifiedMemoryAllocation(neoContext->getDevice(0)->getRootDeviceIndex(), size, unifiedMemoryProperties, neoContext->getSpecialQueue()); } @@ -3570,6 +3572,11 @@ cl_int clGetMemAllocInfoINTEL( retVal = info.set(unifiedMemoryAllocation->allocationFlagsProperty); return retVal; } + case CL_MEM_ALLOC_DEVICE_INTEL: { + retVal = info.set(static_cast(unifiedMemoryAllocation->device)); + return retVal; + } + default: { } } diff --git a/unit_tests/api/cl_unified_shared_memory_tests.inl b/unit_tests/api/cl_unified_shared_memory_tests.inl index e697fc67a4..e119610c6e 100644 --- a/unit_tests/api/cl_unified_shared_memory_tests.inl +++ b/unit_tests/api/cl_unified_shared_memory_tests.inl @@ -369,6 +369,64 @@ TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithValidUnif EXPECT_EQ(CL_SUCCESS, retVal); } +TEST(clUnifiedSharedMemoryTests, givenDeviceAllocationWhenItIsQueriedForDeviceThenProperDeviceIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSizeRet = 0; + auto device = mockContext.getDevice(0u); + cl_device_id clDevice = device; + auto unifiedMemoryDeviceAllocation = clDeviceMemAllocINTEL(&mockContext, device, nullptr, 4, 0, &retVal); + + cl_device_id returnedDevice; + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemoryDeviceAllocation, CL_MEM_ALLOC_DEVICE_INTEL, sizeof(returnedDevice), &returnedDevice, ¶mValueSizeRet); + + EXPECT_EQ(CL_SUCCESS, retVal); + EXPECT_EQ(paramValueSizeRet, sizeof(returnedDevice)); + EXPECT_EQ(returnedDevice, clDevice); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryDeviceAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, givenSharedAllocationWhenItIsQueriedForDeviceThenProperDeviceIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSizeRet = 0; + auto device = mockContext.getDevice(0u); + cl_device_id clDevice = device; + auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, device, nullptr, 4, 0, &retVal); + + cl_device_id returnedDevice; + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemorySharedAllocation, CL_MEM_ALLOC_DEVICE_INTEL, sizeof(returnedDevice), &returnedDevice, ¶mValueSizeRet); + + EXPECT_EQ(CL_SUCCESS, retVal); + EXPECT_EQ(paramValueSizeRet, sizeof(returnedDevice)); + EXPECT_EQ(returnedDevice, clDevice); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemorySharedAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, givenHostAllocationWhenItIsQueriedForDeviceThenProperDeviceIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSizeRet = 0; + auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 4, 0, &retVal); + + cl_device_id returnedDevice; + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemoryHostAllocation, CL_MEM_ALLOC_DEVICE_INTEL, sizeof(returnedDevice), &returnedDevice, ¶mValueSizeRet); + + EXPECT_EQ(CL_SUCCESS, retVal); + EXPECT_EQ(paramValueSizeRet, sizeof(returnedDevice)); + EXPECT_EQ(returnedDevice, nullptr); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithAllocationBasePtrParamNameThenProperFieldsAreSet) { MockContext mockContext; cl_int retVal = CL_SUCCESS;