Skip to content

Commit 84baf5b

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

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

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" ON)
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 <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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ if(UMF_BUILD_OS_MEMORY_PROVIDER)
5858
endif()
5959
endif()
6060

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

0 commit comments

Comments
 (0)