From 0758661c520e712c4b9be20de578ce44614481a3 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Wed, 19 Jun 2019 14:10:53 +0200 Subject: [PATCH] [9/n] Unified Shared Memory - Wire in support for clMemFreeINTEL. Related-To: NEO-3148 Change-Id: Ibef61b7d9bdfa3021533faa0019b6b443145f1be Signed-off-by: Mrozek, Michal --- runtime/api/api.cpp | 14 ++++++- runtime/memory_manager/svm_memory_manager.cpp | 4 +- runtime/memory_manager/svm_memory_manager.h | 2 +- .../api/cl_unified_shared_memory_tests.inl | 39 +++++++++++++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/runtime/api/api.cpp b/runtime/api/api.cpp index 566a2e8c6b..ff2036eeb0 100644 --- a/runtime/api/api.cpp +++ b/runtime/api/api.cpp @@ -3443,7 +3443,19 @@ void *clSharedMemAllocINTEL( cl_int clMemFreeINTEL( cl_context context, const void *ptr) { - return CL_OUT_OF_HOST_MEMORY; + + Context *neoContext = nullptr; + auto retVal = validateObjects(WithCastToInternal(context, &neoContext)); + + if (retVal != CL_SUCCESS) { + return retVal; + } + + if (!neoContext->getSVMAllocsManager()->freeSVMAlloc(const_cast(ptr))) { + return CL_INVALID_VALUE; + } + + return CL_SUCCESS; } cl_int clGetMemAllocInfoINTEL( diff --git a/runtime/memory_manager/svm_memory_manager.cpp b/runtime/memory_manager/svm_memory_manager.cpp index a88f90d665..444e2fc595 100644 --- a/runtime/memory_manager/svm_memory_manager.cpp +++ b/runtime/memory_manager/svm_memory_manager.cpp @@ -118,7 +118,7 @@ SvmAllocationData *SVMAllocsManager::getSVMAlloc(const void *ptr) { return SVMAllocs.get(ptr); } -void SVMAllocsManager::freeSVMAlloc(void *ptr) { +bool SVMAllocsManager::freeSVMAlloc(void *ptr) { SvmAllocationData *svmData = getSVMAlloc(ptr); if (svmData) { std::unique_lock lock(mtx); @@ -127,7 +127,9 @@ void SVMAllocsManager::freeSVMAlloc(void *ptr) { } else { freeSvmAllocationWithDeviceStorage(svmData); } + return true; } + return false; } void *SVMAllocsManager::createZeroCopySvmAllocation(size_t size, const SvmAllocationProperties &svmProperties) { diff --git a/runtime/memory_manager/svm_memory_manager.h b/runtime/memory_manager/svm_memory_manager.h index adf16a8f9e..781446ee12 100644 --- a/runtime/memory_manager/svm_memory_manager.h +++ b/runtime/memory_manager/svm_memory_manager.h @@ -77,7 +77,7 @@ class SVMAllocsManager { void *createSVMAlloc(size_t size, const SvmAllocationProperties svmProperties); void *createUnifiedMemoryAllocation(size_t size, const UnifiedMemoryProperties svmProperties); SvmAllocationData *getSVMAlloc(const void *ptr); - void freeSVMAlloc(void *ptr); + bool freeSVMAlloc(void *ptr); size_t getNumAllocs() const { return SVMAllocs.getNumAllocs(); } void insertSvmMapOperation(void *regionSvmPtr, size_t regionSize, void *baseSvmPtr, size_t offset, bool readOnlyMap); diff --git a/unit_tests/api/cl_unified_shared_memory_tests.inl b/unit_tests/api/cl_unified_shared_memory_tests.inl index 2795393c03..c163ff1cf9 100644 --- a/unit_tests/api/cl_unified_shared_memory_tests.inl +++ b/unit_tests/api/cl_unified_shared_memory_tests.inl @@ -32,7 +32,8 @@ TEST(clUnifiedSharedMemoryTests, whenClHostMemAllocIntelIsCalledThenItAllocatesH EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::HOST_UNIFIED_MEMORY); EXPECT_EQ(graphicsAllocation->gpuAllocation->getGpuAddress(), castToUint64(unifiedMemoryHostAllocation)); - allocationsManager->freeSVMAlloc(unifiedMemoryHostAllocation); + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); } TEST(clUnifiedSharedMemoryTests, whenClDeviceMemAllocINTELisCalledThenOutOfHostMemoryErrorIsReturned) { @@ -47,9 +48,41 @@ TEST(clUnifiedSharedMemoryTests, whenClSharedMemAllocINTELisCalledThenOutOfHostM EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal); } -TEST(clUnifiedSharedMemoryTests, whenClMemFreeINTELisCalledThenOutOfHostMemoryErrorIsReturned) { +TEST(clUnifiedSharedMemoryTests, whenClMemFreeINTELisCalledWithIncorrectContextThenReturnError) { auto retVal = clMemFreeINTEL(0, nullptr); - EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal); + EXPECT_EQ(CL_INVALID_CONTEXT, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClMemFreeINTELisCalledWithValidUmPointerThenMemoryIsFreed) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 4, 0, &retVal); + + auto allocationsManager = mockContext.getSVMAllocsManager(); + EXPECT_EQ(1u, allocationsManager->getNumAllocs()); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); + + EXPECT_EQ(0u, allocationsManager->getNumAllocs()); +} + +TEST(clUnifiedSharedMemoryTests, whenClMemFreeINTELisCalledWithInvalidUmPointerThenMemoryIsNotFreed) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 4, 0, &retVal); + + auto allocationsManager = mockContext.getSVMAllocsManager(); + EXPECT_EQ(1u, allocationsManager->getNumAllocs()); + + retVal = clMemFreeINTEL(&mockContext, ptrOffset(unifiedMemoryHostAllocation, 4)); + EXPECT_EQ(CL_INVALID_VALUE, retVal); + + EXPECT_EQ(1u, allocationsManager->getNumAllocs()); + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); + + EXPECT_EQ(0u, allocationsManager->getNumAllocs()); } TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledThenOutOfHostMemoryErrorIsReturned) {