Skip to content

Commit b818231

Browse files
committed
Initial memspace and memory_target APIs
1 parent 76a90a4 commit b818231

File tree

11 files changed

+423
-0
lines changed

11 files changed

+423
-0
lines changed

include/umf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212

1313
#include <umf/memory_pool.h>
1414
#include <umf/memory_provider.h>
15+
#include <umf/memspace.h>
16+
#include <umf/memspace_policy.h>
1517

1618
#endif /* UMF_UNIFIED_MEMORY_FRAMEWORK_H */

include/umf/memspace.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_MEMSPACE_H
11+
#define UMF_MEMSPACE_H 1
12+
13+
#include <umf/base.h>
14+
#include <umf/memory_pool.h>
15+
#include <umf/memory_provider.h>
16+
#include <umf/memspace_policy.h>
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
typedef struct umf_memspace_t *umf_memspace_handle_t;
23+
24+
// TODO: consider 3rd function that would only return pool ops (as opposed
25+
// to actual pool instance with a provider). This could be useful for UR,
26+
// where we could ask UMF for a best pool (e.g. based on policy) but supply
27+
// a custom provider (L0/CUDA, etc.).
28+
enum umf_result_t umfPoolCreateFromMemspace(umf_memspace_handle_t memspace,
29+
umf_memspace_policy_handle_t policy,
30+
umf_memory_pool_handle_t *pool);
31+
enum umf_result_t
32+
umfMemoryProviderCreateFromMemspace(umf_memspace_handle_t memspace,
33+
umf_memspace_policy_handle_t policy,
34+
umf_memory_provider_handle_t *provider);
35+
36+
// TODO: iteration, filtering, sorting API
37+
// can we just define memspace as an array of targets?
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#endif /* UMF_MEMSPACE_H */

include/umf/memspace_policy.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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_MEMSPACE_POLICY_H
11+
#define UMF_MEMSPACE_POLICY_H 1
12+
13+
#include <umf/base.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
typedef struct umf_memspace_policy_t *umf_memspace_policy_handle_t;
20+
21+
// TODO: define different policies
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
27+
#endif /* UMF_MEMSPACE_POLICY_H */

include/umf/memspaces/memspace_numa.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_MEMSPACE_NUMA_H
11+
#define UMF_MEMSPACE_NUMA_H 1
12+
13+
#include <umf/base.h>
14+
#include <umf/memspace.h>
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
enum umf_result_t
21+
umfMemspaceCreateFromNumaId(size_t id,
22+
umf_memspace_handle_t *memspace /* out */);
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
28+
#endif /* UMF_MEMSPACE_NUMA_H */

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ set(UMF_SOURCES
88
memory_pool.c
99
memory_provider.c
1010
memory_provider_get_last_failed.c
11+
memory_target.c
12+
memory_targets/memory_target_numa.c
13+
memspace.c
1114
pool/pool_disjoint.cpp
1215
provider/provider_os_memory.c
1316
provider/provider_tracking.c

src/memory_target.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#include <assert.h>
11+
#include <stdlib.h>
12+
13+
#include "memory_target.h"
14+
#include "memory_target_ops.h"
15+
16+
enum umf_result_t
17+
umfMemoryTargetCreate(struct umf_memory_target_ops_t *ops, void *params,
18+
umf_memory_target_handle_t *memory_target) {
19+
umf_memory_target_handle_t target = (struct umf_memory_target_t *)malloc(
20+
sizeof(struct umf_memory_target_t));
21+
if (!target) {
22+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
23+
}
24+
25+
assert(ops->version == UMF_VERSION_CURRENT);
26+
27+
target->ops = ops;
28+
29+
void *target_priv;
30+
enum umf_result_t ret = ops->initialize(params, &target_priv);
31+
if (ret != UMF_RESULT_SUCCESS) {
32+
free(target);
33+
return ret;
34+
}
35+
36+
target->priv = target_priv;
37+
38+
*memory_target = target;
39+
40+
return UMF_RESULT_SUCCESS;
41+
}
42+
43+
void umfMemoryTargetDestroy(umf_memory_target_handle_t memory_target) {
44+
memory_target->ops->finalize(memory_target->priv);
45+
free(memory_target);
46+
}

src/memory_target.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_MEMORY_TARGET_H
11+
#define UMF_MEMORY_TARGET_H 1
12+
13+
#include <umf/base.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
struct umf_memory_target_ops_t;
20+
21+
struct umf_memory_target_t {
22+
struct umf_memory_target_ops_t *ops;
23+
void *priv;
24+
};
25+
26+
typedef struct umf_memory_target_t *umf_memory_target_handle_t;
27+
28+
enum umf_result_t
29+
umfMemoryTargetCreate(struct umf_memory_target_ops_t *ops, void *params,
30+
umf_memory_target_handle_t *memory_target);
31+
32+
void umfMemoryTargetDestroy(umf_memory_target_handle_t memory_target);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* UMF_MEMORY_TARGET_H */

src/memory_target_ops.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_MEMORY_TARGET_OPS_H
11+
#define UMF_MEMORY_TARGET_OPS_H 1
12+
13+
#include <stdbool.h>
14+
15+
#include <umf/base.h>
16+
#include <umf/memspace.h>
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
typedef struct umf_memory_target_t *umf_memory_target_handle_t;
23+
24+
struct umf_memory_target_ops_t {
25+
/// Version of the ops structure.
26+
/// Should be initialized using UMF_VERSION_CURRENT
27+
uint32_t version;
28+
29+
enum umf_result_t (*initialize)(void *params, void **mem_target);
30+
void (*finalize)(void *mem_target);
31+
32+
enum umf_result_t (*pool_create_from_memspace)(
33+
umf_memspace_handle_t memspace, void **mem_targets,
34+
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool);
35+
36+
enum umf_result_t (*memory_provider_create_from_memspace)(
37+
umf_memspace_handle_t memspace, void **mem_targets,
38+
umf_memspace_policy_handle_t policy,
39+
umf_memory_provider_handle_t *provider);
40+
41+
// TODO: query API (capacity, distance, etc.)
42+
};
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* #ifndef UMF_MEMORY_TARGET_OPS_H */
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
#include <stdlib.h>
11+
12+
#include "memory_target_numa.h"
13+
14+
struct numa_memory_target_t {
15+
size_t id;
16+
};
17+
18+
static enum umf_result_t numa_initialize(void *params, void **mem_target) {
19+
struct umf_numa_memory_target_config_t *config =
20+
(struct umf_numa_memory_target_config_t *)params;
21+
22+
struct numa_memory_target_t *numa_target =
23+
malloc(sizeof(struct numa_memory_target_t));
24+
if (!numa_target) {
25+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
26+
}
27+
28+
numa_target->id = config->id;
29+
*mem_target = numa_target;
30+
return UMF_RESULT_SUCCESS;
31+
}
32+
33+
static void numa_finalize(void *mem_target) { free(mem_target); }
34+
35+
static enum umf_result_t numa_pool_create_from_memspace(
36+
umf_memspace_handle_t memspace, void **mem_targets,
37+
umf_memspace_policy_handle_t policy, umf_memory_pool_handle_t *pool) {
38+
// TODO: cast mem_targets to (struct numa_memory_target_t**), parse ids and initialize os provider
39+
(void)memspace;
40+
(void)mem_targets;
41+
(void)policy;
42+
(void)pool;
43+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
44+
}
45+
46+
static enum umf_result_t numa_memory_provider_create_from_memspace(
47+
umf_memspace_handle_t memspace, void **mem_targets,
48+
umf_memspace_policy_handle_t policy,
49+
umf_memory_provider_handle_t *provider) {
50+
// TODO: cast mem_targets to (struct numa_memory_target_t**), parse ids and initialize os provider
51+
(void)memspace;
52+
(void)mem_targets;
53+
(void)policy;
54+
(void)provider;
55+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
56+
}
57+
58+
struct umf_memory_target_ops_t UMF_MEMORY_TARGET_NUMA_OPS = {
59+
.initialize = numa_initialize,
60+
.finalize = numa_finalize,
61+
.pool_create_from_memspace = numa_pool_create_from_memspace,
62+
.memory_provider_create_from_memspace =
63+
numa_memory_provider_create_from_memspace};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_MEMORY_TARGET_NUMA_H
11+
#define UMF_MEMORY_TARGET_NUMA_H 1
12+
13+
#include <umf.h>
14+
#include <umf/memspace.h>
15+
16+
#include "../memory_target.h"
17+
#include "../memory_target_ops.h"
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
struct umf_numa_memory_target_config_t {
24+
size_t id;
25+
};
26+
27+
extern struct umf_memory_target_ops_t UMF_MEMORY_TARGET_NUMA_OPS;
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* UMF_MEMORY_TARGET_NUMA_H */

0 commit comments

Comments
 (0)