diff --git a/opencl/source/context/context.h b/opencl/source/context/context.h index ecf0af5b18..9664bd89f7 100644 --- a/opencl/source/context/context.h +++ b/opencl/source/context/context.h @@ -16,6 +16,7 @@ #include "opencl/source/gtpin/gtpin_notify.h" #include "opencl/source/helpers/base_object.h" #include "opencl/source/helpers/destructor_callbacks.h" +#include "opencl/source/mem_obj/map_operations_handler.h" #include #include @@ -93,6 +94,8 @@ class Context : public BaseObject<_cl_context> { return svmAllocsManager; } + auto &getMapOperationsStorage() { return mapOperationsStorage; } + const std::set &getRootDeviceIndices() const; uint32_t getMaxRootDeviceIndex() const; @@ -199,6 +202,7 @@ class Context : public BaseObject<_cl_context> { void *userData = nullptr; MemoryManager *memoryManager = nullptr; SVMAllocsManager *svmAllocsManager = nullptr; + MapOperationsStorage mapOperationsStorage = {}; StackVec specialQueues; DeviceQueue *defaultDeviceQueue = nullptr; DriverDiagnostics *driverDiagnostics = nullptr; diff --git a/opencl/source/mem_obj/map_operations_handler.cpp b/opencl/source/mem_obj/map_operations_handler.cpp index 9d16cbfd23..b3ab011bf6 100644 --- a/opencl/source/mem_obj/map_operations_handler.cpp +++ b/opencl/source/mem_obj/map_operations_handler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -72,3 +72,21 @@ void MapOperationsHandler::remove(void *mappedPtr) { } } } + +MapOperationsHandler &NEO::MapOperationsStorage::getHandler(cl_mem memObj) { + return handlers[memObj]; +} + +MapOperationsHandler *NEO::MapOperationsStorage::getHandlerIfExists(cl_mem memObj) { + auto iterator = handlers.find(memObj); + if (iterator == handlers.end()) { + return nullptr; + } + + return &iterator->second; +} + +void NEO::MapOperationsStorage::removeHandler(cl_mem memObj) { + auto iterator = handlers.find(memObj); + handlers.erase(iterator); +} diff --git a/opencl/source/mem_obj/map_operations_handler.h b/opencl/source/mem_obj/map_operations_handler.h index 8a6933a856..5e0a1878c5 100644 --- a/opencl/source/mem_obj/map_operations_handler.h +++ b/opencl/source/mem_obj/map_operations_handler.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,6 +9,7 @@ #include "opencl/source/helpers/properties_helper.h" #include +#include #include namespace NEO { @@ -28,4 +29,16 @@ class MapOperationsHandler { mutable std::mutex mtx; }; +class MapOperationsStorage { + public: + using HandlersMap = std::unordered_map; + + MapOperationsHandler &getHandler(cl_mem memObj); + MapOperationsHandler *getHandlerIfExists(cl_mem memObj); + void removeHandler(cl_mem memObj); + + protected: + HandlersMap handlers{}; +}; + } // namespace NEO diff --git a/opencl/source/mem_obj/mem_obj.cpp b/opencl/source/mem_obj/mem_obj.cpp index 9e60c8cdc5..f2444a6943 100644 --- a/opencl/source/mem_obj/mem_obj.cpp +++ b/opencl/source/mem_obj/mem_obj.cpp @@ -62,9 +62,14 @@ MemObj::~MemObj() { if (allocatedMapPtr != nullptr) { needWait = true; } - if (mapOperationsHandler.size() > 0 && !getCpuAddressForMapping()) { - needWait = true; + + if (auto mapOperationsHandler = getMapOperationsHandlerIfExists(); mapOperationsHandler != nullptr) { + if (mapOperationsHandler->size() > 0 && !getCpuAddressForMapping()) { + needWait = true; + } + context->getMapOperationsStorage().removeHandler(this); } + if (!destructorCallbacks.empty()) { needWait = true; } @@ -172,7 +177,7 @@ cl_int MemObj::getMemObjectInfo(cl_mem_info paramName, case CL_MEM_MAP_COUNT: srcParamSize = sizeof(mapCount); - mapCount = static_cast(mapOperationsHandler.size()); + mapCount = static_cast(getMapOperationsHandler().size()); srcParam = &mapCount; break; @@ -382,11 +387,26 @@ void *MemObj::getBasePtrForMap(uint32_t rootDeviceIndex) { } } +MapOperationsHandler &MemObj::getMapOperationsHandler() { + return context->getMapOperationsStorage().getHandler(this); +} + +MapOperationsHandler *MemObj::getMapOperationsHandlerIfExists() { + return context->getMapOperationsStorage().getHandlerIfExists(this); +} + bool MemObj::addMappedPtr(void *ptr, size_t ptrLength, cl_map_flags &mapFlags, MemObjSizeArray &size, MemObjOffsetArray &offset, uint32_t mipLevel) { - return mapOperationsHandler.add(ptr, ptrLength, mapFlags, size, offset, - mipLevel); + return getMapOperationsHandler().add(ptr, ptrLength, mapFlags, size, offset, mipLevel); +} + +bool MemObj::findMappedPtr(void *mappedPtr, MapInfo &outMapInfo) { + return getMapOperationsHandler().find(mappedPtr, outMapInfo); +} + +void MemObj::removeMappedPtr(void *mappedPtr) { + getMapOperationsHandler().remove(mappedPtr); } bool MemObj::isTiledAllocation() const { diff --git a/opencl/source/mem_obj/mem_obj.h b/opencl/source/mem_obj/mem_obj.h index d8519fa1a0..fadd0d4016 100644 --- a/opencl/source/mem_obj/mem_obj.h +++ b/opencl/source/mem_obj/mem_obj.h @@ -86,9 +86,11 @@ class MemObj : public BaseObject<_cl_mem> { bool getIsObjectRedescribed() const { return isObjectRedescribed; }; size_t getSize() const; + MapOperationsHandler &getMapOperationsHandler(); + MapOperationsHandler *getMapOperationsHandlerIfExists(); bool addMappedPtr(void *ptr, size_t ptrLength, cl_map_flags &mapFlags, MemObjSizeArray &size, MemObjOffsetArray &offset, uint32_t mipLevel); - bool findMappedPtr(void *mappedPtr, MapInfo &outMapInfo) { return mapOperationsHandler.find(mappedPtr, outMapInfo); } - void removeMappedPtr(void *mappedPtr) { mapOperationsHandler.remove(mappedPtr); } + bool findMappedPtr(void *mappedPtr, MapInfo &outMapInfo); + void removeMappedPtr(void *mappedPtr); void *getBasePtrForMap(uint32_t rootDeviceIndex); MOCKABLE_VIRTUAL void setAllocatedMapPtr(void *allocatedMapPtr); @@ -173,7 +175,6 @@ class MemObj : public BaseObject<_cl_mem> { void *memoryStorage; void *hostPtr; void *allocatedMapPtr = nullptr; - MapOperationsHandler mapOperationsHandler; size_t offset = 0; MemObj *associatedMemObject = nullptr; cl_uint refCount = 0; diff --git a/opencl/test/unit_test/api/cl_enqueue_unmap_mem_object_tests.inl b/opencl/test/unit_test/api/cl_enqueue_unmap_mem_object_tests.inl index fb52c9a5f2..69e451e306 100644 --- a/opencl/test/unit_test/api/cl_enqueue_unmap_mem_object_tests.inl +++ b/opencl/test/unit_test/api/cl_enqueue_unmap_mem_object_tests.inl @@ -89,7 +89,9 @@ TEST_F(clEnqueueUnmapMemObjTests, givenInvalidAddressWhenUnmappingOnGpuThenRetur } TEST_F(clEnqueueUnmapMemObjTests, GivenInvalidMemObjectTypeWhenUnmappingImageThenInvalidMemObjectIsReturned) { - MockBuffer buffer{}; + MockContext context{}; + MockGraphicsAllocation allocation{}; + MockBuffer buffer{&context, allocation}; cl_int retVal = CL_SUCCESS; auto mappedPtr = clEnqueueMapBuffer(pCommandQueue, &buffer, CL_TRUE, CL_MAP_READ, 0, 1, 0, nullptr, nullptr, &retVal); diff --git a/opencl/test/unit_test/command_queue/enqueue_waitlist_tests.cpp b/opencl/test/unit_test/command_queue/enqueue_waitlist_tests.cpp index 7384969f2e..55c1d48cec 100644 --- a/opencl/test/unit_test/command_queue/enqueue_waitlist_tests.cpp +++ b/opencl/test/unit_test/command_queue/enqueue_waitlist_tests.cpp @@ -59,7 +59,7 @@ struct EnqueueWaitlistTest : public EnqueueWaitlistFixture, void SetUp() override { EnqueueWaitlistFixture::SetUp(); buffer = BufferHelper<>::create(); - bufferNonZeroCopy = new UnalignedBuffer; + bufferNonZeroCopy = new UnalignedBuffer(BufferDefaults::context, &bufferNonZeroCopyAlloc); image = Image1dHelper<>::create(BufferDefaults::context); imageNonZeroCopy = ImageHelper>::create(BufferDefaults::context); } @@ -75,6 +75,7 @@ struct EnqueueWaitlistTest : public EnqueueWaitlistFixture, cl_int retVal = CL_SUCCESS; cl_int error = CL_SUCCESS; + MockGraphicsAllocation bufferNonZeroCopyAlloc{nullptr, MemoryConstants::pageSize}; Buffer *buffer; Buffer *bufferNonZeroCopy; Image *image; diff --git a/opencl/test/unit_test/command_queue/multiple_map_buffer_tests.cpp b/opencl/test/unit_test/command_queue/multiple_map_buffer_tests.cpp index 258f9010ea..5aac4d6a87 100644 --- a/opencl/test/unit_test/command_queue/multiple_map_buffer_tests.cpp +++ b/opencl/test/unit_test/command_queue/multiple_map_buffer_tests.cpp @@ -20,8 +20,6 @@ using namespace NEO; struct MultipleMapBufferTest : public ClDeviceFixture, public ::testing::Test { template struct MockBuffer : public BufferHw { - using Buffer::mapOperationsHandler; - template MockBuffer(Params... params) : BufferHw(params...) { this->createFunction = BufferHw::create; @@ -135,13 +133,13 @@ HWTEST_F(MultipleMapBufferTest, givenValidReadAndWriteBufferWhenMappedOnGpuThenA size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 0, nullptr, nullptr, nullptr); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->readBufferCalled, 1u); EXPECT_EQ(cmdQ->enqueueSize, size); EXPECT_EQ(cmdQ->enqueueOffset, offset); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->writeBufferCalled, 1u); EXPECT_EQ(cmdQ->enqueueSize, size); EXPECT_EQ(cmdQ->enqueueOffset, offset); @@ -157,11 +155,11 @@ HWTEST_F(MultipleMapBufferTest, givenReadOnlyMapWhenUnmappedOnGpuThenEnqueueMark size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_READ, offset, size, 0, nullptr, nullptr, nullptr); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->readBufferCalled, 1u); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->writeBufferCalled, 0u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); } @@ -175,12 +173,12 @@ HWTEST_F(MultipleMapBufferTest, givenWriteInvalidateMapWhenMappedOnGpuThenCallEn size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE_INVALIDATE_REGION, offset, size, 0, nullptr, nullptr, nullptr); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->readBufferCalled, 0u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->writeBufferCalled, 1u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); } @@ -190,7 +188,7 @@ HWTEST_F(MultipleMapBufferTest, givenNotMappedPtrWhenUnmapedOnGpuThenReturnError auto cmdQ = createMockCmdQ(); EXPECT_FALSE(buffer->mappingOnCpuAllowed()); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), buffer->getBasePtrForMap(cmdQ->getDevice().getRootDeviceIndex()), 0, nullptr, nullptr); EXPECT_EQ(CL_INVALID_VALUE, retVal); } @@ -206,7 +204,7 @@ HWTEST_F(MultipleMapBufferTest, givenErrorFromReadBufferWhenMappedOnGpuThenDontA void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_READ, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr); EXPECT_EQ(CL_OUT_OF_RESOURCES, retVal); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapBufferTest, givenErrorFromWriteBufferWhenUnmappedOnGpuThenDontRemoveMappedPtr) { @@ -219,12 +217,12 @@ HWTEST_F(MultipleMapBufferTest, givenErrorFromWriteBufferWhenUnmappedOnGpuThenDo size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); EXPECT_EQ(1u, cmdQ->writeBufferCalled); EXPECT_EQ(CL_OUT_OF_RESOURCES, retVal); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapBufferTest, givenUnblockedQueueWhenMappedOnCpuThenAddMappedPtrAndRemoveOnUnmap) { @@ -236,13 +234,13 @@ HWTEST_F(MultipleMapBufferTest, givenUnblockedQueueWhenMappedOnCpuThenAddMappedP size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(1u, buffer->transferToHostPtrCalled); EXPECT_EQ(buffer->copySize, size); EXPECT_EQ(buffer->copyOffset, offset); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(1u, buffer->transferFromHostPtrCalled); EXPECT_EQ(buffer->copySize, size); EXPECT_EQ(buffer->copyOffset, offset); @@ -257,11 +255,11 @@ HWTEST_F(MultipleMapBufferTest, givenUnblockedQueueWhenReadOnlyMappedOnCpuThenDo size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_READ, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(1u, buffer->transferToHostPtrCalled); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(0u, buffer->transferFromHostPtrCalled); } @@ -274,11 +272,11 @@ HWTEST_F(MultipleMapBufferTest, givenUnblockedQueueWhenWriteInvalidateMappedOnCp size_t size = 3; void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE_INVALIDATE_REGION, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(0u, buffer->transferToHostPtrCalled); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(1u, buffer->transferFromHostPtrCalled); } @@ -296,14 +294,14 @@ HWTEST_F(MultipleMapBufferTest, givenBlockedQueueWhenMappedOnCpuThenAddMappedPtr void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 1, &clMapEvent, nullptr, &retVal); mapEvent.setStatus(CL_COMPLETE); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(buffer->copySize, size); EXPECT_EQ(buffer->copyOffset, offset); EXPECT_EQ(1u, buffer->transferToHostPtrCalled); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 1, &clUnmapEvent, nullptr); unmapEvent.setStatus(CL_COMPLETE); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(buffer->copySize, size); EXPECT_EQ(buffer->copyOffset, offset); EXPECT_EQ(1u, buffer->transferFromHostPtrCalled); @@ -324,11 +322,11 @@ HWTEST_F(MultipleMapBufferTest, givenBlockedQueueWhenMappedReadOnlyOnCpuThenDont mapEvent.setStatus(CL_COMPLETE); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(1u, buffer->transferToHostPtrCalled); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); retVal = clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtr, 1, &clUnmapEvent, nullptr); unmapEvent.setStatus(CL_COMPLETE); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(0u, buffer->transferFromHostPtrCalled); } @@ -355,26 +353,26 @@ HWTEST_F(MultipleMapBufferTest, givenMultimpleMapsWhenUnmappingThenRemoveCorrect mappedPtrs[i].ptr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, mappedPtrs[i].offset[0], mappedPtrs[i].size[0], 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtrs[i].ptr); - EXPECT_EQ(i + 1, buffer->mapOperationsHandler.size()); + EXPECT_EQ(i + 1, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->enqueueSize, mappedPtrs[i].size[0]); EXPECT_EQ(cmdQ->enqueueOffset, mappedPtrs[i].offset[0]); } // reordered unmap clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtrs[1].ptr, 0, nullptr, nullptr); - EXPECT_EQ(2u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(2u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[1].ptr); EXPECT_EQ(cmdQ->enqueueSize, mappedPtrs[1].size[0]); EXPECT_EQ(cmdQ->enqueueOffset, mappedPtrs[1].offset[0]); clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtrs[2].ptr, 0, nullptr, nullptr); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[2].ptr); EXPECT_EQ(cmdQ->enqueueSize, mappedPtrs[2].size[0]); EXPECT_EQ(cmdQ->enqueueOffset, mappedPtrs[2].offset[0]); clEnqueueUnmapMemObject(cmdQ.get(), buffer.get(), mappedPtrs[0].ptr, 0, nullptr, nullptr); - EXPECT_EQ(0u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(0u, buffer->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[0].ptr); EXPECT_EQ(cmdQ->enqueueSize, mappedPtrs[0].size[0]); EXPECT_EQ(cmdQ->enqueueOffset, mappedPtrs[0].offset[0]); @@ -390,13 +388,13 @@ HWTEST_F(MultipleMapBufferTest, givenOverlapingPtrWhenMappingOnGpuForWriteThenRe void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_READ, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); offset++; void *mappedPtr2 = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr2); EXPECT_EQ(CL_INVALID_OPERATION, retVal); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapBufferTest, givenOverlapingPtrWhenMappingOnCpuForWriteThenReturnError) { @@ -409,11 +407,11 @@ HWTEST_F(MultipleMapBufferTest, givenOverlapingPtrWhenMappingOnCpuForWriteThenRe void *mappedPtr = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_READ, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); offset++; void *mappedPtr2 = clEnqueueMapBuffer(cmdQ.get(), buffer.get(), CL_FALSE, CL_MAP_WRITE, offset, size, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr2); EXPECT_EQ(CL_INVALID_OPERATION, retVal); - EXPECT_EQ(1u, buffer->mapOperationsHandler.size()); + EXPECT_EQ(1u, buffer->getMapOperationsHandler().size()); } diff --git a/opencl/test/unit_test/command_queue/multiple_map_image_tests.cpp b/opencl/test/unit_test/command_queue/multiple_map_image_tests.cpp index 57c8560998..d4b17ebcea 100644 --- a/opencl/test/unit_test/command_queue/multiple_map_image_tests.cpp +++ b/opencl/test/unit_test/command_queue/multiple_map_image_tests.cpp @@ -21,7 +21,6 @@ extern ImageFactoryFuncs imageFactory[IGFX_MAX_CORE]; struct MultipleMapImageTest : public ClDeviceFixture, public ::testing::Test { template struct MockImage : public ImageHw { - using Image::mapOperationsHandler; using ImageHw::isZeroCopy; using ImageHw::ImageHw; @@ -156,13 +155,13 @@ HWTEST_F(MultipleMapImageTest, givenValidReadAndWriteImageWhenMappedOnGpuThenAdd MemObjSizeArray region = {{3, 4, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->enqueueRegion, region); EXPECT_EQ(cmdQ->enqueueOrigin, origin); EXPECT_EQ(cmdQ->readImageCalled, 1u); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->enqueueRegion, region); EXPECT_EQ(cmdQ->enqueueOrigin, origin); EXPECT_EQ(cmdQ->unmapPtr, mappedPtr); @@ -181,13 +180,13 @@ HWTEST_F(MultipleMapImageTest, givenReadOnlyMapWhenUnmappedOnGpuThenEnqueueMarke MemObjSizeArray region = {{3, 4, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_READ, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->enqueueRegion, region); EXPECT_EQ(cmdQ->enqueueOrigin, origin); EXPECT_EQ(cmdQ->readImageCalled, 1u); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->writeImageCalled, 0u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); } @@ -204,12 +203,12 @@ HWTEST_F(MultipleMapImageTest, givenWriteInvalidateMapWhenMappedOnGpuThenEnqueue MemObjSizeArray region = {{3, 4, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->readImageCalled, 0u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->writeImageCalled, 1u); EXPECT_EQ(cmdQ->enqueueMarkerCalled, 1u); EXPECT_EQ(cmdQ->enqueueRegion, region); @@ -221,7 +220,7 @@ HWTEST_F(MultipleMapImageTest, givenNotMappedPtrWhenUnmapedThenReturnError) { auto cmdQ = createMockCmdQ(); EXPECT_EQ(!UnitTestHelper::tiledImagesSupported, image->mappingOnCpuAllowed()); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), image->getBasePtrForMap(cmdQ->getDevice().getRootDeviceIndex()), 0, nullptr, nullptr); EXPECT_EQ(CL_INVALID_VALUE, retVal); } @@ -240,7 +239,7 @@ HWTEST_F(MultipleMapImageTest, givenErrorFromReadImageWhenMappedOnGpuThenDontAdd void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_READ, origin, region, nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr); EXPECT_EQ(CL_OUT_OF_RESOURCES, retVal); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapImageTest, givenErrorFromWriteImageWhenUnmappedOnGpuThenDontRemoveMappedPtr) { @@ -256,12 +255,12 @@ HWTEST_F(MultipleMapImageTest, givenErrorFromWriteImageWhenUnmappedOnGpuThenDont size_t region[] = {2, 1, 1}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, origin, region, nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); EXPECT_EQ(CL_OUT_OF_RESOURCES, retVal); EXPECT_EQ(cmdQ->writeImageCalled, 1u); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapImageTest, givenUnblockedQueueWhenMappedOnCpuThenAddMappedPtrAndRemoveOnUnmap) { @@ -274,13 +273,13 @@ HWTEST_F(MultipleMapImageTest, givenUnblockedQueueWhenMappedOnCpuThenAddMappedPt MemObjSizeArray region = {{3, 1, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(1u, image->transferToHostPtrCalled); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(1u, image->transferFromHostPtrCalled); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); @@ -297,13 +296,13 @@ HWTEST_F(MultipleMapImageTest, givenUnblockedQueueWhenReadOnlyUnmappedOnCpuThenD MemObjSizeArray region = {{3, 1, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_READ, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(1u, image->transferToHostPtrCalled); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(0u, image->transferFromHostPtrCalled); } @@ -318,11 +317,11 @@ HWTEST_F(MultipleMapImageTest, givenUnblockedQueueWhenWriteInvalidateMappedOnCpu MemObjSizeArray region = {{3, 1, 1}}; void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(0u, image->transferToHostPtrCalled); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(1u, image->transferFromHostPtrCalled); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); @@ -345,13 +344,13 @@ HWTEST_F(MultipleMapImageTest, givenBlockedQueueWhenMappedOnCpuThenAddMappedPtrA mapEvent.setStatus(CL_COMPLETE); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(1u, image->transferToHostPtrCalled); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 1, &clUnmapEvent, nullptr); unmapEvent.setStatus(CL_COMPLETE); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(1u, image->transferFromHostPtrCalled); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); @@ -374,13 +373,13 @@ HWTEST_F(MultipleMapImageTest, givenBlockedQueueWhenMappedReadOnlyOnCpuThenDontM mapEvent.setStatus(CL_COMPLETE); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(1u, image->transferToHostPtrCalled); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(image->copyRegion, region); EXPECT_EQ(image->copyOrigin, origin); retVal = clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtr, 1, &clUnmapEvent, nullptr); unmapEvent.setStatus(CL_COMPLETE); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(0u, image->transferFromHostPtrCalled); } @@ -408,26 +407,26 @@ HWTEST_F(MultipleMapImageTest, givenMultimpleMapsWhenUnmappingThenRemoveCorrectP mappedPtrs[i].ptr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, &mappedPtrs[i].offset[0], &mappedPtrs[i].size[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtrs[i].ptr); - EXPECT_EQ(i + 1, image->mapOperationsHandler.size()); + EXPECT_EQ(i + 1, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->enqueueRegion, mappedPtrs[i].size); EXPECT_EQ(cmdQ->enqueueOrigin, mappedPtrs[i].offset); } // reordered unmap clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtrs[1].ptr, 0, nullptr, nullptr); - EXPECT_EQ(2u, image->mapOperationsHandler.size()); + EXPECT_EQ(2u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[1].ptr); EXPECT_EQ(cmdQ->enqueueRegion, mappedPtrs[1].size); EXPECT_EQ(cmdQ->enqueueOrigin, mappedPtrs[1].offset); clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtrs[2].ptr, 0, nullptr, nullptr); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[2].ptr); EXPECT_EQ(cmdQ->enqueueRegion, mappedPtrs[2].size); EXPECT_EQ(cmdQ->enqueueOrigin, mappedPtrs[2].offset); clEnqueueUnmapMemObject(cmdQ.get(), image.get(), mappedPtrs[0].ptr, 0, nullptr, nullptr); - EXPECT_EQ(0u, image->mapOperationsHandler.size()); + EXPECT_EQ(0u, image->getMapOperationsHandler().size()); EXPECT_EQ(cmdQ->unmapPtr, mappedPtrs[0].ptr); EXPECT_EQ(cmdQ->enqueueRegion, mappedPtrs[0].size); EXPECT_EQ(cmdQ->enqueueOrigin, mappedPtrs[0].offset); @@ -443,13 +442,13 @@ HWTEST_F(MultipleMapImageTest, givenOverlapingPtrWhenMappingForWriteThenReturnEr void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_READ, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); origin[0]++; void *mappedPtr2 = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr2); EXPECT_EQ(CL_INVALID_OPERATION, retVal); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); } HWTEST_F(MultipleMapImageTest, givenOverlapingPtrWhenMappingOnCpuForWriteThenReturnError) { @@ -462,12 +461,12 @@ HWTEST_F(MultipleMapImageTest, givenOverlapingPtrWhenMappingOnCpuForWriteThenRet void *mappedPtr = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_READ, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_NE(nullptr, mappedPtr); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); origin[0]++; void *mappedPtr2 = clEnqueueMapImage(cmdQ.get(), image.get(), CL_TRUE, CL_MAP_WRITE, &origin[0], ®ion[0], nullptr, nullptr, 0, nullptr, nullptr, &retVal); EXPECT_EQ(nullptr, mappedPtr2); EXPECT_EQ(CL_INVALID_OPERATION, retVal); - EXPECT_EQ(1u, image->mapOperationsHandler.size()); + EXPECT_EQ(1u, image->getMapOperationsHandler().size()); } } // namespace NEO diff --git a/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_2_tests.cpp b/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_2_tests.cpp index b355023fda..67ea8e77e6 100644 --- a/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_2_tests.cpp +++ b/opencl/test/unit_test/command_stream/command_stream_receiver_flush_task_2_tests.cpp @@ -283,7 +283,9 @@ HWTEST_F(CommandStreamReceiverFlushTaskTests, GivenNonBlockingMapEnqueueWhenFini size_t tempBuffer[] = {0, 1, 2}; cl_int retVal; - AlignedBuffer mockBuffer; + auto cpuAllocation = std::make_unique(MemoryConstants::pageSize); + MockGraphicsAllocation allocation{cpuAllocation.get(), MemoryConstants::pageSize}; + AlignedBuffer mockBuffer{&ctx, &allocation}; uint32_t taskCount = 0; taskLevel = taskCount; diff --git a/opencl/test/unit_test/mem_obj/map_operations_handler_tests.cpp b/opencl/test/unit_test/mem_obj/map_operations_handler_tests.cpp index 73d92d47da..18fdc02ae5 100644 --- a/opencl/test/unit_test/mem_obj/map_operations_handler_tests.cpp +++ b/opencl/test/unit_test/mem_obj/map_operations_handler_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -8,6 +8,7 @@ #include "shared/source/helpers/ptr_math.h" #include "opencl/source/mem_obj/map_operations_handler.h" +#include "opencl/test/unit_test/mocks/mock_buffer.h" #include "test.h" #include @@ -160,3 +161,56 @@ TEST_P(MapOperationsHandlerOverlapTests, givenAlreadyMappedPtrWhenAskingForOverl INSTANTIATE_TEST_CASE_P(MapOperationsHandlerOverlapTests, MapOperationsHandlerOverlapTests, ::testing::ValuesIn(overlappingCombinations)); + +struct MapOperationsStorageWhitebox : MapOperationsStorage { + using MapOperationsStorage::handlers; +}; + +TEST(MapOperationsStorageTest, givenMapOperationsStorageWhenGetHandlerIsUsedThenCreateHandler) { + MockBuffer buffer1{}; + MockBuffer buffer2{}; + + MapOperationsStorageWhitebox storage{}; + EXPECT_EQ(0u, storage.handlers.size()); + + storage.getHandler(&buffer1); + EXPECT_EQ(1u, storage.handlers.size()); + + storage.getHandler(&buffer2); + EXPECT_EQ(2u, storage.handlers.size()); + + storage.getHandler(&buffer1); + EXPECT_EQ(2u, storage.handlers.size()); +} + +TEST(MapOperationsStorageTest, givenMapOperationsStorageWhenGetHandlerIfExistsIsUsedThenDoNotCreateHandler) { + MockBuffer buffer1{}; + MockBuffer buffer2{}; + + MapOperationsStorageWhitebox storage{}; + EXPECT_EQ(0u, storage.handlers.size()); + EXPECT_EQ(nullptr, storage.getHandlerIfExists(&buffer1)); + EXPECT_EQ(nullptr, storage.getHandlerIfExists(&buffer2)); + + storage.getHandler(&buffer1); + EXPECT_EQ(1u, storage.handlers.size()); + EXPECT_NE(nullptr, storage.getHandlerIfExists(&buffer1)); + EXPECT_EQ(nullptr, storage.getHandlerIfExists(&buffer2)); + + storage.getHandler(&buffer2); + EXPECT_EQ(2u, storage.handlers.size()); + EXPECT_NE(nullptr, storage.getHandlerIfExists(&buffer1)); + EXPECT_NE(nullptr, storage.getHandlerIfExists(&buffer2)); + EXPECT_NE(storage.getHandlerIfExists(&buffer1), storage.getHandlerIfExists(&buffer2)); +} + +TEST(MapOperationsStorageTest, givenMapOperationsStorageWhenRemoveHandlerIsUsedThenRemoveHandler) { + MockBuffer buffer{}; + MapOperationsStorageWhitebox storage{}; + + storage.getHandler(&buffer); + ASSERT_EQ(1u, storage.handlers.size()); + + storage.removeHandler(&buffer); + EXPECT_EQ(0u, storage.handlers.size()); +} diff --git a/opencl/test/unit_test/mocks/mock_buffer.h b/opencl/test/unit_test/mocks/mock_buffer.h index 0c2e889e9b..efdf96392d 100644 --- a/opencl/test/unit_test/mocks/mock_buffer.h +++ b/opencl/test/unit_test/mocks/mock_buffer.h @@ -35,16 +35,21 @@ class MockBuffer : public MockBufferStorage, public Buffer { using Buffer::magic; using Buffer::offset; using Buffer::size; + using MemObj::context; using MemObj::isZeroCopy; using MemObj::memObjectType; using MockBufferStorage::device; - MockBuffer(GraphicsAllocation &alloc) + + MockBuffer(GraphicsAllocation &alloc) : MockBuffer(nullptr, alloc) {} + + MockBuffer(Context *context, GraphicsAllocation &alloc) : MockBufferStorage(), Buffer( - nullptr, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), + context, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), CL_MEM_USE_HOST_PTR, 0, alloc.getUnderlyingBufferSize(), alloc.getUnderlyingBuffer(), alloc.getUnderlyingBuffer(), GraphicsAllocationHelper::toMultiGraphicsAllocation(&alloc), true, false, false), externalAlloc(&alloc) { } + MockBuffer() : MockBufferStorage(), Buffer( nullptr, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), @@ -53,9 +58,8 @@ class MockBuffer : public MockBufferStorage, public Buffer { } ~MockBuffer() override { if (externalAlloc != nullptr) { - // no ownership over graphics allocation - // return to mock allocations - this->multiGraphicsAllocation.addAllocation(&this->mockGfxAllocation); + // no ownership over graphics allocation, do not release it + this->multiGraphicsAllocation.removeAllocation(0u); } } void setArgStateful(void *memory, bool forceNonAuxMode, bool disableL3, bool alignSizeForAuxTranslation, bool isReadOnly, const Device &device, bool useGlobalAtomics, bool areMultipleSubDevicesInContext) override { @@ -73,15 +77,29 @@ class AlignedBuffer : public MockBufferStorage, public Buffer { CL_MEM_USE_HOST_PTR, 0, sizeof(data) / 2, alignUp(&data, 64), alignUp(&data, 64), GraphicsAllocationHelper::toMultiGraphicsAllocation(&mockGfxAllocation), true, false, false) { } - AlignedBuffer(GraphicsAllocation *gfxAllocation) + + AlignedBuffer(GraphicsAllocation *gfxAllocation) : AlignedBuffer(nullptr, gfxAllocation) {} + + AlignedBuffer(Context *context, GraphicsAllocation *gfxAllocation) : MockBufferStorage(), Buffer( - nullptr, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), + context, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), CL_MEM_USE_HOST_PTR, 0, sizeof(data) / 2, alignUp(&data, 64), alignUp(&data, 64), - GraphicsAllocationHelper::toMultiGraphicsAllocation(gfxAllocation), true, false, false) { + GraphicsAllocationHelper::toMultiGraphicsAllocation(gfxAllocation), true, false, false), + externalAlloc(gfxAllocation) { } + + ~AlignedBuffer() override { + if (externalAlloc != nullptr) { + // no ownership over graphics allocation, do not release it + this->multiGraphicsAllocation.removeAllocation(0u); + } + } + void setArgStateful(void *memory, bool forceNonAuxMode, bool disableL3, bool alignSizeForAuxTranslation, bool isReadOnly, const Device &device, bool useGlobalAtomics, bool areMultipleSubDevicesInContext) override { Buffer::setSurfaceState(this->device.get(), memory, forceNonAuxMode, disableL3, getSize(), getCpuAddress(), 0, &mockGfxAllocation, 0, 0, false, false); } + + GraphicsAllocation *externalAlloc = nullptr; }; class UnalignedBuffer : public MockBufferStorage, public Buffer { @@ -93,15 +111,29 @@ class UnalignedBuffer : public MockBufferStorage, public Buffer { CL_MEM_USE_HOST_PTR, 0, sizeof(data) / 2, alignUp(&data, 4), alignUp(&data, 4), GraphicsAllocationHelper::toMultiGraphicsAllocation(&mockGfxAllocation), false, false, false) { } - UnalignedBuffer(GraphicsAllocation *gfxAllocation) + + UnalignedBuffer(GraphicsAllocation *gfxAllocation) : UnalignedBuffer(nullptr, gfxAllocation) {} + + UnalignedBuffer(Context *context, GraphicsAllocation *gfxAllocation) : MockBufferStorage(true), Buffer( - nullptr, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), + context, ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, MockBufferStorage::device.get()), CL_MEM_USE_HOST_PTR, 0, sizeof(data) / 2, alignUp(&data, 4), alignUp(&data, 4), - GraphicsAllocationHelper::toMultiGraphicsAllocation(gfxAllocation), false, false, false) { + GraphicsAllocationHelper::toMultiGraphicsAllocation(gfxAllocation), false, false, false), + externalAlloc(gfxAllocation) { } + + ~UnalignedBuffer() override { + if (externalAlloc != nullptr) { + // no ownership over graphics allocation, do not release it + this->multiGraphicsAllocation.removeAllocation(0u); + } + } + void setArgStateful(void *memory, bool forceNonAuxMode, bool disableL3, bool alignSizeForAuxTranslation, bool isReadOnly, const Device &device, bool useGlobalAtomics, bool areMultipleSubDevicesInContext) override { Buffer::setSurfaceState(this->device.get(), memory, forceNonAuxMode, disableL3, getSize(), getCpuAddress(), 0, &mockGfxAllocation, 0, 0, false, false); } + + GraphicsAllocation *externalAlloc = nullptr; }; class MockPublicAccessBuffer : public Buffer { diff --git a/opencl/test/unit_test/mt_tests/command_queue/ioq_task_tests_mt.cpp b/opencl/test/unit_test/mt_tests/command_queue/ioq_task_tests_mt.cpp index 8cf7ecd589..2c9bd55a9f 100644 --- a/opencl/test/unit_test/mt_tests/command_queue/ioq_task_tests_mt.cpp +++ b/opencl/test/unit_test/mt_tests/command_queue/ioq_task_tests_mt.cpp @@ -86,7 +86,8 @@ TEST_F(IOQTaskTestsMt, GivenBlockedOnUserEventWhenEnqueingMarkerThenSuccessIsRet } TEST_F(IOQTaskTestsMt, GivenMultipleThreadsWhenMappingBufferThenEventsAreCompleted) { - AlignedBuffer alignedBuffer; + MockGraphicsAllocation alignedBufferAlloc{nullptr, MemoryConstants::pageSize}; + AlignedBuffer alignedBuffer{pContext, &alignedBufferAlloc}; auto userEvent = clCreateUserEvent(pContext, &retVal); EXPECT_EQ(CL_SUCCESS, retVal);