20#if KMP_AFFINITY_SUPPORTED
22class KMPHwlocAffinity :
public KMPAffinity {
24 class Mask :
public KMPAffinity::Mask {
29 mask = hwloc_bitmap_alloc();
32 ~Mask() { hwloc_bitmap_free(mask); }
33 void set(
int i)
override { hwloc_bitmap_set(mask, i); }
34 bool is_set(
int i)
const override {
return hwloc_bitmap_isset(mask, i); }
35 void clear(
int i)
override { hwloc_bitmap_clr(mask, i); }
36 void zero()
override { hwloc_bitmap_zero(mask); }
37 void copy(
const KMPAffinity::Mask *src)
override {
38 const Mask *convert =
static_cast<const Mask *
>(src);
39 hwloc_bitmap_copy(mask, convert->mask);
41 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
42 const Mask *convert =
static_cast<const Mask *
>(rhs);
43 hwloc_bitmap_and(mask, mask, convert->mask);
45 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
46 const Mask *convert =
static_cast<const Mask *
>(rhs);
47 hwloc_bitmap_or(mask, mask, convert->mask);
49 void bitwise_not()
override { hwloc_bitmap_not(mask, mask); }
50 int begin()
const override {
return hwloc_bitmap_first(mask); }
51 int end()
const override {
return -1; }
52 int next(
int previous)
const override {
53 return hwloc_bitmap_next(mask, previous);
55 int get_system_affinity(
bool abort_on_error)
override {
56 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
57 "Illegal get affinity operation when not capable");
59 hwloc_get_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
65 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
69 int set_system_affinity(
bool abort_on_error)
const override {
70 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
71 "Illegal set affinity operation when not capable");
73 hwloc_set_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
79 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
84 int set_process_affinity(
bool abort_on_error)
const override {
85 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
86 "Illegal set process affinity operation when not capable");
88 const hwloc_topology_support *support =
89 hwloc_topology_get_support(__kmp_hwloc_topology);
90 if (support->cpubind->set_proc_cpubind) {
92 retval = hwloc_set_cpubind(__kmp_hwloc_topology, mask,
93 HWLOC_CPUBIND_PROCESS);
98 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
103 int get_proc_group()
const override {
106 if (__kmp_num_proc_groups == 1) {
109 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
111 unsigned long first_32_bits = hwloc_bitmap_to_ith_ulong(mask, i * 2);
112 unsigned long second_32_bits =
113 hwloc_bitmap_to_ith_ulong(mask, i * 2 + 1);
114 if (first_32_bits == 0 && second_32_bits == 0) {
126 void determine_capable(
const char *var)
override {
127 const hwloc_topology_support *topology_support;
128 if (__kmp_hwloc_topology == NULL) {
129 if (hwloc_topology_init(&__kmp_hwloc_topology) < 0) {
130 __kmp_hwloc_error = TRUE;
131 if (__kmp_affinity.flags.verbose) {
132 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_init()");
135 if (hwloc_topology_load(__kmp_hwloc_topology) < 0) {
136 __kmp_hwloc_error = TRUE;
137 if (__kmp_affinity.flags.verbose) {
138 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_load()");
142 topology_support = hwloc_topology_get_support(__kmp_hwloc_topology);
147 if (topology_support && topology_support->cpubind->set_thisthread_cpubind &&
148 topology_support->cpubind->get_thisthread_cpubind &&
149 topology_support->discovery->pu && !__kmp_hwloc_error) {
151 KMP_AFFINITY_ENABLE(TRUE);
154 __kmp_hwloc_error = TRUE;
155 KMP_AFFINITY_DISABLE();
158 void bind_thread(
int which)
override {
159 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
160 "Illegal set affinity operation when not capable");
161 KMPAffinity::Mask *mask;
162 KMP_CPU_ALLOC_ON_STACK(mask);
164 KMP_CPU_SET(which, mask);
165 __kmp_set_system_affinity(mask, TRUE);
166 KMP_CPU_FREE_FROM_STACK(mask);
168 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
169 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
170 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
171 return new Mask[num];
173 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
174 Mask *hwloc_array =
static_cast<Mask *
>(array);
175 delete[] hwloc_array;
177 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
178 int index)
override {
179 Mask *hwloc_array =
static_cast<Mask *
>(array);
180 return &(hwloc_array[index]);
182 api_type get_api_type()
const override {
return HWLOC; }
186#if KMP_OS_LINUX || KMP_OS_FREEBSD
192#include <sys/syscall.h>
193#if KMP_ARCH_X86 || KMP_ARCH_ARM
194#ifndef __NR_sched_setaffinity
195#define __NR_sched_setaffinity 241
196#elif __NR_sched_setaffinity != 241
197#error Wrong code for setaffinity system call.
199#ifndef __NR_sched_getaffinity
200#define __NR_sched_getaffinity 242
201#elif __NR_sched_getaffinity != 242
202#error Wrong code for getaffinity system call.
204#elif KMP_ARCH_AARCH64
205#ifndef __NR_sched_setaffinity
206#define __NR_sched_setaffinity 122
207#elif __NR_sched_setaffinity != 122
208#error Wrong code for setaffinity system call.
210#ifndef __NR_sched_getaffinity
211#define __NR_sched_getaffinity 123
212#elif __NR_sched_getaffinity != 123
213#error Wrong code for getaffinity system call.
215#elif KMP_ARCH_RISCV64
216#ifndef __NR_sched_setaffinity
217#define __NR_sched_setaffinity 122
218#elif __NR_sched_setaffinity != 122
219#error Wrong code for setaffinity system call.
221#ifndef __NR_sched_getaffinity
222#define __NR_sched_getaffinity 123
223#elif __NR_sched_getaffinity != 123
224#error Wrong code for getaffinity system call.
227#ifndef __NR_sched_setaffinity
228#define __NR_sched_setaffinity 203
229#elif __NR_sched_setaffinity != 203
230#error Wrong code for setaffinity system call.
232#ifndef __NR_sched_getaffinity
233#define __NR_sched_getaffinity 204
234#elif __NR_sched_getaffinity != 204
235#error Wrong code for getaffinity system call.
238#ifndef __NR_sched_setaffinity
239#define __NR_sched_setaffinity 222
240#elif __NR_sched_setaffinity != 222
241#error Wrong code for setaffinity system call.
243#ifndef __NR_sched_getaffinity
244#define __NR_sched_getaffinity 223
245#elif __NR_sched_getaffinity != 223
246#error Wrong code for getaffinity system call.
249#ifndef __NR_sched_setaffinity
250#define __NR_sched_setaffinity 4239
251#elif __NR_sched_setaffinity != 4239
252#error Wrong code for setaffinity system call.
254#ifndef __NR_sched_getaffinity
255#define __NR_sched_getaffinity 4240
256#elif __NR_sched_getaffinity != 4240
257#error Wrong code for getaffinity system call.
260#ifndef __NR_sched_setaffinity
261#define __NR_sched_setaffinity 5195
262#elif __NR_sched_setaffinity != 5195
263#error Wrong code for setaffinity system call.
265#ifndef __NR_sched_getaffinity
266#define __NR_sched_getaffinity 5196
267#elif __NR_sched_getaffinity != 5196
268#error Wrong code for getaffinity system call.
270#elif KMP_ARCH_LOONGARCH64
271#ifndef __NR_sched_setaffinity
272#define __NR_sched_setaffinity 122
273#elif __NR_sched_setaffinity != 122
274#error Wrong code for setaffinity system call.
276#ifndef __NR_sched_getaffinity
277#define __NR_sched_getaffinity 123
278#elif __NR_sched_getaffinity != 123
279#error Wrong code for getaffinity system call.
281#elif KMP_ARCH_RISCV64
282#ifndef __NR_sched_setaffinity
283#define __NR_sched_setaffinity 122
284#elif __NR_sched_setaffinity != 122
285#error Wrong code for setaffinity system call.
287#ifndef __NR_sched_getaffinity
288#define __NR_sched_getaffinity 123
289#elif __NR_sched_getaffinity != 123
290#error Wrong code for getaffinity system call.
293#error Unknown or unsupported architecture
297#include <pthread_np.h>
299class KMPNativeAffinity :
public KMPAffinity {
300 class Mask :
public KMPAffinity::Mask {
301 typedef unsigned long mask_t;
302 typedef decltype(__kmp_affin_mask_size) mask_size_type;
303 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
304 static const mask_t ONE = 1;
305 mask_size_type get_num_mask_types()
const {
306 return __kmp_affin_mask_size /
sizeof(mask_t);
311 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
316 void set(
int i)
override {
317 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
319 bool is_set(
int i)
const override {
320 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
322 void clear(
int i)
override {
323 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
325 void zero()
override {
326 mask_size_type e = get_num_mask_types();
327 for (mask_size_type i = 0; i < e; ++i)
330 void copy(
const KMPAffinity::Mask *src)
override {
331 const Mask *convert =
static_cast<const Mask *
>(src);
332 mask_size_type e = get_num_mask_types();
333 for (mask_size_type i = 0; i < e; ++i)
334 mask[i] = convert->mask[i];
336 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
337 const Mask *convert =
static_cast<const Mask *
>(rhs);
338 mask_size_type e = get_num_mask_types();
339 for (mask_size_type i = 0; i < e; ++i)
340 mask[i] &= convert->mask[i];
342 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
343 const Mask *convert =
static_cast<const Mask *
>(rhs);
344 mask_size_type e = get_num_mask_types();
345 for (mask_size_type i = 0; i < e; ++i)
346 mask[i] |= convert->mask[i];
348 void bitwise_not()
override {
349 mask_size_type e = get_num_mask_types();
350 for (mask_size_type i = 0; i < e; ++i)
351 mask[i] = ~(mask[i]);
353 int begin()
const override {
355 while (retval < end() && !is_set(retval))
359 int end()
const override {
361 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
364 int next(
int previous)
const override {
365 int retval = previous + 1;
366 while (retval < end() && !is_set(retval))
370 int get_system_affinity(
bool abort_on_error)
override {
371 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
372 "Illegal get affinity operation when not capable");
375 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
377 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
378 reinterpret_cast<cpuset_t *
>(mask));
379 int retval = (r == 0 ? 0 : -1);
385 if (abort_on_error) {
386 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
390 int set_system_affinity(
bool abort_on_error)
const override {
391 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
392 "Illegal set affinity operation when not capable");
395 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
397 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
398 reinterpret_cast<cpuset_t *
>(mask));
399 int retval = (r == 0 ? 0 : -1);
405 if (abort_on_error) {
406 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
411 void determine_capable(
const char *env_var)
override {
412 __kmp_affinity_determine_capable(env_var);
414 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
415 KMPAffinity::Mask *allocate_mask()
override {
416 KMPNativeAffinity::Mask *retval =
new Mask();
419 void deallocate_mask(KMPAffinity::Mask *m)
override {
420 KMPNativeAffinity::Mask *native_mask =
421 static_cast<KMPNativeAffinity::Mask *
>(m);
424 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
425 return new Mask[num];
427 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
428 Mask *linux_array =
static_cast<Mask *
>(array);
429 delete[] linux_array;
431 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
432 int index)
override {
433 Mask *linux_array =
static_cast<Mask *
>(array);
434 return &(linux_array[index]);
436 api_type get_api_type()
const override {
return NATIVE_OS; }
441class KMPNativeAffinity :
public KMPAffinity {
442 class Mask :
public KMPAffinity::Mask {
443 typedef ULONG_PTR mask_t;
444 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
449 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
455 void set(
int i)
override {
456 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
458 bool is_set(
int i)
const override {
459 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
461 void clear(
int i)
override {
462 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
464 void zero()
override {
465 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
468 void copy(
const KMPAffinity::Mask *src)
override {
469 const Mask *convert =
static_cast<const Mask *
>(src);
470 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
471 mask[i] = convert->mask[i];
473 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
474 const Mask *convert =
static_cast<const Mask *
>(rhs);
475 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
476 mask[i] &= convert->mask[i];
478 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
479 const Mask *convert =
static_cast<const Mask *
>(rhs);
480 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
481 mask[i] |= convert->mask[i];
483 void bitwise_not()
override {
484 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
485 mask[i] = ~(mask[i]);
487 int begin()
const override {
489 while (retval < end() && !is_set(retval))
493 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
494 int next(
int previous)
const override {
495 int retval = previous + 1;
496 while (retval < end() && !is_set(retval))
500 int set_process_affinity(
bool abort_on_error)
const override {
501 if (__kmp_num_proc_groups <= 1) {
502 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
503 DWORD error = GetLastError();
504 if (abort_on_error) {
505 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
513 int set_system_affinity(
bool abort_on_error)
const override {
514 if (__kmp_num_proc_groups > 1) {
517 int group = get_proc_group();
519 if (abort_on_error) {
520 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
527 ga.Mask = mask[group];
528 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
530 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
531 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
532 DWORD error = GetLastError();
533 if (abort_on_error) {
534 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
540 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
541 DWORD error = GetLastError();
542 if (abort_on_error) {
543 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
551 int get_system_affinity(
bool abort_on_error)
override {
552 if (__kmp_num_proc_groups > 1) {
555 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
556 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
557 DWORD error = GetLastError();
558 if (abort_on_error) {
559 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
560 KMP_ERR(error), __kmp_msg_null);
564 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
568 mask[ga.Group] = ga.Mask;
570 mask_t newMask, sysMask, retval;
571 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
572 DWORD error = GetLastError();
573 if (abort_on_error) {
574 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
575 KMP_ERR(error), __kmp_msg_null);
579 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
581 DWORD error = GetLastError();
582 if (abort_on_error) {
583 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
584 KMP_ERR(error), __kmp_msg_null);
588 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
590 DWORD error = GetLastError();
591 if (abort_on_error) {
592 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
593 KMP_ERR(error), __kmp_msg_null);
600 int get_proc_group()
const override {
602 if (__kmp_num_proc_groups == 1) {
605 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
615 void determine_capable(
const char *env_var)
override {
616 __kmp_affinity_determine_capable(env_var);
618 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
619 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
620 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
621 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
622 return new Mask[num];
624 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
625 Mask *windows_array =
static_cast<Mask *
>(array);
626 delete[] windows_array;
628 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
629 int index)
override {
630 Mask *windows_array =
static_cast<Mask *
>(array);
631 return &(windows_array[index]);
633 api_type get_api_type()
const override {
return NATIVE_OS; }
639struct kmp_hw_attr_t {
643 unsigned reserved : 15;
645 static const int UNKNOWN_CORE_EFF = -1;
648 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
649 valid(0), reserved(0) {}
650 void set_core_type(kmp_hw_core_type_t type) {
654 void set_core_eff(
int eff) {
658 kmp_hw_core_type_t get_core_type()
const {
659 return (kmp_hw_core_type_t)core_type;
661 int get_core_eff()
const {
return core_eff; }
662 bool is_core_type_valid()
const {
663 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
665 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
666 operator bool()
const {
return valid; }
668 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
669 core_eff = UNKNOWN_CORE_EFF;
672 bool contains(
const kmp_hw_attr_t &other)
const {
673 if (!valid && !other.valid)
675 if (valid && other.valid) {
676 if (other.is_core_type_valid()) {
677 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
680 if (other.is_core_eff_valid()) {
681 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
688 bool operator==(
const kmp_hw_attr_t &rhs)
const {
689 return (rhs.valid == valid && rhs.core_eff == core_eff &&
690 rhs.core_type == core_type);
692 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
695#if KMP_AFFINITY_SUPPORTED
696KMP_BUILD_ASSERT(
sizeof(kmp_hw_attr_t) ==
sizeof(kmp_affinity_attrs_t));
699class kmp_hw_thread_t {
701 static const int UNKNOWN_ID = -1;
702 static const int MULTIPLE_ID = -2;
703 static int compare_ids(
const void *a,
const void *b);
704 static int compare_compact(
const void *a,
const void *b);
705 int ids[KMP_HW_LAST];
706 int sub_ids[KMP_HW_LAST];
713 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
720class kmp_topology_t {
746 int num_core_efficiencies;
748 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
754 kmp_hw_thread_t *hw_threads;
760 kmp_hw_t equivalent[KMP_HW_LAST];
769 void _insert_layer(kmp_hw_t type,
const int *ids);
771#if KMP_GROUP_AFFINITY
773 void _insert_windows_proc_groups();
779 void _gather_enumeration_information();
783 void _remove_radix1_layers();
786 void _discover_uniformity();
797 void _set_last_level_cache();
802 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
803 bool find_all =
false)
const;
807 kmp_topology_t() =
delete;
808 kmp_topology_t(
const kmp_topology_t &t) =
delete;
809 kmp_topology_t(kmp_topology_t &&t) =
delete;
810 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
811 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
813 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
814 static void deallocate(kmp_topology_t *);
817 kmp_hw_thread_t &at(
int index) {
818 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
819 return hw_threads[index];
821 const kmp_hw_thread_t &at(
int index)
const {
822 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
823 return hw_threads[index];
825 int get_num_hw_threads()
const {
return num_hw_threads; }
827 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
828 kmp_hw_thread_t::compare_ids);
832 bool check_ids()
const;
836 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
840#if KMP_AFFINITY_SUPPORTED
842 void set_granularity(kmp_affinity_t &stgs)
const;
844 bool filter_hw_subset();
845 bool is_close(
int hwt1,
int hwt2,
int level)
const;
846 bool is_uniform()
const {
return flags.uniform; }
849 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
return equivalent[type]; }
851 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
852 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
853 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
854 kmp_hw_t real_type2 = equivalent[type2];
855 if (real_type2 == KMP_HW_UNKNOWN)
857 equivalent[type1] = real_type2;
860 KMP_FOREACH_HW_TYPE(type) {
861 if (equivalent[type] == type1) {
862 equivalent[type] = real_type2;
868 int calculate_ratio(
int level1,
int level2)
const {
869 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
870 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
872 for (
int level = level1; level > level2; --level)
876 int get_ratio(
int level)
const {
877 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
880 int get_depth()
const {
return depth; };
881 kmp_hw_t get_type(
int level)
const {
882 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
885 int get_level(kmp_hw_t type)
const {
886 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
887 int eq_type = equivalent[type];
888 if (eq_type == KMP_HW_UNKNOWN)
890 for (
int i = 0; i < depth; ++i)
891 if (types[i] == eq_type)
895 int get_count(
int level)
const {
896 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
900 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
901 return _get_ncores_with_attr(attr, -1,
true);
905 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
906 return _get_ncores_with_attr(attr, above,
false);
909#if KMP_AFFINITY_SUPPORTED
910 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
911 void sort_compact(kmp_affinity_t &affinity) {
912 compact = affinity.compact;
913 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
914 kmp_hw_thread_t::compare_compact);
917 void print(
const char *env_var =
"KMP_AFFINITY")
const;
920extern kmp_topology_t *__kmp_topology;
922class kmp_hw_subset_t {
923 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
931 int offset[MAX_ATTRS];
932 kmp_hw_attr_t attr[MAX_ATTRS];
935 const static int USE_ALL = (std::numeric_limits<int>::max)();
944 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
947 static int hw_subset_compare(
const void *i1,
const void *i2) {
948 kmp_hw_t type1 = ((
const item_t *)i1)->type;
949 kmp_hw_t type2 = ((
const item_t *)i2)->type;
950 int level1 = __kmp_topology->get_level(type1);
951 int level2 = __kmp_topology->get_level(type2);
952 return level1 - level2;
957 kmp_hw_subset_t() =
delete;
958 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
959 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
960 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
961 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
963 static kmp_hw_subset_t *allocate() {
964 int initial_capacity = 5;
965 kmp_hw_subset_t *retval =
966 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
968 retval->capacity = initial_capacity;
970 retval->absolute =
false;
971 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
974 static void deallocate(kmp_hw_subset_t *subset) {
975 __kmp_free(subset->items);
978 void set_absolute() { absolute =
true; }
979 bool is_absolute()
const {
return absolute; }
980 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
981 for (
int i = 0; i < depth; ++i) {
984 if (items[i].type == type) {
985 int idx = items[i].num_attrs++;
986 if ((
size_t)idx >= MAX_ATTRS)
988 items[i].num[idx] = num;
989 items[i].offset[idx] = offset;
990 items[i].attr[idx] = attr;
994 if (depth == capacity - 1) {
996 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
997 for (
int i = 0; i < depth; ++i)
998 new_items[i] = items[i];
1002 items[depth].num_attrs = 1;
1003 items[depth].type = type;
1004 items[depth].num[0] = num;
1005 items[depth].offset[0] = offset;
1006 items[depth].attr[0] = attr;
1008 set |= (1ull << type);
1010 int get_depth()
const {
return depth; }
1011 const item_t &at(
int index)
const {
1012 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1013 return items[index];
1015 item_t &at(
int index) {
1016 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1017 return items[index];
1019 void remove(
int index) {
1020 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1021 set &= ~(1ull << items[index].type);
1022 for (
int j = index + 1; j < depth; ++j) {
1023 items[j - 1] = items[j];
1028 KMP_DEBUG_ASSERT(__kmp_topology);
1029 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1031 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1033 printf(
"**********************\n");
1034 printf(
"*** kmp_hw_subset: ***\n");
1035 printf(
"* depth: %d\n", depth);
1036 printf(
"* items:\n");
1037 for (
int i = 0; i < depth; ++i) {
1038 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1039 for (
int j = 0; j < items[i].num_attrs; ++j) {
1040 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1041 items[i].offset[j]);
1042 if (!items[i].attr[j]) {
1043 printf(
" (none)\n");
1046 " core_type = %s, core_eff = %d\n",
1047 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1048 items[i].attr[j].get_core_eff());
1052 printf(
"* set: 0x%llx\n", set);
1053 printf(
"* absolute: %d\n", absolute);
1054 printf(
"**********************\n");
1057extern kmp_hw_subset_t *__kmp_hw_subset;
1065class hierarchy_info {
1069 static const kmp_uint32 maxLeaves = 4;
1070 static const kmp_uint32 minBranch = 4;
1076 kmp_uint32 maxLevels;
1083 kmp_uint32 base_num_threads;
1084 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1085 volatile kmp_int8 uninitialized;
1087 volatile kmp_int8 resizing;
1093 kmp_uint32 *numPerLevel;
1094 kmp_uint32 *skipPerLevel;
1096 void deriveLevels() {
1097 int hier_depth = __kmp_topology->get_depth();
1098 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1099 numPerLevel[level] = __kmp_topology->get_ratio(i);
1104 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1107 if (!uninitialized && numPerLevel) {
1108 __kmp_free(numPerLevel);
1110 uninitialized = not_initialized;
1114 void init(
int num_addrs) {
1115 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1116 &uninitialized, not_initialized, initializing);
1117 if (bool_result == 0) {
1118 while (TCR_1(uninitialized) != initialized)
1122 KMP_DEBUG_ASSERT(bool_result == 1);
1132 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1133 skipPerLevel = &(numPerLevel[maxLevels]);
1134 for (kmp_uint32 i = 0; i < maxLevels;
1137 skipPerLevel[i] = 1;
1141 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1144 numPerLevel[0] = maxLeaves;
1145 numPerLevel[1] = num_addrs / maxLeaves;
1146 if (num_addrs % maxLeaves)
1150 base_num_threads = num_addrs;
1151 for (
int i = maxLevels - 1; i >= 0;
1153 if (numPerLevel[i] != 1 || depth > 1)
1156 kmp_uint32 branch = minBranch;
1157 if (numPerLevel[0] == 1)
1158 branch = num_addrs / maxLeaves;
1159 if (branch < minBranch)
1161 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1162 while (numPerLevel[d] > branch ||
1163 (d == 0 && numPerLevel[d] > maxLeaves)) {
1164 if (numPerLevel[d] & 1)
1166 numPerLevel[d] = numPerLevel[d] >> 1;
1167 if (numPerLevel[d + 1] == 1)
1169 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1171 if (numPerLevel[0] == 1) {
1172 branch = branch >> 1;
1178 for (kmp_uint32 i = 1; i < depth; ++i)
1179 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1181 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1182 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1184 uninitialized = initialized;
1188 void resize(kmp_uint32 nproc) {
1189 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1190 while (bool_result == 0) {
1192 if (nproc <= base_num_threads)
1195 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1197 KMP_DEBUG_ASSERT(bool_result != 0);
1198 if (nproc <= base_num_threads)
1202 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1203 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1205 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1206 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1207 numPerLevel[i - 1] *= 2;
1211 if (nproc > old_sz) {
1212 while (nproc > old_sz) {
1220 kmp_uint32 *old_numPerLevel = numPerLevel;
1221 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1222 numPerLevel = skipPerLevel = NULL;
1224 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1225 skipPerLevel = &(numPerLevel[maxLevels]);
1228 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1230 numPerLevel[i] = old_numPerLevel[i];
1231 skipPerLevel[i] = old_skipPerLevel[i];
1235 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1238 skipPerLevel[i] = 1;
1242 __kmp_free(old_numPerLevel);
1246 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1247 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1249 base_num_threads = nproc;