Skip to content

Commit b73c757

Browse files
Add indirect allocations to residency at kernel submission time
Change-Id: Idc6ce7ac72de84107990a5c9786c868d4bfa4322 Signed-off-by: Raiyan Latif <raiyan.latif@intel.com>
1 parent cc49e79 commit b73c757

File tree

5 files changed

+123
-3
lines changed

5 files changed

+123
-3
lines changed

level_zero/core/source/cmdlist/cmdlist.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ struct CommandList : _ze_command_list_handle_t {
136136
return commandListPreemptionMode;
137137
}
138138

139+
UnifiedMemoryControls getUnifiedMemoryControls() const {
140+
return unifiedMemoryControls;
141+
}
142+
143+
bool hasIndirectAllocationsAllowed() const {
144+
return indirectAllocationsAllowed;
145+
}
146+
139147
NEO::PreemptionMode obtainFunctionPreemptionMode(Kernel *kernel);
140148

141149
std::vector<Kernel *> &getPrintfFunctionContainer() {
@@ -171,6 +179,8 @@ struct CommandList : _ze_command_list_handle_t {
171179
uint32_t commandListPerThreadScratchSize = 0u;
172180
NEO::PreemptionMode commandListPreemptionMode = NEO::PreemptionMode::Initial;
173181
bool isCopyOnlyCmdList = false;
182+
UnifiedMemoryControls unifiedMemoryControls;
183+
bool indirectAllocationsAllowed = false;
174184
};
175185

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

level_zero/core/source/cmdlist/cmdlist_hw_base.inl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::appendLaunchKernelWithParams(z
5050

5151
if (kernel->hasIndirectAllocationsAllowed()) {
5252
UnifiedMemoryControls unifiedMemoryControls = kernel->getUnifiedMemoryControls();
53-
auto svmAllocsManager = device->getDriverHandle()->getSvmAllocsManager();
54-
auto &residencyContainer = commandContainer.getResidencyContainer();
5553

56-
svmAllocsManager->addInternalAllocationsToResidencyContainer(residencyContainer, unifiedMemoryControls.generateMask());
54+
if (unifiedMemoryControls.indirectDeviceAllocationsAllowed) {
55+
this->unifiedMemoryControls.indirectDeviceAllocationsAllowed = true;
56+
}
57+
if (unifiedMemoryControls.indirectHostAllocationsAllowed) {
58+
this->unifiedMemoryControls.indirectHostAllocationsAllowed = true;
59+
}
60+
if (unifiedMemoryControls.indirectSharedAllocationsAllowed) {
61+
this->unifiedMemoryControls.indirectSharedAllocationsAllowed = true;
62+
}
63+
64+
this->indirectAllocationsAllowed = true;
5765
}
5866

5967
NEO::EncodeDispatchKernel<GfxFamily>::encode(commandContainer,

level_zero/core/source/cmdqueue/cmdqueue_hw.inl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#include "shared/source/helpers/interlocked_max.h"
2121
#include "shared/source/helpers/preamble.h"
2222
#include "shared/source/memory_manager/memory_manager.h"
23+
#include "shared/source/memory_manager/residency_container.h"
2324
#include "shared/source/os_interface/os_context.h"
2425
#include "shared/source/page_fault_manager/cpu_page_fault_manager.h"
26+
#include "shared/source/unified_memory/unified_memory.h"
2527

2628
#include "level_zero/core/source/cmdlist/cmdlist.h"
2729
#include "level_zero/core/source/cmdlist/cmdlist_hw.h"
@@ -114,6 +116,15 @@ ze_result_t CommandQueueHw<gfxCoreFamily>::executeCommandLists(
114116
for (auto i = 0u; i < numCommandLists; i++) {
115117
auto commandList = CommandList::fromHandle(phCommandLists[i]);
116118

119+
bool indirectAllocationsAllowed = commandList->hasIndirectAllocationsAllowed();
120+
if (indirectAllocationsAllowed) {
121+
UnifiedMemoryControls unifiedMemoryControls = commandList->getUnifiedMemoryControls();
122+
123+
auto svmAllocsManager = device->getDriverHandle()->getSvmAllocsManager();
124+
svmAllocsManager->addInternalAllocationsToResidencyContainer(commandList->commandContainer.getResidencyContainer(),
125+
unifiedMemoryControls.generateMask());
126+
}
127+
117128
totalCmdBuffers += commandList->commandContainer.getCmdBufferAllocations().size();
118129
spaceForResidency += commandList->commandContainer.getResidencyContainer().size();
119130
auto commandListPreemption = commandList->getCommandListPreemptionMode();

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ namespace ult {
2222

2323
using CommandListAppendLaunchKernel = Test<ModuleFixture>;
2424

25+
HWTEST_F(CommandListAppendLaunchKernel, givenKernelWithIndirectAllocationsAllowedThenCommandListReturnsExpectedIndirectAllocationsAllowed) {
26+
createKernel();
27+
kernel->unifiedMemoryControls.indirectDeviceAllocationsAllowed = true;
28+
kernel->unifiedMemoryControls.indirectSharedAllocationsAllowed = true;
29+
kernel->unifiedMemoryControls.indirectHostAllocationsAllowed = true;
30+
EXPECT_TRUE(kernel->getUnifiedMemoryControls().indirectDeviceAllocationsAllowed);
31+
EXPECT_TRUE(kernel->hasIndirectAllocationsAllowed());
32+
33+
ze_group_count_t groupCount{1, 1, 1};
34+
std::unique_ptr<L0::CommandList> commandList(CommandList::create(productFamily, device, false));
35+
auto result = commandList->appendLaunchKernel(kernel->toHandle(), &groupCount, nullptr, 0, nullptr);
36+
37+
ASSERT_EQ(ZE_RESULT_SUCCESS, result);
38+
ASSERT_TRUE(commandList->hasIndirectAllocationsAllowed());
39+
}
40+
41+
HWTEST_F(CommandListAppendLaunchKernel, givenKernelWithIndirectAllocationsNotAllowedThenCommandListReturnsExpectedIndirectAllocationsAllowed) {
42+
createKernel();
43+
kernel->unifiedMemoryControls.indirectDeviceAllocationsAllowed = false;
44+
kernel->unifiedMemoryControls.indirectSharedAllocationsAllowed = false;
45+
kernel->unifiedMemoryControls.indirectHostAllocationsAllowed = false;
46+
47+
ze_group_count_t groupCount{1, 1, 1};
48+
std::unique_ptr<L0::CommandList> commandList(CommandList::create(productFamily, device, false));
49+
auto result = commandList->appendLaunchKernel(kernel->toHandle(), &groupCount, nullptr, 0, nullptr);
50+
51+
ASSERT_EQ(ZE_RESULT_SUCCESS, result);
52+
ASSERT_FALSE(commandList->hasIndirectAllocationsAllowed());
53+
}
54+
2555
HWTEST_F(CommandListAppendLaunchKernel, givenNotEnoughSpaceInCommandStreamWhenAppendingKernelThenBbEndIsAddedAndNewCmdBufferAllocated) {
2656
using MI_BATCH_BUFFER_END = typename FamilyType::MI_BATCH_BUFFER_END;
2757
createKernel();

level_zero/core/test/unit_tests/sources/cmdqueue/test_cmdqueue.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "level_zero/core/source/driver/driver_handle_imp.h"
1616
#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
17+
#include "level_zero/core/test/unit_tests/fixtures/module_fixture.h"
18+
#include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h"
1719
#include "level_zero/core/test/unit_tests/mocks/mock_cmdqueue.h"
1820
#include "level_zero/core/test/unit_tests/mocks/mock_kernel.h"
1921
#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h"
@@ -284,5 +286,64 @@ HWTEST_F(CommandQueueCommands, givenCommandQueueWhenExecutingCommandListsThenHar
284286
commandQueue->destroy();
285287
}
286288

289+
using CommandQueueIndirectAllocations = Test<ModuleFixture>;
290+
HWTEST_F(CommandQueueIndirectAllocations, givenCommandQueueWhenExecutingCommandListsThenExpectedIndirectAllocationsAddedToResidencyContainer) {
291+
const ze_command_queue_desc_t desc = {
292+
ZE_COMMAND_QUEUE_DESC_VERSION_CURRENT,
293+
ZE_COMMAND_QUEUE_FLAG_NONE,
294+
ZE_COMMAND_QUEUE_MODE_DEFAULT,
295+
ZE_COMMAND_QUEUE_PRIORITY_NORMAL,
296+
0};
297+
298+
MockCsrHw2<FamilyType> csr(*neoDevice->getExecutionEnvironment(), 0);
299+
csr.initializeTagAllocation();
300+
csr.setupContext(*neoDevice->getDefaultEngine().osContext);
301+
302+
L0::CommandQueue *commandQueue = CommandQueue::create(productFamily,
303+
device,
304+
&csr,
305+
&desc,
306+
true);
307+
ASSERT_NE(nullptr, commandQueue);
308+
309+
std::unique_ptr<L0::CommandList> commandList(CommandList::create(productFamily, device, true));
310+
311+
void *deviceAlloc = nullptr;
312+
auto result = device->getDriverHandle()->allocDeviceMem(device->toHandle(), ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT, 16384u, 4096u, &deviceAlloc);
313+
ASSERT_EQ(ZE_RESULT_SUCCESS, result);
314+
315+
auto gpuAlloc = device->getDriverHandle()->getSvmAllocsManager()->getSVMAllocs()->get(deviceAlloc)->gpuAllocation;
316+
ASSERT_NE(nullptr, gpuAlloc);
317+
318+
createKernel();
319+
kernel->unifiedMemoryControls.indirectDeviceAllocationsAllowed = true;
320+
EXPECT_TRUE(kernel->getUnifiedMemoryControls().indirectDeviceAllocationsAllowed);
321+
322+
ze_group_count_t groupCount{1, 1, 1};
323+
result = commandList->appendLaunchKernel(kernel->toHandle(),
324+
&groupCount,
325+
nullptr,
326+
0,
327+
nullptr);
328+
ASSERT_EQ(ZE_RESULT_SUCCESS, result);
329+
330+
auto itorEvent = std::find(std::begin(commandList->commandContainer.getResidencyContainer()),
331+
std::end(commandList->commandContainer.getResidencyContainer()),
332+
gpuAlloc);
333+
EXPECT_EQ(itorEvent, std::end(commandList->commandContainer.getResidencyContainer()));
334+
335+
auto commandListHandle = commandList->toHandle();
336+
result = commandQueue->executeCommandLists(1, &commandListHandle, nullptr, false);
337+
ASSERT_EQ(ZE_RESULT_SUCCESS, result);
338+
339+
itorEvent = std::find(std::begin(commandList->commandContainer.getResidencyContainer()),
340+
std::end(commandList->commandContainer.getResidencyContainer()),
341+
gpuAlloc);
342+
EXPECT_NE(itorEvent, std::end(commandList->commandContainer.getResidencyContainer()));
343+
344+
device->getDriverHandle()->getSvmAllocsManager()->freeSVMAlloc(deviceAlloc);
345+
commandQueue->destroy();
346+
}
347+
287348
} // namespace ult
288349
} // namespace L0

0 commit comments

Comments
 (0)