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_AIX
200#include <sys/syscall.h>
201#if KMP_ARCH_X86 || KMP_ARCH_ARM
202#ifndef __NR_sched_setaffinity
203#define __NR_sched_setaffinity 241
204#elif __NR_sched_setaffinity != 241
205#error Wrong code for setaffinity system call.
207#ifndef __NR_sched_getaffinity
208#define __NR_sched_getaffinity 242
209#elif __NR_sched_getaffinity != 242
210#error Wrong code for getaffinity system call.
212#elif KMP_ARCH_AARCH64
213#ifndef __NR_sched_setaffinity
214#define __NR_sched_setaffinity 122
215#elif __NR_sched_setaffinity != 122
216#error Wrong code for setaffinity system call.
218#ifndef __NR_sched_getaffinity
219#define __NR_sched_getaffinity 123
220#elif __NR_sched_getaffinity != 123
221#error Wrong code for getaffinity system call.
223#elif KMP_ARCH_RISCV64
224#ifndef __NR_sched_setaffinity
225#define __NR_sched_setaffinity 122
226#elif __NR_sched_setaffinity != 122
227#error Wrong code for setaffinity system call.
229#ifndef __NR_sched_getaffinity
230#define __NR_sched_getaffinity 123
231#elif __NR_sched_getaffinity != 123
232#error Wrong code for getaffinity system call.
235#ifndef __NR_sched_setaffinity
236#define __NR_sched_setaffinity 203
237#elif __NR_sched_setaffinity != 203
238#error Wrong code for setaffinity system call.
240#ifndef __NR_sched_getaffinity
241#define __NR_sched_getaffinity 204
242#elif __NR_sched_getaffinity != 204
243#error Wrong code for getaffinity system call.
246#ifndef __NR_sched_setaffinity
247#define __NR_sched_setaffinity 222
248#elif __NR_sched_setaffinity != 222
249#error Wrong code for setaffinity system call.
251#ifndef __NR_sched_getaffinity
252#define __NR_sched_getaffinity 223
253#elif __NR_sched_getaffinity != 223
254#error Wrong code for getaffinity system call.
257#ifndef __NR_sched_setaffinity
258#define __NR_sched_setaffinity 4239
259#elif __NR_sched_setaffinity != 4239
260#error Wrong code for setaffinity system call.
262#ifndef __NR_sched_getaffinity
263#define __NR_sched_getaffinity 4240
264#elif __NR_sched_getaffinity != 4240
265#error Wrong code for getaffinity system call.
268#ifndef __NR_sched_setaffinity
269#define __NR_sched_setaffinity 5195
270#elif __NR_sched_setaffinity != 5195
271#error Wrong code for setaffinity system call.
273#ifndef __NR_sched_getaffinity
274#define __NR_sched_getaffinity 5196
275#elif __NR_sched_getaffinity != 5196
276#error Wrong code for getaffinity system call.
278#elif KMP_ARCH_LOONGARCH64
279#ifndef __NR_sched_setaffinity
280#define __NR_sched_setaffinity 122
281#elif __NR_sched_setaffinity != 122
282#error Wrong code for setaffinity system call.
284#ifndef __NR_sched_getaffinity
285#define __NR_sched_getaffinity 123
286#elif __NR_sched_getaffinity != 123
287#error Wrong code for getaffinity system call.
289#elif KMP_ARCH_RISCV64
290#ifndef __NR_sched_setaffinity
291#define __NR_sched_setaffinity 122
292#elif __NR_sched_setaffinity != 122
293#error Wrong code for setaffinity system call.
295#ifndef __NR_sched_getaffinity
296#define __NR_sched_getaffinity 123
297#elif __NR_sched_getaffinity != 123
298#error Wrong code for getaffinity system call.
301#ifndef __NR_sched_setaffinity
302#define __NR_sched_setaffinity 203
303#elif __NR_sched_setaffinity != 203
304#error Wrong code for setaffinity system call.
306#ifndef __NR_sched_getaffinity
307#define __NR_sched_getaffinity 204
308#elif __NR_sched_getaffinity != 204
309#error Wrong code for getaffinity system call.
312#ifndef __NR_sched_setaffinity
313#define __NR_sched_setaffinity 239
314#elif __NR_sched_setaffinity != 239
315#error Wrong code for setaffinity system call.
317#ifndef __NR_sched_getaffinity
318#define __NR_sched_getaffinity 240
319#elif __NR_sched_getaffinity != 240
320#error Wrong code for getaffinity system call.
323#error Unknown or unsupported architecture
327#include <pthread_np.h>
331#define VMI_MAXRADS 64
333class KMPNativeAffinity :
public KMPAffinity {
334 class Mask :
public KMPAffinity::Mask {
335 typedef unsigned long mask_t;
336 typedef decltype(__kmp_affin_mask_size) mask_size_type;
337 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
338 static const mask_t ONE = 1;
339 mask_size_type get_num_mask_types()
const {
340 return __kmp_affin_mask_size /
sizeof(mask_t);
345 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
350 void set(
int i)
override {
351 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
353 bool is_set(
int i)
const override {
354 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
356 void clear(
int i)
override {
357 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
359 void zero()
override {
360 mask_size_type e = get_num_mask_types();
361 for (mask_size_type i = 0; i < e; ++i)
364 bool empty()
const override {
365 mask_size_type e = get_num_mask_types();
366 for (mask_size_type i = 0; i < e; ++i)
367 if (mask[i] != (mask_t)0)
371 void copy(
const KMPAffinity::Mask *src)
override {
372 const Mask *convert =
static_cast<const Mask *
>(src);
373 mask_size_type e = get_num_mask_types();
374 for (mask_size_type i = 0; i < e; ++i)
375 mask[i] = convert->mask[i];
377 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
378 const Mask *convert =
static_cast<const Mask *
>(rhs);
379 mask_size_type e = get_num_mask_types();
380 for (mask_size_type i = 0; i < e; ++i)
381 mask[i] &= convert->mask[i];
383 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
384 const Mask *convert =
static_cast<const Mask *
>(rhs);
385 mask_size_type e = get_num_mask_types();
386 for (mask_size_type i = 0; i < e; ++i)
387 mask[i] |= convert->mask[i];
389 void bitwise_not()
override {
390 mask_size_type e = get_num_mask_types();
391 for (mask_size_type i = 0; i < e; ++i)
392 mask[i] = ~(mask[i]);
394 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
395 const Mask *convert =
static_cast<const Mask *
>(rhs);
396 mask_size_type e = get_num_mask_types();
397 for (mask_size_type i = 0; i < e; ++i)
398 if (mask[i] != convert->mask[i])
402 int begin()
const override {
404 while (retval < end() && !is_set(retval))
408 int end()
const override {
410 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
413 int next(
int previous)
const override {
414 int retval = previous + 1;
415 while (retval < end() && !is_set(retval))
422 int get_system_affinity(
bool abort_on_error)
override {
423 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
424 "Illegal get affinity operation when not capable");
426 (void)abort_on_error;
429 for (
int i = 0; i < __kmp_xproc; ++i)
430 KMP_CPU_SET(i,
this);
433 int set_system_affinity(
bool abort_on_error)
const override {
434 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
436 "Illegal set affinity operation when not capable");
439 int gtid = __kmp_entry_gtid();
440 int tid = thread_self();
444 int retval = bindprocessor(BINDTHREAD, tid, PROCESSOR_CLASS_ANY);
448 KMP_CPU_SET_ITERATE(location,
this) {
449 if (KMP_CPU_ISSET(location,
this)) {
450 retval = bindprocessor(BINDTHREAD, tid, location);
451 if (retval == -1 && errno == 1) {
456 rsh = rs_alloc(RS_EMPTY);
457 rsid.at_pid = getpid();
458 if (RS_DEFAULT_RSET != ra_getrset(R_PROCESS, rsid, 0, rsh)) {
459 retval = ra_detachrset(R_PROCESS, rsid, 0);
460 retval = bindprocessor(BINDTHREAD, tid, location);
464 KA_TRACE(10, (
"__kmp_set_system_affinity: Done binding "
470 if (abort_on_error) {
471 __kmp_fatal(KMP_MSG(FunctionError,
"bindprocessor()"),
472 KMP_ERR(error), __kmp_msg_null);
473 KA_TRACE(10, (
"__kmp_set_system_affinity: Error binding "
474 "T#%d to cpu=%d, errno=%d.\n",
475 gtid, location, error));
483 int get_system_affinity(
bool abort_on_error)
override {
484 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
485 "Illegal get affinity operation when not capable");
488 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
490 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
491 reinterpret_cast<cpuset_t *
>(mask));
492 int retval = (r == 0 ? 0 : -1);
498 if (abort_on_error) {
499 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_getaffinity_np()"),
500 KMP_ERR(error), __kmp_msg_null);
504 int set_system_affinity(
bool abort_on_error)
const override {
505 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
506 "Illegal set affinity operation when not capable");
509 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
511 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
512 reinterpret_cast<cpuset_t *
>(mask));
513 int retval = (r == 0 ? 0 : -1);
519 if (abort_on_error) {
520 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_setaffinity_np()"),
521 KMP_ERR(error), __kmp_msg_null);
527 void determine_capable(
const char *env_var)
override {
528 __kmp_affinity_determine_capable(env_var);
530 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
531 KMPAffinity::Mask *allocate_mask()
override {
532 KMPNativeAffinity::Mask *retval =
new Mask();
535 void deallocate_mask(KMPAffinity::Mask *m)
override {
536 KMPNativeAffinity::Mask *native_mask =
537 static_cast<KMPNativeAffinity::Mask *
>(m);
540 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
541 return new Mask[num];
543 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
544 Mask *linux_array =
static_cast<Mask *
>(array);
545 delete[] linux_array;
547 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
548 int index)
override {
549 Mask *linux_array =
static_cast<Mask *
>(array);
550 return &(linux_array[index]);
552 api_type get_api_type()
const override {
return NATIVE_OS; }
557class KMPNativeAffinity :
public KMPAffinity {
558 class Mask :
public KMPAffinity::Mask {
559 typedef ULONG_PTR mask_t;
560 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
565 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
571 void set(
int i)
override {
572 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
574 bool is_set(
int i)
const override {
575 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
577 void clear(
int i)
override {
578 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
580 void zero()
override {
581 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
584 bool empty()
const override {
585 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
590 void copy(
const KMPAffinity::Mask *src)
override {
591 const Mask *convert =
static_cast<const Mask *
>(src);
592 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
593 mask[i] = convert->mask[i];
595 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
596 const Mask *convert =
static_cast<const Mask *
>(rhs);
597 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
598 mask[i] &= convert->mask[i];
600 void bitwise_or(
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_not()
override {
606 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
607 mask[i] = ~(mask[i]);
609 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
610 const Mask *convert =
static_cast<const Mask *
>(rhs);
611 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
612 if (mask[i] != convert->mask[i])
616 int begin()
const override {
618 while (retval < end() && !is_set(retval))
622 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
623 int next(
int previous)
const override {
624 int retval = previous + 1;
625 while (retval < end() && !is_set(retval))
629 int set_process_affinity(
bool abort_on_error)
const override {
630 if (__kmp_num_proc_groups <= 1) {
631 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
632 DWORD error = GetLastError();
633 if (abort_on_error) {
634 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
642 int set_system_affinity(
bool abort_on_error)
const override {
643 if (__kmp_num_proc_groups > 1) {
646 int group = get_proc_group();
648 if (abort_on_error) {
649 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
656 ga.Mask = mask[group];
657 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
659 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
660 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
661 DWORD error = GetLastError();
662 if (abort_on_error) {
663 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
669 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
670 DWORD error = GetLastError();
671 if (abort_on_error) {
672 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
680 int get_system_affinity(
bool abort_on_error)
override {
681 if (__kmp_num_proc_groups > 1) {
684 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
685 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
686 DWORD error = GetLastError();
687 if (abort_on_error) {
688 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
689 KMP_ERR(error), __kmp_msg_null);
693 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
697 mask[ga.Group] = ga.Mask;
699 mask_t newMask, sysMask, retval;
700 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
701 DWORD error = GetLastError();
702 if (abort_on_error) {
703 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
704 KMP_ERR(error), __kmp_msg_null);
708 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
710 DWORD error = GetLastError();
711 if (abort_on_error) {
712 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
713 KMP_ERR(error), __kmp_msg_null);
717 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
719 DWORD error = GetLastError();
720 if (abort_on_error) {
721 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
722 KMP_ERR(error), __kmp_msg_null);
729 int get_proc_group()
const override {
731 if (__kmp_num_proc_groups == 1) {
734 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
744 void determine_capable(
const char *env_var)
override {
745 __kmp_affinity_determine_capable(env_var);
747 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
748 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
749 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
750 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
751 return new Mask[num];
753 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
754 Mask *windows_array =
static_cast<Mask *
>(array);
755 delete[] windows_array;
757 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
758 int index)
override {
759 Mask *windows_array =
static_cast<Mask *
>(array);
760 return &(windows_array[index]);
762 api_type get_api_type()
const override {
return NATIVE_OS; }
768struct kmp_hw_attr_t {
772 unsigned reserved : 15;
774 static const int UNKNOWN_CORE_EFF = -1;
777 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
778 valid(0), reserved(0) {}
779 void set_core_type(kmp_hw_core_type_t type) {
783 void set_core_eff(
int eff) {
787 kmp_hw_core_type_t get_core_type()
const {
788 return (kmp_hw_core_type_t)core_type;
790 int get_core_eff()
const {
return core_eff; }
791 bool is_core_type_valid()
const {
792 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
794 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
795 operator bool()
const {
return valid; }
797 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
798 core_eff = UNKNOWN_CORE_EFF;
801 bool contains(
const kmp_hw_attr_t &other)
const {
802 if (!valid && !other.valid)
804 if (valid && other.valid) {
805 if (other.is_core_type_valid()) {
806 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
809 if (other.is_core_eff_valid()) {
810 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
817#if KMP_AFFINITY_SUPPORTED
818 bool contains(
const kmp_affinity_attrs_t &attr)
const {
819 if (!valid && !attr.valid)
821 if (valid && attr.valid) {
822 if (attr.core_type != KMP_HW_CORE_TYPE_UNKNOWN)
823 return (is_core_type_valid() &&
824 (get_core_type() == (kmp_hw_core_type_t)attr.core_type));
825 if (attr.core_eff != UNKNOWN_CORE_EFF)
826 return (is_core_eff_valid() && (get_core_eff() == attr.core_eff));
832 bool operator==(
const kmp_hw_attr_t &rhs)
const {
833 return (rhs.valid == valid && rhs.core_eff == core_eff &&
834 rhs.core_type == core_type);
836 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
839#if KMP_AFFINITY_SUPPORTED
840KMP_BUILD_ASSERT(
sizeof(kmp_hw_attr_t) ==
sizeof(kmp_affinity_attrs_t));
843class kmp_hw_thread_t {
845 static const int UNKNOWN_ID = -1;
846 static const int MULTIPLE_ID = -2;
847 static int compare_ids(
const void *a,
const void *b);
848 static int compare_compact(
const void *a,
const void *b);
849 int ids[KMP_HW_LAST];
850 int sub_ids[KMP_HW_LAST];
857 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
864class kmp_topology_t {
890 int num_core_efficiencies;
892 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
898 kmp_hw_thread_t *hw_threads;
904 kmp_hw_t equivalent[KMP_HW_LAST];
913 void _insert_layer(kmp_hw_t type,
const int *ids);
915#if KMP_GROUP_AFFINITY
917 void _insert_windows_proc_groups();
923 void _gather_enumeration_information();
927 void _remove_radix1_layers();
930 void _discover_uniformity();
941 void _set_last_level_cache();
946 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
947 bool find_all =
false)
const;
951 kmp_topology_t() =
delete;
952 kmp_topology_t(
const kmp_topology_t &t) =
delete;
953 kmp_topology_t(kmp_topology_t &&t) =
delete;
954 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
955 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
957 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
958 static void deallocate(kmp_topology_t *);
961 kmp_hw_thread_t &at(
int index) {
962 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
963 return hw_threads[index];
965 const kmp_hw_thread_t &at(
int index)
const {
966 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
967 return hw_threads[index];
969 int get_num_hw_threads()
const {
return num_hw_threads; }
971 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
972 kmp_hw_thread_t::compare_ids);
976 bool check_ids()
const;
980 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
984#if KMP_AFFINITY_SUPPORTED
986 void set_granularity(kmp_affinity_t &stgs)
const;
987 bool is_close(
int hwt1,
int hwt2,
const kmp_affinity_t &stgs)
const;
988 bool restrict_to_mask(
const kmp_affin_mask_t *mask);
989 bool filter_hw_subset();
991 bool is_uniform()
const {
return flags.uniform; }
994 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
995 if (type == KMP_HW_UNKNOWN)
996 return KMP_HW_UNKNOWN;
997 return equivalent[type];
1000 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
1001 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
1002 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
1003 kmp_hw_t real_type2 = equivalent[type2];
1004 if (real_type2 == KMP_HW_UNKNOWN)
1006 equivalent[type1] = real_type2;
1009 KMP_FOREACH_HW_TYPE(type) {
1010 if (equivalent[type] == type1) {
1011 equivalent[type] = real_type2;
1017 int calculate_ratio(
int level1,
int level2)
const {
1018 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
1019 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
1021 for (
int level = level1; level > level2; --level)
1025 int get_ratio(
int level)
const {
1026 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1027 return ratio[level];
1029 int get_depth()
const {
return depth; };
1030 kmp_hw_t get_type(
int level)
const {
1031 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1032 return types[level];
1034 int get_level(kmp_hw_t type)
const {
1035 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
1036 int eq_type = equivalent[type];
1037 if (eq_type == KMP_HW_UNKNOWN)
1039 for (
int i = 0; i < depth; ++i)
1040 if (types[i] == eq_type)
1044 int get_count(
int level)
const {
1045 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1046 return count[level];
1049 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
1050 return _get_ncores_with_attr(attr, -1,
true);
1054 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
1055 return _get_ncores_with_attr(attr, above,
false);
1058#if KMP_AFFINITY_SUPPORTED
1059 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
1060 void sort_compact(kmp_affinity_t &affinity) {
1061 compact = affinity.compact;
1062 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
1063 kmp_hw_thread_t::compare_compact);
1066 void print(
const char *env_var =
"KMP_AFFINITY")
const;
1069extern kmp_topology_t *__kmp_topology;
1071class kmp_hw_subset_t {
1072 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
1080 int offset[MAX_ATTRS];
1081 kmp_hw_attr_t attr[MAX_ATTRS];
1084 const static int USE_ALL = (std::numeric_limits<int>::max)();
1093 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
1096 static int hw_subset_compare(
const void *i1,
const void *i2) {
1097 kmp_hw_t type1 = ((
const item_t *)i1)->type;
1098 kmp_hw_t type2 = ((
const item_t *)i2)->type;
1099 int level1 = __kmp_topology->get_level(type1);
1100 int level2 = __kmp_topology->get_level(type2);
1101 return level1 - level2;
1106 kmp_hw_subset_t() =
delete;
1107 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
1108 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
1109 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
1110 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
1112 static kmp_hw_subset_t *allocate() {
1113 int initial_capacity = 5;
1114 kmp_hw_subset_t *retval =
1115 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
1117 retval->capacity = initial_capacity;
1119 retval->absolute =
false;
1120 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
1123 static void deallocate(kmp_hw_subset_t *subset) {
1124 __kmp_free(subset->items);
1127 void set_absolute() { absolute =
true; }
1128 bool is_absolute()
const {
return absolute; }
1129 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
1130 for (
int i = 0; i < depth; ++i) {
1133 if (items[i].type == type) {
1134 int idx = items[i].num_attrs++;
1135 if ((
size_t)idx >= MAX_ATTRS)
1137 items[i].num[idx] = num;
1138 items[i].offset[idx] = offset;
1139 items[i].attr[idx] = attr;
1143 if (depth == capacity - 1) {
1145 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
1146 for (
int i = 0; i < depth; ++i)
1147 new_items[i] = items[i];
1151 items[depth].num_attrs = 1;
1152 items[depth].type = type;
1153 items[depth].num[0] = num;
1154 items[depth].offset[0] = offset;
1155 items[depth].attr[0] = attr;
1157 set |= (1ull << type);
1159 int get_depth()
const {
return depth; }
1160 const item_t &at(
int index)
const {
1161 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1162 return items[index];
1164 item_t &at(
int index) {
1165 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1166 return items[index];
1168 void remove(
int index) {
1169 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1170 set &= ~(1ull << items[index].type);
1171 for (
int j = index + 1; j < depth; ++j) {
1172 items[j - 1] = items[j];
1177 KMP_DEBUG_ASSERT(__kmp_topology);
1178 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1180 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1182 printf(
"**********************\n");
1183 printf(
"*** kmp_hw_subset: ***\n");
1184 printf(
"* depth: %d\n", depth);
1185 printf(
"* items:\n");
1186 for (
int i = 0; i < depth; ++i) {
1187 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1188 for (
int j = 0; j < items[i].num_attrs; ++j) {
1189 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1190 items[i].offset[j]);
1191 if (!items[i].attr[j]) {
1192 printf(
" (none)\n");
1195 " core_type = %s, core_eff = %d\n",
1196 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1197 items[i].attr[j].get_core_eff());
1201 printf(
"* set: 0x%llx\n", set);
1202 printf(
"* absolute: %d\n", absolute);
1203 printf(
"**********************\n");
1206extern kmp_hw_subset_t *__kmp_hw_subset;
1214class hierarchy_info {
1218 static const kmp_uint32 maxLeaves = 4;
1219 static const kmp_uint32 minBranch = 4;
1225 kmp_uint32 maxLevels;
1232 kmp_uint32 base_num_threads;
1233 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1234 volatile kmp_int8 uninitialized;
1236 volatile kmp_int8 resizing;
1242 kmp_uint32 *numPerLevel;
1243 kmp_uint32 *skipPerLevel;
1245 void deriveLevels() {
1246 int hier_depth = __kmp_topology->get_depth();
1247 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1248 numPerLevel[level] = __kmp_topology->get_ratio(i);
1253 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1256 if (!uninitialized && numPerLevel) {
1257 __kmp_free(numPerLevel);
1259 uninitialized = not_initialized;
1263 void init(
int num_addrs) {
1264 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1265 &uninitialized, not_initialized, initializing);
1266 if (bool_result == 0) {
1267 while (TCR_1(uninitialized) != initialized)
1271 KMP_DEBUG_ASSERT(bool_result == 1);
1281 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1282 skipPerLevel = &(numPerLevel[maxLevels]);
1283 for (kmp_uint32 i = 0; i < maxLevels;
1286 skipPerLevel[i] = 1;
1290 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1293 numPerLevel[0] = maxLeaves;
1294 numPerLevel[1] = num_addrs / maxLeaves;
1295 if (num_addrs % maxLeaves)
1299 base_num_threads = num_addrs;
1300 for (
int i = maxLevels - 1; i >= 0;
1302 if (numPerLevel[i] != 1 || depth > 1)
1305 kmp_uint32 branch = minBranch;
1306 if (numPerLevel[0] == 1)
1307 branch = num_addrs / maxLeaves;
1308 if (branch < minBranch)
1310 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1311 while (numPerLevel[d] > branch ||
1312 (d == 0 && numPerLevel[d] > maxLeaves)) {
1313 if (numPerLevel[d] & 1)
1315 numPerLevel[d] = numPerLevel[d] >> 1;
1316 if (numPerLevel[d + 1] == 1)
1318 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1320 if (numPerLevel[0] == 1) {
1321 branch = branch >> 1;
1327 for (kmp_uint32 i = 1; i < depth; ++i)
1328 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1330 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1331 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1333 uninitialized = initialized;
1337 void resize(kmp_uint32 nproc) {
1338 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1339 while (bool_result == 0) {
1341 if (nproc <= base_num_threads)
1344 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1346 KMP_DEBUG_ASSERT(bool_result != 0);
1347 if (nproc <= base_num_threads)
1351 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1352 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1354 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1355 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1356 numPerLevel[i - 1] *= 2;
1360 if (nproc > old_sz) {
1361 while (nproc > old_sz) {
1369 kmp_uint32 *old_numPerLevel = numPerLevel;
1370 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1371 numPerLevel = skipPerLevel = NULL;
1373 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1374 skipPerLevel = &(numPerLevel[maxLevels]);
1377 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1379 numPerLevel[i] = old_numPerLevel[i];
1380 skipPerLevel[i] = old_skipPerLevel[i];
1384 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1387 skipPerLevel[i] = 1;
1391 __kmp_free(old_numPerLevel);
1395 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1396 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1398 base_num_threads = nproc;