Skip to content

Commit bd9cd46

Browse files
Unified memory sharing 10/n
Enable creating allocations from non-NT handles Change-Id: Ifd8c67dfd5624182aed76457b1d80bcc2659dd45 Signed-off-by: Maciej Dziuban <[email protected]> Related-To: NEO-3771
1 parent 9562daa commit bd9cd46

File tree

7 files changed

+89
-26
lines changed

7 files changed

+89
-26
lines changed

runtime/sharings/unified/unified_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using namespace NEO;
2020
Buffer *UnifiedBuffer::createSharedUnifiedBuffer(Context *context, cl_mem_flags flags, UnifiedSharingMemoryDescription extMem, cl_int *errcodeRet) {
2121
ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS);
2222

23-
auto graphicsAllocation = UnifiedBuffer::createGraphicsAllocation(context, extMem);
23+
auto graphicsAllocation = UnifiedBuffer::createGraphicsAllocation(context, extMem, GraphicsAllocation::AllocationType::SHARED_BUFFER);
2424
if (!graphicsAllocation) {
2525
errorCode.set(CL_INVALID_MEM_OBJECT);
2626
return nullptr;

runtime/sharings/unified/unified_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Image *UnifiedImage::createSharedUnifiedImage(Context *context, cl_mem_flags fla
2929
imgInfo.imgDesc = Image::convertDescriptor(*imageDesc);
3030
imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat;
3131

32-
GraphicsAllocation *graphicsAllocation = createGraphicsAllocation(context, description);
32+
GraphicsAllocation *graphicsAllocation = createGraphicsAllocation(context, description, GraphicsAllocation::AllocationType::SHARED_IMAGE);
3333
if (!graphicsAllocation) {
3434
errorCode.set(CL_INVALID_MEM_OBJECT);
3535
return nullptr;

runtime/sharings/unified/unified_sharing.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ void UnifiedSharing::synchronizeObject(UpdateData &updateData) {
3030
void UnifiedSharing::releaseResource(MemObj *memObject) {
3131
}
3232

33-
GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description) {
33+
GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description, GraphicsAllocation::AllocationType allocationType) {
34+
auto memoryManager = context->getMemoryManager();
3435
switch (description.type) {
3536
case UnifiedSharingHandleType::Win32Nt: {
36-
auto graphicsAllocation = context->getMemoryManager()->createGraphicsAllocationFromNTHandle(description.handle, 0u);
37-
return graphicsAllocation;
37+
return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, 0u);
38+
}
39+
case UnifiedSharingHandleType::LinuxFd:
40+
case UnifiedSharingHandleType::Win32Shared: {
41+
const AllocationProperties properties{0u, false, 0u, allocationType, false};
42+
return memoryManager->createGraphicsAllocationFromSharedHandle(((osHandle)(uint64_t)description.handle), properties, false);
3843
}
3944
default:
4045
return nullptr;

runtime/sharings/unified/unified_sharing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class UnifiedSharing : public SharingHandler {
3939
void synchronizeObject(UpdateData &updateData) override;
4040
void releaseResource(MemObj *memObject) override;
4141

42-
static GraphicsAllocation *createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description);
42+
static GraphicsAllocation *createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description, GraphicsAllocation::AllocationType allocationType);
4343

4444
private:
4545
UnifiedSharingFunctions *sharingFunctions;

unit_tests/sharings/unified/unified_sharing_buffer_tests.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Intel Corporation
2+
* Copyright (C) 2019-2020 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -57,15 +57,7 @@ TEST_F(UnifiedSharingBufferTestsWithMemoryManager, givenUnsupportedHandleTypeWhe
5757
desc.handle = reinterpret_cast<void *>(0x1234);
5858

5959
auto buffer = std::unique_ptr<Buffer>(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal));
60-
ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal);
61-
62-
desc.type = UnifiedSharingHandleType::Win32Shared;
63-
buffer = std::unique_ptr<Buffer>(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal));
64-
ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal);
65-
66-
desc.type = UnifiedSharingHandleType::LinuxFd;
67-
buffer = std::unique_ptr<Buffer>(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal));
68-
ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal);
60+
EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal);
6961
}
7062

7163
TEST_F(UnifiedSharingBufferTestsWithMemoryManager, givenValidContextAndMemoryManagerWhenCreatingBufferFromSharedHandleThenReturnSuccess) {

unit_tests/sharings/unified/unified_sharing_image_tests.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenUnsupportedHandleTypeWhen
6666
auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc,
6767
&format, &imageDesc, &retVal));
6868
EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal);
69-
70-
desc.type = UnifiedSharingHandleType::Win32Shared;
71-
image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc,
72-
&format, &imageDesc, &retVal));
73-
EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal);
74-
75-
desc.type = UnifiedSharingHandleType::LinuxFd;
76-
image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc,
77-
&format, &imageDesc, &retVal));
78-
EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal);
7969
}
8070

8171
TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenValidContextAndMemoryManagerWhenCreatingImageFromSharedHandleThenReturnSuccess) {

unit_tests/sharings/unified/unified_sharing_tests.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,79 @@ TEST_F(UnifiedSharingTestsWithMemoryManager, givenUnifiedSharingHandlerWhenAcqui
162162
sharingHandler->release(buffer.get());
163163
EXPECT_EQ(1u, sharingHandler->releaseResourceCalled);
164164
}
165+
166+
struct UnifiedSharingCreateAllocationTests : UnifiedSharingTestsWithMemoryManager {
167+
struct MemoryManagerCheckingAllocationMethod : MockMemoryManager {
168+
using MockMemoryManager::MockMemoryManager;
169+
170+
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override {
171+
this->createFromNTHandleCalled = true;
172+
this->handle = (osHandle)(uint64_t)handle;
173+
return nullptr;
174+
}
175+
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) override {
176+
this->createFromSharedHandleCalled = true;
177+
this->handle = handle;
178+
this->properties = std::make_unique<AllocationProperties>(properties);
179+
return nullptr;
180+
}
181+
182+
bool createFromNTHandleCalled = false;
183+
bool createFromSharedHandleCalled = false;
184+
osHandle handle;
185+
std::unique_ptr<AllocationProperties> properties;
186+
};
187+
188+
struct MockSharingHandler : UnifiedSharing {
189+
using UnifiedSharing::createGraphicsAllocation;
190+
};
191+
192+
void SetUp() override {
193+
UnifiedSharingTestsWithMemoryManager::SetUp();
194+
this->memoryManager = std::make_unique<MemoryManagerCheckingAllocationMethod>();
195+
this->memoryManagerBackup = std::make_unique<VariableBackup<MemoryManager *>>(&this->context->memoryManager, this->memoryManager.get());
196+
}
197+
198+
std::unique_ptr<MemoryManagerCheckingAllocationMethod> memoryManager;
199+
std::unique_ptr<VariableBackup<MemoryManager *>> memoryManagerBackup;
200+
};
201+
202+
TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsNtHandleWhenCreateGraphicsAllocationIsCalledThenUseNtHandleMethod) {
203+
UnifiedSharingMemoryDescription desc{};
204+
desc.handle = reinterpret_cast<void *>(0x1234);
205+
desc.type = UnifiedSharingHandleType::Win32Nt;
206+
GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE;
207+
MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType);
208+
209+
EXPECT_TRUE(memoryManager->createFromNTHandleCalled);
210+
EXPECT_FALSE(memoryManager->createFromSharedHandleCalled);
211+
EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle);
212+
}
213+
214+
TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) {
215+
UnifiedSharingMemoryDescription desc{};
216+
desc.handle = reinterpret_cast<void *>(0x1234);
217+
desc.type = UnifiedSharingHandleType::Win32Shared;
218+
GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE;
219+
MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType);
220+
221+
EXPECT_FALSE(memoryManager->createFromNTHandleCalled);
222+
EXPECT_TRUE(memoryManager->createFromSharedHandleCalled);
223+
EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle);
224+
const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false};
225+
EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags);
226+
}
227+
228+
TEST_F(UnifiedSharingCreateAllocationTests, givenLinuxSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) {
229+
UnifiedSharingMemoryDescription desc{};
230+
desc.handle = reinterpret_cast<void *>(0x1234);
231+
desc.type = UnifiedSharingHandleType::LinuxFd;
232+
GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE;
233+
MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType);
234+
235+
EXPECT_FALSE(memoryManager->createFromNTHandleCalled);
236+
EXPECT_TRUE(memoryManager->createFromSharedHandleCalled);
237+
EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle);
238+
const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false};
239+
EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags);
240+
}

0 commit comments

Comments
 (0)