Skip to content

Commit 98006aa

Browse files
Add support for SVM pointer reuse to enqueueReadBuffer.
Change-Id: I7a6718b2ebe48912a19af3da5e233acd84bdd3ef Signed-off-by: Michal Mrozek <michal.mrozek@intel.com>
1 parent c50d8e3 commit 98006aa

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

runtime/command_queue/enqueue_read_buffer.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* Copyright (C) 2017-2019 Intel Corporation
2+
* Copyright (C) 2017-2020 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
66
*/
77

88
#pragma once
99
#include "core/helpers/cache_policy.h"
10+
#include "core/memory_manager/unified_memory_manager.h"
1011
#include "runtime/built_ins/built_ins.h"
1112
#include "runtime/command_queue/command_queue_hw.h"
1213
#include "runtime/command_queue/enqueue_common.h"
@@ -69,6 +70,21 @@ cl_int CommandQueueHw<GfxFamily>::enqueueReadBuffer(
6970
GeneralSurface mapSurface;
7071
Surface *surfaces[] = {&bufferSurf, nullptr};
7172

73+
//check if we are dealing with SVM pointer here for which we already have an allocation
74+
if (!mapAllocation && this->getContext().getSVMAllocsManager()) {
75+
auto svmEntry = this->getContext().getSVMAllocsManager()->getSVMAlloc(ptr);
76+
if (svmEntry) {
77+
if (svmEntry->memoryType == DEVICE_UNIFIED_MEMORY) {
78+
return CL_INVALID_OPERATION;
79+
}
80+
if ((svmEntry->gpuAllocation->getGpuAddress() + svmEntry->size) < (castToUint64(ptr) + size)) {
81+
return CL_INVALID_OPERATION;
82+
}
83+
84+
mapAllocation = svmEntry->cpuAllocation ? svmEntry->cpuAllocation : svmEntry->gpuAllocation;
85+
}
86+
}
87+
7288
if (mapAllocation) {
7389
surfaces[1] = &mapSurface;
7490
mapSurface.setGraphicsAllocation(mapAllocation);

unit_tests/memory_manager/unified_memory_manager_tests.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ TEST_F(ShareableUnifiedMemoryManagerPropertiesTest, givenShareableUnifiedPropert
580580
svmManager->freeSVMAlloc(ptr);
581581
}
582582

583-
TEST(UnfiedSharedMemoryTransferCalls, givenHostUSMllocationWhenPointerIsUsedAsWriteBufferSourceThenUSMAllocationIsReused) {
583+
TEST(UnfiedSharedMemoryTransferCalls, givenHostUSMllocationWhenPointerIsUsedForTransferCallsThenUSMAllocationIsReused) {
584584
MockContext mockContext;
585585
cl_context clContext = &mockContext;
586586

@@ -608,13 +608,18 @@ TEST(UnfiedSharedMemoryTransferCalls, givenHostUSMllocationWhenPointerIsUsedAsWr
608608

609609
EXPECT_EQ(1u, svmAllocation->gpuAllocation->getTaskCount(osContextId));
610610

611+
status = clEnqueueReadBuffer(commandQueue, buffer, false, 0u, 4096u, hostMemory, 0u, nullptr, nullptr);
612+
ASSERT_EQ(CL_SUCCESS, status);
613+
EXPECT_TRUE(temporaryAllocations.peekIsEmpty());
614+
EXPECT_EQ(2u, svmAllocation->gpuAllocation->getTaskCount(osContextId));
615+
611616
status = clReleaseMemObject(buffer);
612617
ASSERT_EQ(CL_SUCCESS, status);
613618
status = clMemFreeINTEL(clContext, hostMemory);
614619
ASSERT_EQ(CL_SUCCESS, status);
615620
clReleaseCommandQueue(commandQueue);
616621
}
617-
TEST(UnfiedSharedMemoryTransferCalls, givenDeviceUsmAllocationWhenItIsPassedToWriteBufferAsSourceThenErrorIsReturned) {
622+
TEST(UnfiedSharedMemoryTransferCalls, givenDeviceUsmAllocationWhenPtrIsUsedForTransferCallsThenErrorIsReturned) {
618623
MockContext mockContext;
619624
cl_context clContext = &mockContext;
620625

@@ -633,14 +638,17 @@ TEST(UnfiedSharedMemoryTransferCalls, givenDeviceUsmAllocationWhenItIsPassedToWr
633638
status = clEnqueueWriteBuffer(commandQueue, buffer, false, 0u, 4096u, deviceMemory, 0u, nullptr, nullptr);
634639
EXPECT_EQ(CL_INVALID_OPERATION, status);
635640

641+
status = clEnqueueReadBuffer(commandQueue, buffer, false, 0u, 4096u, deviceMemory, 0u, nullptr, nullptr);
642+
ASSERT_EQ(CL_INVALID_OPERATION, status);
643+
636644
status = clReleaseMemObject(buffer);
637645
ASSERT_EQ(CL_SUCCESS, status);
638646
status = clMemFreeINTEL(clContext, deviceMemory);
639647
ASSERT_EQ(CL_SUCCESS, status);
640648
clReleaseCommandQueue(commandQueue);
641649
}
642650

643-
TEST(UnfiedSharedMemoryTransferCalls, givenHostAllocationThatIsSmallerThenWriteBufferTranfserSizeWhenTransferCallIsEmittedThenErrorIsReturned) {
651+
TEST(UnfiedSharedMemoryTransferCalls, givenHostAllocationThatIsSmallerThenTransferRequirementsThenErrorIsReturned) {
644652
MockContext mockContext;
645653
cl_context clContext = &mockContext;
646654

@@ -660,14 +668,17 @@ TEST(UnfiedSharedMemoryTransferCalls, givenHostAllocationThatIsSmallerThenWriteB
660668
status = clEnqueueWriteBuffer(commandQueue, buffer, false, 0u, 4096u, hostMemory, 0u, nullptr, nullptr);
661669
EXPECT_EQ(CL_INVALID_OPERATION, status);
662670

671+
status = clEnqueueReadBuffer(commandQueue, buffer, false, 0u, 4096u, hostMemory, 0u, nullptr, nullptr);
672+
ASSERT_EQ(CL_INVALID_OPERATION, status);
673+
663674
status = clReleaseMemObject(buffer);
664675
ASSERT_EQ(CL_SUCCESS, status);
665676
status = clMemFreeINTEL(clContext, hostMemory);
666677
ASSERT_EQ(CL_SUCCESS, status);
667678
clReleaseCommandQueue(commandQueue);
668679
}
669680

670-
TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithoutLocalMemoryWhenPointerIsUsedAsWriteBufferSourceThenUSMAllocationIsReused) {
681+
TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithoutLocalMemoryWhenPointerIsUsedAsTranfserParameterThenUSMAllocationIsReused) {
671682
DebugManagerStateRestore restore;
672683
DebugManager.flags.EnableLocalMemory.set(0);
673684

@@ -697,14 +708,19 @@ TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithoutLocalMemoryW
697708

698709
EXPECT_EQ(1u, svmAllocation->gpuAllocation->getTaskCount(osContextId));
699710

711+
status = clEnqueueReadBuffer(commandQueue, buffer, false, 0u, 4096u, sharedMemory, 0u, nullptr, nullptr);
712+
ASSERT_EQ(CL_SUCCESS, status);
713+
EXPECT_TRUE(temporaryAllocations.peekIsEmpty());
714+
EXPECT_EQ(2u, svmAllocation->gpuAllocation->getTaskCount(osContextId));
715+
700716
status = clReleaseMemObject(buffer);
701717
ASSERT_EQ(CL_SUCCESS, status);
702718
status = clMemFreeINTEL(clContext, sharedMemory);
703719
ASSERT_EQ(CL_SUCCESS, status);
704720
clReleaseCommandQueue(commandQueue);
705721
}
706722

707-
TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithLocalMemoryWhenPointerIsUsedAsWriteBufferSourceThenUSMAllocationIsReused) {
723+
TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithLocalMemoryWhenPointerIsUsedAsTransferParameterThenUSMAllocationIsReused) {
708724
DebugManagerStateRestore restore;
709725
DebugManager.flags.EnableLocalMemory.set(1);
710726

@@ -737,6 +753,11 @@ TEST(UnfiedSharedMemoryTransferCalls, givenSharedUSMllocationWithLocalMemoryWhen
737753

738754
EXPECT_EQ(2u, svmAllocation->cpuAllocation->getTaskCount(osContextId));
739755

756+
status = clEnqueueReadBuffer(commandQueue, buffer, false, 0u, 4096u, sharedMemory, 0u, nullptr, nullptr);
757+
ASSERT_EQ(CL_SUCCESS, status);
758+
EXPECT_TRUE(temporaryAllocations.peekIsEmpty());
759+
EXPECT_EQ(3u, svmAllocation->cpuAllocation->getTaskCount(osContextId));
760+
740761
status = clReleaseMemObject(buffer);
741762
ASSERT_EQ(CL_SUCCESS, status);
742763
status = clMemFreeINTEL(clContext, sharedMemory);

0 commit comments

Comments
 (0)