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.
271#error Unknown or unsupported architecture
275#include <pthread_np.h>
277class KMPNativeAffinity :
public KMPAffinity {
278 class Mask :
public KMPAffinity::Mask {
279 typedef unsigned long mask_t;
280 typedef decltype(__kmp_affin_mask_size) mask_size_type;
281 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
282 static const mask_t ONE = 1;
283 mask_size_type get_num_mask_types()
const {
284 return __kmp_affin_mask_size /
sizeof(mask_t);
289 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
294 void set(
int i)
override {
295 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
297 bool is_set(
int i)
const override {
298 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
300 void clear(
int i)
override {
301 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
303 void zero()
override {
304 mask_size_type e = get_num_mask_types();
305 for (mask_size_type i = 0; i < e; ++i)
308 void copy(
const KMPAffinity::Mask *src)
override {
309 const Mask *convert =
static_cast<const Mask *
>(src);
310 mask_size_type e = get_num_mask_types();
311 for (mask_size_type i = 0; i < e; ++i)
312 mask[i] = convert->mask[i];
314 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
315 const Mask *convert =
static_cast<const Mask *
>(rhs);
316 mask_size_type e = get_num_mask_types();
317 for (mask_size_type i = 0; i < e; ++i)
318 mask[i] &= convert->mask[i];
320 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
321 const Mask *convert =
static_cast<const Mask *
>(rhs);
322 mask_size_type e = get_num_mask_types();
323 for (mask_size_type i = 0; i < e; ++i)
324 mask[i] |= convert->mask[i];
326 void bitwise_not()
override {
327 mask_size_type e = get_num_mask_types();
328 for (mask_size_type i = 0; i < e; ++i)
329 mask[i] = ~(mask[i]);
331 int begin()
const override {
333 while (retval < end() && !is_set(retval))
337 int end()
const override {
339 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
342 int next(
int previous)
const override {
343 int retval = previous + 1;
344 while (retval < end() && !is_set(retval))
348 int get_system_affinity(
bool abort_on_error)
override {
349 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
350 "Illegal get affinity operation when not capable");
353 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
355 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
356 reinterpret_cast<cpuset_t *
>(mask));
357 int retval = (r == 0 ? 0 : -1);
363 if (abort_on_error) {
364 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
368 int set_system_affinity(
bool abort_on_error)
const override {
369 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
370 "Illegal set affinity operation when not capable");
373 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
375 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
376 reinterpret_cast<cpuset_t *
>(mask));
377 int retval = (r == 0 ? 0 : -1);
383 if (abort_on_error) {
384 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
389 void determine_capable(
const char *env_var)
override {
390 __kmp_affinity_determine_capable(env_var);
392 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
393 KMPAffinity::Mask *allocate_mask()
override {
394 KMPNativeAffinity::Mask *retval =
new Mask();
397 void deallocate_mask(KMPAffinity::Mask *m)
override {
398 KMPNativeAffinity::Mask *native_mask =
399 static_cast<KMPNativeAffinity::Mask *
>(m);
402 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
403 return new Mask[num];
405 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
406 Mask *linux_array =
static_cast<Mask *
>(array);
407 delete[] linux_array;
409 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
410 int index)
override {
411 Mask *linux_array =
static_cast<Mask *
>(array);
412 return &(linux_array[index]);
414 api_type get_api_type()
const override {
return NATIVE_OS; }
419class KMPNativeAffinity :
public KMPAffinity {
420 class Mask :
public KMPAffinity::Mask {
421 typedef ULONG_PTR mask_t;
422 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
427 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
433 void set(
int i)
override {
434 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
436 bool is_set(
int i)
const override {
437 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
439 void clear(
int i)
override {
440 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
442 void zero()
override {
443 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
446 void copy(
const KMPAffinity::Mask *src)
override {
447 const Mask *convert =
static_cast<const Mask *
>(src);
448 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
449 mask[i] = convert->mask[i];
451 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
452 const Mask *convert =
static_cast<const Mask *
>(rhs);
453 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
454 mask[i] &= convert->mask[i];
456 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
457 const Mask *convert =
static_cast<const Mask *
>(rhs);
458 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
459 mask[i] |= convert->mask[i];
461 void bitwise_not()
override {
462 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
463 mask[i] = ~(mask[i]);
465 int begin()
const override {
467 while (retval < end() && !is_set(retval))
471 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
472 int next(
int previous)
const override {
473 int retval = previous + 1;
474 while (retval < end() && !is_set(retval))
478 int set_process_affinity(
bool abort_on_error)
const override {
479 if (__kmp_num_proc_groups <= 1) {
480 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
481 DWORD error = GetLastError();
482 if (abort_on_error) {
483 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
491 int set_system_affinity(
bool abort_on_error)
const override {
492 if (__kmp_num_proc_groups > 1) {
495 int group = get_proc_group();
497 if (abort_on_error) {
498 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
505 ga.Mask = mask[group];
506 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
508 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
509 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
510 DWORD error = GetLastError();
511 if (abort_on_error) {
512 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
518 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
519 DWORD error = GetLastError();
520 if (abort_on_error) {
521 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
529 int get_system_affinity(
bool abort_on_error)
override {
530 if (__kmp_num_proc_groups > 1) {
533 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
534 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
535 DWORD error = GetLastError();
536 if (abort_on_error) {
537 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
538 KMP_ERR(error), __kmp_msg_null);
542 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
546 mask[ga.Group] = ga.Mask;
548 mask_t newMask, sysMask, retval;
549 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
550 DWORD error = GetLastError();
551 if (abort_on_error) {
552 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
553 KMP_ERR(error), __kmp_msg_null);
557 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
559 DWORD error = GetLastError();
560 if (abort_on_error) {
561 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
562 KMP_ERR(error), __kmp_msg_null);
566 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
568 DWORD error = GetLastError();
569 if (abort_on_error) {
570 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
571 KMP_ERR(error), __kmp_msg_null);
578 int get_proc_group()
const override {
580 if (__kmp_num_proc_groups == 1) {
583 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
593 void determine_capable(
const char *env_var)
override {
594 __kmp_affinity_determine_capable(env_var);
596 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
597 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
598 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
599 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
600 return new Mask[num];
602 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
603 Mask *windows_array =
static_cast<Mask *
>(array);
604 delete[] windows_array;
606 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
607 int index)
override {
608 Mask *windows_array =
static_cast<Mask *
>(array);
609 return &(windows_array[index]);
611 api_type get_api_type()
const override {
return NATIVE_OS; }
617struct kmp_hw_attr_t {
621 unsigned reserved : 15;
623 static const int UNKNOWN_CORE_EFF = -1;
626 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
627 valid(0), reserved(0) {}
628 void set_core_type(kmp_hw_core_type_t type) {
632 void set_core_eff(
int eff) {
636 kmp_hw_core_type_t get_core_type()
const {
637 return (kmp_hw_core_type_t)core_type;
639 int get_core_eff()
const {
return core_eff; }
640 bool is_core_type_valid()
const {
641 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
643 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
644 operator bool()
const {
return valid; }
646 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
647 core_eff = UNKNOWN_CORE_EFF;
650 bool contains(
const kmp_hw_attr_t &other)
const {
651 if (!valid && !other.valid)
653 if (valid && other.valid) {
654 if (other.is_core_type_valid()) {
655 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
658 if (other.is_core_eff_valid()) {
659 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
666 bool operator==(
const kmp_hw_attr_t &rhs)
const {
667 return (rhs.valid == valid && rhs.core_eff == core_eff &&
668 rhs.core_type == core_type);
670 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
673class kmp_hw_thread_t {
675 static const int UNKNOWN_ID = -1;
676 static int compare_ids(
const void *a,
const void *b);
677 static int compare_compact(
const void *a,
const void *b);
678 int ids[KMP_HW_LAST];
679 int sub_ids[KMP_HW_LAST];
686 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
693class kmp_topology_t {
719 int num_core_efficiencies;
721 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
727 kmp_hw_thread_t *hw_threads;
733 kmp_hw_t equivalent[KMP_HW_LAST];
742 void _insert_layer(kmp_hw_t type,
const int *ids);
744#if KMP_GROUP_AFFINITY
746 void _insert_windows_proc_groups();
752 void _gather_enumeration_information();
756 void _remove_radix1_layers();
759 void _discover_uniformity();
770 void _set_last_level_cache();
775 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
776 bool find_all =
false)
const;
780 kmp_topology_t() =
delete;
781 kmp_topology_t(
const kmp_topology_t &t) =
delete;
782 kmp_topology_t(kmp_topology_t &&t) =
delete;
783 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
784 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
786 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
787 static void deallocate(kmp_topology_t *);
790 kmp_hw_thread_t &at(
int index) {
791 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
792 return hw_threads[index];
794 const kmp_hw_thread_t &at(
int index)
const {
795 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
796 return hw_threads[index];
798 int get_num_hw_threads()
const {
return num_hw_threads; }
800 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
801 kmp_hw_thread_t::compare_ids);
805 bool check_ids()
const;
809 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
813#if KMP_AFFINITY_SUPPORTED
815 void set_granularity(kmp_affinity_t &stgs)
const;
817 bool filter_hw_subset();
818 bool is_close(
int hwt1,
int hwt2,
int level)
const;
819 bool is_uniform()
const {
return flags.uniform; }
822 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
return equivalent[type]; }
824 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
825 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
826 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
827 kmp_hw_t real_type2 = equivalent[type2];
828 if (real_type2 == KMP_HW_UNKNOWN)
830 equivalent[type1] = real_type2;
833 KMP_FOREACH_HW_TYPE(type) {
834 if (equivalent[type] == type1) {
835 equivalent[type] = real_type2;
841 int calculate_ratio(
int level1,
int level2)
const {
842 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
843 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
845 for (
int level = level1; level > level2; --level)
849 int get_ratio(
int level)
const {
850 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
853 int get_depth()
const {
return depth; };
854 kmp_hw_t get_type(
int level)
const {
855 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
858 int get_level(kmp_hw_t type)
const {
859 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
860 int eq_type = equivalent[type];
861 if (eq_type == KMP_HW_UNKNOWN)
863 for (
int i = 0; i < depth; ++i)
864 if (types[i] == eq_type)
868 int get_count(
int level)
const {
869 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
873 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
874 return _get_ncores_with_attr(attr, -1,
true);
878 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
879 return _get_ncores_with_attr(attr, above,
false);
882#if KMP_AFFINITY_SUPPORTED
883 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
884 void sort_compact(kmp_affinity_t &affinity) {
885 compact = affinity.compact;
886 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
887 kmp_hw_thread_t::compare_compact);
890 void print(
const char *env_var =
"KMP_AFFINITY")
const;
893extern kmp_topology_t *__kmp_topology;
895class kmp_hw_subset_t {
896 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
904 int offset[MAX_ATTRS];
905 kmp_hw_attr_t attr[MAX_ATTRS];
908 const static int USE_ALL = (std::numeric_limits<int>::max)();
917 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
920 static int hw_subset_compare(
const void *i1,
const void *i2) {
921 kmp_hw_t type1 = ((
const item_t *)i1)->type;
922 kmp_hw_t type2 = ((
const item_t *)i2)->type;
923 int level1 = __kmp_topology->get_level(type1);
924 int level2 = __kmp_topology->get_level(type2);
925 return level1 - level2;
930 kmp_hw_subset_t() =
delete;
931 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
932 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
933 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
934 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
936 static kmp_hw_subset_t *allocate() {
937 int initial_capacity = 5;
938 kmp_hw_subset_t *retval =
939 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
941 retval->capacity = initial_capacity;
943 retval->absolute =
false;
944 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
947 static void deallocate(kmp_hw_subset_t *subset) {
948 __kmp_free(subset->items);
951 void set_absolute() { absolute =
true; }
952 bool is_absolute()
const {
return absolute; }
953 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
954 for (
int i = 0; i < depth; ++i) {
957 if (items[i].type == type) {
958 int idx = items[i].num_attrs++;
959 if ((
size_t)idx >= MAX_ATTRS)
961 items[i].num[idx] = num;
962 items[i].offset[idx] = offset;
963 items[i].attr[idx] = attr;
967 if (depth == capacity - 1) {
969 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
970 for (
int i = 0; i < depth; ++i)
971 new_items[i] = items[i];
975 items[depth].num_attrs = 1;
976 items[depth].type = type;
977 items[depth].num[0] = num;
978 items[depth].offset[0] = offset;
979 items[depth].attr[0] = attr;
981 set |= (1ull << type);
983 int get_depth()
const {
return depth; }
984 const item_t &at(
int index)
const {
985 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
988 item_t &at(
int index) {
989 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
992 void remove(
int index) {
993 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
994 set &= ~(1ull << items[index].type);
995 for (
int j = index + 1; j < depth; ++j) {
996 items[j - 1] = items[j];
1001 KMP_DEBUG_ASSERT(__kmp_topology);
1002 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1004 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1006 printf(
"**********************\n");
1007 printf(
"*** kmp_hw_subset: ***\n");
1008 printf(
"* depth: %d\n", depth);
1009 printf(
"* items:\n");
1010 for (
int i = 0; i < depth; ++i) {
1011 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1012 for (
int j = 0; j < items[i].num_attrs; ++j) {
1013 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1014 items[i].offset[j]);
1015 if (!items[i].attr[j]) {
1016 printf(
" (none)\n");
1019 " core_type = %s, core_eff = %d\n",
1020 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1021 items[i].attr[j].get_core_eff());
1025 printf(
"* set: 0x%llx\n", set);
1026 printf(
"* absolute: %d\n", absolute);
1027 printf(
"**********************\n");
1030extern kmp_hw_subset_t *__kmp_hw_subset;
1038class hierarchy_info {
1042 static const kmp_uint32 maxLeaves = 4;
1043 static const kmp_uint32 minBranch = 4;
1049 kmp_uint32 maxLevels;
1056 kmp_uint32 base_num_threads;
1057 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1058 volatile kmp_int8 uninitialized;
1060 volatile kmp_int8 resizing;
1066 kmp_uint32 *numPerLevel;
1067 kmp_uint32 *skipPerLevel;
1069 void deriveLevels() {
1070 int hier_depth = __kmp_topology->get_depth();
1071 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1072 numPerLevel[level] = __kmp_topology->get_ratio(i);
1077 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1080 if (!uninitialized && numPerLevel) {
1081 __kmp_free(numPerLevel);
1083 uninitialized = not_initialized;
1087 void init(
int num_addrs) {
1088 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1089 &uninitialized, not_initialized, initializing);
1090 if (bool_result == 0) {
1091 while (TCR_1(uninitialized) != initialized)
1095 KMP_DEBUG_ASSERT(bool_result == 1);
1105 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1106 skipPerLevel = &(numPerLevel[maxLevels]);
1107 for (kmp_uint32 i = 0; i < maxLevels;
1110 skipPerLevel[i] = 1;
1114 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1117 numPerLevel[0] = maxLeaves;
1118 numPerLevel[1] = num_addrs / maxLeaves;
1119 if (num_addrs % maxLeaves)
1123 base_num_threads = num_addrs;
1124 for (
int i = maxLevels - 1; i >= 0;
1126 if (numPerLevel[i] != 1 || depth > 1)
1129 kmp_uint32 branch = minBranch;
1130 if (numPerLevel[0] == 1)
1131 branch = num_addrs / maxLeaves;
1132 if (branch < minBranch)
1134 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1135 while (numPerLevel[d] > branch ||
1136 (d == 0 && numPerLevel[d] > maxLeaves)) {
1137 if (numPerLevel[d] & 1)
1139 numPerLevel[d] = numPerLevel[d] >> 1;
1140 if (numPerLevel[d + 1] == 1)
1142 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1144 if (numPerLevel[0] == 1) {
1145 branch = branch >> 1;
1151 for (kmp_uint32 i = 1; i < depth; ++i)
1152 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1154 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1155 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1157 uninitialized = initialized;
1161 void resize(kmp_uint32 nproc) {
1162 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1163 while (bool_result == 0) {
1165 if (nproc <= base_num_threads)
1168 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1170 KMP_DEBUG_ASSERT(bool_result != 0);
1171 if (nproc <= base_num_threads)
1175 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1176 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1178 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1179 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1180 numPerLevel[i - 1] *= 2;
1184 if (nproc > old_sz) {
1185 while (nproc > old_sz) {
1193 kmp_uint32 *old_numPerLevel = numPerLevel;
1194 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1195 numPerLevel = skipPerLevel = NULL;
1197 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1198 skipPerLevel = &(numPerLevel[maxLevels]);
1201 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1203 numPerLevel[i] = old_numPerLevel[i];
1204 skipPerLevel[i] = old_skipPerLevel[i];
1208 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1211 skipPerLevel[i] = 1;
1215 __kmp_free(old_numPerLevel);
1219 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1220 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1222 base_num_threads = nproc;