Skip to content

Commit cd24d10

Browse files
authored
[OpenMP] Fix preprocessor mismatches between include and usages of hwloc (#158349)
Fix #156679 There is a mismatch between the preprocessor guards around the include of `hwloc.h` and those protecting its usages, leading to build failures on Darwin: spack/spack-packages#1212 This change introduces `KMP_HWLOC_ENABLED` that reflects whether hwloc is actually used.
1 parent c9b07f3 commit cd24d10

File tree

7 files changed

+57
-54
lines changed

7 files changed

+57
-54
lines changed

openmp/runtime/src/kmp.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ class kmp_stats_list;
9797
// OMPD_SKIP_HWLOC used in libompd/omp-icv.cpp to avoid OMPD depending on hwloc
9898
#if KMP_USE_HWLOC && KMP_AFFINITY_SUPPORTED && !defined(OMPD_SKIP_HWLOC)
9999
#include "hwloc.h"
100+
#define KMP_HWLOC_ENABLED 1
100101
#ifndef HWLOC_OBJ_NUMANODE
101102
#define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
102103
#endif
103104
#ifndef HWLOC_OBJ_PACKAGE
104105
#define HWLOC_OBJ_PACKAGE HWLOC_OBJ_SOCKET
105106
#endif
107+
#else
108+
#define KMP_HWLOC_ENABLED 0
106109
#endif
107110

108111
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
@@ -672,10 +675,10 @@ typedef BOOL (*kmp_SetThreadGroupAffinity_t)(HANDLE, const GROUP_AFFINITY *,
672675
extern kmp_SetThreadGroupAffinity_t __kmp_SetThreadGroupAffinity;
673676
#endif /* KMP_OS_WINDOWS */
674677

675-
#if KMP_USE_HWLOC && !defined(OMPD_SKIP_HWLOC)
678+
#if KMP_HWLOC_ENABLED
676679
extern hwloc_topology_t __kmp_hwloc_topology;
677680
extern int __kmp_hwloc_error;
678-
#endif
681+
#endif // KMP_HWLOC_ENABLED
679682

680683
extern size_t __kmp_affin_mask_size;
681684
#define KMP_AFFINITY_CAPABLE() (__kmp_affin_mask_size > 0)
@@ -784,10 +787,10 @@ class KMPAffinity {
784787
static void destroy_api();
785788
enum api_type {
786789
NATIVE_OS
787-
#if KMP_USE_HWLOC
790+
#if KMP_HWLOC_ENABLED
788791
,
789792
HWLOC
790-
#endif
793+
#endif // KMP_HWLOC_ENABLED
791794
};
792795
virtual api_type get_api_type() const {
793796
KMP_ASSERT(0);
@@ -856,9 +859,9 @@ enum affinity_top_method {
856859
affinity_top_method_group,
857860
#endif /* KMP_GROUP_AFFINITY */
858861
affinity_top_method_flat,
859-
#if KMP_USE_HWLOC
862+
#if KMP_HWLOC_ENABLED
860863
affinity_top_method_hwloc,
861-
#endif
864+
#endif // KMP_HWLOC_ENABLED
862865
affinity_top_method_default
863866
};
864867

@@ -1125,9 +1128,9 @@ typedef struct kmp_allocator_t {
11251128
omp_alloctrait_value_t target_access;
11261129
omp_alloctrait_value_t atomic_scope;
11271130
size_t part_size;
1128-
#if KMP_USE_HWLOC
1131+
#if KMP_HWLOC_ENABLED
11291132
omp_alloctrait_value_t membind;
1130-
#endif
1133+
#endif // KMP_HWLOC_ENABLED
11311134
} kmp_allocator_t;
11321135

11331136
extern omp_allocator_handle_t __kmpc_init_allocator(int gtid,
@@ -2087,12 +2090,12 @@ typedef struct dispatch_shared_info {
20872090
#if KMP_USE_HIER_SCHED
20882091
void *hier;
20892092
#endif
2090-
#if KMP_USE_HWLOC
2093+
#if KMP_HWLOC_ENABLED
20912094
// When linking with libhwloc, the ORDERED EPCC test slows down on big
20922095
// machines (> 48 cores). Performance analysis showed that a cache thrash
20932096
// was occurring and this padding helps alleviate the problem.
20942097
char padding[64];
2095-
#endif
2098+
#endif // KMP_HWLOC_ENABLED
20962099
} dispatch_shared_info_t;
20972100

20982101
typedef struct kmp_disp {

openmp/runtime/src/kmp_affinity.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#if KMP_USE_HIER_SCHED
2020
#include "kmp_dispatch_hier.h"
2121
#endif
22-
#if KMP_USE_HWLOC
22+
#if KMP_HWLOC_ENABLED
2323
// Copied from hwloc
2424
#define HWLOC_GROUP_KIND_INTEL_MODULE 102
2525
#define HWLOC_GROUP_KIND_INTEL_TILE 103
2626
#define HWLOC_GROUP_KIND_INTEL_DIE 104
2727
#define HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP 220
28-
#endif
28+
#endif // KMP_HWLOC_ENABLED
2929
#include <ctype.h>
3030

3131
// The machine topology
@@ -1440,15 +1440,15 @@ void KMPAffinity::pick_api() {
14401440
KMPAffinity *affinity_dispatch;
14411441
if (picked_api)
14421442
return;
1443-
#if KMP_USE_HWLOC
1443+
#if KMP_HWLOC_ENABLED
14441444
// Only use Hwloc if affinity isn't explicitly disabled and
14451445
// user requests Hwloc topology method
14461446
if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
14471447
__kmp_affinity.type != affinity_disabled) {
14481448
affinity_dispatch = new KMPHwlocAffinity();
14491449
__kmp_hwloc_available = true;
14501450
} else
1451-
#endif
1451+
#endif // KMP_HWLOC_ENABLED
14521452
{
14531453
affinity_dispatch = new KMPNativeAffinity();
14541454
}
@@ -1699,7 +1699,7 @@ kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
16991699
// Original mask is a subset of full mask in multiple processor groups topology
17001700
kmp_affin_mask_t *__kmp_affin_origMask = NULL;
17011701

1702-
#if KMP_USE_HWLOC
1702+
#if KMP_HWLOC_ENABLED
17031703
static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
17041704
#if HWLOC_API_VERSION >= 0x00020000
17051705
return hwloc_obj_type_is_cache(obj->type);
@@ -2007,7 +2007,7 @@ static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *const msg_id) {
20072007
__kmp_topology->sort_ids();
20082008
return true;
20092009
}
2010-
#endif // KMP_USE_HWLOC
2010+
#endif // KMP_HWLOC_ENABLED
20112011

20122012
// If we don't know how to retrieve the machine's processor topology, or
20132013
// encounter an error in doing so, this routine is called to form a "flat"
@@ -4854,7 +4854,7 @@ static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
48544854
// In the default code path, errors are not fatal - we just try using
48554855
// another method. We only emit a warning message if affinity is on, or the
48564856
// verbose flag is set, an the nowarnings flag was not set.
4857-
#if KMP_USE_HWLOC
4857+
#if KMP_HWLOC_ENABLED
48584858
if (!success &&
48594859
__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
48604860
if (!__kmp_hwloc_error) {
@@ -4866,7 +4866,7 @@ static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
48664866
KMP_INFORM(AffIgnoringHwloc, env_var);
48674867
}
48684868
}
4869-
#endif
4869+
#endif // KMP_HWLOC_ENABLED
48704870

48714871
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
48724872
if (!success) {
@@ -4914,7 +4914,7 @@ static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
49144914
// If the user has specified that a paricular topology discovery method is to be
49154915
// used, then we abort if that method fails. The exception is group affinity,
49164916
// which might have been implicitly set.
4917-
#if KMP_USE_HWLOC
4917+
#if KMP_HWLOC_ENABLED
49184918
else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
49194919
KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
49204920
success = __kmp_affinity_create_hwloc_map(&msg_id);
@@ -4923,7 +4923,7 @@ static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
49234923
KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
49244924
}
49254925
}
4926-
#endif // KMP_USE_HWLOC
4926+
#endif // KMP_HWLOC_ENABLED
49274927

49284928
#if KMP_ARCH_X86 || KMP_ARCH_X86_64
49294929
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
@@ -5322,12 +5322,12 @@ void __kmp_affinity_uninitialize(void) {
53225322
__kmp_free(__kmp_osid_to_hwthread_map);
53235323
__kmp_osid_to_hwthread_map = NULL;
53245324
}
5325-
#if KMP_USE_HWLOC
5325+
#if KMP_HWLOC_ENABLED
53265326
if (__kmp_hwloc_topology != NULL) {
53275327
hwloc_topology_destroy(__kmp_hwloc_topology);
53285328
__kmp_hwloc_topology = NULL;
53295329
}
5330-
#endif
5330+
#endif // KMP_HWLOC_ENABLED
53315331
if (__kmp_hw_subset) {
53325332
kmp_hw_subset_t::deallocate(__kmp_hw_subset);
53335333
__kmp_hw_subset = nullptr;

openmp/runtime/src/kmp_affinity.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <limits>
1919

2020
#if KMP_AFFINITY_SUPPORTED
21-
#if KMP_USE_HWLOC
21+
#if KMP_HWLOC_ENABLED
2222
class KMPHwlocAffinity : public KMPAffinity {
2323
public:
2424
class Mask : public KMPAffinity::Mask {
@@ -109,7 +109,7 @@ class KMPHwlocAffinity : public KMPAffinity {
109109
}
110110
return error;
111111
}
112-
#endif
112+
#endif // KMP_OS_WINDOWS
113113
int get_proc_group() const override {
114114
int group = -1;
115115
#if KMP_OS_WINDOWS
@@ -191,7 +191,7 @@ class KMPHwlocAffinity : public KMPAffinity {
191191
}
192192
api_type get_api_type() const override { return HWLOC; }
193193
};
194-
#endif /* KMP_USE_HWLOC */
194+
#endif /* KMP_HWLOC_ENABLED */
195195

196196
#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
197197
KMP_OS_AIX

openmp/runtime/src/kmp_alloc.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "kmp_io.h"
1515
#include "kmp_wrapper_malloc.h"
1616

17-
#if KMP_USE_HWLOC
17+
#if KMP_HWLOC_ENABLED
1818
#if HWLOC_API_VERSION > 0x00020300
1919
#define KMP_HWLOC_LOCATION_TYPE_CPUSET HWLOC_LOCATION_TYPE_CPUSET
2020
#elif HWLOC_API_VERSION == 0x00020300
@@ -26,7 +26,7 @@ enum hwloc_memattr_id_e {
2626
HWLOC_MEMATTR_ID_CAPACITY
2727
};
2828
#endif
29-
#endif // KMP_USE_HWLOC
29+
#endif // KMP_HWLOC_ENABLED
3030

3131
// Disable bget when it is not used
3232
#if KMP_USE_BGET
@@ -1545,7 +1545,7 @@ void __kmp_fini_memkind() {
15451545
#endif
15461546
}
15471547

1548-
#if KMP_USE_HWLOC
1548+
#if KMP_HWLOC_ENABLED
15491549
static bool __kmp_is_hwloc_membind_supported(hwloc_membind_policy_t policy) {
15501550
#if HWLOC_API_VERSION >= 0x00020300
15511551
const hwloc_topology_support *support;
@@ -1561,7 +1561,7 @@ static bool __kmp_is_hwloc_membind_supported(hwloc_membind_policy_t policy) {
15611561
return false;
15621562
#else
15631563
return false;
1564-
#endif
1564+
#endif // KMP_HWLOC_ENABLED
15651565
}
15661566

15671567
void *__kmp_hwloc_alloc_membind(hwloc_memattr_id_e attr, size_t size,
@@ -1611,7 +1611,7 @@ void *__kmp_hwloc_membind_policy(omp_memspace_handle_t ms, size_t size,
16111611
return NULL;
16121612
#endif
16131613
}
1614-
#endif // KMP_USE_HWLOC
1614+
#endif // KMP_HWLOC_ENABLED
16151615

16161616
void __kmp_init_target_mem() {
16171617
*(void **)(&kmp_target_alloc_host) = KMP_DLSYM("llvm_omp_target_alloc_host");
@@ -1680,13 +1680,13 @@ omp_allocator_handle_t __kmpc_init_allocator(int gtid, omp_memspace_handle_t ms,
16801680
al->fb_data = RCAST(kmp_allocator_t *, traits[i].value);
16811681
break;
16821682
case omp_atk_partition:
1683-
#if KMP_USE_HWLOC
1683+
#if KMP_HWLOC_ENABLED
16841684
al->membind = (omp_alloctrait_value_t)traits[i].value;
16851685
KMP_DEBUG_ASSERT(al->membind == omp_atv_environment ||
16861686
al->membind == omp_atv_nearest ||
16871687
al->membind == omp_atv_blocked ||
16881688
al->membind == omp_atv_interleaved);
1689-
#endif
1689+
#endif // KMP_HWLOC_ENABLED
16901690
al->memkind = RCAST(void **, traits[i].value);
16911691
break;
16921692
case omp_atk_pin_device:
@@ -1980,7 +1980,7 @@ void *__kmp_alloc(int gtid, size_t algn, size_t size,
19801980
}
19811981
}
19821982

1983-
#if KMP_USE_HWLOC
1983+
#if KMP_HWLOC_ENABLED
19841984
if (__kmp_hwloc_available) {
19851985
if (__kmp_is_hwloc_membind_supported(HWLOC_MEMBIND_BIND)) {
19861986
if (allocator < kmp_max_mem_alloc) {
@@ -2074,7 +2074,7 @@ void *__kmp_alloc(int gtid, size_t algn, size_t size,
20742074
ptr = hwloc_alloc(__kmp_hwloc_topology, desc.size_a);
20752075
}
20762076
} else {
2077-
#endif
2077+
#endif // KMP_HWLOC_ENABLED
20782078
if (__kmp_memkind_available) {
20792079
if (allocator < kmp_max_mem_alloc) {
20802080
// pre-defined allocator
@@ -2201,9 +2201,9 @@ void *__kmp_alloc(int gtid, size_t algn, size_t size,
22012201
KMP_ASSERT(0); // abort fallback requested
22022202
} // no sense to look for another fallback because of same internal alloc
22032203
}
2204-
#if KMP_USE_HWLOC
2204+
#if KMP_HWLOC_ENABLED
22052205
}
2206-
#endif
2206+
#endif // KMP_HWLOC_ENABLED
22072207
KE_TRACE(10, ("__kmp_alloc: T#%d %p=alloc(%d)\n", gtid, ptr, desc.size_a));
22082208
if (ptr == NULL)
22092209
return NULL;
@@ -2339,7 +2339,7 @@ void ___kmpc_free(int gtid, void *ptr, omp_allocator_handle_t allocator) {
23392339
kmp_target_unlock_mem(desc.ptr_alloc, device);
23402340
}
23412341

2342-
#if KMP_USE_HWLOC
2342+
#if KMP_HWLOC_ENABLED
23432343
if (__kmp_hwloc_available) {
23442344
if (oal > kmp_max_mem_alloc && al->pool_size > 0) {
23452345
kmp_uint64 used =
@@ -2349,7 +2349,7 @@ void ___kmpc_free(int gtid, void *ptr, omp_allocator_handle_t allocator) {
23492349
}
23502350
hwloc_free(__kmp_hwloc_topology, desc.ptr_alloc, desc.size_a);
23512351
} else {
2352-
#endif
2352+
#endif // KMP_HWLOC_ENABLED
23532353
if (__kmp_memkind_available) {
23542354
if (oal < kmp_max_mem_alloc) {
23552355
// pre-defined allocator
@@ -2378,9 +2378,9 @@ void ___kmpc_free(int gtid, void *ptr, omp_allocator_handle_t allocator) {
23782378
}
23792379
__kmp_thread_free(__kmp_thread_from_gtid(gtid), desc.ptr_alloc);
23802380
}
2381-
#if KMP_USE_HWLOC
2381+
#if KMP_HWLOC_ENABLED
23822382
}
2383-
#endif
2383+
#endif // KMP_HWLOC_ENABLED
23842384
}
23852385

23862386
/* If LEAK_MEMORY is defined, __kmp_free() will *not* free memory. It causes

openmp/runtime/src/kmp_dispatch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ template <typename T> struct dispatch_shared_info_template {
182182
#if KMP_USE_HIER_SCHED
183183
kmp_hier_t<T> *hier;
184184
#endif
185-
#if KMP_USE_HWLOC
185+
#if KMP_HWLOC_ENABLED
186186
// When linking with libhwloc, the ORDERED EPCC test slowsdown on big
187187
// machines (> 48 cores). Performance analysis showed that a cache thrash
188188
// was occurring and this padding helps alleviate the problem.
189189
char padding[64];
190-
#endif
190+
#endif // KMP_HWLOC_ENABLED
191191
};
192192

193193
/* ------------------------------------------------------------------------ */

openmp/runtime/src/kmp_global.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ enum mic_type __kmp_mic_type = non_mic;
248248

249249
KMPAffinity *__kmp_affinity_dispatch = NULL;
250250

251-
#if KMP_USE_HWLOC
251+
#if KMP_HWLOC_ENABLED
252252
int __kmp_hwloc_error = FALSE;
253253
hwloc_topology_t __kmp_hwloc_topology = NULL;
254-
#endif
254+
#endif // KMP_HWLOC_ENABLED
255255

256256
#if KMP_OS_WINDOWS
257257
#if KMP_GROUP_AFFINITY

0 commit comments

Comments
 (0)