Skip to content

Commit 6228e5c

Browse files
committed
Add fill and copy functions to utils_level_zero.h
1 parent cd8794b commit 6228e5c

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

examples/basic/utils_level_zero.h

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,175 @@ static inline int find_gpu_device(ze_driver_handle_t driver,
217217
return ret;
218218
}
219219

220+
int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device,
221+
void *ptr, size_t size, const void *pattern,
222+
size_t pattern_size) {
223+
int ret = 0;
224+
225+
ze_command_queue_desc_t commandQueueDesc = {
226+
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
227+
NULL,
228+
0,
229+
0,
230+
0,
231+
ZE_COMMAND_QUEUE_MODE_DEFAULT,
232+
ZE_COMMAND_QUEUE_PRIORITY_NORMAL};
233+
234+
ze_command_list_desc_t commandListDesc = {
235+
ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0,
236+
ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING};
237+
238+
ze_command_queue_handle_t hCommandQueue;
239+
ze_result_t ze_result = zeCommandQueueCreate(
240+
context, device, &commandQueueDesc, &hCommandQueue);
241+
if (ze_result != ZE_RESULT_SUCCESS) {
242+
fprintf(stderr, "zeCommandQueueCreate() failed!\n");
243+
return -1;
244+
}
245+
246+
ze_command_list_handle_t hCommandList;
247+
ze_result =
248+
zeCommandListCreate(context, device, &commandListDesc, &hCommandList);
249+
if (ze_result != ZE_RESULT_SUCCESS) {
250+
fprintf(stderr, "zeCommandListCreate() failed!\n");
251+
ret = -1;
252+
goto err_queue_destroy;
253+
}
254+
255+
// fill memory with a pattern
256+
ze_result = zeCommandListAppendMemoryFill(
257+
hCommandList, ptr, pattern, pattern_size, size, NULL, 0, NULL);
258+
if (ze_result != ZE_RESULT_SUCCESS) {
259+
fprintf(stderr, "zeCommandListAppendMemoryFill() failed!\n");
260+
ret = -1;
261+
goto err_list_destroy;
262+
}
263+
264+
// close and execute the command list
265+
ze_result = zeCommandListClose(hCommandList);
266+
if (ze_result != ZE_RESULT_SUCCESS) {
267+
fprintf(stderr, "zeCommandListClose() failed!\n");
268+
ret = -1;
269+
goto err_list_destroy;
270+
}
271+
272+
ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1,
273+
&hCommandList, NULL);
274+
if (ze_result != ZE_RESULT_SUCCESS) {
275+
fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n");
276+
ret = -1;
277+
goto err_list_destroy;
278+
}
279+
280+
// sync
281+
ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX);
282+
if (ze_result != ZE_RESULT_SUCCESS) {
283+
fprintf(stderr, "zeCommandQueueSynchronize() failed!\n");
284+
ret = -1;
285+
goto err_list_destroy;
286+
}
287+
288+
// cleanup
289+
err_list_destroy:
290+
ze_result = zeCommandListDestroy(hCommandList);
291+
if (ze_result != ZE_RESULT_SUCCESS) {
292+
fprintf(stderr, "zeCommandListDestroy() failed!\n");
293+
ret = -1;
294+
}
295+
296+
err_queue_destroy:
297+
ze_result = zeCommandQueueDestroy(hCommandQueue);
298+
if (ze_result != ZE_RESULT_SUCCESS) {
299+
fprintf(stderr, "zeCommandQueueDestroy() failed!\n");
300+
ret = -1;
301+
}
302+
303+
return ret;
304+
}
305+
306+
int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device,
307+
void *dst_ptr, void *src_ptr, size_t size) {
308+
int ret = 0;
309+
ze_command_queue_desc_t commandQueueDesc = {
310+
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
311+
NULL,
312+
0,
313+
0,
314+
0,
315+
ZE_COMMAND_QUEUE_MODE_DEFAULT,
316+
ZE_COMMAND_QUEUE_PRIORITY_NORMAL};
317+
318+
ze_command_list_desc_t commandListDesc = {
319+
ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0,
320+
ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING};
321+
322+
ze_command_queue_handle_t hCommandQueue;
323+
ze_result_t ze_result = zeCommandQueueCreate(
324+
context, device, &commandQueueDesc, &hCommandQueue);
325+
if (ze_result != ZE_RESULT_SUCCESS) {
326+
fprintf(stderr, "zeCommandQueueCreate() failed!\n");
327+
return -1;
328+
}
329+
330+
ze_command_list_handle_t hCommandList;
331+
ze_result =
332+
zeCommandListCreate(context, device, &commandListDesc, &hCommandList);
333+
if (ze_result != ZE_RESULT_SUCCESS) {
334+
fprintf(stderr, "zeCommandListCreate() failed!\n");
335+
ret = -1;
336+
goto err_queue_destroy;
337+
}
338+
339+
// copy from device memory to host memory
340+
ze_result = zeCommandListAppendMemoryCopy(hCommandList, dst_ptr, src_ptr,
341+
size, NULL, 0, NULL);
342+
if (ze_result != ZE_RESULT_SUCCESS) {
343+
fprintf(stderr, "zeCommandListAppendMemoryCopy() failed!\n");
344+
ret = -1;
345+
goto err_list_destroy;
346+
}
347+
348+
// close and execute the command list
349+
ze_result = zeCommandListClose(hCommandList);
350+
if (ze_result != ZE_RESULT_SUCCESS) {
351+
fprintf(stderr, "zeCommandListClose() failed!\n");
352+
ret = -1;
353+
goto err_list_destroy;
354+
}
355+
356+
ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1,
357+
&hCommandList, NULL);
358+
if (ze_result != ZE_RESULT_SUCCESS) {
359+
fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n");
360+
ret = -1;
361+
goto err_list_destroy;
362+
}
363+
364+
ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX);
365+
if (ze_result != ZE_RESULT_SUCCESS) {
366+
fprintf(stderr, "zeCommandQueueSynchronize() failed!\n");
367+
ret = -1;
368+
goto err_list_destroy;
369+
}
370+
371+
// cleanup
372+
err_list_destroy:
373+
ze_result = zeCommandListDestroy(hCommandList);
374+
if (ze_result != ZE_RESULT_SUCCESS) {
375+
fprintf(stderr, "zeCommandListDestroy() failed!\n");
376+
ret = -1;
377+
}
378+
379+
err_queue_destroy:
380+
ze_result = zeCommandQueueDestroy(hCommandQueue);
381+
if (ze_result != ZE_RESULT_SUCCESS) {
382+
fprintf(stderr, "zeCommandQueueDestroy() failed!\n");
383+
ret = -1;
384+
}
385+
386+
return ret;
387+
}
388+
220389
int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) {
221390
ze_result_t ze_result;
222391
ze_context_desc_t ctxtDesc = {

0 commit comments

Comments
 (0)