Skip to content

Commit e5a15fc

Browse files
authored
Merge pull request #500 from lplewa/mempolicy
add mempolicy with support of interleave, bind and preferred
2 parents 5efb153 + 60e3327 commit e5a15fc

18 files changed

+340
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#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+
typedef const struct umf_mempolicy_t *umf_const_mempolicy_handle_t;
21+
22+
typedef enum umf_mempolicy_membind_t {
23+
/// Interleave memory from all memory in memspace
24+
UMF_MEMPOLICY_INTERLEAVE,
25+
/// Bind memory to namespace
26+
UMF_MEMPOLICY_BIND,
27+
/// Prefer memory from namespace but fallback to other memory if not available
28+
UMF_MEMPOLICY_PREFERRED
29+
} umf_mempolicy_membind_t;
30+
31+
///
32+
/// @brief Creates a new memory policy
33+
/// @param bind memory binding policy
34+
/// @param hPolicy [out] handle to the newly created memory policy
35+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
36+
///
37+
umf_result_t umfMempolicyCreate(umf_mempolicy_membind_t bind,
38+
umf_mempolicy_handle_t *hPolicy);
39+
40+
///
41+
/// @brief Destroys memory policy
42+
/// @param hPolicy handle to memory policy
43+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
44+
///
45+
umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t hPolicy);
46+
47+
///
48+
/// @brief Sets custom part size for interleaved memory policy - by default it's interleaved by pages
49+
/// @param hPolicy handle to memory policy
50+
/// @param partSize size of the part or zero to use default part size (page size)
51+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
52+
///
53+
umf_result_t umfMempolicySetInterleavePartSize(umf_mempolicy_handle_t hPolicy,
54+
size_t partSize);
55+
#ifdef __cplusplus
56+
}
57+
#endif
58+
59+
#endif /* UMF_MEMPOLICY_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_const_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_const_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.

scripts/docs_config/api.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,18 @@ TODO: Add general information about memspaces.
104104
Memspace
105105
------------------------------------------
106106
.. doxygenfile:: memspace.h
107-
:sections: define enum typedef func var
107+
:sections: define enum typedef func
108+
109+
Mempolicy
110+
==========================================
111+
112+
TODO: Add general information about mempolicies.
113+
114+
Mempolicy
115+
------------------------------------------
116+
.. doxygenfile:: mempolicy.h
117+
:sections: define enum typedef func
118+
108119

109120
Inter-Process Communication
110121
==========================================
@@ -118,4 +129,4 @@ operations for this API to work. Otherwise IPC APIs return an error.
118129
IPC API
119130
------------------------------------------
120131
.. doxygenfile:: ipc.h
121-
:sections: define enum typedef func var
132+
:sections: define enum typedef func var

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/libumf.def.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ EXPORTS
3333
umfMemoryProviderPurgeForce
3434
umfMemoryProviderPurgeLazy
3535
umfMemoryProviderPutIPCHandle
36+
umfMempolicyCreate;
37+
umfMempolicyDestroy;
38+
umfMempolicySetInterleavePartSize;
3639
umfMemspaceDestroy
3740
umfOpenIPCHandle
3841
umfOsMemoryProviderOps

src/libumf.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ UMF_1.0 {
2828
umfMemoryProviderPurgeForce;
2929
umfMemoryProviderPurgeLazy;
3030
umfMemoryProviderPutIPCHandle;
31+
umfMempolicyCreate;
32+
umfMempolicyDestroy;
33+
umfMempolicySetInterleavePartSize;
3134
umfMemspaceCreateFromNumaArray;
3235
umfMemspaceDestroy;
3336
umfMemspaceHighestBandwidthGet;

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: 2 additions & 4 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

@@ -33,11 +31,11 @@ typedef struct umf_memory_target_ops_t {
3331

3432
umf_result_t (*pool_create_from_memspace)(
3533
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
36-
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool);
34+
umf_const_mempolicy_handle_t policy, umf_memory_pool_handle_t *pool);
3735

3836
umf_result_t (*memory_provider_create_from_memspace)(
3937
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
40-
umf_memspace_policy_handle_t policy,
38+
umf_const_mempolicy_handle_t policy,
4139
umf_memory_provider_handle_t *provider);
4240

4341
umf_result_t (*get_capacity)(void *memoryTarget, size_t *capacity);

src/memory_targets/memory_target_numa.c

Lines changed: 24 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_internal.h"
2122
#include "topology.h"
2223
#include "utils_log.h"
2324

@@ -48,10 +49,9 @@ static void numa_finalize(void *memTarget) { umf_ba_global_free(memTarget); }
4849

4950
static umf_result_t numa_memory_provider_create_from_memspace(
5051
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
51-
umf_memspace_policy_handle_t policy,
52+
umf_const_mempolicy_handle_t policy,
5253
umf_memory_provider_handle_t *provider) {
53-
// TODO: apply policy
54-
(void)policy;
54+
5555
struct numa_memory_target_t **numaTargets =
5656
(struct numa_memory_target_t **)memTargets;
5757

@@ -70,6 +70,26 @@ static umf_result_t numa_memory_provider_create_from_memspace(
7070
}
7171

7272
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();
73+
74+
if (policy) {
75+
switch (policy->type) {
76+
case UMF_MEMPOLICY_INTERLEAVE:
77+
params.numa_mode = UMF_NUMA_MODE_INTERLEAVE;
78+
params.part_size = policy->ops.part_size;
79+
break;
80+
case UMF_MEMPOLICY_BIND:
81+
params.numa_mode = UMF_NUMA_MODE_BIND;
82+
break;
83+
case UMF_MEMPOLICY_PREFERRED:
84+
params.numa_mode = UMF_NUMA_MODE_PREFERRED;
85+
break;
86+
default:
87+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
88+
}
89+
} else {
90+
params.numa_mode = UMF_NUMA_MODE_BIND;
91+
}
92+
7393
params.numa_list =
7494
umf_ba_global_alloc(sizeof(*params.numa_list) * numNodesProvider);
7595

@@ -82,7 +102,6 @@ static umf_result_t numa_memory_provider_create_from_memspace(
82102
}
83103

84104
params.numa_list_len = numNodesProvider;
85-
params.numa_mode = UMF_NUMA_MODE_BIND;
86105

87106
umf_memory_provider_handle_t numaProvider = NULL;
88107
int ret = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &params,
@@ -101,7 +120,7 @@ static umf_result_t numa_memory_provider_create_from_memspace(
101120

102121
static umf_result_t numa_pool_create_from_memspace(
103122
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
104-
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool) {
123+
umf_const_mempolicy_handle_t policy, umf_memory_pool_handle_t *pool) {
105124
(void)memspace;
106125
(void)memTargets;
107126
(void)numTargets;

src/mempolicy.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#include <umf/mempolicy.h>
10+
11+
#include "base_alloc_global.h"
12+
#include "mempolicy_internal.h"
13+
14+
umf_result_t umfMempolicyCreate(umf_mempolicy_membind_t bind,
15+
umf_mempolicy_handle_t *policy) {
16+
if (policy == NULL) {
17+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
18+
}
19+
20+
*policy = umf_ba_global_alloc(sizeof(**policy));
21+
22+
if (*policy == NULL) {
23+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
24+
}
25+
26+
(*policy)->type = bind;
27+
if (bind == UMF_MEMPOLICY_INTERLEAVE) {
28+
(*policy)->ops.part_size = 0;
29+
}
30+
return UMF_RESULT_SUCCESS;
31+
}
32+
33+
umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t policy) {
34+
umf_ba_global_free(policy);
35+
return UMF_RESULT_SUCCESS;
36+
}
37+
38+
umf_result_t umfMempolicySetInterleavePartSize(umf_mempolicy_handle_t policy,
39+
size_t partSize) {
40+
if (policy == NULL) {
41+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
42+
}
43+
44+
if (policy->type != UMF_MEMPOLICY_INTERLEAVE) {
45+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
46+
}
47+
48+
policy->ops.part_size = partSize;
49+
return UMF_RESULT_SUCCESS;
50+
}

src/mempolicy_internal.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#ifndef UMF_MEMPOLICY_INTERNAL_H
11+
#define UMF_MEMPOLICY_INTERNAL_H 1
12+
13+
#include <umf/mempolicy.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
typedef struct umf_mempolicy_t {
20+
umf_mempolicy_membind_t type;
21+
union {
22+
size_t part_size;
23+
} ops;
24+
} umf_mempolicy_t;
25+
26+
typedef const umf_mempolicy_t umf_const_mempolicy_t;
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
32+
#endif /* UMF_MEMPOLICY_INTERNAL_H */

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_const_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_const_mempolicy_handle_t policy,
8181
umf_memory_provider_handle_t *provider) {
8282
if (!memspace || !provider) {
8383
return UMF_RESULT_ERROR_INVALID_ARGUMENT;

0 commit comments

Comments
 (0)