Skip to content

Commit 03252ee

Browse files
kgibalaCompute-Runtime-Automation
authored andcommitted
Add support for write combined in unified memory
Related-To: NEO-3374 Change-Id: I610ad2d71b056f2bc5b8f4bda72e7f08a45cf59d Signed-off-by: Krzysztof Gibala <[email protected]>
1 parent 2c84c14 commit 03252ee

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

core/memory_manager/unified_memory_manager.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,17 @@ void *SVMAllocsManager::createSVMAlloc(uint32_t rootDeviceIndex, size_t size, co
9595
void *SVMAllocsManager::createUnifiedMemoryAllocation(uint32_t rootDeviceIndex, size_t size, const UnifiedMemoryProperties &memoryProperties) {
9696
size_t alignedSize = alignUp<size_t>(size, MemoryConstants::pageSize64k);
9797

98+
GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
99+
if (memoryProperties.memoryType == InternalMemoryType::DEVICE_UNIFIED_MEMORY) {
100+
if (memoryProperties.allocationFlags.allocFlags.allocWriteCombined) {
101+
allocationType = GraphicsAllocation::AllocationType::WRITE_COMBINED;
102+
} else {
103+
allocationType = GraphicsAllocation::AllocationType::BUFFER;
104+
}
105+
}
98106
AllocationProperties unifiedMemoryProperties{rootDeviceIndex, true,
99107
alignedSize,
100-
memoryProperties.memoryType == InternalMemoryType::DEVICE_UNIFIED_MEMORY ? GraphicsAllocation::AllocationType::BUFFER : GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY,
108+
allocationType,
101109
false};
102110

103111
GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties);

runtime/api/api.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,6 +3457,11 @@ void *clHostMemAllocINTEL(
34573457
return nullptr;
34583458
}
34593459

3460+
if (isValueSet(unifiedMemoryProperties.allocationFlags.allAllocFlags, CL_MEM_ALLOC_WRITE_COMBINED_INTEL)) {
3461+
err.set(CL_INVALID_VALUE);
3462+
return nullptr;
3463+
}
3464+
34603465
return neoContext->getSVMAllocsManager()->createUnifiedMemoryAllocation(neoContext->getDevice(0)->getRootDeviceIndex(), size, unifiedMemoryProperties);
34613466
}
34623467

@@ -3531,6 +3536,11 @@ void *clSharedMemAllocINTEL(
35313536
}
35323537
unifiedMemoryProperties.device = device;
35333538

3539+
if (isValueSet(unifiedMemoryProperties.allocationFlags.allAllocFlags, CL_MEM_ALLOC_WRITE_COMBINED_INTEL)) {
3540+
err.set(CL_INVALID_VALUE);
3541+
return nullptr;
3542+
}
3543+
35343544
return neoContext->getSVMAllocsManager()->createSharedUnifiedMemoryAllocation(neoContext->getDevice(0)->getRootDeviceIndex(), size, unifiedMemoryProperties, neoContext->getSpecialQueue());
35353545
}
35363546

unit_tests/api/cl_unified_shared_memory_tests.inl

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ TEST(clUnifiedSharedMemoryTests, whenHostMemAllocWithInvalidPropertiesTokenThenE
230230
EXPECT_EQ(CL_INVALID_VALUE, retVal);
231231
}
232232

233+
TEST(clUnifiedSharedMemoryTests, whenHostMemAllocWithInvalidWriteCombinedTokenThenErrorIsReturned) {
234+
MockContext mockContext;
235+
cl_int retVal = CL_SUCCESS;
236+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
237+
238+
auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, properties, 4, 0, &retVal);
239+
240+
EXPECT_EQ(nullptr, unifiedMemoryHostAllocation);
241+
EXPECT_EQ(CL_INVALID_VALUE, retVal);
242+
}
243+
233244
TEST(clUnifiedSharedMemoryTests, whenDeviceMemAllocWithInvalidPropertiesTokenThenErrorIsReturned) {
234245
MockContext mockContext;
235246
cl_int retVal = CL_SUCCESS;
@@ -253,6 +264,17 @@ TEST(clUnifiedSharedMemoryTests, whenSharedMemAllocWithInvalidPropertiesTokenThe
253264
EXPECT_EQ(CL_INVALID_VALUE, retVal);
254265
}
255266

267+
TEST(clUnifiedSharedMemoryTests, whenSharedMemAllocWithInvalidWriteCombinedTokenThenErrorIsReturned) {
268+
MockContext mockContext;
269+
cl_int retVal = CL_SUCCESS;
270+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
271+
272+
auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, 4, 0, &retVal);
273+
274+
EXPECT_EQ(nullptr, unifiedMemorySharedAllocation);
275+
EXPECT_EQ(CL_INVALID_VALUE, retVal);
276+
}
277+
256278
TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocWithoutPropertiesWhenGetMemAllocFlagsThenDefaultValueIsReturned) {
257279
uint64_t defaultValue = CL_MEM_ALLOC_DEFAULT_INTEL;
258280
MockContext mockContext;
@@ -277,7 +299,7 @@ TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocTypeIsCalledWithValidUnifiedMe
277299
size_t paramValueSize = sizeof(cl_mem_properties_intel);
278300
cl_mem_properties_intel paramValue = 0;
279301
size_t paramValueSizeRet = 0;
280-
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
302+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_DEFAULT_INTEL, 0};
281303

282304
auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, properties, 4, 0, &retVal);
283305

@@ -313,7 +335,7 @@ TEST(clUnifiedSharedMemoryTests, whenClGetMemAllocTypeIsCalledWithValidUnifiedMe
313335
size_t paramValueSize = sizeof(cl_mem_properties_intel);
314336
cl_mem_properties_intel paramValue = 0;
315337
size_t paramValueSizeRet = 0;
316-
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
338+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_DEFAULT_INTEL, 0};
317339

318340
auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, 4, 0, &retVal);
319341

@@ -760,3 +782,56 @@ TEST_F(clUnifiedSharedMemoryEventTests, whenClEnqueueMemFillINTELIsCalledWithEve
760782
EXPECT_EQ(expectedCmd, actualCmd);
761783
clMemFreeINTEL(this->context, unfiedMemorySharedAllocation);
762784
}
785+
786+
TEST(clUnifiedSharedMemoryTests, givenDefaulMemPropertiesWhenClDeviceMemAllocIntelIsCalledThenItAllocatesDeviceUnifiedMemoryAllocationWithProperAllocationTypeAndSize) {
787+
MockContext mockContext;
788+
cl_int retVal = CL_SUCCESS;
789+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_DEFAULT_INTEL, 0};
790+
auto allocationSize = 4000u;
791+
auto unfiedMemoryDeviceAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal);
792+
EXPECT_EQ(CL_SUCCESS, retVal);
793+
ASSERT_NE(nullptr, unfiedMemoryDeviceAllocation);
794+
795+
auto allocationsManager = mockContext.getSVMAllocsManager();
796+
EXPECT_EQ(1u, allocationsManager->getNumAllocs());
797+
auto graphicsAllocation = allocationsManager->getSVMAlloc(unfiedMemoryDeviceAllocation);
798+
EXPECT_EQ(graphicsAllocation->size, allocationSize);
799+
EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::DEVICE_UNIFIED_MEMORY);
800+
EXPECT_EQ(GraphicsAllocation::AllocationType::BUFFER, graphicsAllocation->gpuAllocation->getAllocationType());
801+
EXPECT_EQ(graphicsAllocation->gpuAllocation->getGpuAddress(), castToUint64(unfiedMemoryDeviceAllocation));
802+
EXPECT_EQ(alignUp(allocationSize, MemoryConstants::pageSize64k), graphicsAllocation->gpuAllocation->getUnderlyingBufferSize());
803+
804+
retVal = clMemFreeINTEL(&mockContext, unfiedMemoryDeviceAllocation);
805+
EXPECT_EQ(CL_SUCCESS, retVal);
806+
}
807+
808+
TEST(clUnifiedSharedMemoryTests, givenValidMemPropertiesWhenClDeviceMemAllocIntelIsCalledThenItAllocatesDeviceUnifiedMemoryAllocationWithProperAllocationTypeAndSize) {
809+
MockContext mockContext;
810+
cl_int retVal = CL_SUCCESS;
811+
auto allocationSize = 4000u;
812+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_FLAGS_INTEL, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
813+
auto unfiedMemoryDeviceAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal);
814+
EXPECT_EQ(CL_SUCCESS, retVal);
815+
ASSERT_NE(nullptr, unfiedMemoryDeviceAllocation);
816+
817+
auto allocationsManager = mockContext.getSVMAllocsManager();
818+
EXPECT_EQ(1u, allocationsManager->getNumAllocs());
819+
auto graphicsAllocation = allocationsManager->getSVMAlloc(unfiedMemoryDeviceAllocation);
820+
EXPECT_EQ(graphicsAllocation->size, allocationSize);
821+
EXPECT_EQ(graphicsAllocation->memoryType, InternalMemoryType::DEVICE_UNIFIED_MEMORY);
822+
EXPECT_EQ(graphicsAllocation->gpuAllocation->getAllocationType(), GraphicsAllocation::AllocationType::WRITE_COMBINED);
823+
EXPECT_EQ(graphicsAllocation->gpuAllocation->getGpuAddress(), castToUint64(unfiedMemoryDeviceAllocation));
824+
EXPECT_EQ(alignUp(allocationSize, MemoryConstants::pageSize64k), graphicsAllocation->gpuAllocation->getUnderlyingBufferSize());
825+
826+
retVal = clMemFreeINTEL(&mockContext, unfiedMemoryDeviceAllocation);
827+
EXPECT_EQ(CL_SUCCESS, retVal);
828+
}
829+
830+
TEST(clUnifiedSharedMemoryTests, givenInvalidMemPropertiesWhenClSharedMemAllocIntelIsCalledThenInvalidValueIsReturned) {
831+
MockContext mockContext;
832+
cl_int retVal = CL_SUCCESS;
833+
cl_mem_properties_intel properties[] = {CL_MEM_ALLOC_WRITE_COMBINED_INTEL, 0};
834+
auto unfiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, 4, 0, &retVal);
835+
EXPECT_EQ(CL_INVALID_VALUE, retVal);
836+
EXPECT_EQ(nullptr, unfiedMemorySharedAllocation);
837+
}

unit_tests/memory_manager/unified_memory_manager_tests.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,33 @@ TEST_F(SVMMemoryAllocatorTest, whenCoherentFlagIsPassedThenAllocationIsCoherent)
165165
svmManager->freeSVMAlloc(ptr);
166166
}
167167

168-
TEST_F(SVMLocalMemoryAllocatorTest, whenDeviceAllocationIsCreatedThenItIsStoredWithProperTypeInAllocationMap) {
168+
TEST_F(SVMLocalMemoryAllocatorTest, whenDeviceAllocationIsCreatedThenItIsStoredWithWriteCombinedTypeInAllocationMap) {
169169
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties;
170170
unifiedMemoryProperties.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
171+
unifiedMemoryProperties.allocationFlags.allocFlags.allocWriteCombined = true;
172+
auto allocationSize = 4000u;
173+
auto ptr = svmManager->createUnifiedMemoryAllocation(0, 4000u, unifiedMemoryProperties);
174+
EXPECT_NE(nullptr, ptr);
175+
auto allocation = svmManager->getSVMAlloc(ptr);
176+
EXPECT_EQ(nullptr, allocation->cpuAllocation);
177+
EXPECT_NE(nullptr, allocation->gpuAllocation);
178+
EXPECT_EQ(InternalMemoryType::DEVICE_UNIFIED_MEMORY, allocation->memoryType);
179+
EXPECT_EQ(allocationSize, allocation->size);
180+
EXPECT_EQ(allocation->gpuAllocation->getMemoryPool(), MemoryPool::LocalMemory);
181+
182+
EXPECT_EQ(alignUp(allocationSize, MemoryConstants::pageSize64k), allocation->gpuAllocation->getUnderlyingBufferSize());
183+
EXPECT_EQ(GraphicsAllocation::AllocationType::WRITE_COMBINED, allocation->gpuAllocation->getAllocationType());
184+
185+
svmManager->freeSVMAlloc(ptr);
186+
}
187+
188+
TEST_F(SVMMemoryAllocatorTest, givenNoWriteCombinedFlagwhenDeviceAllocationIsCreatedThenItIsStoredWithProperTypeInAllocationMap) {
189+
if (is32bit) {
190+
GTEST_SKIP();
191+
}
192+
SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties;
193+
unifiedMemoryProperties.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
194+
unifiedMemoryProperties.allocationFlags.allocFlags.allocWriteCombined = false;
171195
auto allocationSize = 4096u;
172196
auto ptr = svmManager->createUnifiedMemoryAllocation(0, 4096u, unifiedMemoryProperties);
173197
EXPECT_NE(nullptr, ptr);
@@ -176,7 +200,6 @@ TEST_F(SVMLocalMemoryAllocatorTest, whenDeviceAllocationIsCreatedThenItIsStoredW
176200
EXPECT_NE(nullptr, allocation->gpuAllocation);
177201
EXPECT_EQ(InternalMemoryType::DEVICE_UNIFIED_MEMORY, allocation->memoryType);
178202
EXPECT_EQ(allocationSize, allocation->size);
179-
EXPECT_EQ(allocation->gpuAllocation->getMemoryPool(), MemoryPool::LocalMemory);
180203

181204
EXPECT_EQ(alignUp(allocationSize, MemoryConstants::pageSize64k), allocation->gpuAllocation->getUnderlyingBufferSize());
182205
EXPECT_EQ(GraphicsAllocation::AllocationType::BUFFER, allocation->gpuAllocation->getAllocationType());

0 commit comments

Comments
 (0)