Skip to content

Move level zero helpers to utils and use it in benchmarks #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ function(add_umf_benchmark)
LIBS ${BENCH_LIBS})

target_include_directories(
${BENCH_NAME}
PRIVATE ${UMF_CMAKE_SOURCE_DIR}/include
${UMF_CMAKE_SOURCE_DIR}/src/utils
${UMF_CMAKE_SOURCE_DIR}/test/common
${UMF_CMAKE_SOURCE_DIR}/examples/common)
${BENCH_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/include
${UMF_CMAKE_SOURCE_DIR}/src/utils)

target_link_directories(${BENCH_NAME} PRIVATE ${ARG_LIBDIRS})

Expand Down Expand Up @@ -84,6 +81,9 @@ function(add_umf_benchmark)
if(UMF_BUILD_LEVEL_ZERO_PROVIDER)
target_compile_definitions(${BENCH_NAME}
PRIVATE UMF_BUILD_LEVEL_ZERO_PROVIDER=1)
target_include_directories(
${BENCH_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/test/common
${LEVEL_ZERO_INCLUDE_DIRS})
endif()
if(UMF_BUILD_CUDA_PROVIDER)
target_compile_definitions(${BENCH_NAME}
Expand All @@ -108,6 +108,7 @@ if(LINUX)
set(LIBS_OPTIONAL ${LIBS_OPTIONAL} m)
endif()
if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER)
set(SRCS_OPTIONAL ${SRCS_OPTIONAL} ../src/utils/utils_level_zero.cpp)
set(LIBS_OPTIONAL ${LIBS_OPTIONAL} ze_loader)
# TODO add CUDA
endif()
Expand All @@ -116,7 +117,7 @@ endif()

add_umf_benchmark(
NAME ubench
SRCS ubench.c
SRCS ubench.c ${SRCS_OPTIONAL}
LIBS ${LIBS_OPTIONAL}
LIBDIRS ${LIB_DIRS})

Expand Down
12 changes: 6 additions & 6 deletions benchmark/ubench.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,28 +450,28 @@ int create_level_zero_params(ze_context_handle_t *context,
uint32_t driver_idx = 0;
ze_driver_handle_t driver = NULL;

int ret = init_level_zero();
int ret = utils_ze_init_level_zero();
if (ret != 0) {
fprintf(stderr, "Failed to init Level 0!\n");
return ret;
}

ret = find_driver_with_gpu(&driver_idx, &driver);
ret = utils_ze_find_driver_with_gpu(&driver_idx, &driver);
if (ret || driver == NULL) {
fprintf(stderr, "Cannot find L0 driver with GPU device!\n");
return ret;
}

ret = create_context(driver, context);
ret = utils_ze_create_context(driver, context);
if (ret != 0) {
fprintf(stderr, "Failed to create L0 context!\n");
return ret;
}

ret = find_gpu_device(driver, device);
ret = utils_ze_find_gpu_device(driver, device);
if (ret) {
fprintf(stderr, "Cannot find GPU device!\n");
destroy_context(*context);
utils_ze_destroy_context(*context);
return ret;
}

Expand Down Expand Up @@ -628,7 +628,7 @@ UBENCH_EX(ipc, disjoint_pool_with_level_zero_provider) {
umfLevelZeroMemoryProviderParamsDestroy(level_zero_params);

err_destroy_context:
destroy_context(context);
utils_ze_destroy_context(context);
}
#endif /* (defined UMF_BUILD_LIBUMF_POOL_DISJOINT && defined UMF_BUILD_LEVEL_ZERO_PROVIDER && defined UMF_BUILD_GPU_TESTS) */

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/ipc_level_zero/ipc_level_zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "umf/pools/pool_disjoint.h"
#include "umf/providers/provider_level_zero.h"

#include "utils_level_zero.h"
#include "examples_level_zero.h"

int create_level_zero_pool(ze_context_handle_t context,
ze_device_handle_t device,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <umf/pools/pool_disjoint.h>
#include <umf/providers/provider_level_zero.h>

#include "utils_level_zero.h"
#include "examples_level_zero.h"

int main(void) {
// A result object for storing UMF API result status
Expand Down
2 changes: 1 addition & 1 deletion examples/memspace_hmat/memspace_hmat.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <stdio.h>
#include <string.h>

#include "utils_examples.h"
#include "examples_utils.h"

// Function to create a memory provider which allocates memory from the specified NUMA node
int createMemoryProvider(umf_memory_provider_handle_t *hProvider,
Expand Down
2 changes: 1 addition & 1 deletion examples/memspace_numa/memspace_numa.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <stdio.h>
#include <string.h>

#include "utils_examples.h"
#include "examples_utils.h"

// Function to create a memory provider which allocates memory from the specified NUMA node
// by using umfMemspaceCreateFromNumaArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include "level_zero_helpers.h"
#include "utils_level_zero.h"

#include <memory>
#include <stdlib.h>
Expand Down Expand Up @@ -297,7 +297,7 @@ int InitLevelZeroOps() {
}
#endif // USE_DLOPEN

static int init_level_zero_lib(void) {
static int utils_ze_init_level_zero_lib(void) {
ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY;
ze_result_t result = libze_ops.zeInit(flags);
if (result != ZE_RESULT_SUCCESS) {
Expand All @@ -306,31 +306,33 @@ static int init_level_zero_lib(void) {
return 0;
}

UTIL_ONCE_FLAG level_zero_init_flag;
int InitResult;
void init_level_zero_once() {
static UTIL_ONCE_FLAG level_zero_init_flag = UTIL_ONCE_FLAG_INIT;
static int InitResult;

static void utils_ze_init_level_zero_once(void) {
InitResult = InitLevelZeroOps();
if (InitResult != 0) {
return;
}
InitResult = init_level_zero_lib();
InitResult = utils_ze_init_level_zero_lib();
}

int init_level_zero() {
utils_init_once(&level_zero_init_flag, init_level_zero_once);
int utils_ze_init_level_zero(void) {
utils_init_once(&level_zero_init_flag, utils_ze_init_level_zero_once);

return InitResult;
}

int get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_) {
int utils_ze_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;

ret = init_level_zero();
ret = utils_ze_init_level_zero();
if (ret != 0) {
fprintf(stderr, "init_level_zero() failed!\n");
fprintf(stderr, "utils_ze_init_level_zero() failed!\n");
goto init_fail;
}

Expand Down Expand Up @@ -374,16 +376,16 @@ int get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_) {
return ret;
}

int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
ze_device_handle_t **devices_) {
int utils_ze_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;

ret = init_level_zero();
ret = utils_ze_init_level_zero();
if (ret != 0) {
fprintf(stderr, "init_level_zero() failed!\n");
fprintf(stderr, "utils_ze_init_level_zero() failed!\n");
goto init_fail;
}

Expand Down Expand Up @@ -426,15 +428,16 @@ int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
return ret;
}

int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
int utils_ze_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);
ret = utils_ze_get_drivers(&drivers_num, &drivers);
if (ret) {
goto fn_fail;
}
Expand All @@ -444,7 +447,7 @@ int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
uint32_t devices_num = 0;
ze_driver_handle_t driver = drivers[i];

ret = get_devices(driver, &devices_num, &devices);
ret = utils_ze_get_devices(driver, &devices_num, &devices);
if (ret) {
goto fn_fail;
}
Expand Down Expand Up @@ -493,13 +496,14 @@ int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
return ret;
}

int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_) {
int utils_ze_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);
ret = utils_ze_get_devices(driver, &devices_num, &devices);
if (ret) {
return ret;
}
Expand Down Expand Up @@ -531,9 +535,9 @@ int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_) {
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 utils_ze_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 = {
Expand Down Expand Up @@ -617,8 +621,9 @@ int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device,
return ret;
}

int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device,
void *dst_ptr, const void *src_ptr, size_t size) {
int utils_ze_level_zero_copy(ze_context_handle_t context,
ze_device_handle_t device, void *dst_ptr,
const void *src_ptr, size_t size) {
int ret = 0;
ze_command_queue_desc_t commandQueueDesc = {
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
Expand Down Expand Up @@ -700,7 +705,8 @@ int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device,
return ret;
}

int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) {
int utils_ze_create_context(ze_driver_handle_t driver,
ze_context_handle_t *context) {
ze_result_t ze_result;
ze_context_desc_t ctxtDesc;
ctxtDesc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
Expand All @@ -716,7 +722,7 @@ int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) {
return 0;
}

int destroy_context(ze_context_handle_t context) {
int utils_ze_destroy_context(ze_context_handle_t context) {
ze_result_t ze_result;
ze_result = libze_ops.zeContextDestroy(context);
if (ze_result != ZE_RESULT_SUCCESS) {
Expand All @@ -727,7 +733,7 @@ int destroy_context(ze_context_handle_t context) {
return 0;
}

ze_memory_type_t get_mem_type(ze_context_handle_t context, void *ptr) {
ze_memory_type_t utils_ze_get_mem_type(ze_context_handle_t context, void *ptr) {
ze_device_handle_t device = NULL;
ze_memory_allocation_properties_t alloc_props;
alloc_props.stype = ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES;
Expand Down
52 changes: 52 additions & 0 deletions src/utils/utils_level_zero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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
*/

#ifndef UMF_UTILS_LEVEL_ZERO_H
#define UMF_UTILS_LEVEL_ZERO_H

#include <umf/providers/provider_level_zero.h>

#include "ze_api.h"

#ifdef __cplusplus
extern "C" {
#endif

int utils_ze_init_level_zero(void);
int utils_ze_init_level_zero(void);

int utils_ze_get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_);

int utils_ze_get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
ze_device_handle_t **devices_);

int utils_ze_find_driver_with_gpu(uint32_t *driver_idx,
ze_driver_handle_t *driver_);

int utils_ze_find_gpu_device(ze_driver_handle_t driver,
ze_device_handle_t *device_);

int utils_ze_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 utils_ze_level_zero_copy(ze_context_handle_t context,
ze_device_handle_t device, void *dst_ptr,
const void *src_ptr, size_t size);

int utils_ze_create_context(ze_driver_handle_t driver,
ze_context_handle_t *context);

int utils_ze_destroy_context(ze_context_handle_t context);

ze_memory_type_t utils_ze_get_mem_type(ze_context_handle_t context, void *ptr);

#ifdef __cplusplus
}
#endif

#endif // UMF_UTILS_LEVEL_ZERO_H
Loading
Loading