13 #ifndef KMP_AFFINITY_H 14 #define KMP_AFFINITY_H 19 #if KMP_AFFINITY_SUPPORTED 21 class KMPHwlocAffinity :
public KMPAffinity {
23 class Mask :
public KMPAffinity::Mask {
28 mask = hwloc_bitmap_alloc();
31 ~Mask() { hwloc_bitmap_free(mask); }
32 void set(
int i)
override { hwloc_bitmap_set(mask, i); }
33 bool is_set(
int i)
const override {
return hwloc_bitmap_isset(mask, i); }
34 void clear(
int i)
override { hwloc_bitmap_clr(mask, i); }
35 void zero()
override { hwloc_bitmap_zero(mask); }
36 void copy(
const KMPAffinity::Mask *src)
override {
37 const Mask *convert =
static_cast<const Mask *
>(src);
38 hwloc_bitmap_copy(mask, convert->mask);
40 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
41 const Mask *convert =
static_cast<const Mask *
>(rhs);
42 hwloc_bitmap_and(mask, mask, convert->mask);
44 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
45 const Mask *convert =
static_cast<const Mask *
>(rhs);
46 hwloc_bitmap_or(mask, mask, convert->mask);
48 void bitwise_not()
override { hwloc_bitmap_not(mask, mask); }
49 int begin()
const override {
return hwloc_bitmap_first(mask); }
50 int end()
const override {
return -1; }
51 int next(
int previous)
const override {
52 return hwloc_bitmap_next(mask, previous);
54 int get_system_affinity(
bool abort_on_error)
override {
55 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
56 "Illegal get affinity operation when not capable");
58 hwloc_get_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
64 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
68 int set_system_affinity(
bool abort_on_error)
const override {
69 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
70 "Illegal get affinity operation when not capable");
72 hwloc_set_cpubind(__kmp_hwloc_topology, mask, HWLOC_CPUBIND_THREAD);
78 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
82 int get_proc_group()
const override {
85 if (__kmp_num_proc_groups == 1) {
88 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
90 unsigned long first_32_bits = hwloc_bitmap_to_ith_ulong(mask, i * 2);
91 unsigned long second_32_bits =
92 hwloc_bitmap_to_ith_ulong(mask, i * 2 + 1);
93 if (first_32_bits == 0 && second_32_bits == 0) {
105 void determine_capable(
const char *var)
override {
106 const hwloc_topology_support *topology_support;
107 if (__kmp_hwloc_topology == NULL) {
108 if (hwloc_topology_init(&__kmp_hwloc_topology) < 0) {
109 __kmp_hwloc_error = TRUE;
110 if (__kmp_affinity_verbose)
111 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_init()");
113 if (hwloc_topology_load(__kmp_hwloc_topology) < 0) {
114 __kmp_hwloc_error = TRUE;
115 if (__kmp_affinity_verbose)
116 KMP_WARNING(AffHwlocErrorOccurred, var,
"hwloc_topology_load()");
119 topology_support = hwloc_topology_get_support(__kmp_hwloc_topology);
124 if (topology_support && topology_support->cpubind->set_thisthread_cpubind &&
125 topology_support->cpubind->get_thisthread_cpubind &&
126 topology_support->discovery->pu && !__kmp_hwloc_error) {
128 KMP_AFFINITY_ENABLE(TRUE);
131 __kmp_hwloc_error = TRUE;
132 KMP_AFFINITY_DISABLE();
135 void bind_thread(
int which)
override {
136 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
137 "Illegal set affinity operation when not capable");
138 KMPAffinity::Mask *mask;
139 KMP_CPU_ALLOC_ON_STACK(mask);
141 KMP_CPU_SET(which, mask);
142 __kmp_set_system_affinity(mask, TRUE);
143 KMP_CPU_FREE_FROM_STACK(mask);
145 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
146 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
147 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
148 return new Mask[num];
150 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
151 Mask *hwloc_array =
static_cast<Mask *
>(array);
152 delete[] hwloc_array;
154 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
155 int index)
override {
156 Mask *hwloc_array =
static_cast<Mask *
>(array);
157 return &(hwloc_array[index]);
159 api_type get_api_type()
const override {
return HWLOC; }
168 #include <sys/syscall.h> 169 #if KMP_ARCH_X86 || KMP_ARCH_ARM 170 #ifndef __NR_sched_setaffinity 171 #define __NR_sched_setaffinity 241 172 #elif __NR_sched_setaffinity != 241 173 #error Wrong code for setaffinity system call. 175 #ifndef __NR_sched_getaffinity 176 #define __NR_sched_getaffinity 242 177 #elif __NR_sched_getaffinity != 242 178 #error Wrong code for getaffinity system call. 180 #elif KMP_ARCH_AARCH64 181 #ifndef __NR_sched_setaffinity 182 #define __NR_sched_setaffinity 122 183 #elif __NR_sched_setaffinity != 122 184 #error Wrong code for setaffinity system call. 186 #ifndef __NR_sched_getaffinity 187 #define __NR_sched_getaffinity 123 188 #elif __NR_sched_getaffinity != 123 189 #error Wrong code for getaffinity system call. 191 #elif KMP_ARCH_X86_64 192 #ifndef __NR_sched_setaffinity 193 #define __NR_sched_setaffinity 203 194 #elif __NR_sched_setaffinity != 203 195 #error Wrong code for setaffinity system call. 197 #ifndef __NR_sched_getaffinity 198 #define __NR_sched_getaffinity 204 199 #elif __NR_sched_getaffinity != 204 200 #error Wrong code for getaffinity system call. 203 #ifndef __NR_sched_setaffinity 204 #define __NR_sched_setaffinity 222 205 #elif __NR_sched_setaffinity != 222 206 #error Wrong code for setaffinity system call. 208 #ifndef __NR_sched_getaffinity 209 #define __NR_sched_getaffinity 223 210 #elif __NR_sched_getaffinity != 223 211 #error Wrong code for getaffinity system call. 214 # ifndef __NR_sched_setaffinity 215 # define __NR_sched_setaffinity 4239 216 # elif __NR_sched_setaffinity != 4239 217 # error Wrong code for setaffinity system call. 219 # ifndef __NR_sched_getaffinity 220 # define __NR_sched_getaffinity 4240 221 # elif __NR_sched_getaffinity != 4240 222 # error Wrong code for getaffinity system call. 224 # elif KMP_ARCH_MIPS64 225 # ifndef __NR_sched_setaffinity 226 # define __NR_sched_setaffinity 5195 227 # elif __NR_sched_setaffinity != 5195 228 # error Wrong code for setaffinity system call. 230 # ifndef __NR_sched_getaffinity 231 # define __NR_sched_getaffinity 5196 232 # elif __NR_sched_getaffinity != 5196 233 # error Wrong code for getaffinity system call. 236 #error Unknown or unsupported architecture 238 class KMPNativeAffinity :
public KMPAffinity {
239 class Mask :
public KMPAffinity::Mask {
240 typedef unsigned char mask_t;
241 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
245 Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
250 void set(
int i)
override {
251 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
253 bool is_set(
int i)
const override {
254 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
256 void clear(
int i)
override {
257 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
259 void zero()
override {
260 for (
size_t i = 0; i < __kmp_affin_mask_size; ++i)
263 void copy(
const KMPAffinity::Mask *src)
override {
264 const Mask *convert =
static_cast<const Mask *
>(src);
265 for (
size_t i = 0; i < __kmp_affin_mask_size; ++i)
266 mask[i] = convert->mask[i];
268 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
269 const Mask *convert =
static_cast<const Mask *
>(rhs);
270 for (
size_t i = 0; i < __kmp_affin_mask_size; ++i)
271 mask[i] &= convert->mask[i];
273 void bitwise_or(
const KMPAffinity::Mask *rhs)
override {
274 const Mask *convert =
static_cast<const Mask *
>(rhs);
275 for (
size_t i = 0; i < __kmp_affin_mask_size; ++i)
276 mask[i] |= convert->mask[i];
278 void bitwise_not()
override {
279 for (
size_t i = 0; i < __kmp_affin_mask_size; ++i)
280 mask[i] = ~(mask[i]);
282 int begin()
const override {
284 while (retval < end() && !is_set(retval))
288 int end()
const override {
return __kmp_affin_mask_size * BITS_PER_MASK_T; }
289 int next(
int previous)
const override {
290 int retval = previous + 1;
291 while (retval < end() && !is_set(retval))
295 int get_system_affinity(
bool abort_on_error)
override {
296 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
297 "Illegal get affinity operation when not capable");
299 syscall(__NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask);
304 if (abort_on_error) {
305 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
309 int set_system_affinity(
bool abort_on_error)
const override {
310 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
311 "Illegal get affinity operation when not capable");
313 syscall(__NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask);
318 if (abort_on_error) {
319 __kmp_fatal(KMP_MSG(FatalSysError), KMP_ERR(error), __kmp_msg_null);
324 void determine_capable(
const char *env_var)
override {
325 __kmp_affinity_determine_capable(env_var);
327 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
328 KMPAffinity::Mask *allocate_mask()
override {
329 KMPNativeAffinity::Mask *retval =
new Mask();
332 void deallocate_mask(KMPAffinity::Mask *m)
override {
333 KMPNativeAffinity::Mask *native_mask =
334 static_cast<KMPNativeAffinity::Mask *
>(m);
337 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
338 return new Mask[num];
340 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
341 Mask *linux_array =
static_cast<Mask *
>(array);
342 delete[] linux_array;
344 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
345 int index)
override {
346 Mask *linux_array =
static_cast<Mask *
>(array);
347 return &(linux_array[index]);
349 api_type get_api_type()
const override {
return NATIVE_OS; }
354 class KMPNativeAffinity :
public KMPAffinity {
355 class Mask :
public KMPAffinity::Mask {
356 typedef ULONG_PTR mask_t;
357 static const int BITS_PER_MASK_T =
sizeof(mask_t) * CHAR_BIT;
362 mask = (mask_t *)__kmp_allocate(
sizeof(mask_t) * __kmp_num_proc_groups);
368 void set(
int i)
override {
369 mask[i / BITS_PER_MASK_T] |= ((mask_t)1 << (i % BITS_PER_MASK_T));
371 bool is_set(
int i)
const override {
372 return (mask[i / BITS_PER_MASK_T] & ((mask_t)1 << (i % BITS_PER_MASK_T)));
374 void clear(
int i)
override {
375 mask[i / BITS_PER_MASK_T] &= ~((mask_t)1 << (i % BITS_PER_MASK_T));
377 void zero()
override {
378 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
381 void copy(
const KMPAffinity::Mask *src)
override {
382 const Mask *convert =
static_cast<const Mask *
>(src);
383 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
384 mask[i] = convert->mask[i];
386 void bitwise_and(
const KMPAffinity::Mask *rhs)
override {
387 const Mask *convert =
static_cast<const Mask *
>(rhs);
388 for (
int i = 0; i < __kmp_num_proc_groups; ++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 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
394 mask[i] |= convert->mask[i];
396 void bitwise_not()
override {
397 for (
int i = 0; i < __kmp_num_proc_groups; ++i)
398 mask[i] = ~(mask[i]);
400 int begin()
const override {
402 while (retval < end() && !is_set(retval))
406 int end()
const override {
return __kmp_num_proc_groups * BITS_PER_MASK_T; }
407 int next(
int previous)
const override {
408 int retval = previous + 1;
409 while (retval < end() && !is_set(retval))
413 int set_system_affinity(
bool abort_on_error)
const override {
414 if (__kmp_num_proc_groups > 1) {
417 int group = get_proc_group();
419 if (abort_on_error) {
420 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
427 ga.Mask = mask[group];
428 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
430 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
431 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
432 DWORD error = GetLastError();
433 if (abort_on_error) {
434 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
440 if (!SetThreadAffinityMask(GetCurrentThread(), *mask)) {
441 DWORD error = GetLastError();
442 if (abort_on_error) {
443 __kmp_fatal(KMP_MSG(CantSetThreadAffMask), KMP_ERR(error),
451 int get_system_affinity(
bool abort_on_error)
override {
452 if (__kmp_num_proc_groups > 1) {
455 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
456 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
457 DWORD error = GetLastError();
458 if (abort_on_error) {
459 __kmp_fatal(KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
460 KMP_ERR(error), __kmp_msg_null);
464 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups) ||
468 mask[ga.Group] = ga.Mask;
470 mask_t newMask, sysMask, retval;
471 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
472 DWORD error = GetLastError();
473 if (abort_on_error) {
474 __kmp_fatal(KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
475 KMP_ERR(error), __kmp_msg_null);
479 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
481 DWORD error = GetLastError();
482 if (abort_on_error) {
483 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
484 KMP_ERR(error), __kmp_msg_null);
488 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
490 DWORD error = GetLastError();
491 if (abort_on_error) {
492 __kmp_fatal(KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
493 KMP_ERR(error), __kmp_msg_null);
500 int get_proc_group()
const override {
502 if (__kmp_num_proc_groups == 1) {
505 for (
int i = 0; i < __kmp_num_proc_groups; i++) {
515 void determine_capable(
const char *env_var)
override {
516 __kmp_affinity_determine_capable(env_var);
518 void bind_thread(
int which)
override { __kmp_affinity_bind_thread(which); }
519 KMPAffinity::Mask *allocate_mask()
override {
return new Mask(); }
520 void deallocate_mask(KMPAffinity::Mask *m)
override {
delete m; }
521 KMPAffinity::Mask *allocate_mask_array(
int num)
override {
522 return new Mask[num];
524 void deallocate_mask_array(KMPAffinity::Mask *array)
override {
525 Mask *windows_array =
static_cast<Mask *
>(array);
526 delete[] windows_array;
528 KMPAffinity::Mask *index_mask_array(KMPAffinity::Mask *array,
529 int index)
override {
530 Mask *windows_array =
static_cast<Mask *
>(array);
531 return &(windows_array[index]);
533 api_type get_api_type()
const override {
return NATIVE_OS; }
540 static const unsigned maxDepth = 32;
541 unsigned labels[maxDepth];
542 unsigned childNums[maxDepth];
545 Address(
unsigned _depth) : depth(_depth), leader(FALSE) {}
546 Address &operator=(
const Address &b) {
548 for (
unsigned i = 0; i < depth; i++) {
549 labels[i] = b.labels[i];
550 childNums[i] = b.childNums[i];
555 bool operator==(
const Address &b)
const {
556 if (depth != b.depth)
558 for (
unsigned i = 0; i < depth; i++)
559 if (labels[i] != b.labels[i])
563 bool isClose(
const Address &b,
int level)
const {
564 if (depth != b.depth)
566 if ((
unsigned)level >= depth)
568 for (
unsigned i = 0; i < (depth - level); i++)
569 if (labels[i] != b.labels[i])
573 bool operator!=(
const Address &b)
const {
return !operator==(b); }
576 printf(
"Depth: %u --- ", depth);
577 for (i = 0; i < depth; i++) {
578 printf(
"%u ", labels[i]);
587 AddrUnsPair(Address _first,
unsigned _second)
588 : first(_first), second(_second) {}
589 AddrUnsPair &operator=(
const AddrUnsPair &b) {
597 printf(
" --- second = %u", second);
599 bool operator==(
const AddrUnsPair &b)
const {
600 if (first != b.first)
602 if (second != b.second)
606 bool operator!=(
const AddrUnsPair &b)
const {
return !operator==(b); }
609 static int __kmp_affinity_cmp_Address_labels(
const void *a,
const void *b) {
610 const Address *aa = &(((
const AddrUnsPair *)a)->first);
611 const Address *bb = &(((
const AddrUnsPair *)b)->first);
612 unsigned depth = aa->depth;
614 KMP_DEBUG_ASSERT(depth == bb->depth);
615 for (i = 0; i < depth; i++) {
616 if (aa->labels[i] < bb->labels[i])
618 if (aa->labels[i] > bb->labels[i])
630 class hierarchy_info {
634 static const kmp_uint32 maxLeaves = 4;
635 static const kmp_uint32 minBranch = 4;
641 kmp_uint32 maxLevels;
648 kmp_uint32 base_num_threads;
649 enum init_status { initialized = 0, not_initialized = 1, initializing = 2 };
650 volatile kmp_int8 uninitialized;
652 volatile kmp_int8 resizing;
658 kmp_uint32 *numPerLevel;
659 kmp_uint32 *skipPerLevel;
661 void deriveLevels(AddrUnsPair *adr2os,
int num_addrs) {
662 int hier_depth = adr2os[0].first.depth;
664 for (
int i = hier_depth - 1; i >= 0; --i) {
666 for (
int j = 0; j < num_addrs; ++j) {
667 int next = adr2os[j].first.childNums[i];
671 numPerLevel[level] = max + 1;
677 : maxLevels(7), depth(1), uninitialized(not_initialized), resizing(0) {}
680 if (!uninitialized && numPerLevel) {
681 __kmp_free(numPerLevel);
683 uninitialized = not_initialized;
687 void init(AddrUnsPair *adr2os,
int num_addrs) {
688 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(
689 &uninitialized, not_initialized, initializing);
690 if (bool_result == 0) {
691 while (TCR_1(uninitialized) != initialized)
695 KMP_DEBUG_ASSERT(bool_result == 1);
705 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
706 skipPerLevel = &(numPerLevel[maxLevels]);
707 for (kmp_uint32 i = 0; i < maxLevels;
715 qsort(adr2os, num_addrs,
sizeof(*adr2os),
716 __kmp_affinity_cmp_Address_labels);
717 deriveLevels(adr2os, num_addrs);
719 numPerLevel[0] = maxLeaves;
720 numPerLevel[1] = num_addrs / maxLeaves;
721 if (num_addrs % maxLeaves)
725 base_num_threads = num_addrs;
726 for (
int i = maxLevels - 1; i >= 0;
728 if (numPerLevel[i] != 1 || depth > 1)
731 kmp_uint32 branch = minBranch;
732 if (numPerLevel[0] == 1)
733 branch = num_addrs / maxLeaves;
734 if (branch < minBranch)
736 for (kmp_uint32 d = 0; d < depth - 1; ++d) {
737 while (numPerLevel[d] > branch ||
738 (d == 0 && numPerLevel[d] > maxLeaves)) {
739 if (numPerLevel[d] & 1)
741 numPerLevel[d] = numPerLevel[d] >> 1;
742 if (numPerLevel[d + 1] == 1)
744 numPerLevel[d + 1] = numPerLevel[d + 1] << 1;
746 if (numPerLevel[0] == 1) {
747 branch = branch >> 1;
753 for (kmp_uint32 i = 1; i < depth; ++i)
754 skipPerLevel[i] = numPerLevel[i - 1] * skipPerLevel[i - 1];
756 for (kmp_uint32 i = depth; i < maxLevels; ++i)
757 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
759 uninitialized = initialized;
763 void resize(kmp_uint32 nproc) {
764 kmp_int8 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
765 while (bool_result == 0) {
767 if (nproc <= base_num_threads)
770 bool_result = KMP_COMPARE_AND_STORE_ACQ8(&resizing, 0, 1);
772 KMP_DEBUG_ASSERT(bool_result != 0);
773 if (nproc <= base_num_threads)
777 kmp_uint32 old_sz = skipPerLevel[depth - 1];
778 kmp_uint32 incs = 0, old_maxLevels = maxLevels;
780 for (kmp_uint32 i = depth; i < maxLevels && nproc > old_sz; ++i) {
781 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
782 numPerLevel[i - 1] *= 2;
786 if (nproc > old_sz) {
787 while (nproc > old_sz) {
795 kmp_uint32 *old_numPerLevel = numPerLevel;
796 kmp_uint32 *old_skipPerLevel = skipPerLevel;
797 numPerLevel = skipPerLevel = NULL;
799 (kmp_uint32 *)__kmp_allocate(maxLevels * 2 *
sizeof(kmp_uint32));
800 skipPerLevel = &(numPerLevel[maxLevels]);
803 for (kmp_uint32 i = 0; i < old_maxLevels;
805 numPerLevel[i] = old_numPerLevel[i];
806 skipPerLevel[i] = old_skipPerLevel[i];
810 for (kmp_uint32 i = old_maxLevels; i < maxLevels;
817 __kmp_free(old_numPerLevel);
821 for (kmp_uint32 i = old_maxLevels; i < maxLevels; ++i)
822 skipPerLevel[i] = 2 * skipPerLevel[i - 1];
824 base_num_threads = nproc;
828 #endif // KMP_AFFINITY_H