Skip to content

Commit 233b141

Browse files
committed
add mempolicy with support of interleave, bind and preferred
Signed-off-by: Łukasz Plewa <[email protected]>
1 parent 7f18587 commit 233b141

15 files changed

+257
-71
lines changed

include/umf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <umf/memory_pool.h>
1414
#include <umf/memory_provider.h>
15+
#include <umf/mempolicy.h>
1516
#include <umf/memspace.h>
16-
#include <umf/memspace_policy.h>
1717

1818
#endif /* UMF_UNIFIED_MEMORY_FRAMEWORK_H */

include/umf/mempolicy.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 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+
#ifndef UMF_MEMPOLICY_H
11+
#define UMF_MEMPOLICY_H 1
12+
13+
#include <umf/base.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
typedef struct umf_mempolicy_t *umf_mempolicy_handle_t;
20+
21+
#define UMF_MEMPOLICY_INTERLEAVE _UMF_MEMPOLICY_INTERLEAVE()
22+
#define UMF_MEMPOLICY_BIND _UMF_MEMPOLICY_BIND()
23+
#define UMF_MEMPOLICY_PREFERRED _UMF_MEMPOLICY_PREFERRED()
24+
25+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_INTERLEAVE(void);
26+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_BIND(void);
27+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_PREFERRED(void);
28+
29+
umf_result_t umfMempolicyCreateInterleave(size_t part_size,
30+
umf_mempolicy_handle_t *policy);
31+
32+
umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t policy);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* UMF_MEMSPACE_POLICY_H */

include/umf/memspace.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <umf/base.h>
1414
#include <umf/memory_pool.h>
1515
#include <umf/memory_provider.h>
16-
#include <umf/memspace_policy.h>
16+
#include <umf/mempolicy.h>
1717

1818
#ifdef __cplusplus
1919
extern "C" {
@@ -29,7 +29,7 @@ typedef struct umf_memspace_t *umf_memspace_handle_t;
2929
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
3030
///
3131
umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t hMemspace,
32-
umf_memspace_policy_handle_t hPolicy,
32+
umf_mempolicy_handle_t hPolicy,
3333
umf_memory_pool_handle_t *hPool);
3434

3535
///
@@ -41,7 +41,7 @@ umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t hMemspace,
4141
///
4242
umf_result_t
4343
umfMemoryProviderCreateFromMemspace(umf_memspace_handle_t hMemspace,
44-
umf_memspace_policy_handle_t hPolicy,
44+
umf_mempolicy_handle_t hPolicy,
4545
umf_memory_provider_handle_t *hProvider);
4646

4747
///

include/umf/memspace_policy.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ set(UMF_SOURCES
5858
memory_provider.c
5959
memory_provider_get_last_failed.c
6060
memory_target.c
61+
mempolicy.c
6162
memspace.c
6263
provider/provider_tracking.c
6364
critnib/critnib.c

src/memory_target.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
extern "C" {
1717
#endif
1818

19-
#include "base_alloc.h"
20-
2119
struct umf_memory_target_ops_t;
2220
typedef struct umf_memory_target_ops_t umf_memory_target_ops_t;
2321

src/memory_target_ops.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#ifndef UMF_MEMORY_TARGET_OPS_H
1111
#define UMF_MEMORY_TARGET_OPS_H 1
1212

13-
#include <stdbool.h>
14-
1513
#include <umf/base.h>
1614
#include <umf/memspace.h>
1715

@@ -31,14 +29,15 @@ typedef struct umf_memory_target_ops_t {
3129

3230
umf_result_t (*clone)(void *memoryTarget, void **outMemoryTarget);
3331

34-
umf_result_t (*pool_create_from_memspace)(
35-
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
36-
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool);
32+
umf_result_t (*pool_create_from_memspace)(umf_memspace_handle_t memspace,
33+
void **memoryTargets,
34+
size_t numTargets,
35+
umf_mempolicy_handle_t policy,
36+
umf_memory_pool_handle_t *pool);
3737

3838
umf_result_t (*memory_provider_create_from_memspace)(
3939
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
40-
umf_memspace_policy_handle_t policy,
41-
umf_memory_provider_handle_t *provider);
40+
umf_mempolicy_handle_t policy, umf_memory_provider_handle_t *provider);
4241

4342
umf_result_t (*get_capacity)(void *memoryTarget, size_t *capacity);
4443
} umf_memory_target_ops_t;

src/memory_targets/memory_target_numa.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "base_alloc.h"
1919
#include "base_alloc_global.h"
2020
#include "memory_target_numa.h"
21+
#include "mempolicy.h"
2122
#include "topology.h"
2223

2324
struct numa_memory_target_t {
@@ -47,10 +48,8 @@ static void numa_finalize(void *memTarget) { umf_ba_global_free(memTarget); }
4748

4849
static umf_result_t numa_memory_provider_create_from_memspace(
4950
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
50-
umf_memspace_policy_handle_t policy,
51-
umf_memory_provider_handle_t *provider) {
52-
// TODO: apply policy
53-
(void)policy;
51+
umf_mempolicy_handle_t policy, umf_memory_provider_handle_t *provider) {
52+
5453
struct numa_memory_target_t **numaTargets =
5554
(struct numa_memory_target_t **)memTargets;
5655

@@ -83,6 +82,21 @@ static umf_result_t numa_memory_provider_create_from_memspace(
8382
params.numa_list_len = numNodesProvider;
8483
params.numa_mode = UMF_NUMA_MODE_BIND;
8584

85+
if (policy) {
86+
switch (policy->type) {
87+
case UMF_MEMSPACE_POLICY_INTERLEAVE:
88+
params.numa_mode = UMF_NUMA_MODE_INTERLEAVE;
89+
params.part_size = policy->ops.part_size;
90+
break;
91+
case UMF_MEMSPACE_POLICY_BIND:
92+
params.numa_mode = UMF_NUMA_MODE_BIND;
93+
break;
94+
case UMF_MEMSPACE_POLICY_PREFERRED:
95+
params.numa_mode = UMF_NUMA_MODE_PREFERRED;
96+
break;
97+
}
98+
}
99+
86100
umf_memory_provider_handle_t numaProvider = NULL;
87101
int ret = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &params,
88102
&numaProvider);
@@ -100,7 +114,7 @@ static umf_result_t numa_memory_provider_create_from_memspace(
100114

101115
static umf_result_t numa_pool_create_from_memspace(
102116
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
103-
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool) {
117+
umf_mempolicy_handle_t policy, umf_memory_pool_handle_t *pool) {
104118
(void)memspace;
105119
(void)memTargets;
106120
(void)numTargets;

src/mempolicy.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
* Copyright (C) 2023-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 "mempolicy.h"
11+
#include "base_alloc_global.h"
12+
13+
static const umf_mempolicy_t umf_mempolicy_interleave_default = {
14+
.type = UMF_MEMSPACE_POLICY_INTERLEAVE,
15+
.ops = {.part_size = 0},
16+
};
17+
18+
static const umf_mempolicy_t umf_mempolicy_bind_default = {
19+
.type = UMF_MEMSPACE_POLICY_BIND,
20+
};
21+
22+
static const umf_mempolicy_t umf_mempolicy_preferred_default = {
23+
.type = UMF_MEMSPACE_POLICY_PREFERRED,
24+
};
25+
26+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_INTERLEAVE(void) {
27+
return (umf_mempolicy_handle_t)&umf_mempolicy_interleave_default;
28+
}
29+
30+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_BIND(void) {
31+
return (umf_mempolicy_handle_t)&umf_mempolicy_bind_default;
32+
}
33+
34+
const umf_mempolicy_handle_t _UMF_MEMPOLICY_PREFERRED(void) {
35+
return (umf_mempolicy_handle_t)&umf_mempolicy_preferred_default;
36+
}
37+
38+
umf_result_t umfMempolicyCreateInterleave(size_t part_size,
39+
umf_mempolicy_handle_t *policy) {
40+
if (policy == NULL) {
41+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
42+
}
43+
44+
*policy = umf_ba_global_alloc(sizeof(**policy));
45+
46+
if (*policy == NULL) {
47+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
48+
}
49+
50+
(*policy)->type = UMF_MEMSPACE_POLICY_INTERLEAVE;
51+
(*policy)->ops.part_size = part_size;
52+
return UMF_RESULT_SUCCESS;
53+
}
54+
55+
umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t policy) {
56+
umf_ba_global_free(policy);
57+
return UMF_RESULT_SUCCESS;
58+
}

src/mempolicy.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 <umf/mempolicy.h>
11+
12+
typedef enum umf_memspace_policy_membind_t {
13+
UMF_MEMSPACE_POLICY_INTERLEAVE,
14+
UMF_MEMSPACE_POLICY_BIND,
15+
UMF_MEMSPACE_POLICY_PREFERRED
16+
} umf_memspace_policy_membind_t;
17+
18+
typedef struct umf_mempolicy_t {
19+
umf_memspace_policy_membind_t type;
20+
union {
21+
size_t part_size;
22+
} ops;
23+
} umf_mempolicy_t;

src/memspace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static umf_result_t memoryTargetHandlesToPriv(umf_memspace_handle_t memspace,
5252
}
5353

5454
umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t memspace,
55-
umf_memspace_policy_handle_t policy,
55+
umf_mempolicy_handle_t policy,
5656
umf_memory_pool_handle_t *pool) {
5757
if (!memspace || !pool) {
5858
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -77,7 +77,7 @@ umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t memspace,
7777

7878
umf_result_t
7979
umfMemoryProviderCreateFromMemspace(umf_memspace_handle_t memspace,
80-
umf_memspace_policy_handle_t policy,
80+
umf_mempolicy_handle_t policy,
8181
umf_memory_provider_handle_t *provider) {
8282
if (!memspace || !provider) {
8383
return UMF_RESULT_ERROR_INVALID_ARGUMENT;

src/provider/provider_os_memory.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,6 @@
2626

2727
#define NODESET_STR_BUF_LEN 1024
2828

29-
typedef struct os_memory_provider_t {
30-
unsigned protection; // combination of OS-specific protection flags
31-
unsigned visibility; // memory visibility mode
32-
int fd; // file descriptor for memory mapping
33-
size_t size_fd; // size of file used for memory mapping
34-
size_t max_size_fd; // maximum size of file used for memory mapping
35-
// A critnib map storing (ptr, fd_offset + 1) pairs. We add 1 to fd_offset
36-
// in order to be able to store fd_offset equal 0, because
37-
// critnib_get() returns value or NULL, so a value cannot equal 0.
38-
// It is needed mainly in the get_ipc_handle and open_ipc_handle hooks
39-
// to mmap a specific part of a file.
40-
critnib *fd_offset_map;
41-
42-
// NUMA config
43-
hwloc_bitmap_t *nodeset;
44-
unsigned nodeset_len;
45-
char *nodeset_str_buf;
46-
hwloc_membind_policy_t numa_policy;
47-
int numa_flags; // combination of hwloc flags
48-
49-
size_t part_size;
50-
size_t alloc_sum; // sum of all allocations - used for manual interleaving
51-
hwloc_topology_t topo;
52-
} os_memory_provider_t;
53-
5429
#define TLS_MSG_BUF_LEN 1024
5530

5631
typedef struct os_last_native_error_t {

src/provider/provider_os_memory_internal.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#ifndef UMF_OS_MEMORY_PROVIDER_INTERNAL_H
99
#define UMF_OS_MEMORY_PROVIDER_INTERNAL_H
1010

11+
#include "critnib.h"
1112
#include "utils_common.h"
13+
#include <hwloc.h>
1214
#include <umf/providers/provider_os_memory.h>
13-
1415
#ifdef __cplusplus
1516
extern "C" {
1617
#endif
@@ -20,6 +21,31 @@ typedef enum umf_purge_advise_t {
2021
UMF_PURGE_FORCE,
2122
} umf_purge_advise_t;
2223

24+
typedef struct os_memory_provider_t {
25+
unsigned protection; // combination of OS-specific protection flags
26+
unsigned visibility; // memory visibility mode
27+
int fd; // file descriptor for memory mapping
28+
size_t size_fd; // size of file used for memory mapping
29+
size_t max_size_fd; // maximum size of file used for memory mapping
30+
// A critnib map storing (ptr, fd_offset + 1) pairs. We add 1 to fd_offset
31+
// in order to be able to store fd_offset equal 0, because
32+
// critnib_get() returns value or NULL, so a value cannot equal 0.
33+
// It is needed mainly in the get_ipc_handle and open_ipc_handle hooks
34+
// to mmap a specific part of a file.
35+
critnib *fd_offset_map;
36+
37+
// NUMA config
38+
hwloc_bitmap_t *nodeset;
39+
unsigned nodeset_len;
40+
char *nodeset_str_buf;
41+
hwloc_membind_policy_t numa_policy;
42+
int numa_flags; // combination of hwloc flags
43+
44+
size_t part_size;
45+
size_t alloc_sum; // sum of all allocations - used for manual interleaving
46+
hwloc_topology_t topo;
47+
} os_memory_provider_t;
48+
2349
umf_result_t os_translate_flags(unsigned in_flags, unsigned max,
2450
umf_result_t (*translate_flag)(unsigned,
2551
unsigned *),

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ if(LINUX) # OS-specific functions are implemented only for Linux now
173173
NAME memspace_highest_capacity
174174
SRCS memspaces/memspace_highest_capacity.cpp
175175
LIBS ${UMF_UTILS_FOR_TEST} ${LIBNUMA_LIBRARIES})
176+
add_umf_test(
177+
NAME mempolicy
178+
SRCS memspaces/mempolicy.cpp
179+
LIBS ${LIBNUMA_LIBRARIES})
176180
endif()
177181

178182
if(UMF_BUILD_GPU_TESTS AND UMF_BUILD_LEVEL_ZERO_PROVIDER)

0 commit comments

Comments
 (0)