From 4e013582026c9e66b2bfedd0220e509963e9a0dc Mon Sep 17 00:00:00 2001 From: Lukasz Dorau Date: Fri, 29 Nov 2024 11:48:07 +0100 Subject: [PATCH 1/2] Move implementation from examples_level_zero.h to examples_level_zero.c Signed-off-by: Lukasz Dorau --- examples/CMakeLists.txt | 3 +- examples/common/examples_level_zero.c | 406 ++++++++++++++++++ examples/common/examples_level_zero.h | 401 +---------------- examples/ipc_level_zero/CMakeLists.txt | 3 +- examples/ipc_level_zero/ipc_level_zero.c | 1 + .../level_zero_shared_memory/CMakeLists.txt | 3 +- .../level_zero_shared_memory.c | 2 + 7 files changed, 425 insertions(+), 394 deletions(-) create mode 100644 examples/common/examples_level_zero.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2012316769..8be80977e4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -49,6 +49,7 @@ if(UMF_BUILD_GPU_EXAMPLES add_umf_executable( NAME ${EXAMPLE_NAME} SRCS level_zero_shared_memory/level_zero_shared_memory.c + common/examples_level_zero.c LIBS disjoint_pool ze_loader umf) target_include_directories( @@ -126,7 +127,7 @@ if(UMF_BUILD_GPU_EXAMPLES add_umf_executable( NAME ${EXAMPLE_NAME} - SRCS ipc_level_zero/ipc_level_zero.c + SRCS ipc_level_zero/ipc_level_zero.c common/examples_level_zero.c LIBS disjoint_pool ze_loader umf) target_include_directories( diff --git a/examples/common/examples_level_zero.c b/examples/common/examples_level_zero.c new file mode 100644 index 0000000000..87bc4ab940 --- /dev/null +++ b/examples/common/examples_level_zero.c @@ -0,0 +1,406 @@ +/* + * + * Copyright (C) 2024 Intel Corporation + * + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + */ + +#include +#include + +#include "examples_level_zero.h" + +int init_level_zero(void) { + ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY; + ze_result_t result = zeInit(flags); + if (result != ZE_RESULT_SUCCESS) { + return -1; + } + return 0; +} + +static inline int get_drivers(uint32_t *drivers_num_, + ze_driver_handle_t **drivers_) { + int ret = 0; + ze_result_t ze_result; + ze_driver_handle_t *drivers = NULL; + uint32_t drivers_num = 0; + + ze_result = zeDriverGet(&drivers_num, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDriverGet() failed!\n"); + ret = -1; + goto fn_fail; + } + if (drivers_num == 0) { + goto fn_exit; + } + + drivers = malloc(drivers_num * sizeof(ze_driver_handle_t)); + if (!drivers) { + ret = -1; + goto fn_fail; + } + + ze_result = zeDriverGet(&drivers_num, drivers); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDriverGet() failed!\n"); + ret = -1; + goto fn_fail; + } + +fn_exit: + *drivers_num_ = drivers_num; + *drivers_ = drivers; + return ret; + +fn_fail: + *drivers_num_ = 0; + if (drivers) { + free(drivers); + *drivers_ = NULL; + } + return ret; +} + +static inline int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_, + ze_device_handle_t **devices_) { + ze_result_t ze_result; + int ret = 0; + uint32_t devices_num = 0; + ze_device_handle_t *devices = NULL; + + ze_result = zeDeviceGet(driver, &devices_num, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDeviceGet() failed!\n"); + ret = -1; + goto fn_fail; + } + if (devices_num == 0) { + goto fn_exit; + } + + devices = malloc(devices_num * sizeof(ze_device_handle_t)); + if (!devices) { + ret = -1; + goto fn_fail; + } + + ze_result = zeDeviceGet(driver, &devices_num, devices); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDeviceGet() failed!\n"); + ret = -1; + goto fn_fail; + } + +fn_exit: + *devices_num_ = devices_num; + *devices_ = devices; + return ret; + +fn_fail: + devices_num = 0; + if (devices) { + free(devices); + devices = NULL; + } + return ret; +} + +int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) { + int ret = 0; + ze_result_t ze_result; + uint32_t drivers_num = 0; + ze_device_handle_t *devices = NULL; + ze_driver_handle_t *drivers = NULL; + ze_driver_handle_t driver_with_gpus = NULL; + + ret = get_drivers(&drivers_num, &drivers); + if (ret) { + goto fn_fail; + } + + /* Find a driver with GPU */ + for (uint32_t i = 0; i < drivers_num; ++i) { + uint32_t devices_num = 0; + ze_driver_handle_t driver = drivers[i]; + + ret = get_devices(driver, &devices_num, &devices); + if (ret) { + goto fn_fail; + } + + for (uint32_t d = 0; d < devices_num; ++d) { + ze_device_handle_t device = devices[d]; + ze_device_properties_t device_properties = { + .stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, .pNext = NULL}; + + ze_result = zeDeviceGetProperties(device, &device_properties); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDeviceGetProperties() failed!\n"); + ret = -1; + goto fn_fail; + } + + if (device_properties.type == ZE_DEVICE_TYPE_GPU) { + driver_with_gpus = driver; + *driver_idx = i; + break; + } + } + + if (devices) { + free(devices); + devices = NULL; + } + + if (driver_with_gpus != NULL) { + goto fn_exit; + } + } + +fn_fail: + if (devices) { + free(devices); + } + +fn_exit: + *driver_ = driver_with_gpus; + if (drivers) { + free(drivers); + } + return ret; +} + +int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_) { + int ret = -1; + uint32_t devices_num = 0; + ze_device_handle_t *devices = NULL; + ze_device_handle_t device; + + ret = get_devices(driver, &devices_num, &devices); + if (ret) { + return ret; + } + + for (uint32_t d = 0; d < devices_num; ++d) { + device = devices[d]; + ze_device_properties_t device_properties = { + .stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, .pNext = NULL}; + + ze_result_t ze_result = + zeDeviceGetProperties(device, &device_properties); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeDeviceGetProperties() failed!\n"); + ret = -1; + break; + } + + if (device_properties.type == ZE_DEVICE_TYPE_GPU) { + *device_ = device; + ret = 0; + break; + } + } + + if (devices) { + free(devices); + } + return ret; +} + +int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device, + void *ptr, size_t size, const void *pattern, + size_t pattern_size) { + int ret = 0; + + ze_command_queue_desc_t commandQueueDesc = { + ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC, + NULL, + 0, + 0, + 0, + ZE_COMMAND_QUEUE_MODE_DEFAULT, + ZE_COMMAND_QUEUE_PRIORITY_NORMAL}; + + ze_command_list_desc_t commandListDesc = { + ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0, + ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING}; + + ze_command_queue_handle_t hCommandQueue; + ze_result_t ze_result = zeCommandQueueCreate( + context, device, &commandQueueDesc, &hCommandQueue); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueCreate() failed!\n"); + return -1; + } + + ze_command_list_handle_t hCommandList; + ze_result = + zeCommandListCreate(context, device, &commandListDesc, &hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListCreate() failed!\n"); + ret = -1; + goto err_queue_destroy; + } + + // fill memory with a pattern + ze_result = zeCommandListAppendMemoryFill( + hCommandList, ptr, pattern, pattern_size, size, NULL, 0, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListAppendMemoryFill() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + // close and execute the command list + ze_result = zeCommandListClose(hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListClose() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1, + &hCommandList, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + // sync + ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueSynchronize() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + // cleanup +err_list_destroy: + ze_result = zeCommandListDestroy(hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListDestroy() failed!\n"); + ret = -1; + } + +err_queue_destroy: + ze_result = zeCommandQueueDestroy(hCommandQueue); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueDestroy() failed!\n"); + ret = -1; + } + + return ret; +} + +int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device, + void *dst_ptr, void *src_ptr, size_t size) { + int ret = 0; + ze_command_queue_desc_t commandQueueDesc = { + ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC, + NULL, + 0, + 0, + 0, + ZE_COMMAND_QUEUE_MODE_DEFAULT, + ZE_COMMAND_QUEUE_PRIORITY_NORMAL}; + + ze_command_list_desc_t commandListDesc = { + ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0, + ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING}; + + ze_command_queue_handle_t hCommandQueue; + ze_result_t ze_result = zeCommandQueueCreate( + context, device, &commandQueueDesc, &hCommandQueue); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueCreate() failed!\n"); + return -1; + } + + ze_command_list_handle_t hCommandList; + ze_result = + zeCommandListCreate(context, device, &commandListDesc, &hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListCreate() failed!\n"); + ret = -1; + goto err_queue_destroy; + } + + // copy from device memory to host memory + ze_result = zeCommandListAppendMemoryCopy(hCommandList, dst_ptr, src_ptr, + size, NULL, 0, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListAppendMemoryCopy() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + // close and execute the command list + ze_result = zeCommandListClose(hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListClose() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1, + &hCommandList, NULL); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueSynchronize() failed!\n"); + ret = -1; + goto err_list_destroy; + } + + // cleanup +err_list_destroy: + ze_result = zeCommandListDestroy(hCommandList); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandListDestroy() failed!\n"); + ret = -1; + } + +err_queue_destroy: + ze_result = zeCommandQueueDestroy(hCommandQueue); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeCommandQueueDestroy() failed!\n"); + ret = -1; + } + + return ret; +} + +int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) { + ze_result_t ze_result; + ze_context_desc_t ctxtDesc = { + .stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC, .pNext = NULL, .flags = 0}; + + ze_result = zeContextCreate(driver, &ctxtDesc, context); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeContextCreate() failed!\n"); + return -1; + } + + return 0; +} + +int destroy_context(ze_context_handle_t context) { + ze_result_t ze_result; + ze_result = zeContextDestroy(context); + if (ze_result != ZE_RESULT_SUCCESS) { + fprintf(stderr, "zeContextDestroy() failed!\n"); + return -1; + } + + return 0; +} diff --git a/examples/common/examples_level_zero.h b/examples/common/examples_level_zero.h index 46f8922787..2d8e92ff21 100644 --- a/examples/common/examples_level_zero.h +++ b/examples/common/examples_level_zero.h @@ -7,11 +7,8 @@ * */ -#ifndef UMF_EXAMPLE_UTILS_LEVEL_ZERO_H -#define UMF_EXAMPLE_UTILS_LEVEL_ZERO_H - -#include -#include +#ifndef UMF_EXAMPLES_LEVEL_ZERO_H +#define UMF_EXAMPLES_LEVEL_ZERO_H // To use the Level Zero API, the Level Zero SDK has to be installed // on the system @@ -21,399 +18,21 @@ #include #endif -static int init_level_zero(void) { - ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY; - ze_result_t result = zeInit(flags); - if (result != ZE_RESULT_SUCCESS) { - return -1; - } - return 0; -} - -static inline int get_drivers(uint32_t *drivers_num_, - ze_driver_handle_t **drivers_) { - int ret = 0; - ze_result_t ze_result; - ze_driver_handle_t *drivers = NULL; - uint32_t drivers_num = 0; - - ze_result = zeDriverGet(&drivers_num, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDriverGet() failed!\n"); - ret = -1; - goto fn_fail; - } - if (drivers_num == 0) { - goto fn_exit; - } - - drivers = malloc(drivers_num * sizeof(ze_driver_handle_t)); - if (!drivers) { - ret = -1; - goto fn_fail; - } - - ze_result = zeDriverGet(&drivers_num, drivers); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDriverGet() failed!\n"); - ret = -1; - goto fn_fail; - } - -fn_exit: - *drivers_num_ = drivers_num; - *drivers_ = drivers; - return ret; - -fn_fail: - *drivers_num_ = 0; - if (drivers) { - free(drivers); - *drivers_ = NULL; - } - return ret; -} - -static inline int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_, - ze_device_handle_t **devices_) { - ze_result_t ze_result; - int ret = 0; - uint32_t devices_num = 0; - ze_device_handle_t *devices = NULL; - - ze_result = zeDeviceGet(driver, &devices_num, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDeviceGet() failed!\n"); - ret = -1; - goto fn_fail; - } - if (devices_num == 0) { - goto fn_exit; - } - - devices = malloc(devices_num * sizeof(ze_device_handle_t)); - if (!devices) { - ret = -1; - goto fn_fail; - } - - ze_result = zeDeviceGet(driver, &devices_num, devices); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDeviceGet() failed!\n"); - ret = -1; - goto fn_fail; - } - -fn_exit: - *devices_num_ = devices_num; - *devices_ = devices; - return ret; - -fn_fail: - devices_num = 0; - if (devices) { - free(devices); - devices = NULL; - } - return ret; -} - -static inline int find_driver_with_gpu(uint32_t *driver_idx, - ze_driver_handle_t *driver_) { - int ret = 0; - ze_result_t ze_result; - uint32_t drivers_num = 0; - ze_device_handle_t *devices = NULL; - ze_driver_handle_t *drivers = NULL; - ze_driver_handle_t driver_with_gpus = NULL; - - ret = get_drivers(&drivers_num, &drivers); - if (ret) { - goto fn_fail; - } - - /* Find a driver with GPU */ - for (uint32_t i = 0; i < drivers_num; ++i) { - uint32_t devices_num = 0; - ze_driver_handle_t driver = drivers[i]; - - ret = get_devices(driver, &devices_num, &devices); - if (ret) { - goto fn_fail; - } - - for (uint32_t d = 0; d < devices_num; ++d) { - ze_device_handle_t device = devices[d]; - ze_device_properties_t device_properties = { - .stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, .pNext = NULL}; - - ze_result = zeDeviceGetProperties(device, &device_properties); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDeviceGetProperties() failed!\n"); - ret = -1; - goto fn_fail; - } - - if (device_properties.type == ZE_DEVICE_TYPE_GPU) { - driver_with_gpus = driver; - *driver_idx = i; - break; - } - } - - if (devices) { - free(devices); - devices = NULL; - } - - if (driver_with_gpus != NULL) { - goto fn_exit; - } - } - -fn_fail: - if (devices) { - free(devices); - } - -fn_exit: - *driver_ = driver_with_gpus; - if (drivers) { - free(drivers); - } - return ret; -} - -static inline int find_gpu_device(ze_driver_handle_t driver, - ze_device_handle_t *device_) { - int ret = -1; - uint32_t devices_num = 0; - ze_device_handle_t *devices = NULL; - ze_device_handle_t device; - - ret = get_devices(driver, &devices_num, &devices); - if (ret) { - return ret; - } +int init_level_zero(void); - for (uint32_t d = 0; d < devices_num; ++d) { - device = devices[d]; - ze_device_properties_t device_properties = { - .stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, .pNext = NULL}; +int create_context(ze_driver_handle_t driver, ze_context_handle_t *context); - ze_result_t ze_result = - zeDeviceGetProperties(device, &device_properties); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeDeviceGetProperties() failed!\n"); - ret = -1; - break; - } +int destroy_context(ze_context_handle_t context); - if (device_properties.type == ZE_DEVICE_TYPE_GPU) { - *device_ = device; - ret = 0; - break; - } - } +int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_); - if (devices) { - free(devices); - } - return ret; -} +int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_); int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device, void *ptr, size_t size, const void *pattern, - size_t pattern_size) { - int ret = 0; - - ze_command_queue_desc_t commandQueueDesc = { - ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC, - NULL, - 0, - 0, - 0, - ZE_COMMAND_QUEUE_MODE_DEFAULT, - ZE_COMMAND_QUEUE_PRIORITY_NORMAL}; - - ze_command_list_desc_t commandListDesc = { - ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0, - ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING}; - - ze_command_queue_handle_t hCommandQueue; - ze_result_t ze_result = zeCommandQueueCreate( - context, device, &commandQueueDesc, &hCommandQueue); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueCreate() failed!\n"); - return -1; - } - - ze_command_list_handle_t hCommandList; - ze_result = - zeCommandListCreate(context, device, &commandListDesc, &hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListCreate() failed!\n"); - ret = -1; - goto err_queue_destroy; - } - - // fill memory with a pattern - ze_result = zeCommandListAppendMemoryFill( - hCommandList, ptr, pattern, pattern_size, size, NULL, 0, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListAppendMemoryFill() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - // close and execute the command list - ze_result = zeCommandListClose(hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListClose() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1, - &hCommandList, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - // sync - ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueSynchronize() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - // cleanup -err_list_destroy: - ze_result = zeCommandListDestroy(hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListDestroy() failed!\n"); - ret = -1; - } - -err_queue_destroy: - ze_result = zeCommandQueueDestroy(hCommandQueue); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueDestroy() failed!\n"); - ret = -1; - } - - return ret; -} + size_t pattern_size); int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device, - void *dst_ptr, void *src_ptr, size_t size) { - int ret = 0; - ze_command_queue_desc_t commandQueueDesc = { - ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC, - NULL, - 0, - 0, - 0, - ZE_COMMAND_QUEUE_MODE_DEFAULT, - ZE_COMMAND_QUEUE_PRIORITY_NORMAL}; - - ze_command_list_desc_t commandListDesc = { - ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0, - ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING}; - - ze_command_queue_handle_t hCommandQueue; - ze_result_t ze_result = zeCommandQueueCreate( - context, device, &commandQueueDesc, &hCommandQueue); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueCreate() failed!\n"); - return -1; - } - - ze_command_list_handle_t hCommandList; - ze_result = - zeCommandListCreate(context, device, &commandListDesc, &hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListCreate() failed!\n"); - ret = -1; - goto err_queue_destroy; - } - - // copy from device memory to host memory - ze_result = zeCommandListAppendMemoryCopy(hCommandList, dst_ptr, src_ptr, - size, NULL, 0, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListAppendMemoryCopy() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - // close and execute the command list - ze_result = zeCommandListClose(hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListClose() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - ze_result = zeCommandQueueExecuteCommandLists(hCommandQueue, 1, - &hCommandList, NULL); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - ze_result = zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueSynchronize() failed!\n"); - ret = -1; - goto err_list_destroy; - } - - // cleanup -err_list_destroy: - ze_result = zeCommandListDestroy(hCommandList); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandListDestroy() failed!\n"); - ret = -1; - } - -err_queue_destroy: - ze_result = zeCommandQueueDestroy(hCommandQueue); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeCommandQueueDestroy() failed!\n"); - ret = -1; - } - - return ret; -} - -int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) { - ze_result_t ze_result; - ze_context_desc_t ctxtDesc = { - .stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC, .pNext = NULL, .flags = 0}; - - ze_result = zeContextCreate(driver, &ctxtDesc, context); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeContextCreate() failed!\n"); - return -1; - } - - return 0; -} - -int destroy_context(ze_context_handle_t context) { - ze_result_t ze_result; - ze_result = zeContextDestroy(context); - if (ze_result != ZE_RESULT_SUCCESS) { - fprintf(stderr, "zeContextDestroy() failed!\n"); - return -1; - } - - return 0; -} + void *dst_ptr, void *src_ptr, size_t size); -#endif // UMF_EXAMPLE_UTILS_LEVEL_ZERO_H +#endif /* UMF_EXAMPLES_LEVEL_ZERO_H */ diff --git a/examples/ipc_level_zero/CMakeLists.txt b/examples/ipc_level_zero/CMakeLists.txt index 78d0e667a6..10483c91e5 100644 --- a/examples/ipc_level_zero/CMakeLists.txt +++ b/examples/ipc_level_zero/CMakeLists.txt @@ -45,7 +45,8 @@ message(STATUS "Level Zero include directory: ${LEVEL_ZERO_INCLUDE_DIRS}") # build the example set(EXAMPLE_NAME umf_example_ipc_level_zero) -add_executable(${EXAMPLE_NAME} ipc_level_zero.c) +add_executable(${EXAMPLE_NAME} ipc_level_zero.c + ${UMF_EXAMPLE_DIR}/common/examples_level_zero.c) target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS} ${UMF_EXAMPLE_DIR}/common) target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_LIBRARY_DIRS} diff --git a/examples/ipc_level_zero/ipc_level_zero.c b/examples/ipc_level_zero/ipc_level_zero.c index c7b74171fe..f8fd2f3617 100644 --- a/examples/ipc_level_zero/ipc_level_zero.c +++ b/examples/ipc_level_zero/ipc_level_zero.c @@ -8,6 +8,7 @@ */ #include +#include #include "umf/ipc.h" #include "umf/memory_pool.h" diff --git a/examples/level_zero_shared_memory/CMakeLists.txt b/examples/level_zero_shared_memory/CMakeLists.txt index 21edf9a84c..06a0c36bd1 100644 --- a/examples/level_zero_shared_memory/CMakeLists.txt +++ b/examples/level_zero_shared_memory/CMakeLists.txt @@ -45,7 +45,8 @@ message(STATUS "Level Zero include directory: ${LEVEL_ZERO_INCLUDE_DIRS}") # build the example set(EXAMPLE_NAME umf_example_level_zero_shared_memory) -add_executable(${EXAMPLE_NAME} level_zero_shared_memory.c) +add_executable(${EXAMPLE_NAME} level_zero_shared_memory.c + ${UMF_EXAMPLE_DIR}/common/examples_level_zero.c) target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS} ${UMF_EXAMPLE_DIR}/common) target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_LIBRARY_DIRS} diff --git a/examples/level_zero_shared_memory/level_zero_shared_memory.c b/examples/level_zero_shared_memory/level_zero_shared_memory.c index 725941f6e7..607da6d4c3 100644 --- a/examples/level_zero_shared_memory/level_zero_shared_memory.c +++ b/examples/level_zero_shared_memory/level_zero_shared_memory.c @@ -7,6 +7,8 @@ * */ +#include + #include #include #include From 022ec6b260fe090d73344fa7418b4603e1e3b568 Mon Sep 17 00:00:00 2001 From: Lukasz Dorau Date: Fri, 29 Nov 2024 13:30:32 +0100 Subject: [PATCH 2/2] Rename examples_level_zero.* to examples_level_zero_helpers.* --- examples/CMakeLists.txt | 5 +++-- .../{examples_level_zero.c => examples_level_zero_helpers.c} | 2 +- .../{examples_level_zero.h => examples_level_zero_helpers.h} | 0 examples/ipc_level_zero/CMakeLists.txt | 5 +++-- examples/ipc_level_zero/ipc_level_zero.c | 2 +- examples/level_zero_shared_memory/CMakeLists.txt | 5 +++-- examples/level_zero_shared_memory/level_zero_shared_memory.c | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) rename examples/common/{examples_level_zero.c => examples_level_zero_helpers.c} (99%) rename examples/common/{examples_level_zero.h => examples_level_zero_helpers.h} (100%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8be80977e4..942579a303 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -49,7 +49,7 @@ if(UMF_BUILD_GPU_EXAMPLES add_umf_executable( NAME ${EXAMPLE_NAME} SRCS level_zero_shared_memory/level_zero_shared_memory.c - common/examples_level_zero.c + common/examples_level_zero_helpers.c LIBS disjoint_pool ze_loader umf) target_include_directories( @@ -127,7 +127,8 @@ if(UMF_BUILD_GPU_EXAMPLES add_umf_executable( NAME ${EXAMPLE_NAME} - SRCS ipc_level_zero/ipc_level_zero.c common/examples_level_zero.c + SRCS ipc_level_zero/ipc_level_zero.c + common/examples_level_zero_helpers.c LIBS disjoint_pool ze_loader umf) target_include_directories( diff --git a/examples/common/examples_level_zero.c b/examples/common/examples_level_zero_helpers.c similarity index 99% rename from examples/common/examples_level_zero.c rename to examples/common/examples_level_zero_helpers.c index 87bc4ab940..5e00838c27 100644 --- a/examples/common/examples_level_zero.c +++ b/examples/common/examples_level_zero_helpers.c @@ -10,7 +10,7 @@ #include #include -#include "examples_level_zero.h" +#include "examples_level_zero_helpers.h" int init_level_zero(void) { ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY; diff --git a/examples/common/examples_level_zero.h b/examples/common/examples_level_zero_helpers.h similarity index 100% rename from examples/common/examples_level_zero.h rename to examples/common/examples_level_zero_helpers.h diff --git a/examples/ipc_level_zero/CMakeLists.txt b/examples/ipc_level_zero/CMakeLists.txt index 10483c91e5..5c17d4c9cb 100644 --- a/examples/ipc_level_zero/CMakeLists.txt +++ b/examples/ipc_level_zero/CMakeLists.txt @@ -45,8 +45,9 @@ message(STATUS "Level Zero include directory: ${LEVEL_ZERO_INCLUDE_DIRS}") # build the example set(EXAMPLE_NAME umf_example_ipc_level_zero) -add_executable(${EXAMPLE_NAME} ipc_level_zero.c - ${UMF_EXAMPLE_DIR}/common/examples_level_zero.c) +add_executable( + ${EXAMPLE_NAME} ipc_level_zero.c + ${UMF_EXAMPLE_DIR}/common/examples_level_zero_helpers.c) target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS} ${UMF_EXAMPLE_DIR}/common) target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_LIBRARY_DIRS} diff --git a/examples/ipc_level_zero/ipc_level_zero.c b/examples/ipc_level_zero/ipc_level_zero.c index f8fd2f3617..e819407174 100644 --- a/examples/ipc_level_zero/ipc_level_zero.c +++ b/examples/ipc_level_zero/ipc_level_zero.c @@ -15,7 +15,7 @@ #include "umf/pools/pool_disjoint.h" #include "umf/providers/provider_level_zero.h" -#include "examples_level_zero.h" +#include "examples_level_zero_helpers.h" int create_level_zero_pool(ze_context_handle_t context, ze_device_handle_t device, diff --git a/examples/level_zero_shared_memory/CMakeLists.txt b/examples/level_zero_shared_memory/CMakeLists.txt index 06a0c36bd1..3711b40941 100644 --- a/examples/level_zero_shared_memory/CMakeLists.txt +++ b/examples/level_zero_shared_memory/CMakeLists.txt @@ -45,8 +45,9 @@ message(STATUS "Level Zero include directory: ${LEVEL_ZERO_INCLUDE_DIRS}") # build the example set(EXAMPLE_NAME umf_example_level_zero_shared_memory) -add_executable(${EXAMPLE_NAME} level_zero_shared_memory.c - ${UMF_EXAMPLE_DIR}/common/examples_level_zero.c) +add_executable( + ${EXAMPLE_NAME} level_zero_shared_memory.c + ${UMF_EXAMPLE_DIR}/common/examples_level_zero_helpers.c) target_include_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS} ${UMF_EXAMPLE_DIR}/common) target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBUMF_LIBRARY_DIRS} diff --git a/examples/level_zero_shared_memory/level_zero_shared_memory.c b/examples/level_zero_shared_memory/level_zero_shared_memory.c index 607da6d4c3..b0f646861d 100644 --- a/examples/level_zero_shared_memory/level_zero_shared_memory.c +++ b/examples/level_zero_shared_memory/level_zero_shared_memory.c @@ -13,7 +13,7 @@ #include #include -#include "examples_level_zero.h" +#include "examples_level_zero_helpers.h" int main(void) { // A result object for storing UMF API result status