20#if KMP_AFFINITY_SUPPORTED
22class KMPHwlocAffinity :
public KMPAffinity {
24 class Mask :
public KMPAffinity::Mask {
29 mask = hwloc_bitmap_alloc();
32 Mask(
const Mask &other) =
delete;
33 Mask &operator=(
const Mask &other) =
delete;
34 ~Mask() { hwloc_bitmap_free(mask); }
35 void set(
int i)
override { hwloc_bitmap_set(mask, i); }
36 bool is_set(
int i)
const override {
return hwloc_bitmap_isset(mask, i); }
37 void clear(
int i)
override { hwloc_bitmap_clr(mask, i); }
38 void zero()
override { hwloc_bitmap_zero(mask); }
39 bool empty()
const override {
return hwloc_bitmap_iszero(mask); }
40 void copy(
const KMPAffinity::Mask *src)
override {
41 const Mask *convert =
static_cast<const Mask *
>(src);
42 hwloc_bitmap_copy(mask, convert->mask);
44 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
45 const Mask *convert =
static_cast<const Mask *
>(rhs);
46 hwloc_bitmap_and(mask, mask, convert->mask);
48 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
49 const Mask *convert =
static_cast<const Mask *
>(rhs);
50 hwloc_bitmap_or(mask, mask, convert->mask);
52 void bitwise_not()
override { hwloc_bitmap_not(mask, mask); }
53 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
54 const Mask *convert =
static_cast<const Mask *
>(rhs);
55 return hwloc_bitmap_isequal(mask, convert->mask);
57 int begin()
const override {
return hwloc_bitmap_first(mask); }
58 int end()
const override {
return -1; }
59 int next(
int previous)
const override {
60 return hwloc_bitmap_next(mask, previous);
62 int get_system_affinity(
bool abort_on_error)
override {
63 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
64 "Illegal get affinity operation when not capable");
66 hwloc_get_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
72 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_get_cpubind()"),
73 KMP_ERR(error), __kmp_msg_null);
77 int set_system_affinity(
bool abort_on_error)
const override {
78 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
79 "Illegal set affinity operation when not capable");
81 hwloc_set_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
87 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_set_cpubind()"),
88 KMP_ERR(error), __kmp_msg_null);
93 int set_process_affinity(
bool abort_on_error)
const override {
94 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
95 "Illegal set process affinity operation when not capable");
97 const hwloc_topology_support *support =
98 hwloc_topology_get_support(__kmp_hwloc_topology);
99 if (support->cpubind->set_proc_cpubind) {
101 retval = hwloc_set_cpubind(__kmp_hwloc_topology, mask,
102 HWLOC_CPUBIND_PROCESS);
107 __kmp_fatal(KMP_MSG(FunctionError,
"hwloc_set_cpubind()"),
108 KMP_ERR(error), __kmp_msg_null);
113 int get_proc_group()
const override {
116 if (__kmp_num_proc_groups == 1) {
119 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
121 unsigned long first_32_bits = hwloc_bitmap_to_ith_ulong(mask, i * 2);
122 unsigned long second_32_bits =
123 hwloc_bitmap_to_ith_ulong(mask, i * 2 + 1);
124 if (first_32_bits == 0 && second_32_bits == 0) {
136 void determine_capable(
const char *var)
override {
137 const hwloc_topology_support *topology_support;
138 if (__kmp_hwloc_topology == NULL) {
139 if (hwloc_topology_init(&__kmp_hwloc_topology) < 0) {
140 __kmp_hwloc_error = TRUE;
141 if (__kmp_affinity.flags.verbose) {
142 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_init()");
145 if (hwloc_topology_load(__kmp_hwloc_topology) < 0) {
146 __kmp_hwloc_error = TRUE;
147 if (__kmp_affinity.flags.verbose) {
148 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_load()");
152 topology_support = hwloc_topology_get_support(__kmp_hwloc_topology);
157 if (topology_support && topology_support->cpubind->set_thisthread_cpubind &&
158 topology_support->cpubind->get_thisthread_cpubind &&
159 topology_support->discovery->pu && !__kmp_hwloc_error) {
161 KMP_AFFINITY_ENABLE(TRUE);
164 __kmp_hwloc_error = TRUE;
165 KMP_AFFINITY_DISABLE();
168 void bind_thread(
int which)
override {
169 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
170 "Illegal set affinity operation when not capable");
171 KMPAffinity::Mask *mask;
172 KMP_CPU_ALLOC_ON_STACK(mask);
174 KMP_CPU_SET(which, mask);
175 __kmp_set_system_affinity(mask, TRUE);
176 KMP_CPU_FREE_FROM_STACK(mask);
178 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
179 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
180 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
181 return new Mask[num];
183 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
184 Mask *hwloc_array =
static_cast<Mask *
>(array);
185 delete[] hwloc_array;
187 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
188 int index)
override {
189 Mask *hwloc_array =
static_cast<Mask *
>(array);
190 return &(hwloc_array[index]);
192 api_type get_api_type()
const override {
return HWLOC; }
196#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
203#include <sys/syscall.h>
204#if KMP_ARCH_X86 || KMP_ARCH_ARM
205#ifndef __NR_sched_setaffinity
206#define __NR_sched_setaffinity 241
207#elif __NR_sched_setaffinity != 241
208#error Wrong code for setaffinity system call.
210#ifndef __NR_sched_getaffinity
211#define __NR_sched_getaffinity 242
212#elif __NR_sched_getaffinity != 242
213#error Wrong code for getaffinity system call.
215#elif KMP_ARCH_AARCH64
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.
226#elif KMP_ARCH_RISCV64
227#ifndef __NR_sched_setaffinity
228#define __NR_sched_setaffinity 122
229#elif __NR_sched_setaffinity != 122
230#error Wrong code for setaffinity system call.
232#ifndef __NR_sched_getaffinity
233#define __NR_sched_getaffinity 123
234#elif __NR_sched_getaffinity != 123
235#error Wrong code for getaffinity system call.
238#ifndef __NR_sched_setaffinity
239#define __NR_sched_setaffinity 203
240#elif __NR_sched_setaffinity != 203
241#error Wrong code for setaffinity system call.
243#ifndef __NR_sched_getaffinity
244#define __NR_sched_getaffinity 204
245#elif __NR_sched_getaffinity != 204
246#error Wrong code for getaffinity system call.
249#ifndef __NR_sched_setaffinity
250#define __NR_sched_setaffinity 222
251#elif __NR_sched_setaffinity != 222
252#error Wrong code for setaffinity system call.
254#ifndef __NR_sched_getaffinity
255#define __NR_sched_getaffinity 223
256#elif __NR_sched_getaffinity != 223
257#error Wrong code for getaffinity system call.
260#ifndef __NR_sched_setaffinity
261#define __NR_sched_setaffinity 4239
262#elif __NR_sched_setaffinity != 4239
263#error Wrong code for setaffinity system call.
265#ifndef __NR_sched_getaffinity
266#define __NR_sched_getaffinity 4240
267#elif __NR_sched_getaffinity != 4240
268#error Wrong code for getaffinity system call.
271#ifndef __NR_sched_setaffinity
272#define __NR_sched_setaffinity 5195
273#elif __NR_sched_setaffinity != 5195
274#error Wrong code for setaffinity system call.
276#ifndef __NR_sched_getaffinity
277#define __NR_sched_getaffinity 5196
278#elif __NR_sched_getaffinity != 5196
279#error Wrong code for getaffinity system call.
281#elif KMP_ARCH_LOONGARCH64
282#ifndef __NR_sched_setaffinity
283#define __NR_sched_setaffinity 122
284#elif __NR_sched_setaffinity != 122
285#error Wrong code for setaffinity system call.
287#ifndef __NR_sched_getaffinity
288#define __NR_sched_getaffinity 123
289#elif __NR_sched_getaffinity != 123
290#error Wrong code for getaffinity system call.
292#elif KMP_ARCH_RISCV64
293#ifndef __NR_sched_setaffinity
294#define __NR_sched_setaffinity 122
295#elif __NR_sched_setaffinity != 122
296#error Wrong code for setaffinity system call.
298#ifndef __NR_sched_getaffinity
299#define __NR_sched_getaffinity 123
300#elif __NR_sched_getaffinity != 123
301#error Wrong code for getaffinity system call.
304#ifndef __NR_sched_setaffinity
305#define __NR_sched_setaffinity 203
306#elif __NR_sched_setaffinity != 203
307#error Wrong code for setaffinity system call.
309#ifndef __NR_sched_getaffinity
310#define __NR_sched_getaffinity 204
311#elif __NR_sched_getaffinity != 204
312#error Wrong code for getaffinity system call.
315#ifndef __NR_sched_setaffinity
316#define __NR_sched_setaffinity 239
317#elif __NR_sched_setaffinity != 239
318#error Wrong code for setaffinity system call.
320#ifndef __NR_sched_getaffinity
321#define __NR_sched_getaffinity 240
322#elif __NR_sched_getaffinity != 240
323#error Wrong code for getaffinity system call.
326#error Unknown or unsupported architecture
328#elif KMP_OS_FREEBSD || KMP_OS_DRAGONFLY
330#include <pthread_np.h>
337#define VMI_MAXRADS 64
338#define GET_NUMBER_SMT_SETS 0x0004
339extern "C" int syssmt(
int flags,
int,
int,
int *);
341class KMPNativeAffinity :
public KMPAffinity {
342 class Mask :
public KMPAffinity::Mask {
343 typedef unsigned long mask_t;
344 typedef decltype(__kmp_affin_mask_size) mask_size_type;
345 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
346 static const mask_t ONE = 1;
347 mask_size_type get_num_mask_types()
const {
348 return __kmp_affin_mask_size /
sizeof(mask_t);
353 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
358 void set(
int i)
override {
359 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
361 bool is_set(
int i)
const override {
362 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
364 void clear(
int i)
override {
365 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
367 void zero()
override {
368 mask_size_type e = get_num_mask_types();
369 for (mask_size_type i = 0; i < e; ++i)
372 bool empty()
const override {
373 mask_size_type e = get_num_mask_types();
374 for (mask_size_type i = 0; i < e; ++i)
375 if (mask[i] != (mask_t)0)
379 void copy(
const KMPAffinity::Mask *src)
override {
380 const Mask *convert =
static_cast<const Mask *
>(src);
381 mask_size_type e = get_num_mask_types();
382 for (mask_size_type i = 0; i < e; ++i)
383 mask[i] = convert->mask[i];
385 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
386 const Mask *convert =
static_cast<const Mask *
>(rhs);
387 mask_size_type e = get_num_mask_types();
388 for (mask_size_type i = 0; i < e; ++i)
389 mask[i] &= convert->mask[i];
391 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
392 const Mask *convert =
static_cast<const Mask *
>(rhs);
393 mask_size_type e = get_num_mask_types();
394 for (mask_size_type i = 0; i < e; ++i)
395 mask[i] |= convert->mask[i];
397 void bitwise_not()
override {
398 mask_size_type e = get_num_mask_types();
399 for (mask_size_type i = 0; i < e; ++i)
400 mask[i] = ~(mask[i]);
402 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
403 const Mask *convert =
static_cast<const Mask *
>(rhs);
404 mask_size_type e = get_num_mask_types();
405 for (mask_size_type i = 0; i < e; ++i)
406 if (mask[i] != convert->mask[i])
410 int begin()
const override {
412 while (retval < end() && !is_set(retval))
416 int end()
const override {
418 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
421 int next(
int previous)
const override {
422 int retval = previous + 1;
423 while (retval < end() && !is_set(retval))
430 int get_system_affinity(
bool abort_on_error)
override {
431 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
432 "Illegal get affinity operation when not capable");
434 (void)abort_on_error;
437 for (
int i = 0; i < __kmp_xproc; ++i)
438 KMP_CPU_SET(i,
this);
441 int set_system_affinity(
bool abort_on_error)
const override {
442 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
444 "Illegal set affinity operation when not capable");
447 int gtid = __kmp_entry_gtid();
448 int tid = thread_self();
452 int retval = bindprocessor(BINDTHREAD, tid, PROCESSOR_CLASS_ANY);
456 KMP_CPU_SET_ITERATE(location,
this) {
457 if (KMP_CPU_ISSET(location,
this)) {
458 retval = bindprocessor(BINDTHREAD, tid, location);
459 if (retval == -1 && errno == 1) {
464 rsh = rs_alloc(RS_EMPTY);
465 rsid.at_pid = getpid();
466 if (RS_DEFAULT_RSET != ra_getrset(R_PROCESS, rsid, 0, rsh)) {
467 retval = ra_detachrset(R_PROCESS, rsid, 0);
468 retval = bindprocessor(BINDTHREAD, tid, location);
472 KA_TRACE(10, (
"__kmp_set_system_affinity: Done binding "
478 if (abort_on_error) {
479 __kmp_fatal(KMP_MSG(FunctionError,
"bindprocessor()"),
480 KMP_ERR(error), __kmp_msg_null);
481 KA_TRACE(10, (
"__kmp_set_system_affinity: Error binding "
482 "T#%d to cpu=%d, errno=%d.\n",
483 gtid, location, error));
491 int get_system_affinity(
bool abort_on_error)
override {
492 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
493 "Illegal get affinity operation when not capable");
496 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
497#elif KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY
498 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
499 reinterpret_cast<cpuset_t *
>(mask));
500 int retval = (r == 0 ? 0 : -1);
506 if (abort_on_error) {
507 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_getaffinity_np()"),
508 KMP_ERR(error), __kmp_msg_null);
512 int set_system_affinity(
bool abort_on_error)
const override {
513 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
514 "Illegal set affinity operation when not capable");
517 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
518#elif KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY
519 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
520 reinterpret_cast<cpuset_t *
>(mask));
521 int retval = (r == 0 ? 0 : -1);
527 if (abort_on_error) {
528 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_setaffinity_np()"),
529 KMP_ERR(error), __kmp_msg_null);
535 void determine_capable(
const char *env_var)
override {
536 __kmp_affinity_determine_capable(env_var);
538 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
539 KMPAffinity::Mask *allocate_mask()
override {
540 KMPNativeAffinity::Mask *retval =
new Mask();
543 void deallocate_mask(KMPAffinity::Mask *m)
override {
544 KMPNativeAffinity::Mask *native_mask =
545 static_cast<KMPNativeAffinity::Mask *
>(m);
548 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
549 return new Mask[num];
551 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
552 Mask *linux_array =
static_cast<Mask *
>(array);
553 delete[] linux_array;
555 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
556 int index)
override {
557 Mask *linux_array =
static_cast<Mask *
>(array);
558 return &(linux_array[index]);
560 api_type get_api_type()
const override {
return NATIVE_OS; }
566class KMPNativeAffinity :
public KMPAffinity {
567 class Mask :
public KMPAffinity::Mask {
568 typedef ULONG_PTR mask_t;
569 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
574 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
580 void set(
int i)
override {
581 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
583 bool is_set(
int i)
const override {
584 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
586 void clear(
int i)
override {
587 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
589 void zero()
override {
590 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
593 bool empty()
const override {
594 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
599 void copy(
const KMPAffinity::Mask *src)
override {
600 const Mask *convert =
static_cast<const Mask *
>(src);
601 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
602 mask[i] = convert->mask[i];
604 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
605 const Mask *convert =
static_cast<const Mask *
>(rhs);
606 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
607 mask[i] &= convert->mask[i];
609 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
610 const Mask *convert =
static_cast<const Mask *
>(rhs);
611 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
612 mask[i] |= convert->mask[i];
614 void bitwise_not()
override {
615 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
616 mask[i] = ~(mask[i]);
618 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
619 const Mask *convert =
static_cast<const Mask *
>(rhs);
620 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
621 if (mask[i] != convert->mask[i])
625 int begin()
const override {
627 while (retval < end() && !is_set(retval))
631 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
632 int next(
int previous)
const override {
633 int retval = previous + 1;
634 while (retval < end() && !is_set(retval))
638 int set_process_affinity(
bool abort_on_error)
const override {
639 if (__kmp_num_proc_groups <= 1) {
640 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
641 DWORD error = GetLastError();
642 if (abort_on_error) {
643 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
651 int set_system_affinity(
bool abort_on_error)
const override {
652 if (__kmp_num_proc_groups > 1) {
655 int group = get_proc_group();
657 if (abort_on_error) {
658 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
665 ga.Mask = mask[group];
666 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
668 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
669 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
670 DWORD error = GetLastError();
671 if (abort_on_error) {
672 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
678 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
679 DWORD error = GetLastError();
680 if (abort_on_error) {
681 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
689 int get_system_affinity(
bool abort_on_error)
override {
690 if (__kmp_num_proc_groups > 1) {
693 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
694 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
695 DWORD error = GetLastError();
696 if (abort_on_error) {
697 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
698 KMP_ERR(error), __kmp_msg_null);
702 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
706 mask[ga.Group] = ga.Mask;
708 mask_t newMask, sysMask, retval;
709 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
710 DWORD error = GetLastError();
711 if (abort_on_error) {
712 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
713 KMP_ERR(error), __kmp_msg_null);
717 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
719 DWORD error = GetLastError();
720 if (abort_on_error) {
721 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
722 KMP_ERR(error), __kmp_msg_null);
726 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
728 DWORD error = GetLastError();
729 if (abort_on_error) {
730 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
731 KMP_ERR(error), __kmp_msg_null);
738 int get_proc_group()
const override {
740 if (__kmp_num_proc_groups == 1) {
743 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
753 void determine_capable(
const char *env_var)
override {
754 __kmp_affinity_determine_capable(env_var);
756 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
757 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
758 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
759 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
760 return new Mask[num];
762 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
763 Mask *windows_array =
static_cast<Mask *
>(array);
764 delete[] windows_array;
766 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
767 int index)
override {
768 Mask *windows_array =
static_cast<Mask *
>(array);
769 return &(windows_array[index]);
771 api_type get_api_type()
const override {
return NATIVE_OS; }
777struct kmp_hw_attr_t {
781 unsigned reserved : 15;
783 static const int UNKNOWN_CORE_EFF = -1;
786 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
787 valid(0), reserved(0) {}
788 void set_core_type(kmp_hw_core_type_t type) {
792 void set_core_eff(
int eff) {
796 kmp_hw_core_type_t get_core_type()
const {
797 return (kmp_hw_core_type_t)core_type;
799 int get_core_eff()
const {
return core_eff; }
800 bool is_core_type_valid()
const {
801 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
803 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
804 operator bool()
const {
return valid; }
806 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
807 core_eff = UNKNOWN_CORE_EFF;
810 bool contains(
const kmp_hw_attr_t &other)
const {
811 if (!valid && !other.valid)
813 if (valid && other.valid) {
814 if (other.is_core_type_valid()) {
815 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
818 if (other.is_core_eff_valid()) {
819 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
826#if KMP_AFFINITY_SUPPORTED
827 bool contains(
const kmp_affinity_attrs_t &attr)
const {
828 if (!valid && !attr.valid)
830 if (valid && attr.valid) {
831 if (attr.core_type != KMP_HW_CORE_TYPE_UNKNOWN)
832 return (is_core_type_valid() &&
833 (get_core_type() == (kmp_hw_core_type_t)attr.core_type));
834 if (attr.core_eff != UNKNOWN_CORE_EFF)
835 return (is_core_eff_valid() && (get_core_eff() == attr.core_eff));
841 bool operator==(
const kmp_hw_attr_t &rhs)
const {
842 return (rhs.valid == valid && rhs.core_eff == core_eff &&
843 rhs.core_type == core_type);
845 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
848#if KMP_AFFINITY_SUPPORTED
849KMP_BUILD_ASSERT(
sizeof(kmp_hw_attr_t) ==
sizeof(kmp_affinity_attrs_t));
852class kmp_hw_thread_t {
854 static const int UNKNOWN_ID = -1;
855 static const int MULTIPLE_ID = -2;
856 static int compare_ids(
const void *a,
const void *b);
857 static int compare_compact(
const void *a,
const void *b);
858 int ids[KMP_HW_LAST];
859 int sub_ids[KMP_HW_LAST];
867 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
874class kmp_topology_t {
900 int num_core_efficiencies;
902 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
908 kmp_hw_thread_t *hw_threads;
914 kmp_hw_t equivalent[KMP_HW_LAST];
922#if KMP_GROUP_AFFINITY
924 void _insert_windows_proc_groups();
930 void _gather_enumeration_information();
934 void _remove_radix1_layers();
937 void _discover_uniformity();
948 void _set_last_level_cache();
953 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
954 bool find_all =
false)
const;
958 kmp_topology_t() =
delete;
959 kmp_topology_t(
const kmp_topology_t &t) =
delete;
960 kmp_topology_t(kmp_topology_t &&t) =
delete;
961 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
962 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
964 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
965 static void deallocate(kmp_topology_t *);
968 kmp_hw_thread_t &at(
int index) {
969 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
970 return hw_threads[index];
972 const kmp_hw_thread_t &at(
int index)
const {
973 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
974 return hw_threads[index];
976 int get_num_hw_threads()
const {
return num_hw_threads; }
978 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
979 kmp_hw_thread_t::compare_ids);
983 void insert_layer(kmp_hw_t type,
const int *ids);
987 bool check_ids()
const;
991 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
995#if KMP_AFFINITY_SUPPORTED
997 void set_granularity(kmp_affinity_t &stgs)
const;
998 bool is_close(
int hwt1,
int hwt2,
const kmp_affinity_t &stgs)
const;
999 bool restrict_to_mask(
const kmp_affin_mask_t *mask);
1000 bool filter_hw_subset();
1002 bool is_uniform()
const {
return flags.uniform; }
1005 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
1006 if (type == KMP_HW_UNKNOWN)
1007 return KMP_HW_UNKNOWN;
1008 return equivalent[type];
1011 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
1012 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
1013 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
1014 kmp_hw_t real_type2 = equivalent[type2];
1015 if (real_type2 == KMP_HW_UNKNOWN)
1017 equivalent[type1] = real_type2;
1020 KMP_FOREACH_HW_TYPE(type) {
1021 if (equivalent[type] == type1) {
1022 equivalent[type] = real_type2;
1028 int calculate_ratio(
int level1,
int level2)
const {
1029 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
1030 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
1032 for (
int level = level1; level > level2; --level)
1036 int get_ratio(
int level)
const {
1037 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1038 return ratio[level];
1040 int get_depth()
const {
return depth; };
1041 kmp_hw_t get_type(
int level)
const {
1042 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1043 return types[level];
1045 int get_level(kmp_hw_t type)
const {
1046 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
1047 int eq_type = equivalent[type];
1048 if (eq_type == KMP_HW_UNKNOWN)
1050 for (
int i = 0; i < depth; ++i)
1051 if (types[i] == eq_type)
1055 int get_count(
int level)
const {
1056 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
1057 return count[level];
1060 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
1061 return _get_ncores_with_attr(attr, -1,
true);
1065 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
1066 return _get_ncores_with_attr(attr, above,
false);
1069#if KMP_AFFINITY_SUPPORTED
1070 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
1071 void sort_compact(kmp_affinity_t &affinity) {
1072 compact = affinity.compact;
1073 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
1074 kmp_hw_thread_t::compare_compact);
1077 void print(
const char *env_var =
"KMP_AFFINITY")
const;
1080extern kmp_topology_t *__kmp_topology;
1082class kmp_hw_subset_t {
1083 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
1091 int offset[MAX_ATTRS];
1092 kmp_hw_attr_t attr[MAX_ATTRS];
1095 const static int USE_ALL = (std::numeric_limits<int>::max)();
1104 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
1107 static int hw_subset_compare(
const void *i1,
const void *i2) {
1108 kmp_hw_t type1 = ((
const item_t *)i1)->type;
1109 kmp_hw_t type2 = ((
const item_t *)i2)->type;
1110 int level1 = __kmp_topology->get_level(type1);
1111 int level2 = __kmp_topology->get_level(type2);
1112 return level1 - level2;
1117 kmp_hw_subset_t() =
delete;
1118 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
1119 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
1120 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
1121 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
1123 static kmp_hw_subset_t *allocate() {
1124 int initial_capacity = 5;
1125 kmp_hw_subset_t *retval =
1126 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
1128 retval->capacity = initial_capacity;
1130 retval->absolute =
false;
1131 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
1134 static void deallocate(kmp_hw_subset_t *subset) {
1135 __kmp_free(subset->items);
1138 void set_absolute() { absolute =
true; }
1139 bool is_absolute()
const {
return absolute; }
1140 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
1141 for (
int i = 0; i < depth; ++i) {
1144 if (items[i].type == type) {
1145 int idx = items[i].num_attrs++;
1146 if ((
size_t)idx >= MAX_ATTRS)
1148 items[i].num[idx] = num;
1149 items[i].offset[idx] = offset;
1150 items[i].attr[idx] = attr;
1154 if (depth == capacity - 1) {
1156 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
1157 for (
int i = 0; i < depth; ++i)
1158 new_items[i] = items[i];
1162 items[depth].num_attrs = 1;
1163 items[depth].type = type;
1164 items[depth].num[0] = num;
1165 items[depth].offset[0] = offset;
1166 items[depth].attr[0] = attr;
1168 set |= (1ull << type);
1170 int get_depth()
const {
return depth; }
1171 const item_t &at(
int index)
const {
1172 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1173 return items[index];
1175 item_t &at(
int index) {
1176 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1177 return items[index];
1179 void remove(
int index) {
1180 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1181 set &= ~(1ull << items[index].type);
1182 for (
int j = index + 1; j < depth; ++j) {
1183 items[j - 1] = items[j];
1188 KMP_DEBUG_ASSERT(__kmp_topology);
1189 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1191 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1205 void canonicalize(
const kmp_topology_t *top) {
1207 kmp_hw_t targeted[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1215 for (kmp_hw_t type : targeted)
1216 if (top->get_level(type) == KMP_HW_UNKNOWN)
1220 for (kmp_hw_t type : targeted) {
1222 for (
int i = 0; i < get_depth(); ++i) {
1223 if (top->get_equivalent_type(items[i].type) == type) {
1229 push_back(USE_ALL, type, 0, kmp_hw_attr_t{});
1237 printf(
"**********************\n");
1238 printf(
"*** kmp_hw_subset: ***\n");
1239 printf(
"* depth: %d\n", depth);
1240 printf(
"* items:\n");
1241 for (
int i = 0; i < depth; ++i) {
1242 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1243 for (
int j = 0; j < items[i].num_attrs; ++j) {
1244 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1245 items[i].offset[j]);
1246 if (!items[i].attr[j]) {
1247 printf(
" (none)\n");
1250 " core_type = %s, core_eff = %d\n",
1251 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1252 items[i].attr[j].get_core_eff());
1256 printf(
"* set: 0x%llx\n", set);
1257 printf(
"* absolute: %d\n", absolute);
1258 printf(
"**********************\n");
1261extern kmp_hw_subset_t *__kmp_hw_subset;
1269class hierarchy_info {
1273 static const kmp_uint32 maxLeaves = 4;
1274 static const kmp_uint32 minBranch = 4;
1280 kmp_uint32 maxLevels;
1287 kmp_uint32 base_num_threads = 0;
1288 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1289 volatile kmp_int8 uninitialized;
1291 volatile kmp_int8 resizing;
1297 kmp_uint32 *numPerLevel =
nullptr;
1298 kmp_uint32 *skipPerLevel =
nullptr;
1300 void deriveLevels() {
1301 int hier_depth = __kmp_topology->get_depth();
1302 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1303 numPerLevel[level] = __kmp_topology->get_ratio(i);
1308 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1311 if (!uninitialized && numPerLevel) {
1312 __kmp_free(numPerLevel);
1314 uninitialized = not_initialized;
1318 void init(
int num_addrs) {
1319 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1320 &uninitialized, not_initialized, initializing);
1321 if (bool_result == 0) {
1322 while (TCR_1(uninitialized) != initialized)
1326 KMP_DEBUG_ASSERT(bool_result == 1);
1336 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1337 skipPerLevel = &(numPerLevel[maxLevels]);
1338 for (kmp_uint32 i = 0; i < maxLevels;
1341 skipPerLevel[i] = 1;
1345 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1348 numPerLevel[0] = maxLeaves;
1349 numPerLevel[1] = num_addrs / maxLeaves;
1350 if (num_addrs % maxLeaves)
1354 base_num_threads = num_addrs;
1355 for (
int i = maxLevels - 1; i >= 0;
1357 if (numPerLevel[i] != 1 || depth > 1)
1360 kmp_uint32 branch = minBranch;
1361 if (numPerLevel[0] == 1)
1362 branch = num_addrs / maxLeaves;
1363 if (branch < minBranch)
1365 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1366 while (numPerLevel[d] > branch ||
1367 (d == 0 && numPerLevel[d] > maxLeaves)) {
1368 if (numPerLevel[d] & 1)
1370 numPerLevel[d] = numPerLevel[d] >> 1;
1371 if (numPerLevel[d + 1] == 1)
1373 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1375 if (numPerLevel[0] == 1) {
1376 branch = branch >> 1;
1382 for (kmp_uint32 i = 1; i < depth; ++i)
1383 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1385 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1386 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1388 uninitialized = initialized;
1392 void resize(kmp_uint32 nproc) {
1393 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1394 while (bool_result == 0) {
1396 if (nproc <= base_num_threads)
1399 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1401 KMP_DEBUG_ASSERT(bool_result != 0);
1402 if (nproc <= base_num_threads)
1406 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1407 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1409 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1410 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1411 numPerLevel[i - 1] *= 2;
1415 if (nproc > old_sz) {
1416 while (nproc > old_sz) {
1424 kmp_uint32 *old_numPerLevel = numPerLevel;
1425 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1426 numPerLevel = skipPerLevel = NULL;
1428 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1429 skipPerLevel = &(numPerLevel[maxLevels]);
1432 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1434 numPerLevel[i] = old_numPerLevel[i];
1435 skipPerLevel[i] = old_skipPerLevel[i];
1439 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1442 skipPerLevel[i] = 1;
1446 __kmp_free(old_numPerLevel);
1450 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1451 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1453 base_num_threads = nproc;