Skip to content

Commit b4f79e0

Browse files
Map/unmap enqueue fixes [5/n]: Unify offset calculation
Change-Id: I53eafe89532d43c5cf5139ed3fac0a87619dc7a3
1 parent 6373251 commit b4f79e0

File tree

9 files changed

+56
-48
lines changed

9 files changed

+56
-48
lines changed

runtime/api/api.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,9 @@ cl_int CL_API_CALL clEnqueueUnmapMemObject(cl_command_queue commandQueue,
24822482
"event", event);
24832483

24842484
if (retVal == CL_SUCCESS) {
2485+
if (pMemObj->peekClMemObjType() == CL_MEM_OBJECT_PIPE) {
2486+
return CL_INVALID_MEM_OBJECT;
2487+
}
24852488
if (!mappedPtr || mappedPtr != pMemObj->getMappedPtr()) {
24862489
return CL_INVALID_VALUE;
24872490
}

runtime/command_queue/command_queue.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,16 @@ bool CommandQueue::sendPerfCountersConfig() {
457457
}
458458

459459
cl_int CommandQueue::enqueueWriteMemObjForUnmap(MemObj *memObj, void *mappedPtr, EventsRequest &eventsRequest) {
460-
auto image = castToObject<Image>(memObj);
461-
if (image) {
462-
auto retVal = enqueueWriteImage(image, CL_FALSE, &image->getMappedOffset()[0], &image->getMappedSize()[0],
463-
image->getHostPtrRowPitch(), image->getHostPtrSlicePitch(), mappedPtr,
464-
eventsRequest.numEventsInWaitList, eventsRequest.eventWaitList, eventsRequest.outEvent);
460+
cl_int retVal;
461+
if (memObj->peekClMemObjType() == CL_MEM_OBJECT_BUFFER) {
462+
auto buffer = castToObject<Buffer>(memObj);
463+
retVal = enqueueWriteBuffer(buffer, CL_TRUE, buffer->getMappedOffset()[0], buffer->getMappedSize()[0], mappedPtr,
464+
eventsRequest.numEventsInWaitList, eventsRequest.eventWaitList, eventsRequest.outEvent);
465+
} else {
466+
auto image = castToObject<Image>(memObj);
467+
retVal = enqueueWriteImage(image, CL_FALSE, &image->getMappedOffset()[0], &image->getMappedSize()[0],
468+
image->getHostPtrRowPitch(), image->getHostPtrSlicePitch(), mappedPtr,
469+
eventsRequest.numEventsInWaitList, eventsRequest.eventWaitList, eventsRequest.outEvent);
465470
bool mustCallFinish = true;
466471
if (!(image->getFlags() & CL_MEM_USE_HOST_PTR)) {
467472
mustCallFinish = true;
@@ -471,32 +476,21 @@ cl_int CommandQueue::enqueueWriteMemObjForUnmap(MemObj *memObj, void *mappedPtr,
471476
if (mustCallFinish) {
472477
finish(true);
473478
}
474-
return retVal;
475-
}
476-
477-
auto buffer = castToObject<Buffer>(memObj);
478-
if (buffer) {
479-
return enqueueWriteBuffer(buffer, CL_TRUE, buffer->getMappedOffset()[0], buffer->getMappedSize()[0], mappedPtr,
480-
eventsRequest.numEventsInWaitList, eventsRequest.eventWaitList, eventsRequest.outEvent);
481479
}
482480

483-
return CL_INVALID_MEM_OBJECT;
481+
return retVal;
484482
}
485483

486484
void *CommandQueue::enqueueReadMemObjForMap(TransferProperties &transferProperties, EventsRequest &eventsRequest, cl_int &errcodeRet) {
487-
void *baseMapPtr = transferProperties.memObj->getBasePtrForMap();
488-
void *returnPtr = nullptr;
485+
void *returnPtr = ptrOffset(transferProperties.memObj->getBasePtrForMap(),
486+
transferProperties.memObj->calculateOffsetForMapping(transferProperties.offsetPtr));
489487

490-
auto buffer = castToObject<Buffer>(transferProperties.memObj);
491-
if (buffer) {
492-
returnPtr = ptrOffset(baseMapPtr, *transferProperties.offsetPtr);
488+
if (transferProperties.memObj->peekClMemObjType() == CL_MEM_OBJECT_BUFFER) {
489+
auto buffer = castToObject<Buffer>(transferProperties.memObj);
493490
errcodeRet = enqueueReadBuffer(buffer, transferProperties.blocking, *transferProperties.offsetPtr, *transferProperties.sizePtr, returnPtr,
494491
eventsRequest.numEventsInWaitList, eventsRequest.eventWaitList, eventsRequest.outEvent);
495492
} else {
496493
auto image = castToObject<Image>(transferProperties.memObj);
497-
498-
returnPtr = ptrOffset(baseMapPtr, image->calculateOffset(image->getHostPtrRowPitch(), image->getHostPtrSlicePitch(), transferProperties.offsetPtr));
499-
500494
errcodeRet = enqueueReadImage(image, transferProperties.blocking, transferProperties.offsetPtr, transferProperties.sizePtr,
501495
image->getHostPtrRowPitch(), image->getHostPtrSlicePitch(), returnPtr, eventsRequest.numEventsInWaitList,
502496
eventsRequest.eventWaitList, eventsRequest.outEvent);

runtime/command_queue/cpu_data_transfer_handler.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,8 @@ void *CommandQueue::cpuDataTransferHandler(TransferProperties &transferPropertie
151151
}
152152

153153
if (transferProperties.cmdType == CL_COMMAND_MAP_BUFFER || transferProperties.cmdType == CL_COMMAND_MAP_IMAGE) {
154-
size_t mapPtrOffset;
155-
if (image) {
156-
mapPtrOffset = image->calculateOffset(image->getImageDesc().image_row_pitch, image->getImageDesc().image_slice_pitch,
157-
transferProperties.offsetPtr);
158-
} else {
159-
mapPtrOffset = *transferProperties.offsetPtr;
160-
}
161-
returnPtr = ptrOffset(transferProperties.memObj->getCpuAddressForMapping(), mapPtrOffset);
154+
returnPtr = ptrOffset(transferProperties.memObj->getCpuAddressForMapping(),
155+
transferProperties.memObj->calculateOffsetForMapping(transferProperties.offsetPtr));
162156
transferProperties.memObj->setMapInfo(returnPtr, transferProperties.sizePtr, transferProperties.offsetPtr);
163157
}
164158

runtime/mem_obj/image.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,10 @@ bool Image::hasAlphaChannel(const cl_image_format *imageFormat) {
11891189
(channelOrder == CL_ABGR);
11901190
}
11911191

1192-
size_t Image::calculateOffset(size_t rowPitch, size_t slicePitch, size_t *origin) const {
1192+
size_t Image::calculateOffsetForMapping(size_t *origin) const {
1193+
size_t rowPitch = mappingOnCpuAllowed() ? imageDesc.image_row_pitch : getHostPtrRowPitch();
1194+
size_t slicePitch = mappingOnCpuAllowed() ? imageDesc.image_slice_pitch : getHostPtrSlicePitch();
1195+
11931196
return getSurfaceFormatInfo().ImageElementSizeInBytes * origin[0] + rowPitch * origin[1] + slicePitch * origin[2];
11941197
}
11951198
} // namespace OCLRT

runtime/mem_obj/image.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ class Image : public MemObj {
142142

143143
uint32_t getQPitch() { return qPitch; }
144144
void setQPitch(uint32_t qPitch) { this->qPitch = qPitch; }
145-
size_t getHostPtrRowPitch() { return hostPtrRowPitch; }
145+
size_t getHostPtrRowPitch() const { return hostPtrRowPitch; }
146146
void setHostPtrRowPitch(size_t pitch) { this->hostPtrRowPitch = pitch; }
147-
size_t getHostPtrSlicePitch() { return hostPtrSlicePitch; }
147+
size_t getHostPtrSlicePitch() const { return hostPtrSlicePitch; }
148148
void setHostPtrSlicePitch(size_t pitch) { this->hostPtrSlicePitch = pitch; }
149149
size_t getImageCount() const { return imageCount; }
150150
void setImageCount(size_t imageCount) { this->imageCount = imageCount; }
@@ -171,7 +171,7 @@ class Image : public MemObj {
171171
cl_int writeNV12Planes(const void *hostPtr, size_t hostPtrRowPitch);
172172
void setMcsSurfaceInfo(McsSurfaceInfo &info) { mcsSurfaceInfo = info; }
173173
const McsSurfaceInfo &getMcsSurfaceInfo() { return mcsSurfaceInfo; }
174-
size_t calculateOffset(size_t rowPitch, size_t slicePitch, size_t *origin) const;
174+
size_t calculateOffsetForMapping(size_t *origin) const override;
175175

176176
const bool isTiledImage;
177177

runtime/mem_obj/mem_obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class MemObj : public BaseObject<_cl_mem> {
124124
void destroyGraphicsAllocation(GraphicsAllocation *allocation, bool asyncDestroy);
125125
bool checkIfMemoryTransferIsRequired(size_t offsetInMemObjest, size_t offsetInHostPtr, const void *ptr, cl_command_type cmdType);
126126
bool mappingOnCpuAllowed() const { return !allowTiling() && !peekSharingHandler(); }
127+
virtual size_t calculateOffsetForMapping(size_t *offset) const { return *offset; }
128+
cl_mem_object_type peekClMemObjType() const { return memObjectType; }
127129

128130
protected:
129131
void getOsSpecificMemObjectInfo(const cl_mem_info &paramName, size_t *srcParamSize, void **srcParam);

unit_tests/command_queue/enqueue_map_image_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ HWTEST_F(EnqueueMapImageTest, givenImageWithouUsetHostPtrFlagWhenMappedOnCpuThen
493493
EXPECT_EQ(region[1], image->getMappedSize()[1]);
494494
EXPECT_EQ(region[2], image->getMappedSize()[2]);
495495

496-
auto offset = image->calculateOffset(image->getImageDesc().image_row_pitch, image->getImageDesc().image_slice_pitch, origin);
497-
auto expectedPtr = ptrOffset(image->getCpuAddressForMapping(), offset);
496+
auto expectedPtr = ptrOffset(image->getCpuAddressForMapping(), image->calculateOffsetForMapping(origin));
498497

499498
EXPECT_EQ(mappedPtr, expectedPtr);
500499
}
@@ -517,8 +516,7 @@ HWTEST_F(EnqueueMapImageTest, givenImageWithUseHostPtrFlagWhenMappedOnCpuThenSet
517516
EXPECT_EQ(region[1], image->getMappedSize()[1]);
518517
EXPECT_EQ(region[2], image->getMappedSize()[2]);
519518

520-
auto offset = image->calculateOffset(image->getImageDesc().image_row_pitch, image->getImageDesc().image_slice_pitch, origin);
521-
auto expectedPtr = ptrOffset(image->getCpuAddressForMapping(), offset);
519+
auto expectedPtr = ptrOffset(image->getCpuAddressForMapping(), image->calculateOffsetForMapping(origin));
522520

523521
EXPECT_EQ(mappedPtr, expectedPtr);
524522
}

unit_tests/mem_obj/image_tests.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -992,16 +992,34 @@ TEST_F(ImageCompressionTests, givenNonTiledImageWhenCreatingAllocationThenDontPr
992992
EXPECT_FALSE(myMemoryManager->capturedImgInfo.preferRenderCompression);
993993
}
994994

995-
TEST(ImageTest, givenImageWhenAskedForPtrOffsetThenReturnCorrectValue) {
995+
TEST(ImageTest, givenImageWhenAskedForPtrOffsetForGpuMappingThenReturnCorrectValue) {
996996
MockContext ctx;
997997
std::unique_ptr<Image> image(ImageHelper<Image3dDefaults>::create(&ctx));
998+
EXPECT_FALSE(image->mappingOnCpuAllowed());
998999

9991000
size_t origin[3] = {4, 5, 6};
1000-
size_t rowPitch = 7;
1001-
size_t slicePitch = 8;
10021001

1003-
auto retOffset = image->calculateOffset(rowPitch, slicePitch, origin);
1004-
size_t expectedOffset = image->getSurfaceFormatInfo().ImageElementSizeInBytes * origin[0] + rowPitch * origin[1] + slicePitch * origin[2];
1002+
auto retOffset = image->calculateOffsetForMapping(origin);
1003+
size_t expectedOffset = image->getSurfaceFormatInfo().ImageElementSizeInBytes * origin[0] +
1004+
image->getHostPtrRowPitch() * origin[1] + image->getHostPtrSlicePitch() * origin[2];
1005+
1006+
EXPECT_EQ(expectedOffset, retOffset);
1007+
}
1008+
1009+
TEST(ImageTest, givenImageWhenAskedForPtrOffsetForCpuMappingThenReturnCorrectValue) {
1010+
DebugManagerStateRestore restore;
1011+
DebugManager.flags.ForceLinearImages.set(true);
1012+
MockContext ctx;
1013+
std::unique_ptr<Image> image(ImageHelper<Image3dDefaults>::create(&ctx));
1014+
1015+
EXPECT_TRUE(image->mappingOnCpuAllowed());
1016+
1017+
size_t origin[3] = {4, 5, 6};
1018+
1019+
auto retOffset = image->calculateOffsetForMapping(origin);
1020+
size_t expectedOffset = image->getSurfaceFormatInfo().ImageElementSizeInBytes * origin[0] +
1021+
image->getImageDesc().image_row_pitch * origin[1] +
1022+
image->getImageDesc().image_slice_pitch * origin[2];
10051023

10061024
EXPECT_EQ(expectedOffset, retOffset);
10071025
}

unit_tests/mem_obj/pipe_tests.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,12 @@ TEST_F(PipeTest, FailedAllocationInjection) {
9999
}
100100

101101
TEST_F(PipeTest, givenPipeWhenEnqueueWriteForUnmapIsCalledThenReturnError) {
102-
struct MyCommandQueue : public CommandQueue {
103-
using CommandQueue::enqueueWriteMemObjForUnmap;
104-
} myCmdQ;
105-
106102
int errCode = CL_SUCCESS;
107103
std::unique_ptr<Pipe> pipe(Pipe::create(&context, CL_MEM_READ_ONLY, 1, 20, nullptr, errCode));
108104
ASSERT_NE(nullptr, pipe);
109105
EXPECT_EQ(CL_SUCCESS, errCode);
110106

111-
EventsRequest eventsRequest(0, nullptr, nullptr);
112-
errCode = myCmdQ.enqueueWriteMemObjForUnmap(pipe.get(), nullptr, eventsRequest);
107+
CommandQueue cmdQ;
108+
errCode = clEnqueueUnmapMemObject(&cmdQ, pipe.get(), nullptr, 0, nullptr, nullptr);
113109
EXPECT_EQ(CL_INVALID_MEM_OBJECT, errCode);
114110
}

0 commit comments

Comments
 (0)