Skip to content

Commit 841cd4b

Browse files
committed
Move level_zero_helpers to utils and use it in benchmarks
1) Add utils_ze_ prefix to all L0 helper functions. 2) Move level_zero_helpers to utils and rename it to utils_level_zero. 3) Use new utils_level_zero in benchmarks. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 5108356 commit 841cd4b

10 files changed

+144
-127
lines changed

benchmark/CMakeLists.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ function(add_umf_benchmark)
4242
LIBS ${BENCH_LIBS})
4343

4444
target_include_directories(
45-
${BENCH_NAME}
46-
PRIVATE ${UMF_CMAKE_SOURCE_DIR}/include
47-
${UMF_CMAKE_SOURCE_DIR}/src/utils
48-
${UMF_CMAKE_SOURCE_DIR}/test/common
49-
${UMF_CMAKE_SOURCE_DIR}/examples/common)
45+
${BENCH_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/include
46+
${UMF_CMAKE_SOURCE_DIR}/src/utils)
5047

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

@@ -84,6 +81,9 @@ function(add_umf_benchmark)
8481
if(UMF_BUILD_LEVEL_ZERO_PROVIDER)
8582
target_compile_definitions(${BENCH_NAME}
8683
PRIVATE UMF_BUILD_LEVEL_ZERO_PROVIDER=1)
84+
target_include_directories(
85+
${BENCH_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/test/common
86+
${LEVEL_ZERO_INCLUDE_DIRS})
8787
endif()
8888
if(UMF_BUILD_CUDA_PROVIDER)
8989
target_compile_definitions(${BENCH_NAME}
@@ -108,6 +108,7 @@ if(LINUX)
108108
set(LIBS_OPTIONAL ${LIBS_OPTIONAL} m)
109109
endif()
110110
if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER)
111+
set(SRCS_OPTIONAL ${SRCS_OPTIONAL} ../src/utils/utils_level_zero.cpp)
111112
set(LIBS_OPTIONAL ${LIBS_OPTIONAL} ze_loader)
112113
# TODO add CUDA
113114
endif()
@@ -116,7 +117,7 @@ endif()
116117

117118
add_umf_benchmark(
118119
NAME ubench
119-
SRCS ubench.c
120+
SRCS ubench.c ${SRCS_OPTIONAL}
120121
LIBS ${LIBS_OPTIONAL}
121122
LIBDIRS ${LIB_DIRS})
122123

benchmark/ubench.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#if (defined UMF_BUILD_LIBUMF_POOL_DISJOINT && \
3434
defined UMF_BUILD_LEVEL_ZERO_PROVIDER && defined UMF_BUILD_GPU_TESTS)
35-
#include "examples_level_zero.h"
35+
#include "utils_level_zero.h"
3636
#endif
3737

3838
// NOTE: with strict compilation flags, ubench compilation throws some
@@ -450,28 +450,28 @@ int create_level_zero_params(ze_context_handle_t *context,
450450
uint32_t driver_idx = 0;
451451
ze_driver_handle_t driver = NULL;
452452

453-
int ret = init_level_zero();
453+
int ret = utils_ze_init_level_zero();
454454
if (ret != 0) {
455455
fprintf(stderr, "Failed to init Level 0!\n");
456456
return ret;
457457
}
458458

459-
ret = find_driver_with_gpu(&driver_idx, &driver);
459+
ret = utils_ze_find_driver_with_gpu(&driver_idx, &driver);
460460
if (ret || driver == NULL) {
461461
fprintf(stderr, "Cannot find L0 driver with GPU device!\n");
462462
return ret;
463463
}
464464

465-
ret = create_context(driver, context);
465+
ret = utils_ze_create_context(driver, context);
466466
if (ret != 0) {
467467
fprintf(stderr, "Failed to create L0 context!\n");
468468
return ret;
469469
}
470470

471-
ret = find_gpu_device(driver, device);
471+
ret = utils_ze_find_gpu_device(driver, device);
472472
if (ret) {
473473
fprintf(stderr, "Cannot find GPU device!\n");
474-
destroy_context(*context);
474+
utils_ze_destroy_context(*context);
475475
return ret;
476476
}
477477

@@ -628,7 +628,7 @@ UBENCH_EX(ipc, disjoint_pool_with_level_zero_provider) {
628628
umfLevelZeroMemoryProviderParamsDestroy(level_zero_params);
629629

630630
err_destroy_context:
631-
destroy_context(context);
631+
utils_ze_destroy_context(context);
632632
}
633633
#endif /* (defined UMF_BUILD_LIBUMF_POOL_DISJOINT && defined UMF_BUILD_LEVEL_ZERO_PROVIDER && defined UMF_BUILD_GPU_TESTS) */
634634

test/providers/level_zero_helpers.cpp renamed to src/utils/utils_level_zero.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8-
#include "level_zero_helpers.h"
8+
#include "utils_level_zero.h"
99

1010
#include <memory>
1111
#include <stdlib.h>
@@ -297,7 +297,7 @@ int InitLevelZeroOps() {
297297
}
298298
#endif // USE_DLOPEN
299299

300-
static int init_level_zero_lib(void) {
300+
static int utils_ze_init_level_zero_lib(void) {
301301
ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY;
302302
ze_result_t result = libze_ops.zeInit(flags);
303303
if (result != ZE_RESULT_SUCCESS) {
@@ -309,29 +309,30 @@ static int init_level_zero_lib(void) {
309309
static UTIL_ONCE_FLAG level_zero_init_flag = UTIL_ONCE_FLAG_INIT;
310310
static int InitResult;
311311

312-
static void init_level_zero_once(void) {
312+
static void utils_ze_init_level_zero_once(void) {
313313
InitResult = InitLevelZeroOps();
314314
if (InitResult != 0) {
315315
return;
316316
}
317-
InitResult = init_level_zero_lib();
317+
InitResult = utils_ze_init_level_zero_lib();
318318
}
319319

320-
static int init_level_zero(void) {
321-
utils_init_once(&level_zero_init_flag, init_level_zero_once);
320+
int utils_ze_init_level_zero(void) {
321+
utils_init_once(&level_zero_init_flag, utils_ze_init_level_zero_once);
322322

323323
return InitResult;
324324
}
325325

326-
int get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_) {
326+
int utils_ze_get_drivers(uint32_t *drivers_num_,
327+
ze_driver_handle_t **drivers_) {
327328
int ret = 0;
328329
ze_result_t ze_result;
329330
ze_driver_handle_t *drivers = NULL;
330331
uint32_t drivers_num = 0;
331332

332-
ret = init_level_zero();
333+
ret = utils_ze_init_level_zero();
333334
if (ret != 0) {
334-
fprintf(stderr, "init_level_zero() failed!\n");
335+
fprintf(stderr, "utils_ze_init_level_zero() failed!\n");
335336
goto init_fail;
336337
}
337338

@@ -375,16 +376,16 @@ int get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_) {
375376
return ret;
376377
}
377378

378-
int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
379-
ze_device_handle_t **devices_) {
379+
int utils_ze_get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
380+
ze_device_handle_t **devices_) {
380381
ze_result_t ze_result;
381382
int ret = 0;
382383
uint32_t devices_num = 0;
383384
ze_device_handle_t *devices = NULL;
384385

385-
ret = init_level_zero();
386+
ret = utils_ze_init_level_zero();
386387
if (ret != 0) {
387-
fprintf(stderr, "init_level_zero() failed!\n");
388+
fprintf(stderr, "utils_ze_init_level_zero() failed!\n");
388389
goto init_fail;
389390
}
390391

@@ -427,15 +428,16 @@ int get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
427428
return ret;
428429
}
429430

430-
int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
431+
int utils_ze_find_driver_with_gpu(uint32_t *driver_idx,
432+
ze_driver_handle_t *driver_) {
431433
int ret = 0;
432434
ze_result_t ze_result;
433435
uint32_t drivers_num = 0;
434436
ze_device_handle_t *devices = NULL;
435437
ze_driver_handle_t *drivers = NULL;
436438
ze_driver_handle_t driver_with_gpus = NULL;
437439

438-
ret = get_drivers(&drivers_num, &drivers);
440+
ret = utils_ze_get_drivers(&drivers_num, &drivers);
439441
if (ret) {
440442
goto fn_fail;
441443
}
@@ -445,7 +447,7 @@ int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
445447
uint32_t devices_num = 0;
446448
ze_driver_handle_t driver = drivers[i];
447449

448-
ret = get_devices(driver, &devices_num, &devices);
450+
ret = utils_ze_get_devices(driver, &devices_num, &devices);
449451
if (ret) {
450452
goto fn_fail;
451453
}
@@ -494,13 +496,14 @@ int find_driver_with_gpu(uint32_t *driver_idx, ze_driver_handle_t *driver_) {
494496
return ret;
495497
}
496498

497-
int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_) {
499+
int utils_ze_find_gpu_device(ze_driver_handle_t driver,
500+
ze_device_handle_t *device_) {
498501
int ret = -1;
499502
uint32_t devices_num = 0;
500503
ze_device_handle_t *devices = NULL;
501504
ze_device_handle_t device;
502505

503-
ret = get_devices(driver, &devices_num, &devices);
506+
ret = utils_ze_get_devices(driver, &devices_num, &devices);
504507
if (ret) {
505508
return ret;
506509
}
@@ -532,9 +535,9 @@ int find_gpu_device(ze_driver_handle_t driver, ze_device_handle_t *device_) {
532535
return ret;
533536
}
534537

535-
int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device,
536-
void *ptr, size_t size, const void *pattern,
537-
size_t pattern_size) {
538+
int utils_ze_level_zero_fill(ze_context_handle_t context,
539+
ze_device_handle_t device, void *ptr, size_t size,
540+
const void *pattern, size_t pattern_size) {
538541
int ret = 0;
539542

540543
ze_command_queue_desc_t commandQueueDesc = {
@@ -618,8 +621,9 @@ int level_zero_fill(ze_context_handle_t context, ze_device_handle_t device,
618621
return ret;
619622
}
620623

621-
int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device,
622-
void *dst_ptr, const void *src_ptr, size_t size) {
624+
int utils_ze_level_zero_copy(ze_context_handle_t context,
625+
ze_device_handle_t device, void *dst_ptr,
626+
const void *src_ptr, size_t size) {
623627
int ret = 0;
624628
ze_command_queue_desc_t commandQueueDesc = {
625629
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
@@ -701,7 +705,8 @@ int level_zero_copy(ze_context_handle_t context, ze_device_handle_t device,
701705
return ret;
702706
}
703707

704-
int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) {
708+
int utils_ze_create_context(ze_driver_handle_t driver,
709+
ze_context_handle_t *context) {
705710
ze_result_t ze_result;
706711
ze_context_desc_t ctxtDesc;
707712
ctxtDesc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
@@ -717,7 +722,7 @@ int create_context(ze_driver_handle_t driver, ze_context_handle_t *context) {
717722
return 0;
718723
}
719724

720-
int destroy_context(ze_context_handle_t context) {
725+
int utils_ze_destroy_context(ze_context_handle_t context) {
721726
ze_result_t ze_result;
722727
ze_result = libze_ops.zeContextDestroy(context);
723728
if (ze_result != ZE_RESULT_SUCCESS) {
@@ -728,7 +733,7 @@ int destroy_context(ze_context_handle_t context) {
728733
return 0;
729734
}
730735

731-
ze_memory_type_t get_mem_type(ze_context_handle_t context, void *ptr) {
736+
ze_memory_type_t utils_ze_get_mem_type(ze_context_handle_t context, void *ptr) {
732737
ze_device_handle_t device = NULL;
733738
ze_memory_allocation_properties_t alloc_props;
734739
alloc_props.stype = ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES;

src/utils/utils_level_zero.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_UTILS_LEVEL_ZERO_H
9+
#define UMF_UTILS_LEVEL_ZERO_H
10+
11+
#include <umf/providers/provider_level_zero.h>
12+
13+
#include "ze_api.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
int utils_ze_init_level_zero(void);
20+
int utils_ze_init_level_zero(void);
21+
22+
int utils_ze_get_drivers(uint32_t *drivers_num_, ze_driver_handle_t **drivers_);
23+
24+
int utils_ze_get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
25+
ze_device_handle_t **devices_);
26+
27+
int utils_ze_find_driver_with_gpu(uint32_t *driver_idx,
28+
ze_driver_handle_t *driver_);
29+
30+
int utils_ze_find_gpu_device(ze_driver_handle_t driver,
31+
ze_device_handle_t *device_);
32+
33+
int utils_ze_level_zero_fill(ze_context_handle_t context,
34+
ze_device_handle_t device, void *ptr, size_t size,
35+
const void *pattern, size_t pattern_size);
36+
37+
int utils_ze_level_zero_copy(ze_context_handle_t context,
38+
ze_device_handle_t device, void *dst_ptr,
39+
const void *src_ptr, size_t size);
40+
41+
int utils_ze_create_context(ze_driver_handle_t driver,
42+
ze_context_handle_t *context);
43+
44+
int utils_ze_destroy_context(ze_context_handle_t context);
45+
46+
ze_memory_type_t utils_ze_get_mem_type(ze_context_handle_t context, void *ptr);
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif // UMF_UTILS_LEVEL_ZERO_H

test/CMakeLists.txt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ FetchContent_MakeAvailable(googletest)
2222
enable_testing()
2323

2424
set(UMF_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
25+
set(UMF_UTILS_DIR ${UMF_CMAKE_SOURCE_DIR}/src/utils)
2526

2627
function(build_umf_test)
2728
# Parameters: * NAME - a name of the test * SRCS - source files * LIBS -
@@ -136,20 +137,22 @@ if(UMF_BUILD_SHARED_LIBRARY)
136137
set(UMF_UTILS_FOR_TEST umf_utils)
137138
if(LINUX OR MACOSX)
138139
set(UMF_UTILS_SOURCES
139-
../src/utils/utils_common.c ../src/utils/utils_posix_common.c
140-
../src/utils/utils_posix_concurrency.c)
140+
${UMF_UTILS_DIR}/utils_common.c
141+
${UMF_UTILS_DIR}/utils_posix_common.c
142+
${UMF_UTILS_DIR}/utils_posix_concurrency.c)
141143
if(LINUX)
142144
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES}
143-
../src/utils/utils_linux_common.c)
145+
${UMF_UTILS_DIR}/utils_linux_common.c)
144146
set(UMF_LOGGER_LIBS rt) # librt for shm_open()
145147
elseif(MACOSX)
146148
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES}
147-
../src/utils/utils_macosx_common.c)
149+
${UMF_UTILS_DIR}/utils_macosx_common.c)
148150
endif()
149151
elseif(WINDOWS)
150152
set(UMF_UTILS_SOURCES
151-
../src/utils/utils_common.c ../src/utils/utils_windows_common.c
152-
../src/utils/utils_windows_concurrency.c)
153+
${UMF_UTILS_DIR}/utils_common.c
154+
${UMF_UTILS_DIR}/utils_windows_common.c
155+
${UMF_UTILS_DIR}/utils_windows_concurrency.c)
153156
endif()
154157
endif()
155158

@@ -358,16 +361,16 @@ if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER)
358361
# dlopen)
359362
add_umf_test(
360363
NAME provider_level_zero
361-
SRCS providers/provider_level_zero.cpp providers/level_zero_helpers.cpp
362-
${BA_SOURCES_FOR_TEST}
364+
SRCS providers/provider_level_zero.cpp
365+
${UMF_UTILS_DIR}/utils_level_zero.cpp ${BA_SOURCES_FOR_TEST}
363366
LIBS ${UMF_UTILS_FOR_TEST} ze_loader)
364367
target_include_directories(umf_test-provider_level_zero
365368
PRIVATE ${LEVEL_ZERO_INCLUDE_DIRS})
366369

367370
add_umf_test(
368371
NAME provider_level_zero_dlopen
369-
SRCS providers/provider_level_zero.cpp providers/level_zero_helpers.cpp
370-
${BA_SOURCES_FOR_TEST}
372+
SRCS providers/provider_level_zero.cpp
373+
${UMF_UTILS_DIR}/utils_level_zero.cpp ${BA_SOURCES_FOR_TEST}
371374
LIBS ${UMF_UTILS_FOR_TEST})
372375
target_compile_definitions(umf_test-provider_level_zero_dlopen
373376
PUBLIC USE_DLOPEN=1)
@@ -580,7 +583,7 @@ if(LINUX)
580583
providers/ipc_level_zero_prov_consumer.c
581584
common/ipc_common.c
582585
providers/ipc_level_zero_prov_common.c
583-
providers/level_zero_helpers.cpp
586+
${UMF_UTILS_DIR}/utils_level_zero.cpp
584587
LIBS
585588
ze_loader
586589
disjoint_pool
@@ -592,7 +595,7 @@ if(LINUX)
592595
providers/ipc_level_zero_prov_producer.c
593596
common/ipc_common.c
594597
providers/ipc_level_zero_prov_common.c
595-
providers/level_zero_helpers.cpp
598+
${UMF_UTILS_DIR}/utils_level_zero.cpp
596599
LIBS
597600
ze_loader
598601
disjoint_pool

0 commit comments

Comments
 (0)