From 87247e69492598760116d53106001b49958a4d1d Mon Sep 17 00:00:00 2001 From: "Milczarek, Slawomir" Date: Thu, 30 Nov 2023 10:28:26 +0000 Subject: [PATCH] fix: Add path for clCreateBuffer with host unified memory ptr Related-To: NEO-9612 Signed-off-by: Milczarek, Slawomir --- opencl/source/mem_obj/buffer.cpp | 24 ++++++++++++----- .../test/unit_test/mem_obj/buffer_tests.cpp | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/opencl/source/mem_obj/buffer.cpp b/opencl/source/mem_obj/buffer.cpp index 7e750d2049..0f41037225 100644 --- a/opencl/source/mem_obj/buffer.cpp +++ b/opencl/source/mem_obj/buffer.cpp @@ -380,13 +380,23 @@ Buffer *Buffer::create(Context *context, if (svmManager) { auto svmData = svmManager->getSVMAlloc(hostPtr); if (svmData) { - allocationInfo.memory = svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); - allocationInfo.allocationType = allocationInfo.memory->getAllocationType(); - allocationInfo.isHostPtrSVM = true; - allocationInfo.zeroCopyAllowed = allocationInfo.memory->getAllocationType() == AllocationType::SVM_ZERO_COPY; - allocationInfo.copyMemoryFromHostPtr = false; - allocationInfo.allocateMemory = false; - allocationInfo.mapAllocation = svmData->cpuAllocation; + if ((svmData->memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY) && memoryManager->isLocalMemorySupported(rootDeviceIndex)) { + allocationInfo.memory = nullptr; + allocationInfo.allocationType = AllocationType::BUFFER; + allocationInfo.isHostPtrSVM = false; + allocationInfo.zeroCopyAllowed = false; + allocationInfo.copyMemoryFromHostPtr = true; + allocationInfo.allocateMemory = true; + allocationInfo.mapAllocation = svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); + } else { + allocationInfo.memory = svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); + allocationInfo.allocationType = allocationInfo.memory->getAllocationType(); + allocationInfo.isHostPtrSVM = true; + allocationInfo.zeroCopyAllowed = allocationInfo.memory->getAllocationType() == AllocationType::SVM_ZERO_COPY; + allocationInfo.copyMemoryFromHostPtr = false; + allocationInfo.allocateMemory = false; + allocationInfo.mapAllocation = svmData->cpuAllocation; + } } } } diff --git a/opencl/test/unit_test/mem_obj/buffer_tests.cpp b/opencl/test/unit_test/mem_obj/buffer_tests.cpp index 5b463cc0b6..c8b79a88bd 100644 --- a/opencl/test/unit_test/mem_obj/buffer_tests.cpp +++ b/opencl/test/unit_test/mem_obj/buffer_tests.cpp @@ -1199,6 +1199,32 @@ TEST_P(ValidHostPtr, GivenSvmHostPtrWhenCreatingBufferThenBufferIsCreatedCorrect } } +TEST_P(ValidHostPtr, GivenUsmHostPtrWhenCreatingBufferThenBufferIsCreatedCorrectly) { + const ClDeviceInfo &devInfo = pClDevice->getDeviceInfo(); + if (devInfo.svmCapabilities != 0) { + auto memoryManager = static_cast(context->getDevice(0)->getMemoryManager()); + memoryManager->localMemorySupported[pDevice->getRootDeviceIndex()] = true; + + NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY, 1, context->getRootDeviceIndices(), context->getDeviceBitfields()); + auto ptr = context->getSVMAllocsManager()->createHostUnifiedMemoryAllocation(MemoryConstants::pageSize64k, unifiedMemoryProperties); + + auto buffer = Buffer::create(context.get(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, 64, ptr, retVal); + EXPECT_NE(nullptr, buffer); + + auto svmData = context->getSVMAllocsManager()->getSVMAlloc(ptr); + ASSERT_NE(nullptr, svmData); + + EXPECT_EQ(AllocationType::BUFFER, buffer->getGraphicsAllocation(pDevice->getRootDeviceIndex())->getAllocationType()); + + auto mapAllocation = buffer->getMapAllocation(pDevice->getRootDeviceIndex()); + ASSERT_NE(nullptr, mapAllocation); + EXPECT_EQ(reinterpret_cast(mapAllocation->getGpuAddress()), ptr); + + delete buffer; + context->getSVMAllocsManager()->freeSVMAlloc(ptr); + } +} + TEST_P(ValidHostPtr, WhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { auto buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), nullptr, flags, 0, testBufferSizeInBytes, pHostPtr, retVal); EXPECT_EQ(retVal, CL_SUCCESS);