13 #ifndef KMP_AFFINITY_H
14 #define KMP_AFFINITY_H
20 #if KMP_AFFINITY_SUPPORTED
22 class 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
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.
234 #elif KMP_ARCH_X86_64
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.
267 #elif KMP_ARCH_MIPS64
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 #error Unknown or unsupported architecture
316 #include <pthread_np.h>
318 class KMPNativeAffinity :
public KMPAffinity {
319 class Mask :
public KMPAffinity::Mask {
320 typedef unsigned long mask_t;
321 typedef decltype(__kmp_affin_mask_size) mask_size_type;
322 static const unsigned int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
323 static const mask_t ONE = 1;
324 mask_size_type get_num_mask_types()
const {
325 return __kmp_affin_mask_size /
sizeof(mask_t);
330 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
335 void set(
int i)
override {
336 mask[i / BITS_PER_MASK_T] |= (ONE << (i % BITS_PER_MASK_T));
338 bool is_set(
int i)
const override {
339 return (mask[i / BITS_PER_MASK_T] & (ONE << (i % BITS_PER_MASK_T)));
341 void clear(
int i)
override {
342 mask[i / BITS_PER_MASK_T] &= ~(ONE << (i % BITS_PER_MASK_T));
344 void zero()
override {
345 mask_size_type e = get_num_mask_types();
346 for (mask_size_type i = 0; i < e; ++i)
349 bool empty()
const override {
350 mask_size_type e = get_num_mask_types();
351 for (mask_size_type i = 0; i < e; ++i)
352 if (mask[i] != (mask_t)0)
356 void copy(
const KMPAffinity::Mask *src)
override {
357 const Mask *convert =
static_cast<const Mask *
>(src);
358 mask_size_type e = get_num_mask_types();
359 for (mask_size_type i = 0; i < e; ++i)
360 mask[i] = convert->mask[i];
362 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
363 const Mask *convert =
static_cast<const Mask *
>(rhs);
364 mask_size_type e = get_num_mask_types();
365 for (mask_size_type i = 0; i < e; ++i)
366 mask[i] &= convert->mask[i];
368 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
369 const Mask *convert =
static_cast<const Mask *
>(rhs);
370 mask_size_type e = get_num_mask_types();
371 for (mask_size_type i = 0; i < e; ++i)
372 mask[i] |= convert->mask[i];
374 void bitwise_not()
override {
375 mask_size_type e = get_num_mask_types();
376 for (mask_size_type i = 0; i < e; ++i)
377 mask[i] = ~(mask[i]);
379 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
380 const Mask *convert =
static_cast<const Mask *
>(rhs);
381 mask_size_type e = get_num_mask_types();
382 for (mask_size_type i = 0; i < e; ++i)
383 if (mask[i] != convert->mask[i])
387 int begin()
const override {
389 while (retval < end() && !is_set(retval))
393 int end()
const override {
395 __kmp_type_convert(get_num_mask_types() * BITS_PER_MASK_T, &e);
398 int next(
int previous)
const override {
399 int retval = previous + 1;
400 while (retval < end() && !is_set(retval))
404 int get_system_affinity(
bool abort_on_error)
override {
405 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
406 "Illegal get affinity operation when not capable");
409 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
411 int r = pthread_getaffinity_np(pthread_self(), __kmp_affin_mask_size,
412 reinterpret_cast<cpuset_t *
>(mask));
413 int retval = (r == 0 ? 0 : -1);
419 if (abort_on_error) {
420 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_getaffinity_np()"),
421 KMP_ERR(error), __kmp_msg_null);
425 int set_system_affinity(
bool abort_on_error)
const override {
426 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
427 "Illegal set affinity operation when not capable");
430 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
432 int r = pthread_setaffinity_np(pthread_self(), __kmp_affin_mask_size,
433 reinterpret_cast<cpuset_t *
>(mask));
434 int retval = (r == 0 ? 0 : -1);
440 if (abort_on_error) {
441 __kmp_fatal(KMP_MSG(FunctionError,
"pthread_setaffinity_np()"),
442 KMP_ERR(error), __kmp_msg_null);
447 void determine_capable(
const char *env_var)
override {
448 __kmp_affinity_determine_capable(env_var);
450 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
451 KMPAffinity::Mask *allocate_mask()
override {
452 KMPNativeAffinity::Mask *retval =
new Mask();
455 void deallocate_mask(KMPAffinity::Mask *m)
override {
456 KMPNativeAffinity::Mask *native_mask =
457 static_cast<KMPNativeAffinity::Mask *
>(m);
460 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
461 return new Mask[num];
463 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
464 Mask *linux_array =
static_cast<Mask *
>(array);
465 delete[] linux_array;
467 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
468 int index)
override {
469 Mask *linux_array =
static_cast<Mask *
>(array);
470 return &(linux_array[index]);
472 api_type get_api_type()
const override {
return NATIVE_OS; }
477 class KMPNativeAffinity :
public KMPAffinity {
478 class Mask :
public KMPAffinity::Mask {
479 typedef ULONG_PTR mask_t;
480 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
485 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
491 void set(
int i)
override {
492 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
494 bool is_set(
int i)
const override {
495 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
497 void clear(
int i)
override {
498 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
500 void zero()
override {
501 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
504 bool empty()
const override {
505 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
510 void copy(
const KMPAffinity::Mask *src)
override {
511 const Mask *convert =
static_cast<const Mask *
>(src);
512 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
513 mask[i] = convert->mask[i];
515 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
516 const Mask *convert =
static_cast<const Mask *
>(rhs);
517 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
518 mask[i] &= convert->mask[i];
520 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
521 const Mask *convert =
static_cast<const Mask *
>(rhs);
522 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
523 mask[i] |= convert->mask[i];
525 void bitwise_not()
override {
526 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
527 mask[i] = ~(mask[i]);
529 bool is_equal(
const KMPAffinity::Mask *rhs)
const override {
530 const Mask *convert =
static_cast<const Mask *
>(rhs);
531 for (
size_t i = 0; i < __kmp_num_proc_groups; ++i)
532 if (mask[i] != convert->mask[i])
536 int begin()
const override {
538 while (retval < end() && !is_set(retval))
542 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
543 int next(
int previous)
const override {
544 int retval = previous + 1;
545 while (retval < end() && !is_set(retval))
549 int set_process_affinity(
bool abort_on_error)
const override {
550 if (__kmp_num_proc_groups <= 1) {
551 if (!SetProcessAffinityMask(GetCurrentProcess(), *mask)) {
552 DWORD error = GetLastError();
553 if (abort_on_error) {
554 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
562 int set_system_affinity(
bool abort_on_error)
const override {
563 if (__kmp_num_proc_groups > 1) {
566 int group = get_proc_group();
568 if (abort_on_error) {
569 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
576 ga.Mask = mask[group];
577 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
579 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
580 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
581 DWORD error = GetLastError();
582 if (abort_on_error) {
583 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
589 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
590 DWORD error = GetLastError();
591 if (abort_on_error) {
592 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
600 int get_system_affinity(
bool abort_on_error)
override {
601 if (__kmp_num_proc_groups > 1) {
604 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
605 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
606 DWORD error = GetLastError();
607 if (abort_on_error) {
608 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
609 KMP_ERR(error), __kmp_msg_null);
613 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
617 mask[ga.Group] = ga.Mask;
619 mask_t newMask, sysMask, retval;
620 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
621 DWORD error = GetLastError();
622 if (abort_on_error) {
623 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
624 KMP_ERR(error), __kmp_msg_null);
628 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
630 DWORD error = GetLastError();
631 if (abort_on_error) {
632 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
633 KMP_ERR(error), __kmp_msg_null);
637 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
639 DWORD error = GetLastError();
640 if (abort_on_error) {
641 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
642 KMP_ERR(error), __kmp_msg_null);
649 int get_proc_group()
const override {
651 if (__kmp_num_proc_groups == 1) {
654 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
664 void determine_capable(
const char *env_var)
override {
665 __kmp_affinity_determine_capable(env_var);
667 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
668 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
669 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
670 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
671 return new Mask[num];
673 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
674 Mask *windows_array =
static_cast<Mask *
>(array);
675 delete[] windows_array;
677 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
678 int index)
override {
679 Mask *windows_array =
static_cast<Mask *
>(array);
680 return &(windows_array[index]);
682 api_type get_api_type()
const override {
return NATIVE_OS; }
688 struct kmp_hw_attr_t {
692 unsigned reserved : 15;
694 static const int UNKNOWN_CORE_EFF = -1;
697 : core_type(KMP_HW_CORE_TYPE_UNKNOWN), core_eff(UNKNOWN_CORE_EFF),
698 valid(0), reserved(0) {}
699 void set_core_type(kmp_hw_core_type_t type) {
703 void set_core_eff(
int eff) {
707 kmp_hw_core_type_t get_core_type()
const {
708 return (kmp_hw_core_type_t)core_type;
710 int get_core_eff()
const {
return core_eff; }
711 bool is_core_type_valid()
const {
712 return core_type != KMP_HW_CORE_TYPE_UNKNOWN;
714 bool is_core_eff_valid()
const {
return core_eff != UNKNOWN_CORE_EFF; }
715 operator bool()
const {
return valid; }
717 core_type = KMP_HW_CORE_TYPE_UNKNOWN;
718 core_eff = UNKNOWN_CORE_EFF;
721 bool contains(
const kmp_hw_attr_t &other)
const {
722 if (!valid && !other.valid)
724 if (valid && other.valid) {
725 if (other.is_core_type_valid()) {
726 if (!is_core_type_valid() || (get_core_type() != other.get_core_type()))
729 if (other.is_core_eff_valid()) {
730 if (!is_core_eff_valid() || (get_core_eff() != other.get_core_eff()))
737 #if KMP_AFFINITY_SUPPORTED
738 bool contains(
const kmp_affinity_attrs_t &attr)
const {
739 if (!valid && !attr.valid)
741 if (valid && attr.valid) {
742 if (attr.core_type != KMP_HW_CORE_TYPE_UNKNOWN)
743 return (is_core_type_valid() &&
744 (get_core_type() == (kmp_hw_core_type_t)attr.core_type));
745 if (attr.core_eff != UNKNOWN_CORE_EFF)
746 return (is_core_eff_valid() && (get_core_eff() == attr.core_eff));
752 bool operator==(
const kmp_hw_attr_t &rhs)
const {
753 return (rhs.valid == valid && rhs.core_eff == core_eff &&
754 rhs.core_type == core_type);
756 bool operator!=(
const kmp_hw_attr_t &rhs)
const {
return !operator==(rhs); }
759 #if KMP_AFFINITY_SUPPORTED
760 KMP_BUILD_ASSERT(
sizeof(kmp_hw_attr_t) ==
sizeof(kmp_affinity_attrs_t));
763 class kmp_hw_thread_t {
765 static const int UNKNOWN_ID = -1;
766 static const int MULTIPLE_ID = -2;
767 static int compare_ids(
const void *a,
const void *b);
768 static int compare_compact(
const void *a,
const void *b);
769 int ids[KMP_HW_LAST];
770 int sub_ids[KMP_HW_LAST];
777 for (
int i = 0; i < (int)KMP_HW_LAST; ++i)
784 class kmp_topology_t {
810 int num_core_efficiencies;
812 kmp_hw_core_type_t core_types[KMP_HW_MAX_NUM_CORE_TYPES];
818 kmp_hw_thread_t *hw_threads;
824 kmp_hw_t equivalent[KMP_HW_LAST];
833 void _insert_layer(kmp_hw_t type,
const int *ids);
835 #if KMP_GROUP_AFFINITY
837 void _insert_windows_proc_groups();
843 void _gather_enumeration_information();
847 void _remove_radix1_layers();
850 void _discover_uniformity();
861 void _set_last_level_cache();
866 int _get_ncores_with_attr(
const kmp_hw_attr_t &attr,
int above,
867 bool find_all =
false)
const;
871 kmp_topology_t() =
delete;
872 kmp_topology_t(
const kmp_topology_t &t) =
delete;
873 kmp_topology_t(kmp_topology_t &&t) =
delete;
874 kmp_topology_t &operator=(
const kmp_topology_t &t) =
delete;
875 kmp_topology_t &operator=(kmp_topology_t &&t) =
delete;
877 static kmp_topology_t *allocate(
int nproc,
int ndepth,
const kmp_hw_t *types);
878 static void deallocate(kmp_topology_t *);
881 kmp_hw_thread_t &at(
int index) {
882 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
883 return hw_threads[index];
885 const kmp_hw_thread_t &at(
int index)
const {
886 KMP_DEBUG_ASSERT(index >= 0 && index < num_hw_threads);
887 return hw_threads[index];
889 int get_num_hw_threads()
const {
return num_hw_threads; }
891 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
892 kmp_hw_thread_t::compare_ids);
896 bool check_ids()
const;
900 void canonicalize(
int pkgs,
int cores_per_pkg,
int thr_per_core,
int cores);
904 #if KMP_AFFINITY_SUPPORTED
906 void set_granularity(kmp_affinity_t &stgs)
const;
907 bool is_close(
int hwt1,
int hwt2,
const kmp_affinity_t &stgs)
const;
908 bool restrict_to_mask(
const kmp_affin_mask_t *mask);
909 bool filter_hw_subset();
911 bool is_uniform()
const {
return flags.uniform; }
914 kmp_hw_t get_equivalent_type(kmp_hw_t type)
const {
915 if (type == KMP_HW_UNKNOWN)
916 return KMP_HW_UNKNOWN;
917 return equivalent[type];
920 void set_equivalent_type(kmp_hw_t type1, kmp_hw_t type2) {
921 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type1);
922 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type2);
923 kmp_hw_t real_type2 = equivalent[type2];
924 if (real_type2 == KMP_HW_UNKNOWN)
926 equivalent[type1] = real_type2;
929 KMP_FOREACH_HW_TYPE(type) {
930 if (equivalent[type] == type1) {
931 equivalent[type] = real_type2;
937 int calculate_ratio(
int level1,
int level2)
const {
938 KMP_DEBUG_ASSERT(level1 >= 0 && level1 < depth);
939 KMP_DEBUG_ASSERT(level2 >= 0 && level2 < depth);
941 for (
int level = level1; level > level2; --level)
945 int get_ratio(
int level)
const {
946 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
949 int get_depth()
const {
return depth; };
950 kmp_hw_t get_type(
int level)
const {
951 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
954 int get_level(kmp_hw_t type)
const {
955 KMP_DEBUG_ASSERT_VALID_HW_TYPE(type);
956 int eq_type = equivalent[type];
957 if (eq_type == KMP_HW_UNKNOWN)
959 for (
int i = 0; i < depth; ++i)
960 if (types[i] == eq_type)
964 int get_count(
int level)
const {
965 KMP_DEBUG_ASSERT(level >= 0 && level < depth);
969 int get_ncores_with_attr(
const kmp_hw_attr_t &attr)
const {
970 return _get_ncores_with_attr(attr, -1,
true);
974 int get_ncores_with_attr_per(
const kmp_hw_attr_t &attr,
int above)
const {
975 return _get_ncores_with_attr(attr, above,
false);
978 #if KMP_AFFINITY_SUPPORTED
979 friend int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b);
980 void sort_compact(kmp_affinity_t &affinity) {
981 compact = affinity.compact;
982 qsort(hw_threads, num_hw_threads,
sizeof(kmp_hw_thread_t),
983 kmp_hw_thread_t::compare_compact);
986 void print(
const char *env_var =
"KMP_AFFINITY")
const;
989 extern kmp_topology_t *__kmp_topology;
991 class kmp_hw_subset_t {
992 const static size_t MAX_ATTRS = KMP_HW_MAX_NUM_CORE_EFFS;
1000 int offset[MAX_ATTRS];
1001 kmp_hw_attr_t attr[MAX_ATTRS];
1004 const static int USE_ALL = (std::numeric_limits<int>::max)();
1013 KMP_BUILD_ASSERT(
sizeof(set) * 8 >= KMP_HW_LAST);
1016 static int hw_subset_compare(
const void *i1,
const void *i2) {
1017 kmp_hw_t type1 = ((
const item_t *)i1)->type;
1018 kmp_hw_t type2 = ((
const item_t *)i2)->type;
1019 int level1 = __kmp_topology->get_level(type1);
1020 int level2 = __kmp_topology->get_level(type2);
1021 return level1 - level2;
1026 kmp_hw_subset_t() =
delete;
1027 kmp_hw_subset_t(
const kmp_hw_subset_t &t) =
delete;
1028 kmp_hw_subset_t(kmp_hw_subset_t &&t) =
delete;
1029 kmp_hw_subset_t &operator=(
const kmp_hw_subset_t &t) =
delete;
1030 kmp_hw_subset_t &operator=(kmp_hw_subset_t &&t) =
delete;
1032 static kmp_hw_subset_t *allocate() {
1033 int initial_capacity = 5;
1034 kmp_hw_subset_t *retval =
1035 (kmp_hw_subset_t *)__kmp_allocate(
sizeof(kmp_hw_subset_t));
1037 retval->capacity = initial_capacity;
1039 retval->absolute =
false;
1040 retval->items = (item_t *)__kmp_allocate(
sizeof(item_t) * initial_capacity);
1043 static void deallocate(kmp_hw_subset_t *subset) {
1044 __kmp_free(subset->items);
1047 void set_absolute() { absolute =
true; }
1048 bool is_absolute()
const {
return absolute; }
1049 void push_back(
int num, kmp_hw_t type,
int offset, kmp_hw_attr_t attr) {
1050 for (
int i = 0; i < depth; ++i) {
1053 if (items[i].type == type) {
1054 int idx = items[i].num_attrs++;
1055 if ((
size_t)idx >= MAX_ATTRS)
1057 items[i].num[idx] = num;
1058 items[i].offset[idx] = offset;
1059 items[i].attr[idx] = attr;
1063 if (depth == capacity - 1) {
1065 item_t *new_items = (item_t *)__kmp_allocate(
sizeof(item_t) * capacity);
1066 for (
int i = 0; i < depth; ++i)
1067 new_items[i] = items[i];
1071 items[depth].num_attrs = 1;
1072 items[depth].type = type;
1073 items[depth].num[0] = num;
1074 items[depth].offset[0] = offset;
1075 items[depth].attr[0] = attr;
1077 set |= (1ull << type);
1079 int get_depth()
const {
return depth; }
1080 const item_t &at(
int index)
const {
1081 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1082 return items[index];
1084 item_t &at(
int index) {
1085 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1086 return items[index];
1088 void remove(
int index) {
1089 KMP_DEBUG_ASSERT(index >= 0 && index < depth);
1090 set &= ~(1ull << items[index].type);
1091 for (
int j = index + 1; j < depth; ++j) {
1092 items[j - 1] = items[j];
1097 KMP_DEBUG_ASSERT(__kmp_topology);
1098 qsort(items, depth,
sizeof(item_t), hw_subset_compare);
1100 bool specified(kmp_hw_t type)
const {
return ((set & (1ull << type)) > 0); }
1102 printf(
"**********************\n");
1103 printf(
"*** kmp_hw_subset: ***\n");
1104 printf(
"* depth: %d\n", depth);
1105 printf(
"* items:\n");
1106 for (
int i = 0; i < depth; ++i) {
1107 printf(
" type: %s\n", __kmp_hw_get_keyword(items[i].type));
1108 for (
int j = 0; j < items[i].num_attrs; ++j) {
1109 printf(
" num: %d, offset: %d, attr: ", items[i].num[j],
1110 items[i].offset[j]);
1111 if (!items[i].attr[j]) {
1112 printf(
" (none)\n");
1115 " core_type = %s, core_eff = %d\n",
1116 __kmp_hw_get_core_type_string(items[i].attr[j].get_core_type()),
1117 items[i].attr[j].get_core_eff());
1121 printf(
"* set: 0x%llx\n", set);
1122 printf(
"* absolute: %d\n", absolute);
1123 printf(
"**********************\n");
1126 extern kmp_hw_subset_t *__kmp_hw_subset;
1134 class hierarchy_info {
1138 static const kmp_uint32 maxLeaves = 4;
1139 static const kmp_uint32 minBranch = 4;
1145 kmp_uint32 maxLevels;
1152 kmp_uint32 base_num_threads;
1153 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
1154 volatile kmp_int8 uninitialized;
1156 volatile kmp_int8 resizing;
1162 kmp_uint32 *numPerLevel;
1163 kmp_uint32 *skipPerLevel;
1165 void deriveLevels() {
1166 int hier_depth = __kmp_topology->get_depth();
1167 for (
int i = hier_depth - 1, level = 0; i >= 0; --i, ++level) {
1168 numPerLevel[level] = __kmp_topology->get_ratio(i);
1173 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
1176 if (!uninitialized && numPerLevel) {
1177 __kmp_free(numPerLevel);
1179 uninitialized = not_initialized;
1183 void init(
int num_addrs) {
1184 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
1185 &uninitialized, not_initialized, initializing);
1186 if (bool_result == 0) {
1187 while (TCR_1(uninitialized) != initialized)
1191 KMP_DEBUG_ASSERT(bool_result == 1);
1201 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1202 skipPerLevel = &(numPerLevel[maxLevels]);
1203 for (kmp_uint32 i = 0; i < maxLevels;
1206 skipPerLevel[i] = 1;
1210 if (__kmp_topology && __kmp_topology->get_depth() > 0) {
1213 numPerLevel[0] = maxLeaves;
1214 numPerLevel[1] = num_addrs / maxLeaves;
1215 if (num_addrs % maxLeaves)
1219 base_num_threads = num_addrs;
1220 for (
int i = maxLevels - 1; i >= 0;
1222 if (numPerLevel[i] != 1 || depth > 1)
1225 kmp_uint32 branch = minBranch;
1226 if (numPerLevel[0] == 1)
1227 branch = num_addrs / maxLeaves;
1228 if (branch < minBranch)
1230 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
1231 while (numPerLevel[d] > branch ||
1232 (d == 0 && numPerLevel[d] > maxLeaves)) {
1233 if (numPerLevel[d] & 1)
1235 numPerLevel[d] = numPerLevel[d] >> 1;
1236 if (numPerLevel[d + 1] == 1)
1238 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
1240 if (numPerLevel[0] == 1) {
1241 branch = branch >> 1;
1247 for (kmp_uint32 i = 1; i < depth; ++i)
1248 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
1250 for (kmp_uint32 i = depth; i < maxLevels; ++i)
1251 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1253 uninitialized = initialized;
1257 void resize(kmp_uint32 nproc) {
1258 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1259 while (bool_result == 0) {
1261 if (nproc <= base_num_threads)
1264 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
1266 KMP_DEBUG_ASSERT(bool_result != 0);
1267 if (nproc <= base_num_threads)
1271 kmp_uint32 old_sz = skipPerLevel[depth - 1];
1272 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
1274 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
1275 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1276 numPerLevel[i - 1] *= 2;
1280 if (nproc > old_sz) {
1281 while (nproc > old_sz) {
1289 kmp_uint32 *old_numPerLevel = numPerLevel;
1290 kmp_uint32 *old_skipPerLevel = skipPerLevel;
1291 numPerLevel = skipPerLevel = NULL;
1293 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
1294 skipPerLevel = &(numPerLevel[maxLevels]);
1297 for (kmp_uint32 i = 0; i < old_maxLevels; ++i) {
1299 numPerLevel[i] = old_numPerLevel[i];
1300 skipPerLevel[i] = old_skipPerLevel[i];
1304 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i) {
1307 skipPerLevel[i] = 1;
1311 __kmp_free(old_numPerLevel);
1315 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
1316 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
1318 base_num_threads = nproc;