Skip to content

Commit eddecb1

Browse files
committed
add Level Zero memory provider
1 parent 57ae985 commit eddecb1

File tree

10 files changed

+247
-0
lines changed

10 files changed

+247
-0
lines changed

.github/workflows/gpu.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: GPU
2+
3+
on: workflow_call
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
ubuntu-build:
10+
name: Build - Ubuntu
11+
12+
strategy:
13+
matrix:
14+
os: ['ubuntu-22.04']
15+
build_type: [Release]
16+
compiler: [{c: gcc, cxx: g++}]
17+
shared_library: ['ON', 'OFF']
18+
runs-on: ${{matrix.os}}
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
23+
24+
- name: Install apt packages
25+
run: |
26+
sudo apt-get update
27+
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev level-zero-dev
28+
29+
- name: Configure build
30+
run: >
31+
cmake
32+
-B ${{github.workspace}}/build
33+
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
34+
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}
35+
-DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}}
36+
-DUMF_BUILD_SHARED_LIBRARY=${{matrix.shared_library}}
37+
-DUMF_BUILD_BENCHMARKS=ON
38+
-DUMF_BUILD_TESTS=ON
39+
-DUMF_FORMAT_CODE_STYLE=ON
40+
-DUMF_DEVELOPER_MODE=ON
41+
-DUMF_BUILD_ZE_MEMORY_PROVIDER=ON
42+
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
43+
-DUMF_ENABLE_POOL_TRACKING=ON
44+
45+
- name: Build UMF
46+
run: cmake --build ${{github.workspace}}/build -j $(nproc)

.github/workflows/pr_push.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ jobs:
8585
Benchmark:
8686
needs: [Build]
8787
uses: ./.github/workflows/benchmarks.yml
88+
GPU:
89+
needs: [Build]
90+
uses: ./.github/workflows/gpu.yml

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ project(unified-memory-framework VERSION 0.1.0 LANGUAGES C)
88
# Build Options
99
option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
1010
option(UMF_BUILD_OS_MEMORY_PROVIDER "Build OS memory provider" ON)
11+
option(UMF_BUILD_ZE_MEMORY_PROVIDER "Build Level Zero memory provider" OFF)
1112
option(UMF_BUILD_LIBUMF_POOL_DISJOINT "Build the libumf_pool_disjoint static library" OFF)
1213
option(UMF_BUILD_LIBUMF_POOL_JEMALLOC "Build the libumf_pool_jemalloc static library" OFF)
1314
option(UMF_BUILD_LIBUMF_POOL_SCALABLE "Build the libumf_pool_scalable static library" OFF)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,20 @@ A memory provider that provides memory from an operating system.
3030

3131
1) Linux OS
3232
2) The `UMF_BUILD_OS_MEMORY_PROVIDER` option turned `ON` (by default)
33+
TODO
3334
3) Required packages:
3435
- libhwloc-dev
3536
4) Required packages for tests:
3637
- libnuma-dev
3738

39+
### Level Zero memory provider (Linux-only yet)
40+
41+
TODO
42+
43+
#### Requirements
44+
45+
TODO
46+
3847
## Memory pool managers
3948

4049
### proxy_pool (part of libumf)
@@ -153,6 +162,7 @@ List of options provided by CMake:
153162
| - | - | - | - |
154163
| UMF_BUILD_SHARED_LIBRARY | Build UMF as shared library | ON/OFF | OFF |
155164
| UMF_BUILD_OS_MEMORY_PROVIDER | Build OS memory provider | ON/OFF | ON |
165+
| UMF_BUILD_ZE_MEMORY_PROVIDER | Build Level Zero memory provider | ON/OFF | OFF |
156166
| UMF_BUILD_LIBUMF_POOL_DISJOINT | Build the libumf_pool_disjoint static library | ON/OFF | OFF |
157167
| UMF_BUILD_LIBUMF_POOL_JEMALLOC | Build the libumf_pool_jemalloc static library | ON/OFF | OFF |
158168
| UMF_BUILD_LIBUMF_POOL_SCALABLE | Build the libumf_pool_scalable static library | ON/OFF | OFF |

benchmark/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ if (UMF_BUILD_OS_MEMORY_PROVIDER)
3838
target_compile_definitions(ubench PRIVATE UMF_BUILD_OS_MEMORY_PROVIDER=1)
3939
endif()
4040

41+
if (UMF_BUILD_ZE_MEMORY_PROVIDER)
42+
target_compile_definitions(ubench PRIVATE UMF_BUILD_ZE_MEMORY_PROVIDER=1)
43+
endif()
44+
4145
if (UMF_BUILD_LIBUMF_POOL_DISJOINT)
4246
target_compile_definitions(ubench PRIVATE UMF_BUILD_LIBUMF_POOL_DISJOINT=1)
4347
endif()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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_ZE_MEMORY_PROVIDER_H
9+
#define UMF_ZE_MEMORY_PROVIDER_H
10+
11+
#include <level_zero/ze_api.h>
12+
13+
#include "umf/memory_provider.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
/// @brief ze memory provider settings struct
20+
typedef struct ze_memory_provider_params_t {
21+
ze_context_handle_t context;
22+
ze_device_handle_t device;
23+
ze_device_mem_alloc_desc_t device_mem_alloc_desc;
24+
} ze_memory_provider_params_t;
25+
26+
umf_memory_provider_ops_t *umfZeMemoryProviderOps(void);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
32+
#endif /* UMF_ZE_MEMORY_PROVIDER_H */

src/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ if(UMF_BUILD_OS_MEMORY_PROVIDER)
5858
endif()
5959
endif()
6060

61+
# TODO static lib!!
62+
if(UMF_BUILD_ZE_MEMORY_PROVIDER)
63+
set(UMF_SOURCES_LINUX ${UMF_SOURCES_LINUX}
64+
provider/provider_ze_memory.c)
65+
if(LINUX)
66+
set(UMF_LIBS ${UMF_LIBS} ze_loader)
67+
endif()
68+
endif()
69+
6170
if(LINUX)
6271
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_LINUX})
6372
elseif(WINDOWS)

src/libumf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ UMF_1.0 {
3535
umfMemoryProviderCreateFromMemspace;
3636
umfMemspaceCreateFromNumaArray;
3737
umfMemspaceDestroy;
38+
umfZeMemoryProviderOps;
3839
local:
3940
*;
4041
};

src/provider/provider_ze_memory.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
#include <assert.h>
9+
#include <stddef.h>
10+
#include <stdio.h> // TODO remove
11+
#include <stdlib.h> // TODO remove
12+
#include <unistd.h>
13+
14+
#include <umf.h>
15+
#include <umf/memory_provider_ops.h>
16+
#include <umf/providers/provider_ze_memory.h>
17+
18+
typedef struct ze_memory_provider_t {
19+
ze_context_handle_t context;
20+
ze_device_handle_t device;
21+
ze_device_mem_alloc_desc_t device_mem_alloc_desc;
22+
23+
} ze_memory_provider_t;
24+
25+
enum umf_result_t ze_memory_provider_initialize(void *params, void **provider) {
26+
ze_memory_provider_params_t *ze_params =
27+
(ze_memory_provider_params_t *)params;
28+
29+
// TODO use ba_alloc here
30+
ze_memory_provider_t *ze_provider =
31+
(ze_memory_provider_t *)malloc(sizeof(ze_memory_provider_t));
32+
if (!ze_provider) {
33+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
34+
}
35+
36+
ze_provider->context = ze_params->context;
37+
ze_provider->device = ze_params->device;
38+
ze_provider->device_mem_alloc_desc = ze_params->device_mem_alloc_desc;
39+
40+
assert(ze_provider->device_mem_alloc_desc.stype ==
41+
ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC);
42+
assert(ze_params->device_mem_alloc_desc.pNext == NULL);
43+
44+
*provider = ze_provider;
45+
46+
return UMF_RESULT_SUCCESS;
47+
}
48+
49+
void ze_memory_provider_finalize(void *provider) {
50+
assert(provider);
51+
ze_memory_provider_t *ze_provider = (struct ze_memory_provider_t *)provider;
52+
53+
free(ze_provider);
54+
}
55+
56+
static enum umf_result_t ze_memory_provider_alloc(void *provider, size_t size,
57+
size_t alignment,
58+
void **resultPtr) {
59+
assert(provider);
60+
assert(resultPtr);
61+
62+
ze_memory_provider_t *ze_provider = (struct ze_memory_provider_t *)provider;
63+
zeMemAllocDevice(ze_provider->context, &ze_provider->device_mem_alloc_desc,
64+
size, alignment, ze_provider->device, resultPtr);
65+
66+
return UMF_RESULT_SUCCESS;
67+
}
68+
69+
static enum umf_result_t ze_memory_provider_free(void *provider, void *ptr,
70+
size_t bytes) {
71+
(void)bytes;
72+
73+
assert(provider);
74+
ze_memory_provider_t *ze_provider = (struct ze_memory_provider_t *)provider;
75+
zeMemFree(ze_provider->context, ptr);
76+
77+
return UMF_RESULT_SUCCESS;
78+
}
79+
80+
void ze_memory_provider_get_last_native_error(void *provider,
81+
const char **ppMessage,
82+
int32_t *pError) {
83+
(void)provider;
84+
(void)ppMessage;
85+
(void)pError;
86+
// TODO
87+
}
88+
89+
static enum umf_result_t
90+
ze_memory_provider_get_min_page_size(void *provider, void *ptr,
91+
size_t *pageSize) {
92+
(void)provider;
93+
(void)ptr;
94+
95+
*pageSize = 1024 * 4; // TODO
96+
return UMF_RESULT_SUCCESS;
97+
}
98+
99+
static enum umf_result_t
100+
ze_memory_provider_get_recommended_page_size(void *provider, size_t size,
101+
size_t *pageSize) {
102+
(void)provider;
103+
(void)size;
104+
105+
*pageSize = 1024 * 4; // TODO
106+
return UMF_RESULT_SUCCESS;
107+
}
108+
109+
const char *ze_memory_provider_get_name(void *provider) {
110+
(void)provider;
111+
return "UMF_ZE_PROVIDER";
112+
}
113+
114+
static enum umf_result_t ze_memory_provider_allocation_merge(void *hProvider,
115+
void *lowPtr,
116+
void *highPtr,
117+
size_t totalSize) {
118+
(void)hProvider;
119+
(void)lowPtr;
120+
(void)highPtr;
121+
(void)totalSize;
122+
123+
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
124+
}
125+
126+
struct umf_memory_provider_ops_t umf_ze_memory_provider_ops = {
127+
.version = UMF_VERSION_CURRENT,
128+
.initialize = ze_memory_provider_initialize,
129+
.finalize = ze_memory_provider_finalize,
130+
.alloc = ze_memory_provider_alloc,
131+
.free = ze_memory_provider_free,
132+
.get_last_native_error = ze_memory_provider_get_last_native_error,
133+
.get_recommended_page_size = ze_memory_provider_get_recommended_page_size,
134+
.get_min_page_size = ze_memory_provider_get_min_page_size,
135+
.purge_lazy = NULL,
136+
.purge_force = NULL,
137+
.get_name = ze_memory_provider_get_name,
138+
.allocation_split = NULL,
139+
.allocation_merge = ze_memory_provider_allocation_merge,
140+
};

test/test_make_install.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
./include/umf/pools/pool_scalable.h
1717
./include/umf/providers
1818
./include/umf/providers/provider_os_memory.h
19+
./include/umf/providers/provider_ze_memory.h
1920
./lib
2021
./lib/cmake
2122
./lib/cmake/unified-memory-framework

0 commit comments

Comments
 (0)