11/*
22 *
3- * Copyright (C) 2024 Intel Corporation
3+ * Copyright (C) 2024-2025 Intel Corporation
44 *
55 * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1313#include "utils_log.h"
1414
1515static hwloc_topology_t topology = NULL ;
16+ static hwloc_topology_t topology_reduced = NULL ;
1617static UTIL_ONCE_FLAG topology_initialized = UTIL_ONCE_FLAG_INIT ;
18+ static UTIL_ONCE_FLAG topology_reduced_initialized = UTIL_ONCE_FLAG_INIT ;
1719
1820void umfDestroyTopology (void ) {
1921 if (topology ) {
@@ -24,22 +26,60 @@ void umfDestroyTopology(void) {
2426 memcpy (& topology_initialized , & is_initialized ,
2527 sizeof (topology_initialized ));
2628 }
29+ if (topology_reduced ) {
30+ hwloc_topology_destroy (topology_reduced );
31+
32+ // portable version of "topology_initialized = UTIL_ONCE_FLAG_INIT;"
33+ static UTIL_ONCE_FLAG is_initialized = UTIL_ONCE_FLAG_INIT ;
34+ memcpy (& topology_reduced_initialized , & is_initialized ,
35+ sizeof (topology_reduced_initialized ));
36+ }
2737}
2838
29- static void umfCreateTopology (void ) {
30- if (hwloc_topology_init (& topology )) {
39+ static void umfCreateTopologyHelper (bool reduced ,
40+ hwloc_topology_t * topology_ptr ) {
41+ if (hwloc_topology_init (topology_ptr )) {
3142 LOG_ERR ("Failed to initialize topology" );
32- topology = NULL ;
43+ * topology_ptr = NULL ;
3344 return ;
3445 }
46+ if (reduced ) {
47+ if (hwloc_topology_set_all_types_filter (* topology_ptr ,
48+ HWLOC_TYPE_FILTER_KEEP_NONE )) {
49+ LOG_ERR ("Failed to set topology filter" );
50+ hwloc_topology_destroy (* topology_ptr );
51+ * topology_ptr = NULL ;
52+ return ;
53+ }
3554
36- if (hwloc_topology_load (topology )) {
55+ if (hwloc_topology_set_type_filter (* topology_ptr , HWLOC_OBJ_CORE ,
56+ HWLOC_TYPE_FILTER_KEEP_ALL )) {
57+ LOG_ERR ("Failed to set core filter" );
58+ hwloc_topology_destroy (* topology_ptr );
59+ * topology_ptr = NULL ;
60+ return ;
61+ }
62+ }
63+ if (hwloc_topology_load (* topology_ptr )) {
3764 LOG_ERR ("Failed to initialize topology" );
38- hwloc_topology_destroy (topology );
39- topology = NULL ;
65+ hwloc_topology_destroy (* topology_ptr );
66+ * topology_ptr = NULL ;
4067 }
4168}
4269
70+ static void umfCreateTopology (void ) {
71+ umfCreateTopologyHelper (false, & topology );
72+ }
73+
74+ static void umfCreateTopologyReduced (void ) {
75+ umfCreateTopologyHelper (true, & topology_reduced );
76+ }
77+
78+ hwloc_topology_t umfGetTopologyReduced (void ) {
79+ utils_init_once (& topology_initialized , umfCreateTopologyReduced );
80+ return topology_reduced ;
81+ }
82+
4383hwloc_topology_t umfGetTopology (void ) {
4484 utils_init_once (& topology_initialized , umfCreateTopology );
4585 return topology ;
0 commit comments