Skip to content

Commit a6056bb

Browse files
committed
Add a basic example
Add an example with allocating memory with the usage of OS Memory Provider and Scalable Pool.
1 parent 4d63cd9 commit a6056bb

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ install(
108108
EXPORT ${PROJECT_NAME}-targets)
109109

110110
add_subdirectory(src)
111+
add_subdirectory(examples)
111112

112113
if(UMF_BUILD_TESTS)
113114
add_subdirectory(test)

examples/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
if(UMF_BUILD_OS_MEMORY_PROVIDER AND UMF_BUILD_LIBUMF_POOL_SCALABLE)
6+
add_umf_executable(NAME basic
7+
SRCS basic/basic.c
8+
LIBS umf
9+
scalable_pool
10+
pthread)
11+
target_include_directories(basic PRIVATE
12+
${UMF_CMAKE_SOURCE_DIR}/include)
13+
endif()

examples/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Examples
2+
3+
This directory contains examples of UMF usage. Each example has a brief description below.
4+
5+
## Basic
6+
7+
This example covers the basics of UMF API. It walks you through a memory provider
8+
and pool allocator creation. OS memory provider and Scalable Pool are used for this purpose.
9+
10+
### Required CMake configuration flags
11+
* UMF_BUILD_OS_MEMORY_PROVIDER=ON
12+
* UMF_BUILD_LIBUMF_POOL_SCALABLE=ON

examples/basic/basic.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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/pools/pool_scalable.h"
11+
#include "umf/providers/provider_os_memory.h"
12+
13+
#include <stdio.h>
14+
#include <string.h>
15+
16+
int main(void) {
17+
// A result object for storing UMF API result status
18+
umf_result_t res;
19+
20+
// Create a memory provider
21+
umf_memory_provider_ops_t *provider_ops = umfOsMemoryProviderOps();
22+
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();
23+
umf_memory_provider_handle_t provider;
24+
25+
res = umfMemoryProviderCreate(provider_ops, &params, &provider);
26+
if (res != UMF_RESULT_SUCCESS) {
27+
printf("Failed to create provider!");
28+
return -1;
29+
}
30+
printf("OS Memory Provider created at %p\n", (void *)provider);
31+
32+
// Allocate memory from memory provider
33+
size_t alloc_size = 5000;
34+
size_t alignment = 0;
35+
void *ptr_provider = NULL;
36+
37+
res = umfMemoryProviderGetRecommendedPageSize(provider, alloc_size,
38+
&alignment);
39+
if (res != UMF_RESULT_SUCCESS || alignment == 0) {
40+
printf("Failed to get recommended page size!");
41+
goto memory_provider_destroy;
42+
}
43+
printf("Recommended page size is %zu bytes\n", alignment);
44+
45+
res =
46+
umfMemoryProviderAlloc(provider, alloc_size, alignment, &ptr_provider);
47+
if (res != UMF_RESULT_SUCCESS) {
48+
printf("Failed to allocate memory from the provider!");
49+
goto memory_provider_destroy;
50+
}
51+
printf("Allocated memory at %p\n", ptr_provider);
52+
53+
// Free allocated memory
54+
res = umfMemoryProviderFree(provider, ptr_provider, alloc_size);
55+
if (res != UMF_RESULT_SUCCESS) {
56+
printf("Failed to free memory to the provider!");
57+
goto memory_provider_destroy;
58+
}
59+
printf("Freed memory at %p\n", ptr_provider);
60+
61+
// Create a memory pool
62+
umf_memory_pool_ops_t *pool_ops = umfScalablePoolOps();
63+
umf_pool_create_flags_t flags = 0;
64+
void *pool_params = NULL;
65+
umf_memory_pool_handle_t pool;
66+
67+
res = umfPoolCreate(pool_ops, provider, pool_params, flags, &pool);
68+
if (res != UMF_RESULT_SUCCESS) {
69+
printf("\nFailed to create pool!");
70+
goto memory_provider_destroy;
71+
}
72+
printf("\nScalable Memory Pool created at %p\n", (void *)pool);
73+
74+
// Allocate some memory
75+
size_t num = 1;
76+
alloc_size = 128;
77+
78+
char *ptr = umfPoolCalloc(pool, num, alloc_size);
79+
if (!ptr) {
80+
printf("Failed to allocate memory!");
81+
goto memory_pool_destroy;
82+
}
83+
84+
strcpy(ptr, "Allocated memory at");
85+
printf("%s %p\n", ptr, (void *)ptr);
86+
87+
// Retrieve a memory pool from a pointer, available with memory tracking
88+
// RFC: Remove this? With memory tracker disabled, NULL returned may be misleading
89+
umf_memory_pool_handle_t check_pool = umfPoolByPtr(ptr);
90+
printf("Memory at %p has been allocated from pool at %p\n", (void *)ptr,
91+
(void *)check_pool);
92+
93+
// Retrieve a memory provider from a pool
94+
umf_memory_provider_handle_t check_provider;
95+
res = umfPoolGetMemoryProvider(pool, &check_provider);
96+
if (res != UMF_RESULT_SUCCESS) {
97+
printf("Failed to retrieve a memory provider for pool!");
98+
goto memory_pool_destroy;
99+
}
100+
printf("Pool at %p has been allocated from provider at %p\n", (void *)pool,
101+
(void *)check_provider);
102+
103+
umfPoolFree(pool, ptr);
104+
umfPoolDestroy(pool);
105+
umfMemoryProviderDestroy(provider);
106+
return 0;
107+
108+
memory_pool_destroy:
109+
umfPoolDestroy(pool);
110+
memory_provider_destroy:
111+
umfMemoryProviderDestroy(provider);
112+
return -1;
113+
}

scripts/docs_config/example-usage.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.. highlight:: c
2+
:linenothreshold: 10
3+
4+
==============================================================================
5+
Example usage
6+
==============================================================================
7+
8+
This section will walk you through a basic usage of memory provider
9+
and pool allocator. OS Memory Provider and Scalable Pool will be used for this purpose.
10+
11+
There are also other memory pools available in the UMF. See `README`_ for
12+
more information.
13+
14+
You can find the full example code in the `examples/basic/basic.c`_ file
15+
in the UMF repository.
16+
17+
TODO: Update the link to upstream
18+
19+
Memory provider usage
20+
------------------------------------------------------------------------------
21+
22+
TODO: Add links wherever possible
23+
24+
First, let's create a memory provider object for coarse-grained allocations.
25+
You have to include the ``provider_os_memory.h`` header with
26+
the OS Memory Provider API::
27+
28+
#include "umf/providers/provider_os_memory.h"
29+
30+
Get a pointer to the OS Memory Provider operations struct and default parameters::
31+
32+
umf_memory_provider_ops_t *provider_ops = umfOsMemoryProviderOps();
33+
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();
34+
35+
The handle to created memory ``provider`` object is returned as the last argument
36+
of ``umfMemoryProviderCreate()``::
37+
38+
umf_memory_provider_handle_t provider;
39+
umfMemoryProviderCreate(provider_ops, &params, &provider);
40+
41+
TODO: Describe more operations used in the example file.
42+
43+
Memory pool usage
44+
------------------------------------------------------------------------------
45+
46+
Having created a memory ``provider``, you can create a Scalable Memory ``pool``
47+
to be used for fine-grained allocations. You have to include
48+
the ``pool_scalable.h`` header with the Scalable Memory Pool API::
49+
50+
#include "umf/pools/pool_scalable.h"
51+
52+
Use the default set of operations for the Scalable Memory Pool,
53+
by retrieving an address of the default ops struct with ``umfScalablePoolOps()``::
54+
55+
umf_memory_pool_ops_t *pool_ops = umfScalablePoolOps();
56+
57+
Here we don't make use of additional flags. See `documentation` for available flags::
58+
59+
umf_pool_create_flags_t flags = 0;
60+
61+
Argument `pool_params` is not used by the Scalable Pool::
62+
63+
void *pool_params = NULL;
64+
65+
The `pool` is retrieved as the last argument of the `umfPoolCreate` function::
66+
67+
umf_memory_pool_handle_t pool;
68+
umfPoolCreate(pool_ops, provider, pool_params, flags, &pool);
69+
70+
TODO: Describe more operations used in the example file.
71+
72+
.. _examples/basic/basic.c: https://github.com/patkamin/unified-memory-framework/blob/main/examples/basic/basic.c
73+
.. _README: https://github.com/patkamin/unified-memory-framework/blob/main/README.md

scripts/docs_config/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ Intel Unified Memory Framework documentation
88
:maxdepth: 3
99

1010
introduction.rst
11+
example-usage.rst
1112
api.rst
1213
glossary.rst

third_party/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Formatting the source code
33
clang-format==15.0.7
44
# Generating HTML documentation
5+
pygments==2.5.2
56
sphinxcontrib_applehelp==1.0.4
67
sphinxcontrib_devhelp==1.0.2
78
sphinxcontrib_htmlhelp==2.0.1

0 commit comments

Comments
 (0)