Skip to content

Commit 0c2ead0

Browse files
committed
Create single, global hwloc topology instance
1 parent cfd1a2a commit 0c2ead0

File tree

6 files changed

+65
-21
lines changed

6 files changed

+65
-21
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ set(UMF_SOURCES
6060
memspace.c
6161
provider/provider_tracking.c
6262
critnib/critnib.c
63-
pool/pool_proxy.c)
63+
pool/pool_proxy.c
64+
topology.c)
6465

6566
set(UMF_SOURCES_LINUX libumf_linux.c)
6667

src/libumf_linux.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#include <stddef.h>
1111

1212
#include "base_alloc_global.h"
13-
#include "memspace_host_all_internal.h"
13+
#include "memspace_internal.h"
1414
#include "provider_tracking.h"
15+
#include "topology.h"
1516
#include "utils_log.h"
1617

1718
umf_memory_tracker_handle_t TRACKER = NULL;
@@ -27,6 +28,7 @@ void __attribute__((destructor)) umfDestroy(void) {
2728
TRACKER = NULL;
2829
umfMemoryTrackerDestroy(t);
2930
umfMemspaceHostAllDestroy();
31+
umfDestroyTopology();
3032
}
3133

3234
void libumfInit(void) {

src/memspace_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct umf_memspace_t {
3030
///
3131
void umfMemspaceDestroy(umf_memspace_handle_t hMemspace);
3232

33+
void umfMemspaceHostAllDestroy(void);
34+
3335
#ifdef __cplusplus
3436
}
3537
#endif

src/memspaces/memspace_host_all.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
#include "base_alloc_global.h"
1515
#include "memory_target_numa.h"
16-
#include "memspace_host_all_internal.h"
1716
#include "memspace_internal.h"
1817
#include "memspace_numa.h"
18+
#include "topology.h"
1919
#include "utils_concurrency.h"
2020

2121
static umf_result_t umfMemspaceHostAllCreate(umf_memspace_handle_t *hMemspace) {
@@ -25,30 +25,25 @@ static umf_result_t umfMemspaceHostAllCreate(umf_memspace_handle_t *hMemspace) {
2525

2626
umf_result_t umf_ret = UMF_RESULT_SUCCESS;
2727

28-
hwloc_topology_t topology;
29-
if (hwloc_topology_init(&topology)) {
28+
hwloc_topology_t topology = umfGetTopology();
29+
if (!topology) {
3030
// TODO: What would be an approrpiate err?
3131
return UMF_RESULT_ERROR_UNKNOWN;
3232
}
3333

34-
if (hwloc_topology_load(topology)) {
35-
umf_ret = UMF_RESULT_ERROR_UNKNOWN;
36-
goto err_topology_destroy;
37-
}
38-
3934
// Shouldn't return -1, as 'HWLOC_OBJ_NUMANODE' doesn't appear to be an
4035
// object that can be present on multiple levels.
4136
// Source: https://www.open-mpi.org/projects/hwloc/doc/hwloc-v2.10.0-letter.pdf
4237
int nNodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NUMANODE);
4338
if (nNodes < 0) {
4439
umf_ret = UMF_RESULT_ERROR_UNKNOWN;
45-
goto err_topology_destroy;
40+
goto err;
4641
}
4742

4843
size_t *nodeIds = umf_ba_global_alloc(nNodes * sizeof(size_t));
4944
if (!nodeIds) {
5045
umf_ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
51-
goto err_topology_destroy;
46+
goto err;
5247
}
5348

5449
// Collect all available NUMA node ids on the platform
@@ -67,8 +62,7 @@ static umf_result_t umfMemspaceHostAllCreate(umf_memspace_handle_t *hMemspace) {
6762

6863
umf_ba_global_free(nodeIds);
6964

70-
err_topology_destroy:
71-
hwloc_topology_destroy(topology);
65+
err:
7266
return umf_ret;
7367
}
7468

src/topology.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 <hwloc.h>
11+
12+
#include "base_alloc_global.h"
13+
#include "utils_concurrency.h"
14+
15+
static hwloc_topology_t topology = NULL;
16+
static UTIL_ONCE_FLAG topology_initialized = UTIL_ONCE_FLAG_INIT;
17+
18+
void umfDestroyTopology(void) {
19+
if (topology) {
20+
hwloc_topology_destroy(topology);
21+
}
22+
}
23+
24+
static void umfCreateTopology(void) {
25+
if (hwloc_topology_init(&topology)) {
26+
fprintf(stderr, "Error: Failed to initialize topology\n");
27+
topology = NULL;
28+
return;
29+
}
30+
31+
if (hwloc_topology_load(topology)) {
32+
fprintf(stderr, "Error: Failed to initialize topology\n");
33+
hwloc_topology_destroy(topology);
34+
topology = NULL;
35+
}
36+
37+
#if defined(_WIN32) && !defined(UMF_SHARED_LIBRARY)
38+
atexit(umfDestroyTopology);
39+
#endif
40+
}
41+
42+
hwloc_topology_t umfGetTopology(void) {
43+
util_init_once(&topology_initialized, umfCreateTopology);
44+
return topology;
45+
}

src/memspaces/memspace_host_all_internal.h renamed to src/topology.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
*
88
*/
99

10-
#ifndef UMF_MEMSPACE_HOST_ALL_INTERNAL_H
11-
#define UMF_MEMSPACE_HOST_ALL_INTERNAL_H 1
10+
#ifndef UMF_TOPOLOGY_H
11+
#define UMF_TOPOLOGY_H 1
12+
13+
#include <hwloc.h>
1214

1315
#ifdef __cplusplus
1416
extern "C" {
1517
#endif
1618

17-
///
18-
/// \brief Destroys the predefined host all memspace.
19-
///
20-
void umfMemspaceHostAllDestroy(void);
19+
hwloc_topology_t umfGetTopology(void);
20+
void umfDestroyTopology(void);
2121

2222
#ifdef __cplusplus
2323
}
2424
#endif
2525

26-
#endif /* UMF_MEMSPACE_HOST_ALL_INTERNAL_H */
26+
#endif /* UMF_TOPOLOGY_H */

0 commit comments

Comments
 (0)