16
16
namespace {
17
17
18
18
// 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) {
27
21
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
+
47
24
if (!DriverSupportsImmediateAppend) {
48
25
logger::error (" Adapter v2 is used but "
49
26
" 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." );
61
28
std::abort ();
62
29
}
63
30
@@ -78,105 +45,103 @@ ur_exp_command_buffer_handle_t_::getWaitListView(
78
45
}
79
46
80
47
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 ));
88
55
}
89
56
90
57
void ur_exp_command_buffer_handle_t_::cleanupCommandBufferResources () {
91
58
// 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 ));
93
60
94
61
// 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));
102
63
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 ));
105
66
}
106
67
}
107
68
108
69
namespace ur ::level_zero {
109
70
110
71
/* *
111
72
* 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.
116
77
* @return UR_RESULT_SUCCESS or an error code on failure
117
78
*/
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 ) {
122
83
123
84
124
85
using queue_group_type = ur_device_handle_t_::queue_group_info_t ::type;
125
86
// that should be call to queue getZeOrdinal,
126
87
// 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 ;
128
89
129
- ZeStruct<ze_command_list_desc_t > ZeCommandListDesc ;
130
- ZeCommandListDesc .commandQueueGroupOrdinal = QueueGroupOrdinal ;
90
+ ZeStruct<ze_command_list_desc_t > zeCommandListDesc ;
91
+ zeCommandListDesc .commandQueueGroupOrdinal = queueGroupOrdinal ;
131
92
132
- ZeCommandListDesc .flags = ZE_COMMAND_LIST_FLAG_IN_ORDER;
93
+ zeCommandListDesc .flags = ZE_COMMAND_LIST_FLAG_IN_ORDER;
133
94
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 ;
138
99
}
139
100
140
- ZE2UR_CALL (zeCommandListCreate, (Context ->getZeHandle (), Device ->ZeDevice ,
141
- &ZeCommandListDesc , &CommandList ));
101
+ ZE2UR_CALL (zeCommandListCreate, (context ->getZeHandle (), device ->ZeDevice ,
102
+ &zeCommandListDesc , &commandList ));
142
103
143
104
return UR_RESULT_SUCCESS;
144
105
}
145
106
146
107
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) {
160
111
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);
163
124
} catch (const std::bad_alloc &) {
164
125
return exceptionToResult (std::current_exception ());
165
126
}
166
127
return UR_RESULT_SUCCESS;
167
128
}
168
129
ur_result_t
169
130
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
+ }
171
136
return UR_RESULT_SUCCESS;
172
137
}
173
138
174
139
ur_result_t
175
140
urCommandBufferReleaseExp (ur_exp_command_buffer_handle_t hCommandBuffer) {
176
- if (!hCommandBuffer->RefCount .decrementAndTest ())
177
- return UR_RESULT_SUCCESS;
178
-
179
141
try {
142
+ if (!hCommandBuffer->RefCount .decrementAndTest ())
143
+ return UR_RESULT_SUCCESS;
144
+
180
145
hCommandBuffer->cleanupCommandBufferResources ();
181
146
} catch (...) {
182
147
delete hCommandBuffer;
@@ -188,17 +153,20 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
188
153
189
154
ur_result_t
190
155
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);
196
159
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 );
199
162
200
- hCommandBuffer->IsFinalized = true ;
163
+ // Close the command lists and have them ready for dispatch.
164
+ ZE2UR_CALL (zeCommandListClose, (hCommandBuffer->zeCommandList .get ()));
201
165
166
+ hCommandBuffer->isFinalized = true ;
167
+ } catch (...) {
168
+ return exceptionToResult (std::current_exception ());
169
+ }
202
170
return UR_RESULT_SUCCESS;
203
171
}
204
172
@@ -228,41 +196,44 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
228
196
std::ignore = numKernelAlternatives;
229
197
std::ignore = kernelAlternatives;
230
198
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);
231
202
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);
237
205
238
- ze_kernel_handle_t hZeKernel = hKernel->getZeHandle (commandBuffer->Device );
206
+ ze_kernel_handle_t hZeKernel = hKernel->getZeHandle (commandBuffer->device );
239
207
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 );
242
210
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));
248
216
249
- auto waitList = commandBuffer->getWaitListView (nullptr , 0 );
217
+ auto waitList = commandBuffer->getWaitListView (nullptr , 0 );
250
218
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
+ };
258
226
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));
262
230
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
+ }
266
237
267
238
return UR_RESULT_SUCCESS;
268
239
}
@@ -273,7 +244,7 @@ ur_result_t urCommandBufferEnqueueExp(
273
244
ur_event_handle_t *phEvent) {
274
245
try {
275
246
return hQueue->enqueueCommandBuffer (
276
- hCommandBuffer->ZeCommandList , phEvent, numEventsInWaitList, phEventWaitList);
247
+ hCommandBuffer->zeCommandList . get () , phEvent, numEventsInWaitList, phEventWaitList);
277
248
} catch (...) {
278
249
return exceptionToResult (std::current_exception ());
279
250
}
@@ -284,25 +255,28 @@ urCommandBufferGetInfoExp(ur_exp_command_buffer_handle_t hCommandBuffer,
284
255
ur_exp_command_buffer_info_t propName,
285
256
size_t propSize, void *pPropValue,
286
257
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 ());
304
279
}
305
-
306
280
return UR_RESULT_ERROR_INVALID_ENUMERATION;
307
281
}
308
282
0 commit comments