Skip to content

Commit a086681

Browse files
committed
add examples
1 parent 245d916 commit a086681

File tree

3 files changed

+269
-0
lines changed

3 files changed

+269
-0
lines changed

examples/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,34 @@ else()
165165
"IPC examples with UMF pool API are supported on Linux only - skipping"
166166
)
167167
endif()
168+
169+
if(LINUX)
170+
set(EXAMPLE_NAME umf_example_memspace_numa)
171+
172+
add_umf_executable(
173+
NAME ${EXAMPLE_NAME}
174+
SRCS memspace/memspace_numa.c
175+
LIBS umf hwloc numa)
176+
177+
target_include_directories(
178+
${EXAMPLE_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/src/utils
179+
${UMF_CMAKE_SOURCE_DIR}/include)
180+
181+
target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
182+
183+
set(EXAMPLE_NAME umf_example_memspace_hmat)
184+
185+
add_umf_executable(
186+
NAME ${EXAMPLE_NAME}
187+
SRCS memspace/memspace_hmat.c
188+
LIBS umf hwloc numa)
189+
190+
target_include_directories(
191+
${EXAMPLE_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/src/utils
192+
${UMF_CMAKE_SOURCE_DIR}/include)
193+
194+
target_link_directories(${EXAMPLE_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
195+
else()
196+
message(
197+
STATUS "Memspace examples API are supported on Linux only - skipping")
198+
endif()

examples/memspace/memspace_hmat.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 <numa.h>
11+
#include <numaif.h>
12+
#include <stdio.h>
13+
#include <string.h>
14+
#include <umf/mempolicy.h>
15+
#include <umf/memspace.h>
16+
17+
// Function to create a memory provider which allocates memory from the specified NUMA node
18+
int createMemoryProvider(umf_memory_provider_handle_t *hProvider,
19+
umf_const_memspace_handle_t hMemspace) {
20+
int ret = 0;
21+
umf_result_t result;
22+
umf_mempolicy_handle_t hPolicy = NULL;
23+
if (hMemspace == NULL) {
24+
fprintf(stderr, "Memspace is NULL - do you have HMAT enabled?\n");
25+
return -1;
26+
}
27+
// Create a mempolicy - mempolicy defines how we want to use memory from memspace.
28+
// In this example, we want to bind memory to the best node in the memspace,
29+
// for the thread that allocates memory.
30+
result = umfMempolicyCreate(UMF_MEMPOLICY_PREFERRED, &hPolicy);
31+
if (result != UMF_RESULT_SUCCESS) {
32+
ret = -1;
33+
fprintf(stderr, "umfMempolicyCreate failed.\n");
34+
goto error_mempolicy;
35+
}
36+
37+
// Create a memory provider using the memory space and memory policy
38+
result = umfMemoryProviderCreateFromMemspace(hMemspace, hPolicy, hProvider);
39+
if (result != UMF_RESULT_SUCCESS) {
40+
ret = -1;
41+
fprintf(stderr, "umfMemoryProviderCreateFromMemspace failed.\n");
42+
goto error_provider;
43+
}
44+
45+
// After creating the memory provider, we can destroy the mempolicy
46+
error_provider:
47+
umfMempolicyDestroy(hPolicy);
48+
error_mempolicy:
49+
return ret;
50+
}
51+
52+
int main(void) {
53+
umf_memory_provider_handle_t hProvider = NULL;
54+
umf_result_t ret;
55+
void *ptr = NULL;
56+
size_t size = 1024;
57+
size_t alignment = 64;
58+
59+
// Check if NUMA is available
60+
if (numa_available() < 0) {
61+
fprintf(stderr, "NUMA is not available on this system.\n");
62+
return -1;
63+
}
64+
65+
// Create the memory provider that allocates memory from the highest bandwidth numa nodes
66+
ret = createMemoryProvider(&hProvider, umfMemspaceHighestBandwidthGet());
67+
if (ret != UMF_RESULT_SUCCESS) {
68+
return -1;
69+
}
70+
71+
// Allocate memory from the memory provider
72+
ret = umfMemoryProviderAlloc(hProvider, size, alignment, &ptr);
73+
if (ret != UMF_RESULT_SUCCESS) {
74+
fprintf(stderr, "umfMemoryProviderAlloc failed.\n");
75+
umfMemoryProviderDestroy(hProvider);
76+
return -1;
77+
}
78+
79+
// Use the allocated memory (ptr) here
80+
memset(ptr, 1, size);
81+
82+
// Lets check the NUMA node of the allocated memory
83+
int nodeId;
84+
int retm = get_mempolicy(&nodeId, NULL, 0, ptr, MPOL_F_ADDR | MPOL_F_NODE);
85+
if (retm != 0) {
86+
fprintf(stderr, "get_mempolicy failed.\n");
87+
umfMemoryProviderFree(hProvider, ptr, size);
88+
umfMemoryProviderDestroy(hProvider);
89+
return -1;
90+
}
91+
92+
printf("Allocated memory at %p from the highest bandwidth node: %d\n", ptr,
93+
nodeId);
94+
95+
// Free the allocated memory
96+
umfMemoryProviderFree(hProvider, ptr, size);
97+
98+
umfMemoryProviderDestroy(hProvider);
99+
100+
// Lets now allocate memory from the lowest latency node
101+
102+
ret = createMemoryProvider(&hProvider, umfMemspaceLowestLatencyGet());
103+
if (ret != UMF_RESULT_SUCCESS) {
104+
return -1;
105+
}
106+
107+
ret = umfMemoryProviderAlloc(hProvider, size, alignment, &ptr);
108+
109+
if (ret != UMF_RESULT_SUCCESS) {
110+
fprintf(stderr, "umfMemoryProviderAlloc failed.\n");
111+
umfMemoryProviderDestroy(hProvider);
112+
return -1;
113+
}
114+
115+
memset(ptr, 1, size);
116+
117+
retm = get_mempolicy(&nodeId, NULL, 0, ptr, MPOL_F_ADDR | MPOL_F_NODE);
118+
if (retm != 0) {
119+
fprintf(stderr, "get_mempolicy failed.\n");
120+
umfMemoryProviderFree(hProvider, ptr, size);
121+
umfMemoryProviderDestroy(hProvider);
122+
return -1;
123+
}
124+
printf("Allocated memory at %p from the lowest latency node: %d\n", ptr,
125+
nodeId);
126+
127+
// Free the allocated memory
128+
umfMemoryProviderFree(hProvider, ptr, size);
129+
130+
umfMemoryProviderDestroy(hProvider);
131+
132+
return 0;
133+
}

examples/memspace/memspace_numa.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 <numa.h>
11+
#include <numaif.h>
12+
#include <stdio.h>
13+
#include <string.h>
14+
#include <umf/mempolicy.h>
15+
#include <umf/memspace.h>
16+
17+
// Function to create a memory provider which allocates memory from the specified NUMA node
18+
int createMemoryProvider(umf_memory_provider_handle_t *hProvider,
19+
unsigned numa) {
20+
int ret = 0;
21+
umf_result_t result;
22+
umf_memspace_handle_t hMemspace = NULL;
23+
umf_mempolicy_handle_t hPolicy = NULL;
24+
25+
// Create a memspace - memspace is a list of memory sources.
26+
// In this example, we create a memspace that contains single numa node;
27+
result = umfMemspaceCreateFromNumaArray(&numa, 1, &hMemspace);
28+
if (result != UMF_RESULT_SUCCESS) {
29+
ret = -1;
30+
fprintf(stderr, "umfMemspaceCreateFromNumaArray failed.\n");
31+
goto error_memspace;
32+
}
33+
34+
// Create a mempolicy - mempolicy defines how we want to use memory from memspace.
35+
// In this example, we want to bind memory to the specified numa node.
36+
result = umfMempolicyCreate(UMF_MEMPOLICY_BIND, &hPolicy);
37+
if (result != UMF_RESULT_SUCCESS) {
38+
ret = -1;
39+
fprintf(stderr, "umfMempolicyCreate failed.\n");
40+
goto error_mempolicy;
41+
}
42+
43+
// Create a memory provider using the memory space and memory policy
44+
result = umfMemoryProviderCreateFromMemspace(hMemspace, hPolicy, hProvider);
45+
if (result != UMF_RESULT_SUCCESS) {
46+
ret = -1;
47+
fprintf(stderr, "umfMemoryProviderCreateFromMemspace failed.\n");
48+
goto error_provider;
49+
}
50+
51+
// After creating the memory provider, we can destroy the memspace and mempolicy
52+
error_provider:
53+
umfMempolicyDestroy(hPolicy);
54+
error_mempolicy:
55+
umfMemspaceDestroy(hMemspace);
56+
error_memspace:
57+
return ret;
58+
}
59+
60+
int main(void) {
61+
umf_memory_provider_handle_t hProvider = NULL;
62+
umf_result_t ret;
63+
64+
// Check if NUMA is available
65+
if (numa_available() < 0) {
66+
fprintf(stderr, "NUMA is not available on this system.\n");
67+
return -1;
68+
}
69+
70+
// Create the memory provider that allocates memory from the specified NUMA node
71+
// In this example, we allocate memory from the NUMA node 0
72+
ret = createMemoryProvider(&hProvider, 0);
73+
if (ret != UMF_RESULT_SUCCESS) {
74+
return -1;
75+
}
76+
77+
// Allocate memory from the memory provider
78+
void *ptr = NULL;
79+
size_t size = 1024;
80+
size_t alignment = 64;
81+
82+
ret = umfMemoryProviderAlloc(hProvider, size, alignment, &ptr);
83+
if (ret != UMF_RESULT_SUCCESS) {
84+
fprintf(stderr, "umfMemoryProviderAlloc failed.\n");
85+
goto error_alloc;
86+
}
87+
88+
// Use the allocated memory (ptr) here
89+
memset(ptr, 1, size);
90+
91+
// Lets check the NUMA node of the allocated memory
92+
int nodeId;
93+
int retm = get_mempolicy(&nodeId, NULL, 0, ptr, MPOL_F_ADDR | MPOL_F_NODE);
94+
if (retm != 0) {
95+
fprintf(stderr, "get_mempolicy failed.\n");
96+
goto error_alloc;
97+
}
98+
printf("Allocated memory at %p from numa_node %d\n", ptr, nodeId);
99+
// Free the allocated memory
100+
umfMemoryProviderFree(hProvider, ptr, size);
101+
error_alloc:
102+
umfMemoryProviderDestroy(hProvider);
103+
104+
return ret == UMF_RESULT_SUCCESS ? 0 : 1;
105+
}

0 commit comments

Comments
 (0)