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 <krzysztof.gibala@intel.com>
This commit is contained in:
Krzysztof Gibala 2021-03-29 16:39:45 +00:00 committed by Compute-Runtime-Automation
parent 0a84f88599
commit c3143ccf1f
6 changed files with 74 additions and 74 deletions

View File

@ -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,6 +313,7 @@ Image *Image::create(Context *context,
allocationInfo[rootDeviceIndex].memory->setDefaultGmm(gmm);
allocationInfo[rootDeviceIndex].zeroCopyAllowed = true;
}
if (!allocationInfo[rootDeviceIndex].zeroCopyAllowed) {
if (allocationInfo[rootDeviceIndex].memory) {
AllocationProperties properties{rootDeviceIndex,
false, // allocateMemory
@ -322,6 +323,7 @@ Image *Image::create(Context *context,
properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true;
allocationInfo[rootDeviceIndex].mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr);
}
}
} else {
if (context->getRootDeviceIndices().size() > 1) {
MemoryProperties memoryPropertiesToSet = memoryProperties;
@ -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);
if (hostPtrForced) {
image->setAllocatedMapPtr(hostPtrForced);
}
for (auto &rootDeviceIndex : context->getRootDeviceIndices()) {

View File

@ -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> image(ImageHelper<ImageWithoutHostPtr>::create(context.get()));
auto pContext = castToObject<Context>(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<char[]> ptr;
char *hostPtr = nullptr;
EXPECT_NE(image->getAllocatedMapPtr(), nullptr);
}
ptr = std::make_unique<char[]>(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> image(ImageHelper<ImageUseHostPtr<Image1dDefaults>>::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>(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<uint8_t *>(hostBuffer);
static_cast<MockMemoryManager *>(context.get()->getMemoryManager())->successAllocatedGraphicsMemoryIndex = 0u;
static_cast<MockMemoryManager *>(context.get()->getMemoryManager())->maxSuccessAllocatedGraphicsMemoryIndex = 0u;
static_cast<MockMemoryManager *>(pContext.memoryManager)->successAllocatedGraphicsMemoryIndex = 0u;
static_cast<MockMemoryManager *>(pContext.memoryManager)->maxSuccessAllocatedGraphicsMemoryIndex = 0u;
std::unique_ptr<Image> image(ImageHelper<ImageWithoutHostPtr>::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<uint8_t *>(hostBuffer);
static_cast<MockMemoryManager *>(context.get()->getMemoryManager())->successAllocatedGraphicsMemoryIndex = 0u;
static_cast<MockMemoryManager *>(context.get()->getMemoryManager())->maxSuccessAllocatedGraphicsMemoryIndex = 1u;
static_cast<MockMemoryManager *>(pContext.memoryManager)->successAllocatedGraphicsMemoryIndex = 0u;
static_cast<MockMemoryManager *>(pContext.memoryManager)->maxSuccessAllocatedGraphicsMemoryIndex = 1u;
std::unique_ptr<Image> image(ImageHelper<ImageWithoutHostPtr>::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);
}
}

View File

@ -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,

View File

@ -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;

View File

@ -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 <typename BaseClass>
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;
}
};

View File

@ -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);
}