Skip to content

Commit 7da53d8

Browse files
committed
Fix most issues with code
1 parent 1716db3 commit 7da53d8

File tree

5 files changed

+139
-168
lines changed

5 files changed

+139
-168
lines changed

scripts/templates/queue_api.cpp.mako

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ from templates import helper as th
1919
*
2020
*/
2121

22+
// This file was generated basing on scripts/templates/queue_api.cpp.mako
23+
2224
#include "queue_api.hpp"
2325
#include "ur_util.hpp"
2426

scripts/templates/queue_api.hpp.mako

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ from templates import helper as th
1919
*
2020
*/
2121

22+
// This file was generated basing on scripts/templates/queue_api.hpp.mako
23+
2224
#pragma once
2325

2426
#include <ur_api.h>

source/adapters/level_zero/v2/command_buffer.cpp

Lines changed: 120 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,15 @@
1616
namespace {
1717

1818
// Checks whether zeCommandListImmediateAppendCommandListsExp can be used for a
19-
// given Context and Device.
20-
void checkImmediateAppendSupport(ur_context_handle_t Context,
21-
ur_device_handle_t Device) {
22-
// TODO The L0 driver is not reporting this extension yet. Once it does,
23-
// switch to using the variable zeDriverImmediateCommandListAppendFound.
24-
25-
// Minimum version that supports zeCommandListImmediateAppendCommandListsExp.
26-
constexpr uint32_t MinDriverVersion = 30898;
19+
// given context.
20+
void checkImmediateAppendSupport(ur_context_handle_t context) {
2721
bool DriverSupportsImmediateAppend =
28-
Context->getPlatform()->isDriverVersionNewerOrSimilar(1, 3,
29-
MinDriverVersion);
30-
31-
// If this environment variable is:
32-
// - Set to 1: the immediate append path will always be enabled as long the
33-
// pre-requisites are met.
34-
// - Set to 0: the immediate append path will always be disabled.
35-
// - Not Defined: The default behaviour will be used which enables the
36-
// immediate append path only for some devices when the pre-requisites are
37-
// met.
38-
const char *AppendEnvVarName = "UR_L0_CMD_BUFFER_USE_IMMEDIATE_APPEND_PATH";
39-
const char *UrRet = std::getenv(AppendEnvVarName);
40-
41-
if (!Device->ImmCommandListUsed) {
42-
logger::error("Adapter v2 is used but immediate command-lists are currently "
43-
"disabled. Immediate command-lists are "
44-
"required to use the adapter v2.");
45-
std::abort();
46-
}
22+
context->getPlatform()->ZeCommandListImmediateAppendExt.Supported;
23+
4724
if (!DriverSupportsImmediateAppend) {
4825
logger::error("Adapter v2 is used but "
4926
"the current driver does not support the "
50-
"zeCommandListImmediateAppendCommandListsExp entrypoint. A "
51-
"driver version of at least {} is required to use the "
52-
"immediate append path.", MinDriverVersion);
53-
std::abort();
54-
}
55-
56-
const bool EnableAppendPath = !UrRet || std::atoi(UrRet) == 1;
57-
if (!Device->isPVC() && !EnableAppendPath) {
58-
logger::error("Adapter v2 is used but "
59-
"immediate append support is not enabled."
60-
"Please set {}=1 to enable it.", AppendEnvVarName);
27+
"zeCommandListImmediateAppendCommandListsExp entrypoint.");
6128
std::abort();
6229
}
6330

@@ -78,105 +45,103 @@ ur_exp_command_buffer_handle_t_::getWaitListView(
7845
}
7946

8047
ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
81-
ur_context_handle_t Context, ur_device_handle_t Device,
82-
ze_command_list_handle_t CommandList,
83-
const ur_exp_command_buffer_desc_t *Desc)
84-
: Context(Context), Device(Device), ZeCommandList(CommandList),
85-
IsUpdatable(Desc ? Desc->isUpdatable : false) {
86-
UR_CALL_THROWS(ur::level_zero::urContextRetain(Context));
87-
UR_CALL_THROWS(ur::level_zero::urDeviceRetain(Device));
48+
ur_context_handle_t context, ur_device_handle_t device,
49+
ze_command_list_handle_t commandList,
50+
const ur_exp_command_buffer_desc_t *desc)
51+
: context(context), device(device), zeCommandList(commandList),
52+
isUpdatable(desc ? desc->isUpdatable : false) {
53+
UR_CALL_THROWS(ur::level_zero::urContextRetain(context));
54+
UR_CALL_THROWS(ur::level_zero::urDeviceRetain(device));
8855
}
8956

9057
void ur_exp_command_buffer_handle_t_::cleanupCommandBufferResources() {
9158
// Release the memory allocated to the Context stored in the command_buffer
92-
UR_CALL_THROWS(ur::level_zero::urContextRelease(Context));
59+
UR_CALL_THROWS(ur::level_zero::urContextRelease(context));
9360

9461
// Release the device
95-
UR_CALL_THROWS(ur::level_zero::urDeviceRelease(Device));
96-
97-
// Release the memory allocated to the CommandList stored in the
98-
// command_buffer
99-
if (ZeCommandList) {
100-
ZE_CALL_NOCHECK(zeCommandListDestroy, (ZeCommandList));
101-
}
62+
UR_CALL_THROWS(ur::level_zero::urDeviceRelease(device));
10263

103-
for (auto &AssociatedKernel : KernelsList) {
104-
UR_CALL_THROWS(ur::level_zero::urKernelRelease(AssociatedKernel));
64+
for (auto &associatedKernel : kernelsList) {
65+
UR_CALL_THROWS(ur::level_zero::urKernelRelease(associatedKernel));
10566
}
10667
}
10768

10869
namespace ur::level_zero {
10970

11071
/**
11172
* Creates a L0 command list
112-
* @param[in] Context The Context associated with the command-list
113-
* @param[in] Device The Device associated with the command-list
114-
* @param[in] IsUpdatable Whether the command-list should be mutable.
115-
* @param[out] CommandList The L0 command-list created by this function.
73+
* @param[in] context The Context associated with the command-list
74+
* @param[in] device The Device associated with the command-list
75+
* @param[in] isUpdatable Whether the command-list should be mutable.
76+
* @param[out] commandList The L0 command-list created by this function.
11677
* @return UR_RESULT_SUCCESS or an error code on failure
11778
*/
118-
ur_result_t createMainCommandList(ur_context_handle_t Context,
119-
ur_device_handle_t Device,
120-
bool IsUpdatable,
121-
ze_command_list_handle_t &CommandList) {
79+
ur_result_t createMainCommandList(ur_context_handle_t context,
80+
ur_device_handle_t device,
81+
bool isUpdatable,
82+
ze_command_list_handle_t &commandList) {
12283

12384

12485
using queue_group_type = ur_device_handle_t_::queue_group_info_t::type;
12586
// that should be call to queue getZeOrdinal,
12687
// but queue is not available while constructing buffer
127-
uint32_t QueueGroupOrdinal = Device->QueueGroup[queue_group_type::Compute].ZeOrdinal;
88+
uint32_t queueGroupOrdinal = device->QueueGroup[queue_group_type::Compute].ZeOrdinal;
12889

129-
ZeStruct<ze_command_list_desc_t> ZeCommandListDesc;
130-
ZeCommandListDesc.commandQueueGroupOrdinal = QueueGroupOrdinal;
90+
ZeStruct<ze_command_list_desc_t> zeCommandListDesc;
91+
zeCommandListDesc.commandQueueGroupOrdinal = queueGroupOrdinal;
13192

132-
ZeCommandListDesc.flags = ZE_COMMAND_LIST_FLAG_IN_ORDER;
93+
zeCommandListDesc.flags = ZE_COMMAND_LIST_FLAG_IN_ORDER;
13394

134-
ZeStruct<ze_mutable_command_list_exp_desc_t> ZeMutableCommandListDesc;
135-
if (IsUpdatable) {
136-
ZeMutableCommandListDesc.flags = 0;
137-
ZeCommandListDesc.pNext = &ZeMutableCommandListDesc;
95+
ZeStruct<ze_mutable_command_list_exp_desc_t> zeMutableCommandListDesc;
96+
if (isUpdatable) {
97+
zeMutableCommandListDesc.flags = 0;
98+
zeCommandListDesc.pNext = &zeMutableCommandListDesc;
13899
}
139100

140-
ZE2UR_CALL(zeCommandListCreate, (Context->getZeHandle(), Device->ZeDevice,
141-
&ZeCommandListDesc, &CommandList));
101+
ZE2UR_CALL(zeCommandListCreate, (context->getZeHandle(), device->ZeDevice,
102+
&zeCommandListDesc, &commandList));
142103

143104
return UR_RESULT_SUCCESS;
144105
}
145106

146107
ur_result_t
147-
urCommandBufferCreateExp(ur_context_handle_t Context, ur_device_handle_t Device,
148-
const ur_exp_command_buffer_desc_t *CommandBufferDesc,
149-
ur_exp_command_buffer_handle_t *CommandBuffer) {
150-
bool IsUpdatable = CommandBufferDesc && CommandBufferDesc->isUpdatable;
151-
checkImmediateAppendSupport(Context, Device);
152-
153-
if (IsUpdatable) {
154-
UR_ASSERT(Context->getPlatform()->ZeMutableCmdListExt.Supported,
155-
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
156-
}
157-
158-
ze_command_list_handle_t ZeCommandList = nullptr;
159-
UR_CALL(createMainCommandList(Context, Device, IsUpdatable, ZeCommandList));
108+
urCommandBufferCreateExp(ur_context_handle_t context, ur_device_handle_t device,
109+
const ur_exp_command_buffer_desc_t *commandBufferDesc,
110+
ur_exp_command_buffer_handle_t *commandBuffer) {
160111
try {
161-
*CommandBuffer = new ur_exp_command_buffer_handle_t_(
162-
Context, Device, ZeCommandList, CommandBufferDesc);
112+
bool isUpdatable = commandBufferDesc && commandBufferDesc->isUpdatable;
113+
checkImmediateAppendSupport(context);
114+
115+
if (isUpdatable) {
116+
UR_ASSERT(context->getPlatform()->ZeMutableCmdListExt.Supported,
117+
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
118+
}
119+
120+
ze_command_list_handle_t zeCommandList = nullptr;
121+
UR_CALL(createMainCommandList(context, device, isUpdatable, zeCommandList));
122+
*commandBuffer = new ur_exp_command_buffer_handle_t_(
123+
context, device, zeCommandList, commandBufferDesc);
163124
} catch (const std::bad_alloc &) {
164125
return exceptionToResult(std::current_exception());
165126
}
166127
return UR_RESULT_SUCCESS;
167128
}
168129
ur_result_t
169130
urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
170-
hCommandBuffer->RefCount.increment();
131+
try {
132+
hCommandBuffer->RefCount.increment();
133+
} catch (const std::bad_alloc &) {
134+
return exceptionToResult(std::current_exception());
135+
}
171136
return UR_RESULT_SUCCESS;
172137
}
173138

174139
ur_result_t
175140
urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
176-
if (!hCommandBuffer->RefCount.decrementAndTest())
177-
return UR_RESULT_SUCCESS;
178-
179141
try {
142+
if (!hCommandBuffer->RefCount.decrementAndTest())
143+
return UR_RESULT_SUCCESS;
144+
180145
hCommandBuffer->cleanupCommandBufferResources();
181146
} catch (...) {
182147
delete hCommandBuffer;
@@ -188,17 +153,20 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
188153

189154
ur_result_t
190155
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
191-
UR_ASSERT(hCommandBuffer, UR_RESULT_ERROR_INVALID_NULL_POINTER);
192-
UR_ASSERT(!hCommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
193-
194-
// It is not allowed to append to command list from multiple threads.
195-
std::scoped_lock<ur_shared_mutex> Guard(hCommandBuffer->Mutex);
156+
try {
157+
UR_ASSERT(hCommandBuffer, UR_RESULT_ERROR_INVALID_NULL_POINTER);
158+
UR_ASSERT(!hCommandBuffer->isFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
196159

197-
// Close the command lists and have them ready for dispatch.
198-
ZE2UR_CALL(zeCommandListClose, (hCommandBuffer->ZeCommandList));
160+
// It is not allowed to append to command list from multiple threads.
161+
std::scoped_lock<ur_shared_mutex> guard(hCommandBuffer->Mutex);
199162

200-
hCommandBuffer->IsFinalized = true;
163+
// Close the command lists and have them ready for dispatch.
164+
ZE2UR_CALL(zeCommandListClose, (hCommandBuffer->zeCommandList.get()));
201165

166+
hCommandBuffer->isFinalized = true;
167+
} catch (...) {
168+
return exceptionToResult(std::current_exception());
169+
}
202170
return UR_RESULT_SUCCESS;
203171
}
204172

@@ -228,41 +196,44 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
228196
std::ignore = numKernelAlternatives;
229197
std::ignore = kernelAlternatives;
230198
std::ignore = command;
199+
try {
200+
UR_ASSERT(hKernel, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
201+
UR_ASSERT(hKernel->getProgramHandle(), UR_RESULT_ERROR_INVALID_NULL_POINTER);
231202

232-
UR_ASSERT(hKernel, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
233-
UR_ASSERT(hKernel->getProgramHandle(), UR_RESULT_ERROR_INVALID_NULL_POINTER);
234-
235-
UR_ASSERT(workDim > 0, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
236-
UR_ASSERT(workDim < 4, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
203+
UR_ASSERT(workDim > 0, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
204+
UR_ASSERT(workDim < 4, UR_RESULT_ERROR_INVALID_WORK_DIMENSION);
237205

238-
ze_kernel_handle_t hZeKernel = hKernel->getZeHandle(commandBuffer->Device);
206+
ze_kernel_handle_t hZeKernel = hKernel->getZeHandle(commandBuffer->device);
239207

240-
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(commandBuffer->Mutex,
241-
hKernel->Mutex);
208+
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> lock(commandBuffer->Mutex,
209+
hKernel->Mutex);
242210

243-
ze_group_count_t zeThreadGroupDimensions{1, 1, 1};
244-
uint32_t WG[3]{};
245-
UR_CALL(calculateKernelWorkDimensions(hZeKernel, commandBuffer->Device,
246-
zeThreadGroupDimensions, WG, workDim,
247-
pGlobalWorkSize, pLocalWorkSize));
211+
ze_group_count_t zeThreadGroupDimensions{1, 1, 1};
212+
uint32_t wg[3]{};
213+
UR_CALL(calculateKernelWorkDimensions(hZeKernel, commandBuffer->device,
214+
zeThreadGroupDimensions, wg, workDim,
215+
pGlobalWorkSize, pLocalWorkSize));
248216

249-
auto waitList = commandBuffer->getWaitListView(nullptr, 0);
217+
auto waitList = commandBuffer->getWaitListView(nullptr, 0);
250218

251-
bool memoryMigrated = false;
252-
auto memoryMigrate = [&](void *src, void *dst, size_t size) {
253-
ZE2UR_CALL_THROWS(zeCommandListAppendMemoryCopy,
254-
(commandBuffer->ZeCommandList, dst, src, size, nullptr,
255-
waitList.second, waitList.first));
256-
memoryMigrated = true;
257-
};
219+
bool memoryMigrated = false;
220+
auto memoryMigrate = [&](void *src, void *dst, size_t size) {
221+
ZE2UR_CALL_THROWS(zeCommandListAppendMemoryCopy,
222+
(commandBuffer->zeCommandList.get(), dst, src, size, nullptr,
223+
waitList.second, waitList.first));
224+
memoryMigrated = true;
225+
};
258226

259-
UR_CALL(hKernel->prepareForSubmission(commandBuffer->Context, commandBuffer->Device, pGlobalWorkOffset,
260-
workDim, WG[0], WG[1], WG[2],
261-
memoryMigrate));
227+
UR_CALL(hKernel->prepareForSubmission(commandBuffer->context, commandBuffer->device, pGlobalWorkOffset,
228+
workDim, wg[0], wg[1], wg[2],
229+
memoryMigrate));
262230

263-
ZE2UR_CALL(zeCommandListAppendLaunchKernel,
264-
(commandBuffer->ZeCommandList, hZeKernel, &zeThreadGroupDimensions,
265-
nullptr, waitList.second, waitList.first));
231+
ZE2UR_CALL(zeCommandListAppendLaunchKernel,
232+
(commandBuffer->zeCommandList.get(), hZeKernel, &zeThreadGroupDimensions,
233+
nullptr, waitList.second, waitList.first));
234+
} catch (...) {
235+
return exceptionToResult(std::current_exception());
236+
}
266237

267238
return UR_RESULT_SUCCESS;
268239
}
@@ -273,7 +244,7 @@ ur_result_t urCommandBufferEnqueueExp(
273244
ur_event_handle_t *phEvent) {
274245
try {
275246
return hQueue->enqueueCommandBuffer(
276-
hCommandBuffer->ZeCommandList, phEvent, numEventsInWaitList, phEventWaitList);
247+
hCommandBuffer->zeCommandList.get(), phEvent, numEventsInWaitList, phEventWaitList);
277248
} catch (...) {
278249
return exceptionToResult(std::current_exception());
279250
}
@@ -284,25 +255,28 @@ urCommandBufferGetInfoExp(ur_exp_command_buffer_handle_t hCommandBuffer,
284255
ur_exp_command_buffer_info_t propName,
285256
size_t propSize, void *pPropValue,
286257
size_t *pPropSizeRet) {
287-
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
288-
289-
switch (propName) {
290-
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
291-
return ReturnValue(uint32_t{hCommandBuffer->RefCount.load()});
292-
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
293-
ur_exp_command_buffer_desc_t Descriptor{};
294-
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
295-
Descriptor.pNext = nullptr;
296-
Descriptor.isUpdatable = hCommandBuffer->IsUpdatable;
297-
Descriptor.isInOrder = true;
298-
Descriptor.enableProfiling = hCommandBuffer->IsProfilingEnabled;
299-
300-
return ReturnValue(Descriptor);
301-
}
302-
default:
303-
assert(!"Command-buffer info request not implemented");
258+
try {
259+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
260+
261+
switch (propName) {
262+
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
263+
return ReturnValue(uint32_t{hCommandBuffer->RefCount.load()});
264+
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
265+
ur_exp_command_buffer_desc_t Descriptor{};
266+
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;
267+
Descriptor.pNext = nullptr;
268+
Descriptor.isUpdatable = hCommandBuffer->isUpdatable;
269+
Descriptor.isInOrder = true;
270+
Descriptor.enableProfiling = hCommandBuffer->isProfilingEnabled;
271+
272+
return ReturnValue(Descriptor);
273+
}
274+
default:
275+
assert(!"Command-buffer info request not implemented");
276+
}
277+
} catch (...) {
278+
return exceptionToResult(std::current_exception());
304279
}
305-
306280
return UR_RESULT_ERROR_INVALID_ENUMERATION;
307281
}
308282

0 commit comments

Comments
 (0)