Skip to content

add mempolicy with support of interleave, bind and preferred #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/umf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <umf/memory_pool.h>
#include <umf/memory_provider.h>
#include <umf/mempolicy.h>
#include <umf/memspace.h>
#include <umf/memspace_policy.h>

#endif /* UMF_UNIFIED_MEMORY_FRAMEWORK_H */
59 changes: 59 additions & 0 deletions include/umf/mempolicy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMPOLICY_H
#define UMF_MEMPOLICY_H 1

#include <umf/base.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct umf_mempolicy_t *umf_mempolicy_handle_t;
typedef const struct umf_mempolicy_t *umf_const_mempolicy_handle_t;

typedef enum umf_mempolicy_membind_t {
/// Interleave memory from all memory in memspace
UMF_MEMPOLICY_INTERLEAVE,
/// Bind memory to namespace
UMF_MEMPOLICY_BIND,
/// Prefer memory from namespace but fallback to other memory if not available
UMF_MEMPOLICY_PREFERRED
} umf_mempolicy_membind_t;

///
/// @brief Creates a new memory policy
/// @param bind memory binding policy
/// @param hPolicy [out] handle to the newly created memory policy
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
umf_result_t umfMempolicyCreate(umf_mempolicy_membind_t bind,
umf_mempolicy_handle_t *hPolicy);

///
/// @brief Destroys memory policy
/// @param hPolicy handle to memory policy
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t hPolicy);

///
/// @brief Sets custom part size for interleaved memory policy - by default it's interleaved by pages
/// @param hPolicy handle to memory policy
/// @param partSize size of the part or zero to use default part size (page size)
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
umf_result_t umfMempolicySetInterleavePartSize(umf_mempolicy_handle_t hPolicy,
size_t partSize);
#ifdef __cplusplus
}
#endif

#endif /* UMF_MEMPOLICY_H */
6 changes: 3 additions & 3 deletions include/umf/memspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <umf/base.h>
#include <umf/memory_pool.h>
#include <umf/memory_provider.h>
#include <umf/memspace_policy.h>
#include <umf/mempolicy.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -29,7 +29,7 @@ typedef struct umf_memspace_t *umf_memspace_handle_t;
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t hMemspace,
umf_memspace_policy_handle_t hPolicy,
umf_const_mempolicy_handle_t hPolicy,
umf_memory_pool_handle_t *hPool);

///
Expand All @@ -41,7 +41,7 @@ umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t hMemspace,
///
umf_result_t
umfMemoryProviderCreateFromMemspace(umf_memspace_handle_t hMemspace,
umf_memspace_policy_handle_t hPolicy,
umf_const_mempolicy_handle_t hPolicy,
umf_memory_provider_handle_t *hProvider);

///
Expand Down
25 changes: 0 additions & 25 deletions include/umf/memspace_policy.h

This file was deleted.

15 changes: 13 additions & 2 deletions scripts/docs_config/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ TODO: Add general information about memspaces.
Memspace
------------------------------------------
.. doxygenfile:: memspace.h
:sections: define enum typedef func var
:sections: define enum typedef func

Mempolicy
==========================================

TODO: Add general information about mempolicies.

Mempolicy
------------------------------------------
.. doxygenfile:: mempolicy.h
:sections: define enum typedef func


Inter-Process Communication
==========================================
Expand All @@ -118,4 +129,4 @@ operations for this API to work. Otherwise IPC APIs return an error.
IPC API
------------------------------------------
.. doxygenfile:: ipc.h
:sections: define enum typedef func var
:sections: define enum typedef func var
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(UMF_SOURCES
memory_provider.c
memory_provider_get_last_failed.c
memory_target.c
mempolicy.c
memspace.c
provider/provider_tracking.c
critnib/critnib.c
Expand Down
3 changes: 3 additions & 0 deletions src/libumf.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ EXPORTS
umfMemoryProviderPurgeForce
umfMemoryProviderPurgeLazy
umfMemoryProviderPutIPCHandle
umfMempolicyCreate;
umfMempolicyDestroy;
umfMempolicySetInterleavePartSize;
umfMemspaceDestroy
umfOpenIPCHandle
umfOsMemoryProviderOps
Expand Down
3 changes: 3 additions & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ UMF_1.0 {
umfMemoryProviderPurgeForce;
umfMemoryProviderPurgeLazy;
umfMemoryProviderPutIPCHandle;
umfMempolicyCreate;
umfMempolicyDestroy;
umfMempolicySetInterleavePartSize;
umfMemspaceCreateFromNumaArray;
umfMemspaceDestroy;
umfMemspaceHighestBandwidthGet;
Expand Down
2 changes: 0 additions & 2 deletions src/memory_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
extern "C" {
#endif

#include "base_alloc.h"

struct umf_memory_target_ops_t;
typedef struct umf_memory_target_ops_t umf_memory_target_ops_t;

Expand Down
6 changes: 2 additions & 4 deletions src/memory_target_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#ifndef UMF_MEMORY_TARGET_OPS_H
#define UMF_MEMORY_TARGET_OPS_H 1

#include <stdbool.h>

#include <umf/base.h>
#include <umf/memspace.h>

Expand All @@ -33,11 +31,11 @@ typedef struct umf_memory_target_ops_t {

umf_result_t (*pool_create_from_memspace)(
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool);
umf_const_mempolicy_handle_t policy, umf_memory_pool_handle_t *pool);

umf_result_t (*memory_provider_create_from_memspace)(
umf_memspace_handle_t memspace, void **memoryTargets, size_t numTargets,
umf_memspace_policy_handle_t policy,
umf_const_mempolicy_handle_t policy,
umf_memory_provider_handle_t *provider);

umf_result_t (*get_capacity)(void *memoryTarget, size_t *capacity);
Expand Down
29 changes: 24 additions & 5 deletions src/memory_targets/memory_target_numa.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base_alloc.h"
#include "base_alloc_global.h"
#include "memory_target_numa.h"
#include "mempolicy_internal.h"
#include "topology.h"
#include "utils_log.h"

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

static umf_result_t numa_memory_provider_create_from_memspace(
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
umf_memspace_policy_handle_t policy,
umf_const_mempolicy_handle_t policy,
umf_memory_provider_handle_t *provider) {
// TODO: apply policy
(void)policy;

struct numa_memory_target_t **numaTargets =
(struct numa_memory_target_t **)memTargets;

Expand All @@ -70,6 +70,26 @@ static umf_result_t numa_memory_provider_create_from_memspace(
}

umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();

if (policy) {
switch (policy->type) {
case UMF_MEMPOLICY_INTERLEAVE:
params.numa_mode = UMF_NUMA_MODE_INTERLEAVE;
params.part_size = policy->ops.part_size;
break;
case UMF_MEMPOLICY_BIND:
params.numa_mode = UMF_NUMA_MODE_BIND;
break;
case UMF_MEMPOLICY_PREFERRED:
params.numa_mode = UMF_NUMA_MODE_PREFERRED;
break;
default:
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
} else {
params.numa_mode = UMF_NUMA_MODE_BIND;
}

params.numa_list =
umf_ba_global_alloc(sizeof(*params.numa_list) * numNodesProvider);

Expand All @@ -82,7 +102,6 @@ static umf_result_t numa_memory_provider_create_from_memspace(
}

params.numa_list_len = numNodesProvider;
params.numa_mode = UMF_NUMA_MODE_BIND;

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

static umf_result_t numa_pool_create_from_memspace(
umf_memspace_handle_t memspace, void **memTargets, size_t numTargets,
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool) {
umf_const_mempolicy_handle_t policy, umf_memory_pool_handle_t *pool) {
(void)memspace;
(void)memTargets;
(void)numTargets;
Expand Down
50 changes: 50 additions & 0 deletions src/mempolicy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#include <umf/mempolicy.h>

#include "base_alloc_global.h"
#include "mempolicy_internal.h"

umf_result_t umfMempolicyCreate(umf_mempolicy_membind_t bind,
umf_mempolicy_handle_t *policy) {
if (policy == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*policy = umf_ba_global_alloc(sizeof(**policy));

if (*policy == NULL) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

(*policy)->type = bind;
if (bind == UMF_MEMPOLICY_INTERLEAVE) {
(*policy)->ops.part_size = 0;
}
return UMF_RESULT_SUCCESS;
}

umf_result_t umfMempolicyDestroy(umf_mempolicy_handle_t policy) {
umf_ba_global_free(policy);
return UMF_RESULT_SUCCESS;
}

umf_result_t umfMempolicySetInterleavePartSize(umf_mempolicy_handle_t policy,
size_t partSize) {
if (policy == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if (policy->type != UMF_MEMPOLICY_INTERLEAVE) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

policy->ops.part_size = partSize;
return UMF_RESULT_SUCCESS;
}
32 changes: 32 additions & 0 deletions src/mempolicy_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMPOLICY_INTERNAL_H
#define UMF_MEMPOLICY_INTERNAL_H 1

#include <umf/mempolicy.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct umf_mempolicy_t {
umf_mempolicy_membind_t type;
union {
size_t part_size;
} ops;
} umf_mempolicy_t;

typedef const umf_mempolicy_t umf_const_mempolicy_t;

#ifdef __cplusplus
}
#endif

#endif /* UMF_MEMPOLICY_INTERNAL_H */
4 changes: 2 additions & 2 deletions src/memspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static umf_result_t memoryTargetHandlesToPriv(umf_memspace_handle_t memspace,
}

umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t memspace,
umf_memspace_policy_handle_t policy,
umf_const_mempolicy_handle_t policy,
umf_memory_pool_handle_t *pool) {
if (!memspace || !pool) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
Expand All @@ -77,7 +77,7 @@ umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t memspace,

umf_result_t
umfMemoryProviderCreateFromMemspace(umf_memspace_handle_t memspace,
umf_memspace_policy_handle_t policy,
umf_const_mempolicy_handle_t policy,
umf_memory_provider_handle_t *provider) {
if (!memspace || !provider) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
Expand Down
Loading