From 8998f898862b9065ff2b8d6f960174271c319e8c Mon Sep 17 00:00:00 2001 From: "Milczarek, Slawomir" Date: Thu, 6 Jun 2019 23:21:08 +0200 Subject: [PATCH] HostPtr allocation with life time of image object for CL_MEM_USE_HOST_PTR Related-To: NEO-3231 Change-Id: I4869e55b3c4b5217c83cc0b53d8c9f8c14b524b2 Signed-off-by: Milczarek, Slawomir --- runtime/mem_obj/image.cpp | 61 +++++++++++-------- .../mem_obj/create_image_aub_tests.cpp | 3 +- .../command_queue/enqueue_map_image_tests.cpp | 4 +- unit_tests/mem_obj/image_tests.cpp | 1 + .../linux/drm_memory_manager_tests.cpp | 43 ++++++++----- 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/runtime/mem_obj/image.cpp b/runtime/mem_obj/image.cpp index fe352dddd6..83d850f1c0 100644 --- a/runtime/mem_obj/image.cpp +++ b/runtime/mem_obj/image.cpp @@ -116,6 +116,7 @@ Image *Image::create(Context *context, UNRECOVERABLE_IF(surfaceFormat == nullptr); Image *image = nullptr; GraphicsAllocation *memory = nullptr; + GraphicsAllocation *mapAllocation = nullptr; MemoryManager *memoryManager = context->getMemoryManager(); Buffer *parentBuffer = castToObject(imageDesc->mem_object); Image *parentImage = castToObject(imageDesc->mem_object); @@ -184,6 +185,32 @@ Image *Image::create(Context *context, imgInfo.preferRenderCompression = MemObjHelper::isSuitableForRenderCompression(isTilingAllowed, flags, context->peekContextType(), true); + switch (imageDesc->image_type) { + case CL_MEM_OBJECT_IMAGE3D: + hostPtrMinSize = hostPtrSlicePitch * imageDepth; + break; + case CL_MEM_OBJECT_IMAGE2D: + if (IsNV12Image(&surfaceFormat->OCLImageFormat)) { + hostPtrMinSize = hostPtrRowPitch * imageHeight + hostPtrRowPitch * imageHeight / 2; + } else { + hostPtrMinSize = hostPtrRowPitch * imageHeight; + } + hostPtrSlicePitch = 0; + break; + case CL_MEM_OBJECT_IMAGE1D_ARRAY: + case CL_MEM_OBJECT_IMAGE2D_ARRAY: + hostPtrMinSize = hostPtrSlicePitch * imageCount; + break; + case CL_MEM_OBJECT_IMAGE1D: + case CL_MEM_OBJECT_IMAGE1D_BUFFER: + hostPtrMinSize = hostPtrRowPitch; + hostPtrSlicePitch = 0; + break; + default: + DEBUG_BREAK_IF("Unsupported cl_image_type"); + break; + } + bool zeroCopy = false; bool transferNeeded = false; if (((imageDesc->image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER) || (imageDesc->image_type == CL_MEM_OBJECT_IMAGE2D)) && (parentBuffer != nullptr)) { @@ -241,7 +268,11 @@ Image *Image::create(Context *context, memory->setDefaultGmm(gmm); zeroCopy = true; } - + if (memory) { + AllocationProperties properties{false, hostPtrMinSize, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR}; + properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; + mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr); + } } else { AllocationProperties allocProperties = MemObjHelper::getAllocationProperties(imgInfo, true, flags); memory = memoryManager->allocateGraphicsMemoryWithProperties(allocProperties); @@ -253,32 +284,6 @@ Image *Image::create(Context *context, } transferNeeded |= !!(flags & CL_MEM_COPY_HOST_PTR); - switch (imageDesc->image_type) { - case CL_MEM_OBJECT_IMAGE3D: - hostPtrMinSize = hostPtrSlicePitch * imageDepth; - break; - case CL_MEM_OBJECT_IMAGE2D: - if (IsNV12Image(&surfaceFormat->OCLImageFormat)) { - hostPtrMinSize = hostPtrRowPitch * imageHeight + hostPtrRowPitch * imageHeight / 2; - } else { - hostPtrMinSize = hostPtrRowPitch * imageHeight; - } - hostPtrSlicePitch = 0; - break; - case CL_MEM_OBJECT_IMAGE1D_ARRAY: - case CL_MEM_OBJECT_IMAGE2D_ARRAY: - hostPtrMinSize = hostPtrSlicePitch * imageCount; - break; - case CL_MEM_OBJECT_IMAGE1D: - case CL_MEM_OBJECT_IMAGE1D_BUFFER: - hostPtrMinSize = hostPtrRowPitch; - hostPtrSlicePitch = 0; - break; - default: - DEBUG_BREAK_IF("Unsupported cl_image_type"); - break; - } - if (!memory) { break; } @@ -359,6 +364,8 @@ Image *Image::create(Context *context, } } + image->mapAllocation = mapAllocation; + if (errcodeRet != CL_SUCCESS) { image->release(); image = nullptr; diff --git a/unit_tests/aub_tests/mem_obj/create_image_aub_tests.cpp b/unit_tests/aub_tests/mem_obj/create_image_aub_tests.cpp index 9e7089a6ea..465bc1071d 100644 --- a/unit_tests/aub_tests/mem_obj/create_image_aub_tests.cpp +++ b/unit_tests/aub_tests/mem_obj/create_image_aub_tests.cpp @@ -314,7 +314,7 @@ HWTEST_P(AUBCreateImageHostPtr, imageWithRowPitchCreatedWithUseHostPtrFlagCopied size_t imageSlicePitch = 0; auto ptr = pCmdQ->enqueueMapImage( image, - false, + true, mapFlags, origin, region, @@ -329,7 +329,6 @@ HWTEST_P(AUBCreateImageHostPtr, imageWithRowPitchCreatedWithUseHostPtrFlagCopied } else { EXPECT_NE(image->getCpuAddress(), ptr); } - pCmdQ->flush(); size_t imageRowPitchRef = 0; image->getImageInfo(CL_IMAGE_ROW_PITCH, sizeof(imageRowPitchRef), &imageRowPitchRef, nullptr); diff --git a/unit_tests/command_queue/enqueue_map_image_tests.cpp b/unit_tests/command_queue/enqueue_map_image_tests.cpp index 88bf45c952..248cbbea7f 100644 --- a/unit_tests/command_queue/enqueue_map_image_tests.cpp +++ b/unit_tests/command_queue/enqueue_map_image_tests.cpp @@ -60,7 +60,7 @@ TEST_F(EnqueueMapImageTest, reuseMappedPtrForTiledImg) { const size_t region[3] = {1, 1, 1}; auto mapAllocation = image->getMapAllocation(); - EXPECT_EQ(nullptr, mapAllocation); + EXPECT_NE(nullptr, mapAllocation); auto ptr1 = pCmdQ->enqueueMapImage( image, true, mapFlags, origin, @@ -69,7 +69,7 @@ TEST_F(EnqueueMapImageTest, reuseMappedPtrForTiledImg) { EXPECT_EQ(CL_SUCCESS, retVal); EXPECT_NE(nullptr, image->getHostPtr()); mapAllocation = image->getMapAllocation(); - EXPECT_EQ(nullptr, mapAllocation); + EXPECT_NE(nullptr, mapAllocation); auto ptr2 = pCmdQ->enqueueMapImage( image, true, mapFlags, origin, diff --git a/unit_tests/mem_obj/image_tests.cpp b/unit_tests/mem_obj/image_tests.cpp index 7f8b6ed4f9..f640a7fff6 100644 --- a/unit_tests/mem_obj/image_tests.cpp +++ b/unit_tests/mem_obj/image_tests.cpp @@ -432,6 +432,7 @@ TEST(TestCreateImageUseHostPtr, givenZeroCopyImageValuesWhenUsingHostPtrThenZero EXPECT_EQ(CL_SUCCESS, retVal); EXPECT_TRUE(image->isMemObjZeroCopy()); EXPECT_EQ(hostPtr, image->getGraphicsAllocation()->getUnderlyingBuffer()); + EXPECT_NE(nullptr, image->getMapAllocation()); alignedFree(hostPtr); } diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index c45ad7b912..efd8097028 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -1363,8 +1363,9 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenTiledImageIsBeingCreatedAn TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenTiledImageIsBeingCreatedFromHostPtrThenallocateGraphicsMemoryForImageIsUsed) { mock->ioctl_expected.gemCreate = 1; mock->ioctl_expected.gemSetTiling = 1; - mock->ioctl_expected.gemWait = 1; - mock->ioctl_expected.gemClose = 1; + mock->ioctl_expected.gemUserptr = 1; + mock->ioctl_expected.gemWait = 2; + mock->ioctl_expected.gemClose = 2; MockContext context(device); context.setMemoryManager(memoryManager); @@ -1379,7 +1380,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenTiledImageIsBeingCreatedFr imageDesc.image_width = 64u; imageDesc.image_height = 64u; - char data[64u * 64u * 4 * 8]; + auto data = alignedMalloc(64u * 64u * 4 * 8, MemoryConstants::pageSize); auto retVal = CL_SUCCESS; @@ -1405,12 +1406,14 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenTiledImageIsBeingCreatedFr EXPECT_EQ(tilingMode, this->mock->setTilingMode); EXPECT_EQ(rowPitch, this->mock->setTilingStride); EXPECT_EQ(1u, this->mock->setTilingHandle); + + alignedFree(data); } TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenMemoryAllocatedForImageThenUnmapSizeCorrectlySetWhenLimitedRangeAllocationUsedOrNotUsed) { - mock->ioctl_expected.gemUserptr = 1; - mock->ioctl_expected.gemWait = 1; - mock->ioctl_expected.gemClose = 1; + mock->ioctl_expected.gemUserptr = 2; + mock->ioctl_expected.gemWait = 2; + mock->ioctl_expected.gemClose = 2; MockContext context; context.setMemoryManager(memoryManager); @@ -1424,7 +1427,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenMemoryAllocatedForImageThe imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; imageDesc.image_width = 64u; - char data[64u * 4 * 8]; + auto data = alignedMalloc(64u * 4 * 8, MemoryConstants::pageSize); auto retVal = CL_SUCCESS; @@ -1446,12 +1449,14 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenMemoryAllocatedForImageThe } else { EXPECT_NE(0u, drmAllocation->getBO()->peekUnmapSize()); } + + alignedFree(data); } TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountZeroisBeingCreatedThenAllocateGraphicsMemoryIsUsed) { - mock->ioctl_expected.gemUserptr = 1; - mock->ioctl_expected.gemWait = 1; - mock->ioctl_expected.gemClose = 1; + mock->ioctl_expected.gemUserptr = 2; + mock->ioctl_expected.gemWait = 2; + mock->ioctl_expected.gemClose = 2; MockContext context; context.setMemoryManager(memoryManager); @@ -1465,7 +1470,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountZer imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; imageDesc.image_width = 64u; - char data[64u * 4 * 8]; + auto data = alignedMalloc(64u * 4 * 8, MemoryConstants::pageSize); auto retVal = CL_SUCCESS; @@ -1487,6 +1492,8 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountZer EXPECT_EQ(0u, this->mock->setTilingHandle); EXPECT_EQ(Sharing::nonSharedResource, imageGraphicsAllocation->peekSharedHandle()); + + alignedFree(data); } TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountNonZeroisBeingCreatedThenAllocateGraphicsMemoryIsUsed) { @@ -1507,7 +1514,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountNon imageDesc.image_width = 64u; imageDesc.num_mip_levels = 1u; - char data[64u * 4 * 8]; + auto data = alignedMalloc(64u * 4 * 8, MemoryConstants::pageSize); auto retVal = CL_SUCCESS; @@ -1530,12 +1537,14 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhenNonTiledImgWithMipCountNon EXPECT_EQ(0u, this->mock->setTilingHandle); EXPECT_EQ(Sharing::nonSharedResource, imageGraphicsAllocation->peekSharedHandle()); + + alignedFree(data); } TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhen1DarrayImageIsBeingCreatedFromHostPtrThenTilingIsNotCalled) { - mock->ioctl_expected.gemUserptr = 1; - mock->ioctl_expected.gemWait = 1; - mock->ioctl_expected.gemClose = 1; + mock->ioctl_expected.gemUserptr = 2; + mock->ioctl_expected.gemWait = 2; + mock->ioctl_expected.gemClose = 2; MockContext context; context.setMemoryManager(memoryManager); @@ -1549,7 +1558,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhen1DarrayImageIsBeingCreated imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; imageDesc.image_width = 64u; - char data[64u * 64u * 4 * 8]; + auto data = alignedMalloc(64u * 4 * 8, MemoryConstants::pageSize); auto retVal = CL_SUCCESS; @@ -1567,6 +1576,8 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWhen1DarrayImageIsBeingCreated EXPECT_EQ(0u, this->mock->setTilingHandle); EXPECT_EQ(Sharing::nonSharedResource, imageGraphicsAllocation->peekSharedHandle()); + + alignedFree(data); } TEST_F(DrmMemoryManagerTest, givenHostPointerNotRequiringCopyWhenAllocateGraphicsMemoryForImageIsCalledThenGraphicsAllocationIsReturned) {