Skip to content

Commit ab608f8

Browse files
committed
add an example for mix of Disjoint Pool and L0
1 parent af5050f commit ab608f8

File tree

7 files changed

+210
-3
lines changed

7 files changed

+210
-3
lines changed

.github/workflows/gpu.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
-DUMF_BUILD_BENCHMARKS=ON
4242
-DUMF_BUILD_TESTS=ON
4343
-DUMF_BUILD_GPU_TESTS=ON
44+
-DUMF_BUILD_GPU_EXAMPLES=ON
4445
-DUMF_FORMAT_CODE_STYLE=ON
4546
-DUMF_DEVELOPER_MODE=ON
4647
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ option(UMF_BUILD_GPU_TESTS "Build UMF GPU tests" OFF)
2222
option(UMF_BUILD_BENCHMARKS "Build UMF benchmarks" OFF)
2323
option(UMF_BUILD_BENCHMARKS_MT "Build UMF multithreaded benchmarks" OFF)
2424
option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
25+
option(UMF_BUILD_GPU_EXAMPLES "Build UMF GPU examples" OFF)
2526
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
2627
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
2728
OFF)
@@ -398,7 +399,7 @@ endif()
398399
install(FILES ${CMAKE_SOURCE_DIR}/LICENSE.TXT
399400
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}/")
400401

401-
install(FILES examples/basic/basic.c
402+
install(FILES examples/advanced/disjoint_level_zero.c examples/basic/basic.c
402403
DESTINATION "${CMAKE_INSTALL_DOCDIR}/examples")
403404

404405
# Add the include directory and the headers target to the install.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ List of options provided by CMake:
100100
| UMF_BUILD_GPU_TESTS | Build UMF GPU tests | ON/OFF | OFF |
101101
| UMF_BUILD_BENCHMARKS | Build UMF benchmarks | ON/OFF | OFF |
102102
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
103+
| UMF_BUILD_GPU_EXAMPLES | Build UMF GPU examples | ON/OFF | OFF |
103104
| UMF_ENABLE_POOL_TRACKING | Build UMF with pool tracking | ON/OFF | ON |
104105
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
105106
| UMF_FORMAT_CODE_STYLE | Add clang and cmake -format-check and -format-apply targets to make | ON/OFF | OFF |

examples/CMakeLists.txt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,41 @@ if(UMF_BUILD_LIBUMF_POOL_SCALABLE AND UMF_ENABLE_POOL_TRACKING)
2323

2424
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example")
2525

26+
if(WINDOWS)
27+
# append PATH to DLLs
28+
set_property(TEST ${EXAMPLE_NAME} PROPERTY ENVIRONMENT_MODIFICATION
29+
"${DLL_PATH_LIST}")
30+
endif()
31+
else()
32+
message(STATUS "Basic example requires UMF_BUILD_LIBUMF_POOL_SCALABLE and "
33+
"UMF_ENABLE_POOL_TRACKING to be turned ON - skipping")
34+
endif()
35+
36+
if(UMF_BUILD_GPU_EXAMPLES
37+
AND UMF_BUILD_LIBUMF_POOL_DISJOINT
38+
AND UMF_BUILD_LEVEL_ZERO_PROVIDER
39+
AND LINUX)
40+
set(EXAMPLE_NAME umf_example_disjoint_level_zero)
41+
42+
add_umf_executable(
43+
NAME ${EXAMPLE_NAME}
44+
SRCS advanced/disjoint_level_zero.c
45+
LIBS umf disjoint_pool ze_loader)
46+
47+
target_include_directories(
48+
${EXAMPLE_NAME}
49+
PRIVATE ${LEVEL_ZERO_INCLUDE_DIRS} ${UMF_CMAKE_SOURCE_DIR}/src/utils
50+
${UMF_CMAKE_SOURCE_DIR}/include)
51+
52+
target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
53+
54+
add_test(
55+
NAME ${EXAMPLE_NAME}
56+
COMMAND ${EXAMPLE_NAME}
57+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
58+
59+
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example")
60+
2661
if(WINDOWS)
2762
# append PATH to DLLs
2863
set_property(TEST ${EXAMPLE_NAME} PROPERTY ENVIRONMENT_MODIFICATION
@@ -31,6 +66,7 @@ if(UMF_BUILD_LIBUMF_POOL_SCALABLE AND UMF_ENABLE_POOL_TRACKING)
3166
else()
3267
message(
3368
STATUS
34-
"Basic example requires UMF_BUILD_LIBUMF_POOL_SCALABLE and UMF_ENABLE_POOL_TRACKING
35-
to be turned ON - skipping")
69+
"Disjoint - Level Zero example requires "
70+
"UMF_BUILD_LIBUMF_POOL_DISJOINT and UMF_BUILD_LEVEL_ZERO_PROVIDER "
71+
"to be turned ON - skipping")
3672
endif()

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ This example covers the basics of UMF API. It walks you through a basic usage of
99
### Required CMake configuration flags
1010
* UMF_BUILD_LIBUMF_POOL_SCALABLE=ON
1111
* UMF_ENABLE_POOL_TRACKING=ON
12+
13+
## Disjoint Pool / Level Zero Provider
14+
15+
TODO
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include <assert.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <ze_api.h>
14+
15+
#include "umf/memory_pool.h"
16+
#include "umf/pools/pool_disjoint.h"
17+
#include "umf/providers/provider_level_zero.h"
18+
19+
ze_result_t level_zero_init(ze_driver_handle_t *driver,
20+
ze_device_handle_t *device,
21+
ze_context_handle_t *context) {
22+
// Initialize the Level Zero driver
23+
ze_result_t ze_result = zeInit(0);
24+
assert(ze_result == ZE_RESULT_SUCCESS);
25+
26+
// Discover all the driver instances
27+
uint32_t driverCount = 0;
28+
ze_result = zeDriverGet(&driverCount, NULL);
29+
assert(ze_result == ZE_RESULT_SUCCESS);
30+
assert(driverCount > 0);
31+
32+
ze_driver_handle_t *all_drivers =
33+
(ze_driver_handle_t *)calloc(driverCount, sizeof(ze_driver_handle_t));
34+
ze_result = zeDriverGet(&driverCount, all_drivers);
35+
assert(ze_result == ZE_RESULT_SUCCESS);
36+
37+
// Find a driver instance with a GPU device
38+
for (uint32_t i = 0; i < driverCount; ++i) {
39+
assert(all_drivers[i] != NULL);
40+
41+
uint32_t deviceCount = 0;
42+
ze_result = zeDeviceGet(all_drivers[i], &deviceCount, NULL);
43+
assert(ze_result == ZE_RESULT_SUCCESS);
44+
assert(deviceCount > 0);
45+
46+
ze_device_handle_t *all_devices = (ze_device_handle_t *)calloc(
47+
deviceCount, sizeof(ze_device_handle_t));
48+
ze_result = zeDeviceGet(all_drivers[i], &deviceCount, all_devices);
49+
assert(ze_result == ZE_RESULT_SUCCESS);
50+
51+
for (uint32_t d = 0; d < deviceCount; ++d) {
52+
assert(all_devices[d] != NULL);
53+
54+
ze_device_properties_t device_properties = {0};
55+
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
56+
ze_result =
57+
zeDeviceGetProperties(all_devices[d], &device_properties);
58+
assert(ze_result == ZE_RESULT_SUCCESS);
59+
60+
/// todo free
61+
62+
if (ZE_DEVICE_TYPE_GPU == device_properties.type) {
63+
*driver = all_drivers[i];
64+
*device = all_devices[d];
65+
break;
66+
}
67+
}
68+
69+
if (NULL != driver) {
70+
break;
71+
}
72+
}
73+
74+
if (NULL == device) {
75+
// todo
76+
// GTEST_SKIP() << "Test skipped, no GPU devices found";
77+
}
78+
79+
// Create context
80+
ze_context_desc_t ctxtDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, NULL, 0};
81+
ze_result = zeContextCreate(*driver, &ctxtDesc, context);
82+
assert(ze_result == ZE_RESULT_SUCCESS);
83+
assert(*context != NULL);
84+
85+
return ze_result;
86+
}
87+
88+
int main(void) {
89+
90+
// A result object for storing UMF API result status
91+
umf_result_t res;
92+
93+
// Initialize Level Zero
94+
ze_driver_handle_t hDriver;
95+
ze_device_handle_t hDevice;
96+
ze_context_handle_t hContext;
97+
level_zero_init(&hDriver, &hDevice, &hContext);
98+
99+
// todo explain + why
100+
level_zero_memory_provider_params_t ze_memory_provider_params;
101+
ze_memory_provider_params.level_zero_context_handle = hContext;
102+
ze_memory_provider_params.level_zero_device_handle = hDevice;
103+
ze_memory_provider_params.memory_type = UMF_MEMORY_TYPE_SHARED;
104+
105+
umf_memory_provider_handle_t ze_memory_provider;
106+
res = umfMemoryProviderCreate(umfLevelZeroMemoryProviderOps(),
107+
&ze_memory_provider_params,
108+
&ze_memory_provider);
109+
if (res != UMF_RESULT_SUCCESS) {
110+
printf("Failed to create a memory provider!");
111+
return -1;
112+
}
113+
printf("Level Zero memory provider created at %p\n",
114+
(void *)ze_memory_provider);
115+
116+
// TODO
117+
umf_disjoint_pool_params_t disjoint_memory_pool_params =
118+
umfDisjointPoolParamsDefault();
119+
disjoint_memory_pool_params.SlabMinSize = 64 * 1024L;
120+
disjoint_memory_pool_params.Capacity = 1;
121+
disjoint_memory_pool_params.MaxPoolableSize = 64 * 1024L;
122+
disjoint_memory_pool_params.PoolTrace = 1;
123+
124+
umf_memory_pool_handle_t ze_disjoint_memory_pool;
125+
res = umfPoolCreate(umfDisjointPoolOps(), ze_memory_provider,
126+
&disjoint_memory_pool_params, UMF_POOL_CREATE_FLAG_NONE,
127+
&ze_disjoint_memory_pool);
128+
if (res != UMF_RESULT_SUCCESS) {
129+
printf("Failed to create a memory pool!");
130+
goto memory_provider_destroy;
131+
}
132+
133+
void *ptr = umfPoolMalloc(ze_disjoint_memory_pool, sizeof(int));
134+
if (res != UMF_RESULT_SUCCESS) {
135+
printf("Failed to allocate memory from the memory pool!");
136+
goto memory_pool_destroy;
137+
}
138+
139+
// Use allocated memory
140+
*(int *)ptr = 1;
141+
142+
// Free allocated memory
143+
res = umfFree(ptr);
144+
if (res != UMF_RESULT_SUCCESS) {
145+
printf("Failed to free memory to the pool!");
146+
goto memory_provider_destroy;
147+
}
148+
printf("Freed memory at %p\n", ptr);
149+
150+
umfPoolDestroy(ze_disjoint_memory_pool);
151+
umfMemoryProviderDestroy(ze_memory_provider);
152+
return 0;
153+
154+
memory_pool_destroy:
155+
umfPoolDestroy(ze_disjoint_memory_pool);
156+
157+
memory_provider_destroy:
158+
umfMemoryProviderDestroy(ze_memory_provider);
159+
return -1;
160+
161+
// todo free(all_drivers);
162+
}

test/test_installation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def _create_match_list(self) -> List[str]:
113113
for example_dir in examples_dirs
114114
for file_path in example_dir.iterdir()
115115
]
116+
# TODO - why we do not preserve example folders?
117+
examples = sorted(examples)
116118
examples.insert(0, "share/doc/umf/examples")
117119
share.extend(examples)
118120
share.append("share/doc/umf/LICENSE.TXT")

0 commit comments

Comments
 (0)