From c3143ccf1fa79bc4f3e04f12bf0809fe9447748c Mon Sep 17 00:00:00 2001 From: Krzysztof Gibala Date: Mon, 29 Mar 2021 16:39:45 +0000 Subject: [PATCH] Change image with host ptr creation logic in multi device setup When multi device config is available, then allocations are created in system memory pool. Related-To: NEO-5508 Signed-off-by: Krzysztof Gibala --- opencl/source/mem_obj/image.cpp | 25 ++--- .../unit_test/api/cl_create_image_tests.cpp | 92 +++++++------------ ...and_stream_receiver_flush_task_3_tests.cpp | 20 ++++ .../test/unit_test/fixtures/image_fixture.cpp | 3 +- .../test/unit_test/fixtures/image_fixture.h | 6 +- opencl/test/unit_test/mem_obj/image_tests.cpp | 2 +- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/opencl/source/mem_obj/image.cpp b/opencl/source/mem_obj/image.cpp index ee5a9c288e..858dfe6ac6 100644 --- a/opencl/source/mem_obj/image.cpp +++ b/opencl/source/mem_obj/image.cpp @@ -285,12 +285,12 @@ Image *Image::create(Context *context, } else { errcodeRet = CL_OUT_OF_HOST_MEMORY; if (memoryProperties.flags.useHostPtr) { - if (!context->isSharedContext) { AllocationProperties allocProperties = MemObjHelper::getAllocationPropertiesWithImageInfo(rootDeviceIndex, imgInfo, false, // allocateMemory memoryProperties, hwInfo, context->getDeviceBitfieldForAllocation(rootDeviceIndex)); + allocProperties.flags.crossRootDeviceAccess = context->getRootDeviceIndices().size() > 1; allocationInfo[rootDeviceIndex].memory = memoryManager->allocateGraphicsMemoryWithProperties(allocProperties, hostPtr); @@ -313,14 +313,16 @@ Image *Image::create(Context *context, allocationInfo[rootDeviceIndex].memory->setDefaultGmm(gmm); allocationInfo[rootDeviceIndex].zeroCopyAllowed = true; } - if (allocationInfo[rootDeviceIndex].memory) { - AllocationProperties properties{rootDeviceIndex, - false, // allocateMemory - hostPtrMinSize, GraphicsAllocation::AllocationType::MAP_ALLOCATION, - false, // isMultiStorageAllocation - context->getDeviceBitfieldForAllocation(rootDeviceIndex)}; - properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; - allocationInfo[rootDeviceIndex].mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr); + if (!allocationInfo[rootDeviceIndex].zeroCopyAllowed) { + if (allocationInfo[rootDeviceIndex].memory) { + AllocationProperties properties{rootDeviceIndex, + false, // allocateMemory + hostPtrMinSize, GraphicsAllocation::AllocationType::MAP_ALLOCATION, + false, // isMultiStorageAllocation + context->getDeviceBitfieldForAllocation(rootDeviceIndex)}; + properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; + allocationInfo[rootDeviceIndex].mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr); + } } } else { if (context->getRootDeviceIndices().size() > 1) { @@ -403,8 +405,9 @@ Image *Image::create(Context *context, image = createImageHw(context, memoryProperties, flags, flagsIntel, imgInfo.size, hostPtrToSet, surfaceFormat->OCLImageFormat, imageDescriptor, allocationInfo[defaultRootDeviceIndex].zeroCopyAllowed, std::move(multiGraphicsAllocation), false, 0, 0, surfaceFormat); - - image->setAllocatedMapPtr(hostPtrForced); + if (hostPtrForced) { + image->setAllocatedMapPtr(hostPtrForced); + } for (auto &rootDeviceIndex : context->getRootDeviceIndices()) { diff --git a/opencl/test/unit_test/api/cl_create_image_tests.cpp b/opencl/test/unit_test/api/cl_create_image_tests.cpp index fab0841aae..4628dce6ab 100644 --- a/opencl/test/unit_test/api/cl_create_image_tests.cpp +++ b/opencl/test/unit_test/api/cl_create_image_tests.cpp @@ -10,6 +10,8 @@ #include "opencl/source/context/context.h" #include "opencl/source/mem_obj/image.h" +#include "opencl/test/unit_test/fixtures/image_fixture.h" +#include "opencl/test/unit_test/fixtures/multi_root_device_fixture.h" #include "opencl/test/unit_test/helpers/unit_test_helper.h" #include "opencl/test/unit_test/mocks/mock_cl_device.h" #include "opencl/test/unit_test/mocks/mock_memory_manager.h" @@ -1360,91 +1362,61 @@ INSTANTIATE_TEST_CASE_P(clCreateNon2dImageFromImageTests, clCreateNon2dImageFromImageTest, ::testing::ValuesIn(non2dImageTypes)); -using clCreateImageWithMultiDeviceContextTests = clCreateImageTest; +using clCreateImageWithMultiDeviceContextTests = MultiRootDeviceFixture; -TEST_F(clCreateImageWithMultiDeviceContextTests, GivenImageCreatedWithContextdWithMultiDeviceThenGraphicsAllocationsAreProperlyFilled) { - UltClDeviceFactory deviceFactory{2, 0}; - DebugManager.flags.EnableMultiRootDeviceContexts.set(true); +TEST_F(clCreateImageWithMultiDeviceContextTests, GivenImageCreatedWithoutHostPtrAndWithContextdWithMultiDeviceThenGraphicsAllocationsAreProperlyCreatedAndMapPtrIsSet) { + REQUIRE_IMAGES_OR_SKIP(defaultHwInfo); + DebugManagerStateRestore dbgRestore; - cl_device_id devices[] = {deviceFactory.rootDevices[0], deviceFactory.rootDevices[1]}; - auto context = clCreateContext(nullptr, 2u, devices, nullptr, nullptr, &retVal); - EXPECT_NE(nullptr, context); - EXPECT_EQ(CL_SUCCESS, retVal); + std::unique_ptr image(ImageHelper::create(context.get())); - auto pContext = castToObject(context); - REQUIRE_IMAGE_SUPPORT_OR_SKIP(pContext); + EXPECT_EQ(image->getMultiGraphicsAllocation().getGraphicsAllocations().size(), 3u); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u), nullptr); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(2u), nullptr); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u), image->getMultiGraphicsAllocation().getGraphicsAllocation(2u)); - EXPECT_EQ(1u, pContext->getMaxRootDeviceIndex()); + EXPECT_TRUE(MemoryPool::isSystemMemoryPool(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u)->getMemoryPool())); + EXPECT_TRUE(MemoryPool::isSystemMemoryPool(image->getMultiGraphicsAllocation().getGraphicsAllocation(2u)->getMemoryPool())); - std::unique_ptr ptr; - char *hostPtr = nullptr; + EXPECT_NE(image->getAllocatedMapPtr(), nullptr); +} - ptr = std::make_unique(alignUp(imageDesc.image_width * imageDesc.image_height * 4, MemoryConstants::pageSize)); - hostPtr = ptr.get(); +TEST_F(clCreateImageWithMultiDeviceContextTests, GivenImageCreatedWithHostPtrAndWithContextdWithMultiDeviceThenGraphicsAllocationsAreProperlyCreatedAndMapPtrIsNotSet) { + REQUIRE_IMAGES_OR_SKIP(defaultHwInfo); + DebugManagerStateRestore dbgRestore; - cl_mem_flags flags = CL_MEM_USE_HOST_PTR; + std::unique_ptr image(ImageHelper>::create(context.get())); - auto image = clCreateImage(context, flags, &imageFormat, &imageDesc, hostPtr, &retVal); - ASSERT_EQ(CL_SUCCESS, retVal); - EXPECT_NE(nullptr, image); + EXPECT_EQ(image->getMultiGraphicsAllocation().getGraphicsAllocations().size(), 3u); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u), nullptr); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(2u), nullptr); + EXPECT_NE(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u), image->getMultiGraphicsAllocation().getGraphicsAllocation(2u)); - Image *imageObj = NEO::castToObject(image); + EXPECT_TRUE(MemoryPool::isSystemMemoryPool(image->getMultiGraphicsAllocation().getGraphicsAllocation(1u)->getMemoryPool())); + EXPECT_TRUE(MemoryPool::isSystemMemoryPool(image->getMultiGraphicsAllocation().getGraphicsAllocation(2u)->getMemoryPool())); - EXPECT_EQ(imageObj->getMultiGraphicsAllocation().getGraphicsAllocations().size(), 2u); - EXPECT_NE(imageObj->getMultiGraphicsAllocation().getGraphicsAllocation(0u), nullptr); - EXPECT_NE(imageObj->getMultiGraphicsAllocation().getGraphicsAllocation(1u), nullptr); - EXPECT_NE(imageObj->getMultiGraphicsAllocation().getGraphicsAllocation(0u), imageObj->getMultiGraphicsAllocation().getGraphicsAllocation(1u)); - - retVal = clReleaseMemObject(image); - EXPECT_EQ(CL_SUCCESS, retVal); - - clReleaseContext(context); + EXPECT_EQ(image->getAllocatedMapPtr(), nullptr); } TEST_F(clCreateImageWithMultiDeviceContextTests, GivenContextdWithMultiDeviceFailingAllocationThenImageAllocateFails) { REQUIRE_IMAGES_OR_SKIP(defaultHwInfo); - UltClDeviceFactory deviceFactory{2, 0}; - DebugManager.flags.EnableMultiRootDeviceContexts.set(true); - - cl_device_id devices[] = {deviceFactory.rootDevices[0], deviceFactory.rootDevices[1]}; - - MockContext pContext(ClDeviceVector(devices, 2)); - - EXPECT_EQ(1u, pContext.getMaxRootDeviceIndex()); - - auto bufferSize = imageDesc.image_width * imageDesc.image_height * 4; - - cl_mem_flags flags = CL_MEM_COPY_HOST_PTR; - { - auto hostBuffer = alignedMalloc(bufferSize, MemoryConstants::pageSize64k); - auto ptrHostBuffer = static_cast(hostBuffer); + static_cast(context.get()->getMemoryManager())->successAllocatedGraphicsMemoryIndex = 0u; + static_cast(context.get()->getMemoryManager())->maxSuccessAllocatedGraphicsMemoryIndex = 0u; - static_cast(pContext.memoryManager)->successAllocatedGraphicsMemoryIndex = 0u; - static_cast(pContext.memoryManager)->maxSuccessAllocatedGraphicsMemoryIndex = 0u; + std::unique_ptr image(ImageHelper::create(context.get())); - auto image = clCreateImage(&pContext, flags, &imageFormat, &imageDesc, ptrHostBuffer, &retVal); - - ASSERT_EQ(CL_OUT_OF_HOST_MEMORY, retVal); EXPECT_EQ(nullptr, image); - - alignedFree(hostBuffer); } { - auto hostBuffer = alignedMalloc(bufferSize, MemoryConstants::pageSize64k); - auto ptrHostBuffer = static_cast(hostBuffer); + static_cast(context.get()->getMemoryManager())->successAllocatedGraphicsMemoryIndex = 0u; + static_cast(context.get()->getMemoryManager())->maxSuccessAllocatedGraphicsMemoryIndex = 1u; - static_cast(pContext.memoryManager)->successAllocatedGraphicsMemoryIndex = 0u; - static_cast(pContext.memoryManager)->maxSuccessAllocatedGraphicsMemoryIndex = 1u; + std::unique_ptr image(ImageHelper::create(context.get())); - auto image = clCreateImage(&pContext, flags, &imageFormat, &imageDesc, ptrHostBuffer, &retVal); - - ASSERT_EQ(CL_OUT_OF_HOST_MEMORY, retVal); EXPECT_EQ(nullptr, image); - - alignedFree(hostBuffer); } } diff --git a/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_3_tests.cpp b/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_3_tests.cpp index 31d12b4b84..60bd130065 100644 --- a/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_3_tests.cpp +++ b/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_3_tests.cpp @@ -1994,6 +1994,26 @@ TEST(MultiRootDeviceCommandStreamReceiverTests, givenMultipleEventInMultiRootDev cl_uint numEventsInWaitList = sizeof(eventWaitList) / sizeof(eventWaitList[0]); + { + cl_event eventWaitList[] = + { + &event1, + &event3, + &event4, + }; + + cl_uint numEventsInWaitList = sizeof(eventWaitList) / sizeof(eventWaitList[0]); + + pCmdQ1->enqueueMarkerWithWaitList( + numEventsInWaitList, + eventWaitList, + nullptr); + + EXPECT_EQ(0u, mockCsr1->waitForCompletionWithTimeoutCalled); + EXPECT_EQ(0u, mockCsr2->waitForCompletionWithTimeoutCalled); + EXPECT_EQ(0u, mockCsr3->waitForCompletionWithTimeoutCalled); + } + { pCmdQ1->enqueueMarkerWithWaitList( numEventsInWaitList, diff --git a/opencl/test/unit_test/fixtures/image_fixture.cpp b/opencl/test/unit_test/fixtures/image_fixture.cpp index 6cad1831e9..7b8e8464f0 100644 --- a/opencl/test/unit_test/fixtures/image_fixture.cpp +++ b/opencl/test/unit_test/fixtures/image_fixture.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2020 Intel Corporation + * Copyright (C) 2017-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -99,5 +99,6 @@ const cl_image_desc Image1dArrayDefaults::imageDesc = { static float imageMemory[imageWidth * imageHeight * imageDepth] = {}; void *Image1dDefaults::hostPtr = imageMemory; +void *ImageWithoutHostPtr::hostPtr = nullptr; NEO::Context *Image1dDefaults::context = nullptr; diff --git a/opencl/test/unit_test/fixtures/image_fixture.h b/opencl/test/unit_test/fixtures/image_fixture.h index b3988cb06a..4d5a607fa4 100644 --- a/opencl/test/unit_test/fixtures/image_fixture.h +++ b/opencl/test/unit_test/fixtures/image_fixture.h @@ -55,6 +55,11 @@ struct LuminanceImage : public Image2dDefaults { static const cl_image_format imageFormat; }; +struct ImageWithoutHostPtr : public Image1dDefaults { + enum { flags = 0 }; + static void *hostPtr; +}; + template struct ImageUseHostPtr : public BaseClass { enum { flags = BaseClass::flags | CL_MEM_USE_HOST_PTR }; @@ -90,7 +95,6 @@ struct ImageHelper { Traits::hostPtr, retVal); - assert(image != nullptr); return image; } }; diff --git a/opencl/test/unit_test/mem_obj/image_tests.cpp b/opencl/test/unit_test/mem_obj/image_tests.cpp index 72f28898b0..90b3abbc09 100644 --- a/opencl/test/unit_test/mem_obj/image_tests.cpp +++ b/opencl/test/unit_test/mem_obj/image_tests.cpp @@ -649,7 +649,7 @@ TEST(TestCreateImageUseHostPtr, givenZeroCopyImageValuesWhenUsingHostPtrThenZero EXPECT_EQ(CL_SUCCESS, retVal); EXPECT_TRUE(image->isMemObjZeroCopy()); EXPECT_EQ(hostPtr, allocation->getUnderlyingBuffer()); - EXPECT_NE(nullptr, image->getMapAllocation(context.getDevice(0)->getRootDeviceIndex())); + EXPECT_EQ(nullptr, image->getMapAllocation(context.getDevice(0)->getRootDeviceIndex())); alignedFree(hostPtr); }