Skip to content

Commit 0634aa3

Browse files
Create resource with given address
Signed-off-by: Lukasz Jobczyk <[email protected]>
1 parent 4cdc4ff commit 0634aa3

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

opencl/extensions/public/cl_ext_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -99,6 +99,7 @@ using cl_unified_shared_memory_capabilities_intel = cl_bitfield;
9999

100100
/* cl_mem_properties_intel */
101101
#define CL_MEM_ALLOC_FLAGS_INTEL 0x4195
102+
#define CL_MEM_ALLOC_USE_HOST_PTR_INTEL 0x1000F
102103

103104
/* cl_mem_alloc_flags_intel - bitfield */
104105
#define CL_MEM_ALLOC_DEFAULT_INTEL 0

opencl/source/helpers/cl_memory_properties_helpers.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Intel Corporation
2+
* Copyright (C) 2021-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -16,7 +16,7 @@ bool ClMemoryPropertiesHelper::parseMemoryProperties(const cl_mem_properties_int
1616
cl_mem_flags &flags, cl_mem_flags_intel &flagsIntel,
1717
cl_mem_alloc_flags_intel &allocflags, MemoryPropertiesHelper::ObjType objectType, Context &context) {
1818
Device *pDevice = &context.getDevice(0)->getDevice();
19-
19+
uintptr_t hostptr = 0;
2020
if (properties != nullptr) {
2121
for (int i = 0; properties[i] != 0; i += 2) {
2222
switch (properties[i]) {
@@ -29,13 +29,17 @@ bool ClMemoryPropertiesHelper::parseMemoryProperties(const cl_mem_properties_int
2929
case CL_MEM_ALLOC_FLAGS_INTEL:
3030
allocflags |= static_cast<cl_mem_alloc_flags_intel>(properties[i + 1]);
3131
break;
32+
case CL_MEM_ALLOC_USE_HOST_PTR_INTEL:
33+
hostptr = static_cast<uintptr_t>(properties[i + 1]);
34+
break;
3235
default:
3336
return false;
3437
}
3538
}
3639
}
3740

3841
memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, allocflags, pDevice);
42+
memoryProperties.hostptr = hostptr;
3943

4044
switch (objectType) {
4145
case MemoryPropertiesHelper::ObjType::BUFFER:

opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4792,6 +4792,19 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerWithoutLocalMemoryAndCpuPtrWhe
47924792
memoryManager.freeGraphicsMemory(allocation);
47934793
}
47944794

4795+
TEST_F(DrmMemoryManagerTest, givenNullDefaultAllocWhenCreateGraphicsAllocationFromExistingStorageThenDoNotImportHandle) {
4796+
TestedDrmMemoryManager memoryManager(false, false, false, *executionEnvironment);
4797+
mock->ioctl_expected.primeFdToHandle = 0;
4798+
4799+
MockAllocationProperties properties(0u, 1u);
4800+
MultiGraphicsAllocation allocation(0u);
4801+
auto alloc = memoryManager.createGraphicsAllocationFromExistingStorage(properties, nullptr, allocation);
4802+
4803+
EXPECT_NE(alloc, nullptr);
4804+
4805+
memoryManager.freeGraphicsMemory(alloc);
4806+
}
4807+
47954808
TEST(DrmMemoryManagerSimpleTest, givenDrmMemoryManagerWhenAllocateInDevicePoolIsCalledThenNullptrAndStatusRetryIsReturned) {
47964809
MockExecutionEnvironment executionEnvironment(defaultHwInfo.get());
47974810
executionEnvironment.rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>();

shared/source/memory_manager/memory_manager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ GraphicsAllocation *MemoryManager::createPaddedAllocation(GraphicsAllocation *in
147147
return allocateGraphicsMemoryWithProperties({inputGraphicsAllocation->getRootDeviceIndex(), sizeWithPadding, AllocationType::INTERNAL_HOST_MEMORY, systemMemoryBitfield});
148148
}
149149

150-
void *MemoryManager::createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation) {
151-
void *ptr = nullptr;
152-
150+
void *MemoryManager::createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation, void *ptr) {
153151
properties.flags.forceSystemMemory = true;
154152
for (auto &rootDeviceIndex : rootDeviceIndices) {
155153
if (multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex)) {
@@ -528,7 +526,8 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &
528526
if (allocationData.flags.shareable || allocationData.flags.isUSMDeviceMemory) {
529527
return allocateMemoryByKMD(allocationData);
530528
}
531-
if (useNonSvmHostPtrAlloc(allocationData.type, allocationData.rootDeviceIndex) || isNonSvmBuffer(allocationData.hostPtr, allocationData.type, allocationData.rootDeviceIndex)) {
529+
if (((false == allocationData.flags.isUSMHostAllocation) || (nullptr == allocationData.hostPtr)) &&
530+
(useNonSvmHostPtrAlloc(allocationData.type, allocationData.rootDeviceIndex) || isNonSvmBuffer(allocationData.hostPtr, allocationData.type, allocationData.rootDeviceIndex))) {
532531
auto allocation = allocateGraphicsMemoryForNonSvmHostPtr(allocationData);
533532
if (allocation) {
534533
allocation->setFlushL3Required(allocationData.flags.flushL3);

shared/source/memory_manager/memory_manager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ class MemoryManager {
116116
GraphicsAllocation *createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding);
117117
virtual GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding);
118118

119-
MOCKABLE_VIRTUAL void *createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation);
119+
MOCKABLE_VIRTUAL void *createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation, void *ptr);
120+
MOCKABLE_VIRTUAL void *createMultiGraphicsAllocationInSystemMemoryPool(std::vector<uint32_t> &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation) {
121+
return createMultiGraphicsAllocationInSystemMemoryPool(rootDeviceIndices, properties, multiGraphicsAllocation, nullptr);
122+
}
120123
virtual GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation);
121124

122125
virtual AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) = 0;

shared/source/memory_manager/unified_memory_manager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ void *SVMAllocsManager::createHostUnifiedMemoryAllocation(size_t size,
149149

150150
auto maxRootDeviceIndex = *std::max_element(rootDeviceIndicesVector.begin(), rootDeviceIndicesVector.end(), std::less<uint32_t const>());
151151
SvmAllocationData allocData(maxRootDeviceIndex);
152+
void *externalHostPointer = reinterpret_cast<void *>(memoryProperties.allocationFlags.hostptr);
152153

153-
void *usmPtr = memoryManager->createMultiGraphicsAllocationInSystemMemoryPool(rootDeviceIndicesVector, unifiedMemoryProperties, allocData.gpuAllocations);
154+
void *usmPtr = memoryManager->createMultiGraphicsAllocationInSystemMemoryPool(rootDeviceIndicesVector, unifiedMemoryProperties, allocData.gpuAllocations, externalHostPointer);
154155
if (!usmPtr) {
155156
return nullptr;
156157
}

shared/source/memory_properties/memory_properties_flags.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 Intel Corporation
2+
* Copyright (C) 2019-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -22,6 +22,7 @@ struct MemoryProperties {
2222
MemoryAllocFlags allocFlags;
2323
uint32_t allAllocFlags = 0;
2424
};
25+
uintptr_t hostptr = 0;
2526
static_assert(sizeof(MemoryProperties::flags) == sizeof(MemoryProperties::allFlags) && sizeof(MemoryProperties::allocFlags) == sizeof(MemoryProperties::allAllocFlags), "");
2627
};
2728
} // namespace NEO

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ void DrmMemoryManager::handleFenceCompletion(GraphicsAllocation *allocation) {
826826

827827
GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) {
828828
auto defaultAlloc = multiGraphicsAllocation.getDefaultGraphicsAllocation();
829-
if (static_cast<DrmAllocation *>(defaultAlloc)->getMmapPtr()) {
829+
if (defaultAlloc && static_cast<DrmAllocation *>(defaultAlloc)->getMmapPtr()) {
830830
properties.size = defaultAlloc->getUnderlyingBufferSize();
831831
properties.gpuAddress = castToUint64(ptr);
832832

0 commit comments

Comments
 (0)