diff --git a/public/cl_ext_private.h b/public/cl_ext_private.h index 88536e80b8..e06ae8e1ef 100644 --- a/public/cl_ext_private.h +++ b/public/cl_ext_private.h @@ -79,3 +79,12 @@ using cl_unified_shared_memory_capabilities_intel = cl_bitfield; #define CL_DEVICE_SINGLE_DEVICE_SHARED_MEM_CAPABILITIES_INTEL 0x10052 #define CL_DEVICE_CROSS_DEVICE_SHARED_MEM_CAPABILITIES_INTEL 0x10053 #define CL_DEVICE_SHARED_SYSTEM_MEM_CAPABILITIES_INTEL 0x10054 + +#define CL_MEM_ALLOC_TYPE_INTEL 0x10020 +#define CL_MEM_ALLOC_BASE_PTR_INTEL 0x10021 +#define CL_MEM_ALLOC_SIZE_INTEL 0x10022 + +#define CL_MEM_TYPE_UNKNOWN_INTEL 0x10030 +#define CL_MEM_TYPE_HOST_INTEL 0x10031 +#define CL_MEM_TYPE_DEVICE_INTEL 0x10032 +#define CL_MEM_TYPE_SHARED_INTEL 0x10033 diff --git a/runtime/api/api.cpp b/runtime/api/api.cpp index 6a576c442d..127297c240 100644 --- a/runtime/api/api.cpp +++ b/runtime/api/api.cpp @@ -3483,7 +3483,52 @@ cl_int clGetMemAllocInfoINTEL( size_t paramValueSize, void *paramValue, size_t *paramValueSizeRet) { - return CL_OUT_OF_HOST_MEMORY; + Context *pContext = nullptr; + cl_int retVal = CL_SUCCESS; + retVal = validateObject(WithCastToInternal(context, &pContext)); + + if (!pContext) { + return retVal; + } + + auto allocationsManager = pContext->getSVMAllocsManager(); + if (!allocationsManager) { + return CL_INVALID_VALUE; + } + + auto unifiedMemoryAllocation = allocationsManager->getSVMAlloc(ptr); + if (!unifiedMemoryAllocation) { + return CL_INVALID_VALUE; + } + + GetInfoHelper info(paramValue, paramValueSize, paramValueSizeRet); + switch (paramName) { + case CL_MEM_ALLOC_TYPE_INTEL: { + if (unifiedMemoryAllocation->memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY) { + retVal = info.set(CL_MEM_TYPE_HOST_INTEL); + return retVal; + } else if (unifiedMemoryAllocation->memoryType == InternalMemoryType::DEVICE_UNIFIED_MEMORY) { + retVal = info.set(CL_MEM_TYPE_DEVICE_INTEL); + return retVal; + } else { + retVal = info.set(CL_MEM_TYPE_SHARED_INTEL); + return retVal; + } + break; + } + case CL_MEM_ALLOC_BASE_PTR_INTEL: { + retVal = info.set(unifiedMemoryAllocation->gpuAllocation->getGpuAddress()); + return retVal; + } + case CL_MEM_ALLOC_SIZE_INTEL: { + retVal = info.set(unifiedMemoryAllocation->size); + return retVal; + } + default: { + } + } + + return CL_INVALID_VALUE; } cl_int clSetKernelArgMemPointerINTEL( diff --git a/unit_tests/api/cl_unified_shared_memory_tests.inl b/unit_tests/api/cl_unified_shared_memory_tests.inl index d0a58817f2..9c86e263eb 100644 --- a/unit_tests/api/cl_unified_shared_memory_tests.inl +++ b/unit_tests/api/cl_unified_shared_memory_tests.inl @@ -124,9 +124,147 @@ TEST(clUnifiedSharedMemoryTests, whenClMemFreeINTELisCalledWithInvalidUmPointerT EXPECT_EQ(0u, allocationsManager->getNumAllocs()); } -TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledThenOutOfHostMemoryErrorIsReturned) { +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithoutContextThenInvalidContextIsReturned) { auto retVal = clGetMemAllocInfoINTEL(0, nullptr, 0, 0, nullptr, nullptr); - EXPECT_EQ(CL_OUT_OF_HOST_MEMORY, retVal); + EXPECT_EQ(CL_INVALID_CONTEXT, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithoutAllocationThenInvalidValueIsReturned) { + MockContext mockContext; + auto retVal = clGetMemAllocInfoINTEL(&mockContext, nullptr, 0, 0, nullptr, nullptr); + EXPECT_EQ(CL_INVALID_VALUE, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithoutSVMAllocationThenInvalidValueIsReturned) { + MockContext mockContext; + delete mockContext.svmAllocsManager; + mockContext.svmAllocsManager = nullptr; + auto retVal = clGetMemAllocInfoINTEL(&mockContext, nullptr, 0, 0, nullptr, nullptr); + EXPECT_EQ(CL_INVALID_VALUE, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithValidUnifiedMemoryHostAllocationThenProperFieldsAreSet) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(cl_int); + cl_int paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 4, 0, &retVal); + auto allocationsManager = mockContext.getSVMAllocsManager(); + auto graphicsAllocation = allocationsManager->getSVMAlloc(unifiedMemoryHostAllocation); + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemoryHostAllocation, CL_MEM_ALLOC_TYPE_INTEL, paramValueSize, ¶mValue, ¶mValueSizeRet); + EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::HOST_UNIFIED_MEMORY); + EXPECT_EQ(CL_MEM_TYPE_HOST_INTEL, paramValue); + EXPECT_EQ(sizeof(cl_int), paramValueSizeRet); + EXPECT_EQ(CL_SUCCESS, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithValidUnifiedMemoryDeviceAllocationThenProperFieldsAreSet) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(cl_int); + cl_int paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemoryDeviceAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 4, 0, &retVal); + auto allocationsManager = mockContext.getSVMAllocsManager(); + auto graphicsAllocation = allocationsManager->getSVMAlloc(unifiedMemoryDeviceAllocation); + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemoryDeviceAllocation, CL_MEM_ALLOC_TYPE_INTEL, paramValueSize, ¶mValue, ¶mValueSizeRet); + + EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::DEVICE_UNIFIED_MEMORY); + EXPECT_EQ(CL_MEM_TYPE_DEVICE_INTEL, paramValue); + EXPECT_EQ(sizeof(cl_int), paramValueSizeRet); + EXPECT_EQ(CL_SUCCESS, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryDeviceAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithValidUnifiedMemorySharedAllocationThenProperFieldsAreSet) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(cl_int); + cl_int paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 4, 0, &retVal); + auto allocationsManager = mockContext.getSVMAllocsManager(); + auto graphicsAllocation = allocationsManager->getSVMAlloc(unifiedMemorySharedAllocation); + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemorySharedAllocation, CL_MEM_ALLOC_TYPE_INTEL, paramValueSize, ¶mValue, ¶mValueSizeRet); + + EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::SHARED_UNIFIED_MEMORY); + EXPECT_EQ(CL_MEM_TYPE_SHARED_INTEL, paramValue); + EXPECT_EQ(sizeof(cl_int), paramValueSizeRet); + EXPECT_EQ(CL_SUCCESS, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemorySharedAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithAllocationBasePtrParamNameThenProperFieldsAreSet) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(uint64_t); + uint64_t paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 4, 0, &retVal); + auto allocationsManager = mockContext.getSVMAllocsManager(); + auto graphicsAllocation = allocationsManager->getSVMAlloc(unifiedMemorySharedAllocation); + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemorySharedAllocation, CL_MEM_ALLOC_BASE_PTR_INTEL, paramValueSize, ¶mValue, ¶mValueSizeRet); + + EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::SHARED_UNIFIED_MEMORY); + EXPECT_EQ(graphicsAllocation->gpuAllocation->getGpuAddress(), paramValue); + EXPECT_EQ(sizeof(uint64_t), paramValueSizeRet); + EXPECT_EQ(CL_SUCCESS, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemorySharedAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithAllocationSizeParamNameThenProperFieldsAreSet) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(size_t); + size_t paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 4, 0, &retVal); + auto allocationsManager = mockContext.getSVMAllocsManager(); + auto graphicsAllocation = allocationsManager->getSVMAlloc(unifiedMemoryHostAllocation); + + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemoryHostAllocation, CL_MEM_ALLOC_SIZE_INTEL, paramValueSize, ¶mValue, ¶mValueSizeRet); + + EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::HOST_UNIFIED_MEMORY); + EXPECT_EQ(graphicsAllocation->size, paramValue); + EXPECT_EQ(sizeof(size_t), paramValueSizeRet); + EXPECT_EQ(CL_SUCCESS, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemoryHostAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); +} + +TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocInfoINTELisCalledWithoutParamNameThenInvalidValueIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + size_t paramValueSize = sizeof(cl_uint); + cl_uint paramValue = 0; + size_t paramValueSizeRet = 0; + + auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 4, 0, &retVal); + retVal = clGetMemAllocInfoINTEL(&mockContext, unifiedMemorySharedAllocation, 0, paramValueSize, ¶mValue, ¶mValueSizeRet); + EXPECT_EQ(CL_INVALID_VALUE, retVal); + + retVal = clMemFreeINTEL(&mockContext, unifiedMemorySharedAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); } TEST(clUnifiedSharedMemoryTests, whenClSetKernelArgMemPointerINTELisCalledWithInvalidKernelThenInvaliKernelErrorIsReturned) { diff --git a/unit_tests/mocks/mock_context.h b/unit_tests/mocks/mock_context.h index bc0626578d..b4419b8571 100644 --- a/unit_tests/mocks/mock_context.h +++ b/unit_tests/mocks/mock_context.h @@ -15,7 +15,7 @@ namespace NEO { class MockContext : public Context { public: using Context::sharingFunctions; - + using Context::svmAllocsManager; MockContext(Device *device, bool noSpecialQueue = false); MockContext( void(CL_CALLBACK *funcNotify)(const char *, const void *, size_t, void *),