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 bool empty()
const override {
return hwloc_bitmap_iszero(mask); }
38 void copy(
const KMPAffinity::Mask *src)
override {
39 const Mask *convert =
static_cast<const Mask *
>(src);
40 hwloc_bitmap_copy(mask, convert->mask);
42 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
43 const Mask *convert =
static_cast<const Mask *
>(rhs);
44 hwloc_bitmap_and(mask, mask, convert->mask);
46 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
47 const Mask *convert =
static_cast<const Mask *
>(rhs);
48 hwloc_bitmap_or(mask, mask, convert->mask);
50 void bitwise_not()
override { hwloc_bitmap_not(mask, mask); }
51 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
52 const Mask *convert =
static_cast<const Mask *
>(rhs);
53 return hwloc_bitmap_isequal(mask, convert->mask);
55 int begin()
const override {
return hwloc_bitmap_first(mask); }
56 int end()
const override {
return -1; }
57 int next(
int previous)
const override {
58 return hwloc_bitmap_next(mask, previous);
60 int get_system_affinity(
bool abort_on_error)
override {
61 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
62 "Illegal get affinity operation when not capable");
64 hwloc_get_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
70 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_get_cpubind()"),
71 KMP_ERR(error), __kmp_msg_null);
75 int set_system_affinity(
bool abort_on_error)
const override {
76 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
77 "Illegal set affinity operation when not capable");
79 hwloc_set_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
85 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_set_cpubind()"),
86 KMP_ERR(error), __kmp_msg_null);
91 int set_process_affinity(
bool abort_on_error)
const override {
92 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
93 "Illegal set process affinity operation when not capable");
95 const hwloc_topology_support *support =
96 hwloc_topology_get_support(__kmp_hwloc_topology);
97 if (support->cpubind->set_proc_cpubind) {
99 retval = hwloc_set_cpubind(__kmp_hwloc_topology, mask,
100 HWLOC_CPUBIND_PROCESS);
105 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_set_cpubind()"),
106 KMP_ERR(error), __kmp_msg_null);
111 int get_proc_group()
const override {
114 if (__kmp_num_proc_groups == 1) {
117 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
119 unsigned long first_32_bits = hwloc_bitmap_to_ith_ulong(mask, i * 2);
120 unsigned long second_32_bits =
121 hwloc_bitmap_to_ith_ulong(mask, i * 2 + 1);
122 if (first_32_bits == 0 && second_32_bits == 0) {
134 void determine_capable(
const char *var)
override {
135 const hwloc_topology_support *topology_support;
136 if (__kmp_hwloc_topology == NULL) {
137 if (hwloc_topology_init(&__kmp_hwloc_topology) < 0) {
138 __kmp_hwloc_error = TRUE;
139 if (__kmp_affinity.flags.verbose) {
140 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_init()");
143 if (hwloc_topology_load(__kmp_hwloc_topology) < 0) {
144 __kmp_hwloc_error = TRUE;
145 if (__kmp_affinity.flags.verbose) {
146 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_load()");
150 topology_support = hwloc_topology_get_support(__kmp_hwloc_topology);
155 if (topology_support && topology_support->cpubind->set_thisthread_cpubind &&
156 topology_support->cpubind->get_thisthread_cpubind &&
157 topology_support->discovery->pu && !__kmp_hwloc_error) {
159 KMP_AFFINITY_ENABLE(TRUE);
162 __kmp_hwloc_error = TRUE;
163 KMP_AFFINITY_DISABLE();
166 void bind_thread(
int which)
override {
167 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
168 "Illegal set affinity operation when not capable");
169 KMPAffinity::Mask *mask;
170 KMP_CPU_ALLOC_ON_STACK(mask);
172 KMP_CPU_SET(which, mask);
173 __kmp_set_system_affinity(mask, TRUE);
174 KMP_CPU_FREE_FROM_STACK(mask);
176 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
177 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
178 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
179 return new Mask[num];
181 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
182 Mask *hwloc_array =
static_cast<Mask *
>(array);
183 delete[] hwloc_array;
185 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
186 int index)
override {
187 Mask *hwloc_array =
static_cast<Mask *
>(array);
188 return &(hwloc_array[index]);
190 api_type get_api_type()
const override {
return HWLOC; }
194#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
201#include <sys/syscall.h>
202#if KMP_ARCH_X86 || KMP_ARCH_ARM
203#ifndef __NR_sched_setaffinity
204#define __NR_sched_setaffinity 241
205#elif __NR_sched_setaffinity != 241
206#error Wrong code for setaffinity system call.
208#ifndef __NR_sched_getaffinity
209#define __NR_sched_getaffinity 242
210#elif __NR_sched_getaffinity != 242
211#error Wrong code for getaffinity system call.
213#elif KMP_ARCH_AARCH64
214#ifndef __NR_sched_setaffinity
215#define __NR_sched_setaffinity 122
216#elif __NR_sched_setaffinity != 122
217#error Wrong code for setaffinity system call.
219#ifndef __NR_sched_getaffinity
220#define __NR_sched_getaffinity 123
221#elif __NR_sched_getaffinity != 123
222#error Wrong code for getaffinity system call.
224#elif KMP_ARCH_RISCV64
225#ifndef __NR_sched_setaffinity
226#define __NR_sched_setaffinity 122
227#elif __NR_sched_setaffinity != 122
228#error Wrong code for setaffinity system call.
230#ifndef __NR_sched_getaffinity
231#define __NR_sched_getaffinity 123
232#elif __NR_sched_getaffinity != 123
233#error Wrong code for getaffinity system call.
236#ifndef __NR_sched_setaffinity
237#define __NR_sched_setaffinity 203
238#elif __NR_sched_setaffinity != 203
239#error Wrong code for setaffinity system call.
241#ifndef __NR_sched_getaffinity
242#define __NR_sched_getaffinity 204
243#elif __NR_sched_getaffinity != 204
244#error Wrong code for getaffinity system call.
247#ifndef __NR_sched_setaffinity
248#define __NR_sched_setaffinity 222
249#elif __NR_sched_setaffinity != 222
250#error Wrong code for setaffinity system call.
252#ifndef __NR_sched_getaffinity
253#define __NR_sched_getaffinity 223
254#elif __NR_sched_getaffinity != 223
255#error Wrong code for getaffinity system call.
258#ifndef __NR_sched_setaffinity
259#define __NR_sched_setaffinity 4239
260#elif __NR_sched_setaffinity != 4239
261#error Wrong code for setaffinity system call.
263#ifndef __NR_sched_getaffinity
264#define __NR_sched_getaffinity 4240
265#elif __NR_sched_getaffinity != 4240
266#error Wrong code for getaffinity system call.
269#ifndef __NR_sched_setaffinity
270#define __NR_sched_setaffinity 5195
271#elif __NR_sched_setaffinity != 5195
272#error Wrong code for setaffinity system call.
274#ifndef __NR_sched_getaffinity
275#define __NR_sched_getaffinity 5196
276#elif __NR_sched_getaffinity != 5196
277#error Wrong code for getaffinity system call.
279#elif KMP_ARCH_LOONGARCH64
280#ifndef __NR_sched_setaffinity
281#define __NR_sched_setaffinity 122
282#elif __NR_sched_setaffinity != 122
283#error Wrong code for setaffinity system call.
285#ifndef __NR_sched_getaffinity
286#define __NR_sched_getaffinity 123
287#elif __NR_sched_getaffinity != 123
288#error Wrong code for getaffinity system call.
290#elif KMP_ARCH_RISCV64
291#ifndef __NR_sched_setaffinity
292#define __NR_sched_setaffinity 122
293#elif __NR_sched_setaffinity != 122
294#error Wrong code for setaffinity system call.
296#ifndef __NR_sched_getaffinity
297#define __NR_sched_getaffinity 123
298#elif __NR_sched_getaffinity != 123
299#error Wrong code for getaffinity system call.
302#ifndef __NR_sched_setaffinity
303#define __NR_sched_setaffinity 203
304#elif __NR_sched_setaffinity != 203
305#error Wrong code for setaffinity system call.
307#ifndef __NR_sched_getaffinity
308#define __NR_sched_getaffinity 204
309#elif __NR_sched_getaffinity != 204
310#error Wrong code for getaffinity system call.
313#ifndef __NR_sched_setaffinity
314#define __NR_sched_setaffinity 239
315#elif __NR_sched_setaffinity != 239
316#error Wrong code for setaffinity system call.
318#ifndef __NR_sched_getaffinity
319#define __NR_sched_getaffinity 240
320#elif __NR_sched_getaffinity != 240
321#error Wrong code for getaffinity system call.
324#error Unknown or unsupported architecture
326#elif KMP_OS_FREEBSD || KMP_OS_DRAGONFLY
328#include <pthread_np.h>
335#define VMI_MAXRADS 64
337class KMPNativeAffinity :
public KMPAffinity {
338 class Mask :
public KMPAffinity::Mask {
339 typedef unsigned long mask_t;
340 typedef decltype(__kmp_affin_mask_size) mask_size_type;
341 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
342 static const mask_t ONE = 1;
343 mask_size_type get_num_mask_types()
const {
344 return __kmp_affin_mask_size /
sizeof(mask_t);
349 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
354 void set(
int i)
override {
355 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
357 bool is_set(
int i)
const override {
358 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
360 void clear(
int i)
override {
361 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
363 void zero()
override {
364 mask_size_type e = get_num_mask_types();
365 for (mask_size_type i = 0; i < e; ++i)
368 bool empty()
const override {
369 mask_size_type e = get_num_mask_types();
370 for (mask_size_type i = 0; i < e; ++i)
371 if (mask[i] != (mask_t)0)
375 void copy(
const KMPAffinity::Mask *src)
override {
376 const Mask *convert =
static_cast<const Mask *
>(src);
377 mask_size_type e = get_num_mask_types();
378 for (mask_size_type i = 0; i < e; ++i)
379 mask[i] = convert->mask[i];
381 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
382 const Mask *convert =
static_cast<const Mask *
>(rhs);
383 mask_size_type e = get_num_mask_types();
384 for (mask_size_type i = 0; i < e; ++i)
385 mask[i] &= convert->mask[i];
387 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
388 const Mask *convert =
static_cast<const Mask *
>(rhs);
389 mask_size_type e = get_num_mask_types();
390 for (mask_size_type i = 0; i < e; ++i)
391 mask[i] |= convert->mask[i];
393 void bitwise_not()
override {
394 mask_size_type e = get_num_mask_types();
395 for (mask_size_type i = 0; i < e; ++i)
396 mask[i] = ~(mask[i]);
398 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
399 const Mask *convert =
static_cast<const Mask *
>(rhs);
400 mask_size_type e = get_num_mask_types();
401 for (mask_size_type i = 0; i < e; ++i)
402 if (mask[i] != convert->mask[i])
406 int begin()
const override {
408 while (retval < end() && !is_set(retval))
412 int end()
const override {
414 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
417 int next(
int previous)
const override {
418 int retval = previous + 1;
419 while (retval < end() && !is_set(retval))
426 int get_system_affinity(
bool abort_on_error)
override {
427 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
428 "Illegal get affinity operation when not capable");
430 (void)abort_on_error;
433 for (
int i = 0; i < __kmp_xproc; ++i)
434 KMP_CPU_SET(i,
this);
437 int set_system_affinity(
bool abort_on_error)
const override {
438 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
440 "Illegal set affinity operation when not capable");
443 int gtid = __kmp_entry_gtid();
444 int tid = thread_self();
448 int retval = bindprocessor(BINDTHREAD, tid, PROCESSOR_CLASS_ANY);
452 KMP_CPU_SET_ITERATE(location,
this) {
453 if (KMP_CPU_ISSET(location,
this)) {
454 retval = bindprocessor(BINDTHREAD, tid, location);
455 if (retval == -1 && errno == 1) {
460 rsh = rs_alloc(RS_EMPTY);
461 rsid.at_pid = getpid();
462 if (RS_DEFAULT_RSET != ra_getrset(R_PROCESS, rsid, 0, rsh)) {
463 retval = ra_detachrset(R_PROCESS, rsid, 0);
464 retval = bindprocessor(BINDTHREAD, tid, location);
468 KA_TRACE(10, (
"__kmp_set_system_affinity: Done binding "
474 if (abort_on_error) {
475 __kmp_fatal(KMP_MSG(FunctionError,
"bindprocessor()"),
476 KMP_ERR(error), __kmp_msg_null);
477 KA_TRACE(10, (
"__kmp_set_system_affinity: Error binding "
478 "T#%d to cpu=%d, errno=%d.\n",
479 gtid, location, error));
487 int get_system_affinity(
bool abort_on_error)
override {
488 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
489 "Illegal get affinity operation when not capable");
492 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
493#elif KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY
494 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
495 reinterpret_cast<cpuset_t *
>(mask));
496 int retval = (r == 0 ? 0 : -1);
502 if (abort_on_error) {
503 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_getaffinity_np()"),
504 KMP_ERR(error), __kmp_msg_null);
508 int set_system_affinity(
bool abort_on_error)
const override {
509 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
510 "Illegal set affinity operation when not capable");
513 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
514#elif KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY
515 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
516 reinterpret_cast<cpuset_t *
>(mask));
517 int retval = (r == 0 ? 0 : -1);
523 if (abort_on_error) {
524 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_setaffinity_np()"),
525 KMP_ERR(error), __kmp_msg_null);
531 void determine_capable(
const char *env_var)
override {
532 __kmp_affinity_determine_capable(env_var);
534 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
535 KMPAffinity::Mask *allocate_mask()
override {
536 KMPNativeAffinity::Mask *retval =
new Mask();
539 void deallocate_mask(KMPAffinity::Mask *m)
override {
540 KMPNativeAffinity::Mask *native_mask =
541 static_cast<KMPNativeAffinity::Mask *
>(m);
544 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
545 return new Mask[num];
547 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
548 Mask *linux_array =
static_cast<Mask *
>(array);
549 delete[] linux_array;
551 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
552 int index)
override {
553 Mask *linux_array =
static_cast<Mask *
>(array);
554 return &(linux_array[index]);
556 api_type get_api_type()
const override {
return NATIVE_OS; }
562class KMPNativeAffinity :
public KMPAffinity {
563 class Mask :
public KMPAffinity::Mask {
564 typedef ULONG_PTR mask_t;
565 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
570 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
576 void set(
int i)
override {
577 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
579 bool is_set(
int i)
const override {
580 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
582 void clear(
int i)
override {
583 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
585 void zero()
override {
586 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
589 bool empty()
const override {
590 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
595 void copy(
const KMPAffinity::Mask *src)
override {
596 const Mask *convert =
static_cast<const Mask *
>(src);
597 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
598 mask[i] = convert->mask[i];
600 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
601 const Mask *convert =
static_cast<const Mask *
>(rhs);
602 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
603 mask[i] &= convert->mask[i];
605 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
606 const Mask *convert =
static_cast<const Mask *
>(rhs);
607 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
608 mask[i] |= convert->mask[i];
610 void bitwise_not()
override {
611 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
612 mask[i] = ~(mask[i]);
614 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
615 const Mask *convert =
static_cast<const Mask *
>(rhs);
616 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
617 if (mask[i] != convert->mask[i])
621 int begin()
const override {
623 while (retval < end() && !is_set(retval))
627 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
628 int next(
int previous)
const override {
629 int retval = previous + 1;
630 while (retval < end() && !is_set(retval))
634 int set_process_affinity(
bool abort_on_error)
const override {
635 if (__kmp_num_proc_groups <= 1) {
636 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
637 DWORD error = GetLastError();
638 if (abort_on_error) {
639 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
647 int set_system_affinity(
bool abort_on_error)
const override {
648 if (__kmp_num_proc_groups > 1) {
651 int group = get_proc_group();
653 if (abort_on_error) {
654 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
661 ga.Mask = mask[group];
662 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
664 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
665 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
666 DWORD error = GetLastError();
667 if (abort_on_error) {
668 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
674 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
675 DWORD error = GetLastError();
676 if (abort_on_error) {
677 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
685 int get_system_affinity(
bool abort_on_error)
override {
686 if (__kmp_num_proc_groups > 1) {
689 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
690 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
691 DWORD error = GetLastError();
692 if (abort_on_error) {
693 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
694 KMP_ERR(error), __kmp_msg_null);
698 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
702 mask[ga.Group] = ga.Mask;
704 mask_t newMask, sysMask, retval;
705 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
706 DWORD error = GetLastError();
707 if (abort_on_error) {
708 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
709 KMP_ERR(error), __kmp_msg_null);
713 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
715 DWORD error = GetLastError();
716 if (abort_on_error) {
717 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
718 KMP_ERR(error), __kmp_msg_null);
722 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
724 DWORD error = GetLastError();
725 if (abort_on_error) {
726 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
727 KMP_ERR(error), __kmp_msg_null);
734 int get_proc_group()
const override {
736 if (__kmp_num_proc_groups == 1) {
739 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
749 void determine_capable(
const char *env_var)
override {
750 __kmp_affinity_determine_capable(env_var);
752 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
753 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
754 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
755 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
756 return new Mask[num];
758 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
759 Mask *windows_array =
static_cast<Mask *
>(array);
760 delete[] windows_array;
762 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
763 int index)
override {
764 Mask *windows_array =
static_cast<Mask *
>(array);
765 return &(windows_array[index]);
767 api_type get_api_type()
const override {
return NATIVE_OS; }
773struct kmp_hw_attr_t {
777 unsigned reserved : 15;
779 static const int UNKNOWN_CORE_EFF = -1;
782 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
783 valid(0), reserved(0) {}
784 void set_core_type(kmp_hw_core_type_t type) {
788 void set_core_eff(
int eff) {
792 kmp_hw_core_type_t get_core_type()
const {
793 return (kmp_hw_core_type_t)core_type;
795 int get_core_eff()
const {
return core_eff; }
796 bool is_core_type_valid()
const {
797 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
799 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
800 operator bool()
const {
return valid; }
802 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
803 core_eff = UNKNOWN_CORE_EFF;
806 bool contains(
const kmp_hw_attr_t &other)
const {
807 if (!valid && !other.valid)
809 if (valid && other.valid) {
810 if (other.is_core_type_valid()) {
811 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
814 if (other.is_core_eff_valid()) {
815 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
822#if KMP_AFFINITY_SUPPORTED
823 bool contains(
const kmp_affinity_attrs_t &attr)
const {
824 if (!valid && !attr.valid)
826 if (valid && attr.valid) {
827 if (attr.core_type != KMP_HW_CORE_TYPE_UNKNOWN)
828 return (is_core_type_valid() &&
829 (get_core_type() == (kmp_hw_core_type_t)attr.core_type));
830 if (attr.core_eff != UNKNOWN_CORE_EFF)
831 return (is_core_eff_valid() && (get_core_eff() == attr.core_eff));
837 bool operator==(
const kmp_hw_attr_t &rhs)
const {
838 return (rhs.valid == valid && rhs.core_eff == core_eff &&
839 rhs.core_type == core_type);
841 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
844#if KMP_AFFINITY_SUPPORTED
845KMP_BUILD_ASSERT(
sizeof(kmp_hw_attr_t) ==
sizeof(kmp_affinity_attrs_t));
848class kmp_hw_thread_t {
850 static const int UNKNOWN_ID = -1;
851 static const int MULTIPLE_ID = -2;
852 static int compare_ids(
const void *a,
const void *b);
853 static int compare_compact(
const void *a,
const void *b);
854 int ids[KMP_HW_LAST];
855 int sub_ids[KMP_HW_LAST];
862 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
869class kmp_topology_t {
895 int num_core_efficiencies;
897 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
903 kmp_hw_thread_t *hw_threads;
909 kmp_hw_t equivalent[KMP_HW_LAST];
918 void _insert_layer(kmp_hw_t type,
const int *ids);
920#if KMP_GROUP_AFFINITY
922 void _insert_windows_proc_groups();
928 void _gather_enumeration_information();
932 void _remove_radix1_layers();
935 void _discover_uniformity();
946 void _set_last_level_cache();
951 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
952 bool find_all =
false)
const;
956 kmp_topology_t() =
delete;
957 kmp_topology_t(
const kmp_topology_t &t) =
delete;
958 kmp_topology_t(kmp_topology_t &&t) =
delete;
959 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
960 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
962 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
963 static void deallocate(kmp_topology_t *);
966 kmp_hw_thread_t &at(
int index) {
967 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
968 return hw_threads[index];
970 const kmp_hw_thread_t &at(
int index)
const {
971 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
972 return hw_threads[index];
974 int get_num_hw_threads()
const {
return num_hw_threads; }
976 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
977 kmp_hw_thread_t::compare_ids);
981 bool check_ids()
const;
985 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
989#if KMP_AFFINITY_SUPPORTED
991 void set_granularity(kmp_affinity_t &stgs)
const;
992 bool is_close(
int hwt1,
int hwt2,
const kmp_affinity_t &stgs)
const;
993 bool restrict_to_mask(
const kmp_affin_mask_t *mask);
994 bool filter_hw_subset();
996 bool is_uniform()
const {
return flags.uniform; }
999 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
1000 if (type == KMP_HW_UNKNOWN)
1001 return KMP_HW_UNKNOWN;
1002 return equivalent[type];
1005 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
1006 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
1007 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
1008 kmp_hw_t real_type2 = equivalent[type2];
1009 if (real_type2 == KMP_HW_UNKNOWN)
1011 equivalent[type1] = real_type2;
1014 KMP_FOREACH_HW_TYPE(type) {
1015 if (equivalent[type] == type1) {
1016 equivalent[type] = real_type2;
1022 int calculate_ratio(
int level1,
int level2)
const {
1023 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
1024 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
1026 for (
int level = level1; level > level2; --level)
1030 int get_ratio(
int level)
const {
1031 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1032 return ratio[level];
1034 int get_depth()
const {
return depth; };
1035 kmp_hw_t get_type(
int level)
const {
1036 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1037 return types[level];
1039 int get_level(kmp_hw_t type)
const {
1040 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
1041 int eq_type = equivalent[type];
1042 if (eq_type == KMP_HW_UNKNOWN)
1044 for (
int i = 0; i < depth; ++i)
1045 if (types[i] == eq_type)
1049 int get_count(
int level)
const {
1050 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1051 return count[level];
1054 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
1055 return _get_ncores_with_attr(attr, -1,
true);
1059 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
1060 return _get_ncores_with_attr(attr, above,
false);
1063#if KMP_AFFINITY_SUPPORTED
1064 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
1065 void sort_compact(kmp_affinity_t &affinity) {
1066 compact = affinity.compact;
1067 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
1068 kmp_hw_thread_t::compare_compact);
1071 void print(
const char *env_var =
"KMP_AFFINITY")
const;
1074extern kmp_topology_t *__kmp_topology;
1076class kmp_hw_subset_t {
1077 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
1085 int offset[MAX_ATTRS];
1086 kmp_hw_attr_t attr[MAX_ATTRS];
1089 const static int USE_ALL = (std::numeric_limits<int>::max)();
1098 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
1101 static int hw_subset_compare(
const void *i1,
const void *i2) {
1102 kmp_hw_t type1 = ((
const item_t *)i1)->type;
1103 kmp_hw_t type2 = ((
const item_t *)i2)->type;
1104 int level1 = __kmp_topology->get_level(type1);
1105 int level2 = __kmp_topology->get_level(type2);
1106 return level1 - level2;
1111 kmp_hw_subset_t() =
delete;
1112 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
1113 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
1114 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
1115 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
1117 static kmp_hw_subset_t *allocate() {
1118 int initial_capacity = 5;
1119 kmp_hw_subset_t *retval =
1120 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
1122 retval->capacity = initial_capacity;
1124 retval->absolute =
false;
1125 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
1128 static void deallocate(kmp_hw_subset_t *subset) {
1129 __kmp_free(subset->items);
1132 void set_absolute() { absolute =
true; }
1133 bool is_absolute()
const {
return absolute; }
1134 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
1135 for (
int i = 0; i < depth; ++i) {
1138 if (items[i].type == type) {
1139 int idx = items[i].num_attrs++;
1140 if ((
size_t)idx >= MAX_ATTRS)
1142 items[i].num[idx] = num;
1143 items[i].offset[idx] = offset;
1144 items[i].attr[idx] = attr;
1148 if (depth == capacity - 1) {
1150 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
1151 for (
int i = 0; i < depth; ++i)
1152 new_items[i] = items[i];
1156 items[depth].num_attrs = 1;
1157 items[depth].type = type;
1158 items[depth].num[0] = num;
1159 items[depth].offset[0] = offset;
1160 items[depth].attr[0] = attr;
1162 set |= (1ull << type);
1164 int get_depth()
const {
return depth; }
1165 const item_t &at(
int index)
const {
1166 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1167 return items[index];
1169 item_t &at(
int index) {
1170 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1171 return items[index];
1173 void remove(
int index) {
1174 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1175 set &= ~(1ull << items[index].type);
1176 for (
int j = index + 1; j < depth; ++j) {
1177 items[j - 1] = items[j];
1182 KMP_DEBUG_ASSERT(__kmp_topology);
1183 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1185 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1199 void canonicalize(
const kmp_topology_t *top) {
1201 kmp_hw_t targeted[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1209 for (kmp_hw_t type : targeted)
1210 if (top->get_level(type) == KMP_HW_UNKNOWN)
1214 for (kmp_hw_t type : targeted) {
1216 for (
int i = 0; i < get_depth(); ++i) {
1217 if (top->get_equivalent_type(items[i].type) == type) {
1223 push_back(USE_ALL, type, 0, kmp_hw_attr_t{});
1231 printf(
"**********************\n");
1232 printf(
"*** kmp_hw_subset: ***\n");
1233 printf(
"* depth: %d\n", depth);
1234 printf(
"* items:\n");
1235 for (
int i = 0; i < depth; ++i) {
1236 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1237 for (
int j = 0; j < items[i].num_attrs; ++j) {
1238 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1239 items[i].offset[j]);
1240 if (!items[i].attr[j]) {
1241 printf(
" (none)\n");
1244 " core_type = %s, core_eff = %d\n",
1245 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1246 items[i].attr[j].get_core_eff());
1250 printf(
"* set: 0x%llx\n", set);
1251 printf(
"* absolute: %d\n", absolute);
1252 printf(
"**********************\n");
1255extern kmp_hw_subset_t *__kmp_hw_subset;
1263class hierarchy_info {
1267 static const kmp_uint32 maxLeaves = 4;
1268 static const kmp_uint32 minBranch = 4;
1274 kmp_uint32 maxLevels;
1281 kmp_uint32 base_num_threads;
1282 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1283 volatile kmp_int8 uninitialized;
1285 volatile kmp_int8 resizing;
1291 kmp_uint32 *numPerLevel;
1292 kmp_uint32 *skipPerLevel;
1294 void deriveLevels() {
1295 int hier_depth = __kmp_topology->get_depth();
1296 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1297 numPerLevel[level] = __kmp_topology->get_ratio(i);
1302 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1305 if (!uninitialized && numPerLevel) {
1306 __kmp_free(numPerLevel);
1308 uninitialized = not_initialized;
1312 void init(
int num_addrs) {
1313 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1314 &uninitialized, not_initialized, initializing);
1315 if (bool_result == 0) {
1316 while (TCR_1(uninitialized) != initialized)
1320 KMP_DEBUG_ASSERT(bool_result == 1);
1330 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1331 skipPerLevel = &(numPerLevel[maxLevels]);
1332 for (kmp_uint32 i = 0; i < maxLevels;
1335 skipPerLevel[i] = 1;
1339 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1342 numPerLevel[0] = maxLeaves;
1343 numPerLevel[1] = num_addrs / maxLeaves;
1344 if (num_addrs % maxLeaves)
1348 base_num_threads = num_addrs;
1349 for (
int i = maxLevels - 1; i >= 0;
1351 if (numPerLevel[i] != 1 || depth > 1)
1354 kmp_uint32 branch = minBranch;
1355 if (numPerLevel[0] == 1)
1356 branch = num_addrs / maxLeaves;
1357 if (branch < minBranch)
1359 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1360 while (numPerLevel[d] > branch ||
1361 (d == 0 && numPerLevel[d] > maxLeaves)) {
1362 if (numPerLevel[d] & 1)
1364 numPerLevel[d] = numPerLevel[d] >> 1;
1365 if (numPerLevel[d + 1] == 1)
1367 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1369 if (numPerLevel[0] == 1) {
1370 branch = branch >> 1;
1376 for (kmp_uint32 i = 1; i < depth; ++i)
1377 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1379 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1380 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1382 uninitialized = initialized;
1386 void resize(kmp_uint32 nproc) {
1387 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1388 while (bool_result == 0) {
1390 if (nproc <= base_num_threads)
1393 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1395 KMP_DEBUG_ASSERT(bool_result != 0);
1396 if (nproc <= base_num_threads)
1400 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1401 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1403 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1404 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1405 numPerLevel[i - 1] *= 2;
1409 if (nproc > old_sz) {
1410 while (nproc > old_sz) {
1418 kmp_uint32 *old_numPerLevel = numPerLevel;
1419 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1420 numPerLevel = skipPerLevel = NULL;
1422 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1423 skipPerLevel = &(numPerLevel[maxLevels]);
1426 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1428 numPerLevel[i] = old_numPerLevel[i];
1429 skipPerLevel[i] = old_skipPerLevel[i];
1433 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1436 skipPerLevel[i] = 1;
1440 __kmp_free(old_numPerLevel);
1444 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1445 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1447 base_num_threads = nproc;