Skip to content

Commit a661f4b

Browse files
Reuse hostPtr allocations
Change-Id: Ie7e24e6630b26809fac1215b66cd90b3cafda53f Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>
1 parent e96981b commit a661f4b

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

level_zero/core/source/cmdlist/cmdlist.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ void CommandList::removeHostPtrAllocations() {
3838
hostPtrMap.clear();
3939
}
4040

41+
NEO::GraphicsAllocation *CommandList::getAllocationFromHostPtrMap(const void *buffer, uint64_t bufferSize) {
42+
auto allocation = hostPtrMap.lower_bound(buffer);
43+
if (allocation != hostPtrMap.end()) {
44+
if (buffer == allocation->first && ptrOffset(allocation->first, allocation->second->getUnderlyingBufferSize()) >= ptrOffset(buffer, bufferSize)) {
45+
return allocation->second;
46+
}
47+
}
48+
if (allocation != hostPtrMap.begin()) {
49+
allocation--;
50+
if (ptrOffset(allocation->first, allocation->second->getUnderlyingBufferSize()) >= ptrOffset(buffer, bufferSize)) {
51+
return allocation->second;
52+
}
53+
}
54+
return nullptr;
55+
}
56+
57+
NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset) {
58+
NEO::GraphicsAllocation *alloc = getAllocationFromHostPtrMap(buffer, bufferSize);
59+
if (alloc) {
60+
*offset += ptrDiff(buffer, alloc->getUnderlyingBuffer());
61+
return alloc;
62+
}
63+
alloc = device->allocateMemoryFromHostPtr(buffer, bufferSize);
64+
hostPtrMap.insert(std::make_pair(buffer, alloc));
65+
return alloc;
66+
}
67+
4168
void CommandList::removeDeallocationContainerData() {
4269
auto memoryManager = device ? device->getNEODevice()->getMemoryManager() : nullptr;
4370

level_zero/core/source/cmdlist/cmdlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ struct CommandList : _ze_command_list_handle_t {
181181
bool isCopyOnlyCmdList = false;
182182
UnifiedMemoryControls unifiedMemoryControls;
183183
bool indirectAllocationsAllowed = false;
184+
NEO::GraphicsAllocation *getAllocationFromHostPtrMap(const void *buffer, uint64_t bufferSize);
185+
NEO::GraphicsAllocation *getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset);
184186
};
185187

186188
using CommandListAllocatorFn = CommandList *(*)(uint32_t);

level_zero/core/source/cmdlist/cmdlist_hw.inl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,9 @@ inline AlignedAllocationData CommandListCoreFamily<gfxCoreFamily>::getAlignedAll
12091209
bool hostPointerNeedsFlush = false;
12101210

12111211
if (srcAllocFound == false) {
1212-
alloc = device->allocateMemoryFromHostPtr(buffer, bufferSize);
1213-
hostPtrMap.insert(std::make_pair(buffer, alloc));
1212+
alloc = getHostPtrAlloc(buffer, bufferSize, &offset);
12141213

1215-
alignedPtr = static_cast<uintptr_t>(alloc->getGpuAddress() - offset);
1214+
alignedPtr = static_cast<uintptr_t>(alignDown(alloc->getGpuAddress(), NEO::EncodeSurfaceState<GfxFamily>::getSurfaceBaseAddressAlignment()));
12161215
hostPointerNeedsFlush = true;
12171216
} else {
12181217
alloc = allocData->gpuAllocation;

level_zero/core/test/unit_tests/mocks/mock_cmdlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>
4141
using BaseClass::appendSignalEventPostWalker;
4242
using BaseClass::commandListPreemptionMode;
4343
using BaseClass::getAlignedAllocation;
44+
using BaseClass::getAllocationFromHostPtrMap;
45+
using BaseClass::getHostPtrAlloc;
4446
using BaseClass::hostPtrMap;
4547

4648
WhiteBox() : ::L0::CommandListCoreFamily<gfxCoreFamily>(BaseClass::defaultNumIddsPerBlock) {}

level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,5 +1070,118 @@ HWTEST2_F(CommandListCreate, givenCopyOnlyCommandListWhenAppendBlitFillCalledWit
10701070
EXPECT_EQ(allocValue, pattern[i % 4]);
10711071
}
10721072
}
1073+
1074+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGettingAllocInRangeThenAllocFromMapReturned, Platforms) {
1075+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1076+
commandList->initialize(device, true);
1077+
uint64_t gpuAddress = 0x1200;
1078+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1079+
size_t allocSize = 0x1000;
1080+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1081+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1082+
1083+
auto newBufferPtr = ptrOffset(cpuPtr, 0x10);
1084+
auto newBufferSize = allocSize - 0x20;
1085+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1086+
EXPECT_NE(newAlloc, nullptr);
1087+
commandList->hostPtrMap.clear();
1088+
}
1089+
1090+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenSizeIsOutOfRangeThenNullPtrReturned, Platforms) {
1091+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1092+
commandList->initialize(device, true);
1093+
uint64_t gpuAddress = 0x1200;
1094+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1095+
size_t allocSize = 0x1000;
1096+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1097+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1098+
1099+
auto newBufferPtr = ptrOffset(cpuPtr, 0x10);
1100+
auto newBufferSize = allocSize + 0x20;
1101+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1102+
EXPECT_EQ(newAlloc, nullptr);
1103+
commandList->hostPtrMap.clear();
1104+
}
1105+
1106+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsOutOfRangeThenNullPtrReturned, Platforms) {
1107+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1108+
commandList->initialize(device, true);
1109+
uint64_t gpuAddress = 0x1200;
1110+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1111+
size_t allocSize = 0x1000;
1112+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1113+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1114+
1115+
auto newBufferPtr = reinterpret_cast<const void *>(gpuAddress - 0x100);
1116+
auto newBufferSize = allocSize - 0x200;
1117+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1118+
EXPECT_EQ(newAlloc, nullptr);
1119+
commandList->hostPtrMap.clear();
1120+
}
1121+
1122+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenGetHostPtrAllocCalledThenCorrectOffsetIsSet, Platforms) {
1123+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1124+
commandList->initialize(device, true);
1125+
uint64_t gpuAddress = 0x1200;
1126+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1127+
size_t allocSize = 0x1000;
1128+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1129+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1130+
size_t expectedOffset = 0x10;
1131+
auto newBufferPtr = ptrOffset(cpuPtr, expectedOffset);
1132+
auto newBufferSize = allocSize - 0x20;
1133+
size_t offset = 0;
1134+
auto newAlloc = commandList->getHostPtrAlloc(newBufferPtr, newBufferSize, &offset);
1135+
EXPECT_NE(newAlloc, nullptr);
1136+
EXPECT_EQ(offset, expectedOffset);
1137+
commandList->hostPtrMap.clear();
1138+
}
1139+
1140+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapThenAllocationReturned, Platforms) {
1141+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1142+
commandList->initialize(device, true);
1143+
uint64_t gpuAddress = 0x1200;
1144+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1145+
size_t allocSize = 0x1000;
1146+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1147+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1148+
1149+
auto newBufferPtr = cpuPtr;
1150+
auto newBufferSize = allocSize - 0x20;
1151+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1152+
EXPECT_EQ(newAlloc, &alloc);
1153+
commandList->hostPtrMap.clear();
1154+
}
1155+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrIsInMapButWithBiggerSizeThenNullPtrReturned, Platforms) {
1156+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1157+
commandList->initialize(device, true);
1158+
uint64_t gpuAddress = 0x1200;
1159+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1160+
size_t allocSize = 0x1000;
1161+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1162+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1163+
1164+
auto newBufferPtr = cpuPtr;
1165+
auto newBufferSize = allocSize + 0x20;
1166+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1167+
EXPECT_EQ(newAlloc, nullptr);
1168+
commandList->hostPtrMap.clear();
1169+
}
1170+
HWTEST2_F(CommandListCreate, givenHostAllocInMapWhenPtrLowerThanAnyInMapThenNullPtrReturned, Platforms) {
1171+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1172+
commandList->initialize(device, true);
1173+
uint64_t gpuAddress = 0x1200;
1174+
const void *cpuPtr = reinterpret_cast<const void *>(gpuAddress);
1175+
size_t allocSize = 0x1000;
1176+
NEO::MockGraphicsAllocation alloc(const_cast<void *>(cpuPtr), gpuAddress, allocSize);
1177+
commandList->hostPtrMap.insert(std::make_pair(cpuPtr, &alloc));
1178+
1179+
auto newBufferPtr = reinterpret_cast<const void *>(gpuAddress - 0x10);
1180+
auto newBufferSize = allocSize - 0x20;
1181+
auto newAlloc = commandList->getAllocationFromHostPtrMap(newBufferPtr, newBufferSize);
1182+
EXPECT_EQ(newAlloc, nullptr);
1183+
commandList->hostPtrMap.clear();
1184+
}
1185+
10731186
} // namespace ult
10741187
} // namespace L0

0 commit comments

Comments
 (0)