14 #include "kmp_affinity.h"
18 #include "kmp_wrapper_getpid.h"
19 #if KMP_USE_HIER_SCHED
20 #include "kmp_dispatch_hier.h"
24 #define HWLOC_GROUP_KIND_INTEL_MODULE 102
25 #define HWLOC_GROUP_KIND_INTEL_TILE 103
26 #define HWLOC_GROUP_KIND_INTEL_DIE 104
27 #define HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP 220
32 kmp_topology_t *__kmp_topology =
nullptr;
34 kmp_hw_subset_t *__kmp_hw_subset =
nullptr;
37 static hierarchy_info machine_hierarchy;
39 void __kmp_cleanup_hierarchy() { machine_hierarchy.fini(); }
41 #if KMP_AFFINITY_SUPPORTED
43 class kmp_full_mask_modifier_t {
44 kmp_affin_mask_t *mask;
47 kmp_full_mask_modifier_t() {
51 ~kmp_full_mask_modifier_t() {
55 void include(
const kmp_affin_mask_t *other) { KMP_CPU_UNION(mask, other); }
58 bool restrict_to_mask() {
60 if (KMP_CPU_EQUAL(__kmp_affin_fullMask, mask) || KMP_CPU_ISEMPTY(mask))
62 return __kmp_topology->restrict_to_mask(mask);
66 static inline const char *
67 __kmp_get_affinity_env_var(
const kmp_affinity_t &affinity,
68 bool for_binding =
false) {
69 if (affinity.flags.omp_places) {
71 return "OMP_PROC_BIND";
74 return affinity.env_var;
78 void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar) {
82 if (TCR_1(machine_hierarchy.uninitialized))
83 machine_hierarchy.init(nproc);
86 if (nproc > machine_hierarchy.base_num_threads)
87 machine_hierarchy.resize(nproc);
89 depth = machine_hierarchy.depth;
90 KMP_DEBUG_ASSERT(depth > 0);
92 thr_bar->depth = depth;
93 __kmp_type_convert(machine_hierarchy.numPerLevel[0] - 1,
94 &(thr_bar->base_leaf_kids));
95 thr_bar->skip_per_level = machine_hierarchy.skipPerLevel;
98 static int nCoresPerPkg, nPackages;
99 static int __kmp_nThreadsPerCore;
100 #ifndef KMP_DFLT_NTH_CORES
101 static int __kmp_ncores;
104 const char *__kmp_hw_get_catalog_string(kmp_hw_t type,
bool plural) {
107 return ((plural) ? KMP_I18N_STR(Sockets) : KMP_I18N_STR(Socket));
109 return ((plural) ? KMP_I18N_STR(Dice) : KMP_I18N_STR(Die));
111 return ((plural) ? KMP_I18N_STR(Modules) : KMP_I18N_STR(Module));
113 return ((plural) ? KMP_I18N_STR(Tiles) : KMP_I18N_STR(Tile));
115 return ((plural) ? KMP_I18N_STR(NumaDomains) : KMP_I18N_STR(NumaDomain));
117 return ((plural) ? KMP_I18N_STR(L3Caches) : KMP_I18N_STR(L3Cache));
119 return ((plural) ? KMP_I18N_STR(L2Caches) : KMP_I18N_STR(L2Cache));
121 return ((plural) ? KMP_I18N_STR(L1Caches) : KMP_I18N_STR(L1Cache));
123 return ((plural) ? KMP_I18N_STR(LLCaches) : KMP_I18N_STR(LLCache));
125 return ((plural) ? KMP_I18N_STR(Cores) : KMP_I18N_STR(Core));
127 return ((plural) ? KMP_I18N_STR(Threads) : KMP_I18N_STR(Thread));
128 case KMP_HW_PROC_GROUP:
129 return ((plural) ? KMP_I18N_STR(ProcGroups) : KMP_I18N_STR(ProcGroup));
132 return KMP_I18N_STR(Unknown);
134 KMP_ASSERT2(
false,
"Unhandled kmp_hw_t enumeration");
135 KMP_BUILTIN_UNREACHABLE;
138 const char *__kmp_hw_get_keyword(kmp_hw_t type,
bool plural) {
141 return ((plural) ?
"sockets" :
"socket");
143 return ((plural) ?
"dice" :
"die");
145 return ((plural) ?
"modules" :
"module");
147 return ((plural) ?
"tiles" :
"tile");
149 return ((plural) ?
"numa_domains" :
"numa_domain");
151 return ((plural) ?
"l3_caches" :
"l3_cache");
153 return ((plural) ?
"l2_caches" :
"l2_cache");
155 return ((plural) ?
"l1_caches" :
"l1_cache");
157 return ((plural) ?
"ll_caches" :
"ll_cache");
159 return ((plural) ?
"cores" :
"core");
161 return ((plural) ?
"threads" :
"thread");
162 case KMP_HW_PROC_GROUP:
163 return ((plural) ?
"proc_groups" :
"proc_group");
166 return ((plural) ?
"unknowns" :
"unknown");
168 KMP_ASSERT2(
false,
"Unhandled kmp_hw_t enumeration");
169 KMP_BUILTIN_UNREACHABLE;
172 const char *__kmp_hw_get_core_type_string(kmp_hw_core_type_t type) {
174 case KMP_HW_CORE_TYPE_UNKNOWN:
175 case KMP_HW_MAX_NUM_CORE_TYPES:
177 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
178 case KMP_HW_CORE_TYPE_ATOM:
179 return "Intel Atom(R) processor";
180 case KMP_HW_CORE_TYPE_CORE:
181 return "Intel(R) Core(TM) processor";
184 KMP_ASSERT2(
false,
"Unhandled kmp_hw_core_type_t enumeration");
185 KMP_BUILTIN_UNREACHABLE;
188 #if KMP_AFFINITY_SUPPORTED
191 #define KMP_AFF_WARNING(s, ...) \
192 if (s.flags.verbose || (s.flags.warnings && (s.type != affinity_none))) { \
193 KMP_WARNING(__VA_ARGS__); \
196 #define KMP_AFF_WARNING(s, ...) KMP_WARNING(__VA_ARGS__)
201 int kmp_hw_thread_t::compare_ids(
const void *a,
const void *b) {
202 const kmp_hw_thread_t *ahwthread = (
const kmp_hw_thread_t *)a;
203 const kmp_hw_thread_t *bhwthread = (
const kmp_hw_thread_t *)b;
204 int depth = __kmp_topology->get_depth();
205 for (
int level = 0; level < depth; ++level) {
208 if (__kmp_is_hybrid_cpu() &&
209 __kmp_topology->get_type(level) == KMP_HW_CORE &&
210 ahwthread->attrs.is_core_eff_valid() &&
211 bhwthread->attrs.is_core_eff_valid()) {
212 if (ahwthread->attrs.get_core_eff() < bhwthread->attrs.get_core_eff())
214 if (ahwthread->attrs.get_core_eff() > bhwthread->attrs.get_core_eff())
217 if (ahwthread->ids[level] == bhwthread->ids[level])
221 if (ahwthread->ids[level] == UNKNOWN_ID)
223 else if (bhwthread->ids[level] == UNKNOWN_ID)
225 else if (ahwthread->ids[level] < bhwthread->ids[level])
227 else if (ahwthread->ids[level] > bhwthread->ids[level])
230 if (ahwthread->os_id < bhwthread->os_id)
232 else if (ahwthread->os_id > bhwthread->os_id)
237 #if KMP_AFFINITY_SUPPORTED
238 int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b) {
240 const kmp_hw_thread_t *aa = (
const kmp_hw_thread_t *)a;
241 const kmp_hw_thread_t *bb = (
const kmp_hw_thread_t *)b;
242 int depth = __kmp_topology->get_depth();
243 int compact = __kmp_topology->compact;
244 KMP_DEBUG_ASSERT(compact >= 0);
245 KMP_DEBUG_ASSERT(compact <= depth);
246 for (i = 0; i < compact; i++) {
247 int j = depth - i - 1;
248 if (aa->sub_ids[j] < bb->sub_ids[j])
250 if (aa->sub_ids[j] > bb->sub_ids[j])
253 for (; i < depth; i++) {
255 if (aa->sub_ids[j] < bb->sub_ids[j])
257 if (aa->sub_ids[j] > bb->sub_ids[j])
264 void kmp_hw_thread_t::print()
const {
265 int depth = __kmp_topology->get_depth();
266 printf(
"%4d ", os_id);
267 for (
int i = 0; i < depth; ++i) {
268 printf(
"%4d (%d) ", ids[i], sub_ids[i]);
271 if (attrs.is_core_type_valid())
272 printf(
" (%s)", __kmp_hw_get_core_type_string(attrs.get_core_type()));
273 if (attrs.is_core_eff_valid())
274 printf(
" (eff=%d)", attrs.get_core_eff());
286 void kmp_topology_t::insert_layer(kmp_hw_t type,
const int *ids) {
290 int previous_id = kmp_hw_thread_t::UNKNOWN_ID;
291 int previous_new_id = kmp_hw_thread_t::UNKNOWN_ID;
295 for (target_layer = 0; target_layer < depth; ++target_layer) {
296 bool layers_equal =
true;
297 bool strictly_above_target_layer =
false;
298 for (
int i = 0; i < num_hw_threads; ++i) {
299 int id = hw_threads[i].ids[target_layer];
301 if (
id != previous_id && new_id == previous_new_id) {
303 strictly_above_target_layer =
true;
304 layers_equal =
false;
306 }
else if (
id == previous_id && new_id != previous_new_id) {
308 layers_equal =
false;
312 previous_new_id = new_id;
314 if (strictly_above_target_layer || layers_equal)
320 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
322 types[target_layer] = type;
323 for (
int k = 0; k < num_hw_threads; ++k) {
324 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
325 hw_threads[k].ids[j] = hw_threads[k].ids[i];
326 hw_threads[k].ids[target_layer] = ids[k];
328 equivalent[type] = type;
332 #if KMP_GROUP_AFFINITY
334 void kmp_topology_t::_insert_windows_proc_groups() {
336 if (__kmp_num_proc_groups == 1)
338 kmp_affin_mask_t *mask;
339 int *ids = (
int *)__kmp_allocate(
sizeof(
int) * num_hw_threads);
341 for (
int i = 0; i < num_hw_threads; ++i) {
343 KMP_CPU_SET(hw_threads[i].os_id, mask);
344 ids[i] = __kmp_get_proc_group(mask);
347 insert_layer(KMP_HW_PROC_GROUP, ids);
351 __kmp_topology->sort_ids();
357 void kmp_topology_t::_remove_radix1_layers() {
358 int preference[KMP_HW_LAST];
359 int top_index1, top_index2;
361 preference[KMP_HW_SOCKET] = 110;
362 preference[KMP_HW_PROC_GROUP] = 100;
363 preference[KMP_HW_CORE] = 95;
364 preference[KMP_HW_THREAD] = 90;
365 preference[KMP_HW_NUMA] = 85;
366 preference[KMP_HW_DIE] = 80;
367 preference[KMP_HW_TILE] = 75;
368 preference[KMP_HW_MODULE] = 73;
369 preference[KMP_HW_L3] = 70;
370 preference[KMP_HW_L2] = 65;
371 preference[KMP_HW_L1] = 60;
372 preference[KMP_HW_LLC] = 5;
375 while (top_index1 < depth - 1 && top_index2 < depth) {
376 kmp_hw_t type1 = types[top_index1];
377 kmp_hw_t type2 = types[top_index2];
378 KMP_ASSERT_VALID_HW_TYPE(type1);
379 KMP_ASSERT_VALID_HW_TYPE(type2);
382 if ((type1 == KMP_HW_THREAD || type1 == KMP_HW_CORE ||
383 type1 == KMP_HW_SOCKET) &&
384 (type2 == KMP_HW_THREAD || type2 == KMP_HW_CORE ||
385 type2 == KMP_HW_SOCKET)) {
386 top_index1 = top_index2++;
390 bool all_same =
true;
391 int id1 = hw_threads[0].ids[top_index1];
392 int id2 = hw_threads[0].ids[top_index2];
393 int pref1 = preference[type1];
394 int pref2 = preference[type2];
395 for (
int hwidx = 1; hwidx < num_hw_threads; ++hwidx) {
396 if (hw_threads[hwidx].ids[top_index1] == id1 &&
397 hw_threads[hwidx].ids[top_index2] != id2) {
401 if (hw_threads[hwidx].ids[top_index2] != id2)
403 id1 = hw_threads[hwidx].ids[top_index1];
404 id2 = hw_threads[hwidx].ids[top_index2];
408 kmp_hw_t remove_type, keep_type;
409 int remove_layer, remove_layer_ids;
412 remove_layer = remove_layer_ids = top_index2;
416 remove_layer = remove_layer_ids = top_index1;
422 remove_layer_ids = top_index2;
425 set_equivalent_type(remove_type, keep_type);
426 for (
int idx = 0; idx < num_hw_threads; ++idx) {
427 kmp_hw_thread_t &hw_thread = hw_threads[idx];
428 for (
int d = remove_layer_ids; d < depth - 1; ++d)
429 hw_thread.ids[d] = hw_thread.ids[d + 1];
431 for (
int idx = remove_layer; idx < depth - 1; ++idx)
432 types[idx] = types[idx + 1];
435 top_index1 = top_index2++;
438 KMP_ASSERT(depth > 0);
441 void kmp_topology_t::_set_last_level_cache() {
442 if (get_equivalent_type(KMP_HW_L3) != KMP_HW_UNKNOWN)
443 set_equivalent_type(KMP_HW_LLC, KMP_HW_L3);
444 else if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
445 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
446 #if KMP_MIC_SUPPORTED
447 else if (__kmp_mic_type == mic3) {
448 if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
449 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
450 else if (get_equivalent_type(KMP_HW_TILE) != KMP_HW_UNKNOWN)
451 set_equivalent_type(KMP_HW_LLC, KMP_HW_TILE);
454 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
457 else if (get_equivalent_type(KMP_HW_L1) != KMP_HW_UNKNOWN)
458 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
460 if (get_equivalent_type(KMP_HW_LLC) == KMP_HW_UNKNOWN) {
461 if (get_equivalent_type(KMP_HW_SOCKET) != KMP_HW_UNKNOWN)
462 set_equivalent_type(KMP_HW_LLC, KMP_HW_SOCKET);
463 else if (get_equivalent_type(KMP_HW_CORE) != KMP_HW_UNKNOWN)
464 set_equivalent_type(KMP_HW_LLC, KMP_HW_CORE);
466 KMP_ASSERT(get_equivalent_type(KMP_HW_LLC) != KMP_HW_UNKNOWN);
470 void kmp_topology_t::_gather_enumeration_information() {
471 int previous_id[KMP_HW_LAST];
472 int max[KMP_HW_LAST];
474 for (
int i = 0; i < depth; ++i) {
475 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
480 int core_level = get_level(KMP_HW_CORE);
481 for (
int i = 0; i < num_hw_threads; ++i) {
482 kmp_hw_thread_t &hw_thread = hw_threads[i];
483 for (
int layer = 0; layer < depth; ++layer) {
484 int id = hw_thread.ids[layer];
485 if (
id != previous_id[layer]) {
487 for (
int l = layer; l < depth; ++l) {
488 if (hw_thread.ids[l] != kmp_hw_thread_t::UNKNOWN_ID)
492 if (hw_thread.ids[layer] != kmp_hw_thread_t::UNKNOWN_ID)
494 for (
int l = layer + 1; l < depth; ++l) {
495 if (max[l] > ratio[l])
501 if (__kmp_is_hybrid_cpu() && core_level >= 0 && layer <= core_level) {
502 if (hw_thread.attrs.is_core_eff_valid() &&
503 hw_thread.attrs.core_eff >= num_core_efficiencies) {
506 num_core_efficiencies = hw_thread.attrs.core_eff + 1;
508 if (hw_thread.attrs.is_core_type_valid()) {
510 for (
int j = 0; j < num_core_types; ++j) {
511 if (hw_thread.attrs.get_core_type() == core_types[j]) {
517 KMP_ASSERT(num_core_types < KMP_HW_MAX_NUM_CORE_TYPES);
518 core_types[num_core_types++] = hw_thread.attrs.get_core_type();
525 for (
int layer = 0; layer < depth; ++layer) {
526 previous_id[layer] = hw_thread.ids[layer];
529 for (
int layer = 0; layer < depth; ++layer) {
530 if (max[layer] > ratio[layer])
531 ratio[layer] = max[layer];
535 int kmp_topology_t::_get_ncores_with_attr(
const kmp_hw_attr_t &attr,
537 bool find_all)
const {
538 int current, current_max;
539 int previous_id[KMP_HW_LAST];
540 for (
int i = 0; i < depth; ++i)
541 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
542 int core_level = get_level(KMP_HW_CORE);
545 KMP_ASSERT(above_level < core_level);
548 for (
int i = 0; i < num_hw_threads; ++i) {
549 kmp_hw_thread_t &hw_thread = hw_threads[i];
550 if (!find_all && hw_thread.ids[above_level] != previous_id[above_level]) {
551 if (current > current_max)
552 current_max = current;
553 current = hw_thread.attrs.contains(attr);
555 for (
int level = above_level + 1; level <= core_level; ++level) {
556 if (hw_thread.ids[level] != previous_id[level]) {
557 if (hw_thread.attrs.contains(attr))
563 for (
int level = 0; level < depth; ++level)
564 previous_id[level] = hw_thread.ids[level];
566 if (current > current_max)
567 current_max = current;
572 void kmp_topology_t::_discover_uniformity() {
574 for (
int level = 0; level < depth; ++level)
576 flags.uniform = (num == count[depth - 1]);
580 void kmp_topology_t::_set_sub_ids() {
581 int previous_id[KMP_HW_LAST];
582 int sub_id[KMP_HW_LAST];
584 for (
int i = 0; i < depth; ++i) {
588 for (
int i = 0; i < num_hw_threads; ++i) {
589 kmp_hw_thread_t &hw_thread = hw_threads[i];
591 for (
int j = 0; j < depth; ++j) {
592 if (hw_thread.ids[j] != previous_id[j]) {
594 for (
int k = j + 1; k < depth; ++k) {
601 for (
int j = 0; j < depth; ++j) {
602 previous_id[j] = hw_thread.ids[j];
605 for (
int j = 0; j < depth; ++j) {
606 hw_thread.sub_ids[j] = sub_id[j];
611 void kmp_topology_t::_set_globals() {
613 int core_level, thread_level, package_level;
614 package_level = get_level(KMP_HW_SOCKET);
615 #if KMP_GROUP_AFFINITY
616 if (package_level == -1)
617 package_level = get_level(KMP_HW_PROC_GROUP);
619 core_level = get_level(KMP_HW_CORE);
620 thread_level = get_level(KMP_HW_THREAD);
622 KMP_ASSERT(core_level != -1);
623 KMP_ASSERT(thread_level != -1);
625 __kmp_nThreadsPerCore = calculate_ratio(thread_level, core_level);
626 if (package_level != -1) {
627 nCoresPerPkg = calculate_ratio(core_level, package_level);
628 nPackages = get_count(package_level);
631 nCoresPerPkg = get_count(core_level);
634 #ifndef KMP_DFLT_NTH_CORES
635 __kmp_ncores = get_count(core_level);
639 kmp_topology_t *kmp_topology_t::allocate(
int nproc,
int ndepth,
640 const kmp_hw_t *types) {
641 kmp_topology_t *retval;
643 size_t size =
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc +
644 sizeof(int) * (
size_t)KMP_HW_LAST * 3;
645 char *bytes = (
char *)__kmp_allocate(size);
646 retval = (kmp_topology_t *)bytes;
648 retval->hw_threads = (kmp_hw_thread_t *)(bytes +
sizeof(kmp_topology_t));
650 retval->hw_threads =
nullptr;
652 retval->num_hw_threads = nproc;
653 retval->depth = ndepth;
655 (
int *)(bytes +
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc);
656 retval->types = (kmp_hw_t *)arr;
657 retval->ratio = arr + (size_t)KMP_HW_LAST;
658 retval->count = arr + 2 * (size_t)KMP_HW_LAST;
659 retval->num_core_efficiencies = 0;
660 retval->num_core_types = 0;
662 for (
int i = 0; i < KMP_HW_MAX_NUM_CORE_TYPES; ++i)
663 retval->core_types[i] = KMP_HW_CORE_TYPE_UNKNOWN;
664 KMP_FOREACH_HW_TYPE(type) { retval->equivalent[type] = KMP_HW_UNKNOWN; }
665 for (
int i = 0; i < ndepth; ++i) {
666 retval->types[i] = types[i];
667 retval->equivalent[types[i]] = types[i];
672 void kmp_topology_t::deallocate(kmp_topology_t *topology) {
674 __kmp_free(topology);
677 bool kmp_topology_t::check_ids()
const {
679 if (num_hw_threads == 0)
681 for (
int i = 1; i < num_hw_threads; ++i) {
682 kmp_hw_thread_t ¤t_thread = hw_threads[i];
683 kmp_hw_thread_t &previous_thread = hw_threads[i - 1];
685 for (
int j = 0; j < depth; ++j) {
686 if (previous_thread.ids[j] != current_thread.ids[j]) {
698 void kmp_topology_t::dump()
const {
699 printf(
"***********************\n");
700 printf(
"*** __kmp_topology: ***\n");
701 printf(
"***********************\n");
702 printf(
"* depth: %d\n", depth);
705 for (
int i = 0; i < depth; ++i)
706 printf(
"%15s ", __kmp_hw_get_keyword(types[i]));
710 for (
int i = 0; i < depth; ++i) {
711 printf(
"%15d ", ratio[i]);
716 for (
int i = 0; i < depth; ++i) {
717 printf(
"%15d ", count[i]);
721 printf(
"* num_core_eff: %d\n", num_core_efficiencies);
722 printf(
"* num_core_types: %d\n", num_core_types);
723 printf(
"* core_types: ");
724 for (
int i = 0; i < num_core_types; ++i)
725 printf(
"%3d ", core_types[i]);
728 printf(
"* equivalent map:\n");
729 KMP_FOREACH_HW_TYPE(i) {
730 const char *key = __kmp_hw_get_keyword(i);
731 const char *value = __kmp_hw_get_keyword(equivalent[i]);
732 printf(
"%-15s -> %-15s\n", key, value);
735 printf(
"* uniform: %s\n", (is_uniform() ?
"Yes" :
"No"));
737 printf(
"* num_hw_threads: %d\n", num_hw_threads);
738 printf(
"* hw_threads:\n");
739 for (
int i = 0; i < num_hw_threads; ++i) {
740 hw_threads[i].print();
742 printf(
"***********************\n");
745 void kmp_topology_t::print(
const char *env_var)
const {
747 int print_types_depth;
748 __kmp_str_buf_init(&buf);
749 kmp_hw_t print_types[KMP_HW_LAST + 2];
752 if (num_hw_threads) {
753 KMP_INFORM(AvailableOSProc, env_var, num_hw_threads);
755 KMP_INFORM(AvailableOSProc, env_var, __kmp_xproc);
760 KMP_INFORM(Uniform, env_var);
762 KMP_INFORM(NonUniform, env_var);
766 KMP_FOREACH_HW_TYPE(type) {
767 kmp_hw_t eq_type = equivalent[type];
768 if (eq_type != KMP_HW_UNKNOWN && eq_type != type) {
769 KMP_INFORM(AffEqualTopologyTypes, env_var,
770 __kmp_hw_get_catalog_string(type),
771 __kmp_hw_get_catalog_string(eq_type));
776 KMP_ASSERT(depth > 0 && depth <= (
int)KMP_HW_LAST);
779 print_types_depth = 0;
780 for (
int level = 0; level < depth; ++level)
781 print_types[print_types_depth++] = types[level];
782 if (equivalent[KMP_HW_CORE] != KMP_HW_CORE) {
784 if (print_types[print_types_depth - 1] == KMP_HW_THREAD) {
787 print_types[print_types_depth - 1] = KMP_HW_CORE;
788 print_types[print_types_depth++] = KMP_HW_THREAD;
790 print_types[print_types_depth++] = KMP_HW_CORE;
794 if (equivalent[KMP_HW_THREAD] != KMP_HW_THREAD)
795 print_types[print_types_depth++] = KMP_HW_THREAD;
797 __kmp_str_buf_clear(&buf);
798 kmp_hw_t numerator_type;
799 kmp_hw_t denominator_type = KMP_HW_UNKNOWN;
800 int core_level = get_level(KMP_HW_CORE);
801 int ncores = get_count(core_level);
803 for (
int plevel = 0, level = 0; plevel < print_types_depth; ++plevel) {
806 numerator_type = print_types[plevel];
807 KMP_ASSERT_VALID_HW_TYPE(numerator_type);
808 if (equivalent[numerator_type] != numerator_type)
811 c = get_ratio(level++);
814 __kmp_str_buf_print(&buf,
"%d %s", c,
815 __kmp_hw_get_catalog_string(numerator_type, plural));
817 __kmp_str_buf_print(&buf,
" x %d %s/%s", c,
818 __kmp_hw_get_catalog_string(numerator_type, plural),
819 __kmp_hw_get_catalog_string(denominator_type));
821 denominator_type = numerator_type;
823 KMP_INFORM(TopologyGeneric, env_var, buf.str, ncores);
826 if (__kmp_is_hybrid_cpu()) {
827 for (
int i = 0; i < num_core_types; ++i) {
828 kmp_hw_core_type_t core_type = core_types[i];
831 attr.set_core_type(core_type);
832 int ncores = get_ncores_with_attr(attr);
834 KMP_INFORM(TopologyHybrid, env_var, ncores,
835 __kmp_hw_get_core_type_string(core_type));
836 KMP_ASSERT(num_core_efficiencies <= KMP_HW_MAX_NUM_CORE_EFFS)
837 for (
int eff = 0; eff < num_core_efficiencies; ++eff) {
838 attr.set_core_eff(eff);
839 int ncores_with_eff = get_ncores_with_attr(attr);
840 if (ncores_with_eff > 0) {
841 KMP_INFORM(TopologyHybridCoreEff, env_var, ncores_with_eff, eff);
848 if (num_hw_threads <= 0) {
849 __kmp_str_buf_free(&buf);
854 KMP_INFORM(OSProcToPhysicalThreadMap, env_var);
855 for (
int i = 0; i < num_hw_threads; i++) {
856 __kmp_str_buf_clear(&buf);
857 for (
int level = 0; level < depth; ++level) {
858 if (hw_threads[i].ids[level] == kmp_hw_thread_t::UNKNOWN_ID)
860 kmp_hw_t type = types[level];
861 __kmp_str_buf_print(&buf,
"%s ", __kmp_hw_get_catalog_string(type));
862 __kmp_str_buf_print(&buf,
"%d ", hw_threads[i].ids[level]);
864 if (__kmp_is_hybrid_cpu())
867 __kmp_hw_get_core_type_string(hw_threads[i].attrs.get_core_type()));
868 KMP_INFORM(OSProcMapToPack, env_var, hw_threads[i].os_id, buf.str);
871 __kmp_str_buf_free(&buf);
874 #if KMP_AFFINITY_SUPPORTED
875 void kmp_topology_t::set_granularity(kmp_affinity_t &affinity)
const {
876 const char *env_var = __kmp_get_affinity_env_var(affinity);
880 if (!__kmp_is_hybrid_cpu()) {
881 if (affinity.core_attr_gran.valid) {
885 affinity, AffIgnoringNonHybrid, env_var,
886 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true));
887 affinity.gran = KMP_HW_CORE;
888 affinity.gran_levels = -1;
889 affinity.core_attr_gran = KMP_AFFINITY_ATTRS_UNKNOWN;
890 affinity.flags.core_types_gran = affinity.flags.core_effs_gran = 0;
891 }
else if (affinity.flags.core_types_gran ||
892 affinity.flags.core_effs_gran) {
894 if (affinity.flags.omp_places) {
896 affinity, AffIgnoringNonHybrid, env_var,
897 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true));
900 KMP_AFF_WARNING(affinity, AffGranularityBad, env_var,
901 "Intel(R) Hybrid Technology core attribute",
902 __kmp_hw_get_catalog_string(KMP_HW_CORE));
904 affinity.gran = KMP_HW_CORE;
905 affinity.gran_levels = -1;
906 affinity.core_attr_gran = KMP_AFFINITY_ATTRS_UNKNOWN;
907 affinity.flags.core_types_gran = affinity.flags.core_effs_gran = 0;
911 if (affinity.gran_levels < 0) {
912 kmp_hw_t gran_type = get_equivalent_type(affinity.gran);
914 if (gran_type == KMP_HW_UNKNOWN) {
916 kmp_hw_t gran_types[3] = {KMP_HW_CORE, KMP_HW_THREAD, KMP_HW_SOCKET};
917 for (
auto g : gran_types) {
918 if (get_equivalent_type(g) != KMP_HW_UNKNOWN) {
923 KMP_ASSERT(gran_type != KMP_HW_UNKNOWN);
925 KMP_AFF_WARNING(affinity, AffGranularityBad, env_var,
926 __kmp_hw_get_catalog_string(affinity.gran),
927 __kmp_hw_get_catalog_string(gran_type));
928 affinity.gran = gran_type;
930 #if KMP_GROUP_AFFINITY
938 if (__kmp_num_proc_groups > 1) {
939 int gran_depth = get_level(gran_type);
940 int proc_group_depth = get_level(KMP_HW_PROC_GROUP);
941 if (gran_depth >= 0 && proc_group_depth >= 0 &&
942 gran_depth < proc_group_depth) {
943 KMP_AFF_WARNING(affinity, AffGranTooCoarseProcGroup, env_var,
944 __kmp_hw_get_catalog_string(affinity.gran));
945 affinity.gran = gran_type = KMP_HW_PROC_GROUP;
949 affinity.gran_levels = 0;
950 for (
int i = depth - 1; i >= 0 && get_type(i) != gran_type; --i)
951 affinity.gran_levels++;
956 void kmp_topology_t::canonicalize() {
957 #if KMP_GROUP_AFFINITY
958 _insert_windows_proc_groups();
960 _remove_radix1_layers();
961 _gather_enumeration_information();
962 _discover_uniformity();
965 _set_last_level_cache();
967 #if KMP_MIC_SUPPORTED
969 if (__kmp_mic_type == mic3) {
970 if (get_level(KMP_HW_L2) != -1)
971 set_equivalent_type(KMP_HW_TILE, KMP_HW_L2);
972 else if (get_level(KMP_HW_TILE) != -1)
973 set_equivalent_type(KMP_HW_L2, KMP_HW_TILE);
978 KMP_ASSERT(depth > 0);
979 for (
int level = 0; level < depth; ++level) {
981 KMP_ASSERT(count[level] > 0 && ratio[level] > 0);
982 KMP_ASSERT_VALID_HW_TYPE(types[level]);
984 KMP_ASSERT(equivalent[types[level]] == types[level]);
989 void kmp_topology_t::canonicalize(
int npackages,
int ncores_per_pkg,
990 int nthreads_per_core,
int ncores) {
993 KMP_FOREACH_HW_TYPE(i) { equivalent[i] = KMP_HW_UNKNOWN; }
994 for (
int level = 0; level < depth; ++level) {
998 count[0] = npackages;
1000 count[2] = __kmp_xproc;
1001 ratio[0] = npackages;
1002 ratio[1] = ncores_per_pkg;
1003 ratio[2] = nthreads_per_core;
1004 equivalent[KMP_HW_SOCKET] = KMP_HW_SOCKET;
1005 equivalent[KMP_HW_CORE] = KMP_HW_CORE;
1006 equivalent[KMP_HW_THREAD] = KMP_HW_THREAD;
1007 types[0] = KMP_HW_SOCKET;
1008 types[1] = KMP_HW_CORE;
1009 types[2] = KMP_HW_THREAD;
1011 _discover_uniformity();
1014 #if KMP_AFFINITY_SUPPORTED
1015 static kmp_str_buf_t *
1016 __kmp_hw_get_catalog_core_string(
const kmp_hw_attr_t &attr, kmp_str_buf_t *buf,
1018 __kmp_str_buf_init(buf);
1019 if (attr.is_core_type_valid())
1020 __kmp_str_buf_print(buf,
"%s %s",
1021 __kmp_hw_get_core_type_string(attr.get_core_type()),
1022 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural));
1024 __kmp_str_buf_print(buf,
"%s eff=%d",
1025 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural),
1026 attr.get_core_eff());
1030 bool kmp_topology_t::restrict_to_mask(
const kmp_affin_mask_t *mask) {
1034 for (
int i = 0; i < num_hw_threads; ++i) {
1035 int os_id = hw_threads[i].os_id;
1036 if (KMP_CPU_ISSET(os_id, mask)) {
1038 hw_threads[new_index] = hw_threads[i];
1041 KMP_CPU_CLR(os_id, __kmp_affin_fullMask);
1046 KMP_DEBUG_ASSERT(new_index <= num_hw_threads);
1047 affected = (num_hw_threads != new_index);
1048 num_hw_threads = new_index;
1052 _gather_enumeration_information();
1053 _discover_uniformity();
1055 _set_last_level_cache();
1058 if (__kmp_num_proc_groups <= 1)
1060 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
1068 bool kmp_topology_t::filter_hw_subset() {
1070 if (!__kmp_hw_subset)
1074 __kmp_hw_subset->sort();
1076 __kmp_hw_subset->canonicalize(__kmp_topology);
1079 bool using_core_types =
false;
1080 bool using_core_effs =
false;
1081 bool is_absolute = __kmp_hw_subset->is_absolute();
1082 int hw_subset_depth = __kmp_hw_subset->get_depth();
1083 kmp_hw_t specified[KMP_HW_LAST];
1084 int *topology_levels = (
int *)KMP_ALLOCA(
sizeof(
int) * hw_subset_depth);
1085 KMP_ASSERT(hw_subset_depth > 0);
1086 KMP_FOREACH_HW_TYPE(i) { specified[i] = KMP_HW_UNKNOWN; }
1087 int core_level = get_level(KMP_HW_CORE);
1088 for (
int i = 0; i < hw_subset_depth; ++i) {
1090 const kmp_hw_subset_t::item_t &item = __kmp_hw_subset->at(i);
1091 int num = item.num[0];
1092 int offset = item.offset[0];
1093 kmp_hw_t type = item.type;
1094 kmp_hw_t equivalent_type = equivalent[type];
1095 int level = get_level(type);
1096 topology_levels[i] = level;
1099 if (equivalent_type != KMP_HW_UNKNOWN) {
1100 __kmp_hw_subset->at(i).type = equivalent_type;
1102 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetNotExistGeneric,
1103 __kmp_hw_get_catalog_string(type));
1109 if (specified[equivalent_type] != KMP_HW_UNKNOWN) {
1110 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetEqvLayers,
1111 __kmp_hw_get_catalog_string(type),
1112 __kmp_hw_get_catalog_string(specified[equivalent_type]));
1115 specified[equivalent_type] = type;
1118 max_count = get_ratio(level);
1120 if (max_count < 0 ||
1121 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1122 bool plural = (num > 1);
1123 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric,
1124 __kmp_hw_get_catalog_string(type, plural));
1130 if (core_level == level) {
1132 for (
int j = 0; j < item.num_attrs; ++j) {
1133 if (item.attr[j].is_core_type_valid())
1134 using_core_types =
true;
1135 if (item.attr[j].is_core_eff_valid())
1136 using_core_effs =
true;
1144 if ((using_core_effs || using_core_types) && !__kmp_is_hybrid_cpu()) {
1145 if (item.num_attrs == 1) {
1146 if (using_core_effs) {
1147 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1150 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1153 using_core_effs =
false;
1154 using_core_types =
false;
1156 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrsNonHybrid);
1162 if (using_core_types && using_core_effs) {
1163 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
"core_type",
1169 if (using_core_effs) {
1170 for (
int j = 0; j < item.num_attrs; ++j) {
1171 if (item.attr[j].is_core_eff_valid()) {
1172 int core_eff = item.attr[j].get_core_eff();
1173 if (core_eff < 0 || core_eff >= num_core_efficiencies) {
1175 __kmp_str_buf_init(&buf);
1176 __kmp_str_buf_print(&buf,
"%d", item.attr[j].get_core_eff());
1177 __kmp_msg(kmp_ms_warning,
1178 KMP_MSG(AffHWSubsetAttrInvalid,
"efficiency", buf.str),
1179 KMP_HNT(ValidValuesRange, 0, num_core_efficiencies - 1),
1181 __kmp_str_buf_free(&buf);
1189 if ((using_core_types || using_core_effs) && !is_absolute) {
1190 for (
int j = 0; j < item.num_attrs; ++j) {
1191 int num = item.num[j];
1192 int offset = item.offset[j];
1193 int level_above = core_level - 1;
1194 if (level_above >= 0) {
1195 max_count = get_ncores_with_attr_per(item.attr[j], level_above);
1196 if (max_count <= 0 ||
1197 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1199 __kmp_hw_get_catalog_core_string(item.attr[j], &buf, num > 0);
1200 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric, buf.str);
1201 __kmp_str_buf_free(&buf);
1208 if ((using_core_types || using_core_effs) && item.num_attrs > 1) {
1209 for (
int j = 0; j < item.num_attrs; ++j) {
1212 if (!item.attr[j]) {
1213 kmp_hw_attr_t other_attr;
1214 for (
int k = 0; k < item.num_attrs; ++k) {
1215 if (item.attr[k] != item.attr[j]) {
1216 other_attr = item.attr[k];
1221 __kmp_hw_get_catalog_core_string(other_attr, &buf, item.num[j] > 0);
1222 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
1223 __kmp_hw_get_catalog_string(KMP_HW_CORE), buf.str);
1224 __kmp_str_buf_free(&buf);
1228 for (
int k = 0; k < j; ++k) {
1229 if (!item.attr[j] || !item.attr[k])
1231 if (item.attr[k] == item.attr[j]) {
1233 __kmp_hw_get_catalog_core_string(item.attr[j], &buf,
1235 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrRepeat, buf.str);
1236 __kmp_str_buf_free(&buf);
1247 int prev_sub_ids[KMP_HW_LAST];
1248 int abs_sub_ids[KMP_HW_LAST];
1249 int core_eff_sub_ids[KMP_HW_MAX_NUM_CORE_EFFS];
1250 int core_type_sub_ids[KMP_HW_MAX_NUM_CORE_TYPES];
1251 for (
size_t i = 0; i < KMP_HW_LAST; ++i) {
1252 abs_sub_ids[i] = -1;
1253 prev_sub_ids[i] = -1;
1255 for (
size_t i = 0; i < KMP_HW_MAX_NUM_CORE_EFFS; ++i)
1256 core_eff_sub_ids[i] = -1;
1257 for (
size_t i = 0; i < KMP_HW_MAX_NUM_CORE_TYPES; ++i)
1258 core_type_sub_ids[i] = -1;
1263 auto is_targeted = [&](
int level) {
1265 for (
int i = 0; i < hw_subset_depth; ++i)
1266 if (topology_levels[i] == level)
1275 auto get_core_type_index = [](
const kmp_hw_thread_t &t) {
1276 switch (t.attrs.get_core_type()) {
1277 case KMP_HW_CORE_TYPE_UNKNOWN:
1278 case KMP_HW_MAX_NUM_CORE_TYPES:
1280 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1281 case KMP_HW_CORE_TYPE_ATOM:
1283 case KMP_HW_CORE_TYPE_CORE:
1287 KMP_ASSERT2(
false,
"Unhandled kmp_hw_thread_t enumeration");
1288 KMP_BUILTIN_UNREACHABLE;
1292 auto get_core_eff_index = [](
const kmp_hw_thread_t &t) {
1293 return t.attrs.get_core_eff();
1296 int num_filtered = 0;
1297 kmp_affin_mask_t *filtered_mask;
1298 KMP_CPU_ALLOC(filtered_mask);
1299 KMP_CPU_COPY(filtered_mask, __kmp_affin_fullMask);
1300 for (
int i = 0; i < num_hw_threads; ++i) {
1301 kmp_hw_thread_t &hw_thread = hw_threads[i];
1304 if (is_absolute || using_core_effs || using_core_types) {
1305 for (
int level = 0; level < get_depth(); ++level) {
1306 if (hw_thread.sub_ids[level] != prev_sub_ids[level]) {
1307 bool found_targeted =
false;
1308 for (
int j = level; j < get_depth(); ++j) {
1309 bool targeted = is_targeted(j);
1310 if (!found_targeted && targeted) {
1311 found_targeted =
true;
1313 if (j == core_level && using_core_effs)
1314 core_eff_sub_ids[get_core_eff_index(hw_thread)]++;
1315 if (j == core_level && using_core_types)
1316 core_type_sub_ids[get_core_type_index(hw_thread)]++;
1317 }
else if (targeted) {
1319 if (j == core_level && using_core_effs)
1320 core_eff_sub_ids[get_core_eff_index(hw_thread)] = 0;
1321 if (j == core_level && using_core_types)
1322 core_type_sub_ids[get_core_type_index(hw_thread)] = 0;
1328 for (
int level = 0; level < get_depth(); ++level)
1329 prev_sub_ids[level] = hw_thread.sub_ids[level];
1333 bool should_be_filtered =
false;
1334 for (
int hw_subset_index = 0; hw_subset_index < hw_subset_depth;
1335 ++hw_subset_index) {
1336 const auto &hw_subset_item = __kmp_hw_subset->at(hw_subset_index);
1337 int level = topology_levels[hw_subset_index];
1340 if ((using_core_effs || using_core_types) && level == core_level) {
1346 kmp_hw_core_type_t core_type = hw_thread.attrs.get_core_type();
1347 int core_eff = hw_thread.attrs.get_core_eff();
1348 for (attr_idx = 0; attr_idx < hw_subset_item.num_attrs; ++attr_idx) {
1349 if (using_core_types &&
1350 hw_subset_item.attr[attr_idx].get_core_type() == core_type)
1352 if (using_core_effs &&
1353 hw_subset_item.attr[attr_idx].get_core_eff() == core_eff)
1357 if (attr_idx == hw_subset_item.num_attrs) {
1358 should_be_filtered =
true;
1362 int num = hw_subset_item.num[attr_idx];
1363 int offset = hw_subset_item.offset[attr_idx];
1364 if (using_core_types)
1365 sub_id = core_type_sub_ids[get_core_type_index(hw_thread)];
1367 sub_id = core_eff_sub_ids[get_core_eff_index(hw_thread)];
1368 if (sub_id < offset ||
1369 (num != kmp_hw_subset_t::USE_ALL && sub_id >= offset + num)) {
1370 should_be_filtered =
true;
1375 int num = hw_subset_item.num[0];
1376 int offset = hw_subset_item.offset[0];
1378 sub_id = abs_sub_ids[level];
1380 sub_id = hw_thread.sub_ids[level];
1381 if (hw_thread.ids[level] == kmp_hw_thread_t::UNKNOWN_ID ||
1383 (num != kmp_hw_subset_t::USE_ALL && sub_id >= offset + num)) {
1384 should_be_filtered =
true;
1390 if (should_be_filtered) {
1391 KMP_CPU_CLR(hw_thread.os_id, filtered_mask);
1397 if (num_filtered == num_hw_threads) {
1398 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAllFiltered);
1403 restrict_to_mask(filtered_mask);
1407 bool kmp_topology_t::is_close(
int hwt1,
int hwt2,
1408 const kmp_affinity_t &stgs)
const {
1409 int hw_level = stgs.gran_levels;
1410 if (hw_level >= depth)
1413 const kmp_hw_thread_t &t1 = hw_threads[hwt1];
1414 const kmp_hw_thread_t &t2 = hw_threads[hwt2];
1415 if (stgs.flags.core_types_gran)
1416 return t1.attrs.get_core_type() == t2.attrs.get_core_type();
1417 if (stgs.flags.core_effs_gran)
1418 return t1.attrs.get_core_eff() == t2.attrs.get_core_eff();
1419 for (
int i = 0; i < (depth - hw_level); ++i) {
1420 if (t1.ids[i] != t2.ids[i])
1428 bool KMPAffinity::picked_api =
false;
1430 void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
1431 void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
1432 void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
1433 void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
1434 void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
1435 void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
1437 void KMPAffinity::pick_api() {
1438 KMPAffinity *affinity_dispatch;
1444 if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
1445 __kmp_affinity.type != affinity_disabled) {
1446 affinity_dispatch =
new KMPHwlocAffinity();
1447 __kmp_hwloc_available =
true;
1451 affinity_dispatch =
new KMPNativeAffinity();
1453 __kmp_affinity_dispatch = affinity_dispatch;
1457 void KMPAffinity::destroy_api() {
1458 if (__kmp_affinity_dispatch != NULL) {
1459 delete __kmp_affinity_dispatch;
1460 __kmp_affinity_dispatch = NULL;
1465 #define KMP_ADVANCE_SCAN(scan) \
1466 while (*scan != '\0') { \
1474 char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
1475 kmp_affin_mask_t *mask) {
1476 int start = 0, finish = 0, previous = 0;
1479 KMP_ASSERT(buf_len >= 40);
1482 char *end = buf + buf_len - 1;
1485 if (mask->begin() == mask->end()) {
1486 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
1487 KMP_ADVANCE_SCAN(scan);
1488 KMP_ASSERT(scan <= end);
1493 start = mask->begin();
1497 for (finish = mask->next(start), previous = start;
1498 finish == previous + 1 && finish != mask->end();
1499 finish = mask->next(finish)) {
1506 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
1507 KMP_ADVANCE_SCAN(scan);
1509 first_range =
false;
1512 if (previous - start > 1) {
1513 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
1516 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
1517 KMP_ADVANCE_SCAN(scan);
1518 if (previous - start > 0) {
1519 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
1522 KMP_ADVANCE_SCAN(scan);
1525 if (start == mask->end())
1533 KMP_ASSERT(scan <= end);
1536 #undef KMP_ADVANCE_SCAN
1542 kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
1543 kmp_affin_mask_t *mask) {
1544 int start = 0, finish = 0, previous = 0;
1549 __kmp_str_buf_clear(buf);
1552 if (mask->begin() == mask->end()) {
1553 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
1558 start = mask->begin();
1562 for (finish = mask->next(start), previous = start;
1563 finish == previous + 1 && finish != mask->end();
1564 finish = mask->next(finish)) {
1571 __kmp_str_buf_print(buf,
"%s",
",");
1573 first_range =
false;
1576 if (previous - start > 1) {
1577 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
1580 __kmp_str_buf_print(buf,
"%u", start);
1581 if (previous - start > 0) {
1582 __kmp_str_buf_print(buf,
",%u", previous);
1587 if (start == mask->end())
1593 static kmp_affin_mask_t *__kmp_parse_cpu_list(
const char *path) {
1594 kmp_affin_mask_t *mask;
1595 KMP_CPU_ALLOC(mask);
1598 int n, begin_cpu, end_cpu;
1600 auto skip_ws = [](FILE *f) {
1604 }
while (isspace(c));
1610 int status = file.
try_open(path,
"r");
1613 while (!feof(file)) {
1615 n = fscanf(file,
"%d", &begin_cpu);
1619 int c = fgetc(file);
1620 if (c == EOF || c ==
',') {
1622 end_cpu = begin_cpu;
1623 }
else if (c ==
'-') {
1626 n = fscanf(file,
"%d", &end_cpu);
1636 if (begin_cpu < 0 || begin_cpu >= __kmp_xproc || end_cpu < 0 ||
1637 end_cpu >= __kmp_xproc || begin_cpu > end_cpu) {
1641 for (
int cpu = begin_cpu; cpu <= end_cpu; ++cpu) {
1642 KMP_CPU_SET(cpu, mask);
1651 kmp_affin_mask_t *__kmp_affinity_get_offline_cpus() {
1652 return __kmp_parse_cpu_list(
"/sys/devices/system/cpu/offline");
1656 int __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
1660 #if KMP_GROUP_AFFINITY
1662 if (__kmp_num_proc_groups > 1) {
1664 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
1665 for (group = 0; group < __kmp_num_proc_groups; group++) {
1667 int num = __kmp_GetActiveProcessorCount(group);
1668 for (i = 0; i < num; i++) {
1669 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1679 kmp_affin_mask_t *offline_cpus = __kmp_affinity_get_offline_cpus();
1680 for (proc = 0; proc < __kmp_xproc; proc++) {
1682 if (KMP_CPU_ISSET(proc, offline_cpus))
1684 KMP_CPU_SET(proc, mask);
1687 KMP_CPU_FREE(offline_cpus);
1696 kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1698 kmp_affin_mask_t *__kmp_affin_origMask = NULL;
1701 static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1702 #if HWLOC_API_VERSION >= 0x00020000
1703 return hwloc_obj_type_is_cache(obj->type);
1705 return obj->type == HWLOC_OBJ_CACHE;
1710 static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1712 if (__kmp_hwloc_is_cache_type(obj)) {
1713 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1714 return KMP_HW_UNKNOWN;
1715 switch (obj->attr->cache.depth) {
1719 #if KMP_MIC_SUPPORTED
1720 if (__kmp_mic_type == mic3) {
1728 return KMP_HW_UNKNOWN;
1731 switch (obj->type) {
1732 case HWLOC_OBJ_PACKAGE:
1733 return KMP_HW_SOCKET;
1734 case HWLOC_OBJ_NUMANODE:
1736 case HWLOC_OBJ_CORE:
1739 return KMP_HW_THREAD;
1740 case HWLOC_OBJ_GROUP:
1741 #if HWLOC_API_VERSION >= 0x00020000
1742 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1744 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1746 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1747 return KMP_HW_MODULE;
1748 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1749 return KMP_HW_PROC_GROUP;
1751 return KMP_HW_UNKNOWN;
1752 #if HWLOC_API_VERSION >= 0x00020100
1757 return KMP_HW_UNKNOWN;
1764 static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1765 hwloc_obj_type_t type) {
1768 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1769 obj->logical_index, type, 0);
1770 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1771 obj->type, first) == obj;
1772 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1781 static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1782 hwloc_obj_t lower) {
1784 hwloc_obj_type_t ltype = lower->type;
1785 int lindex = lower->logical_index - 1;
1788 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1789 while (obj && lindex >= 0 &&
1790 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1791 if (obj->userdata) {
1792 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1797 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1800 lower->userdata = RCAST(
void *, sub_id + 1);
1804 static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1806 int hw_thread_index, sub_id;
1808 hwloc_obj_t pu, obj, root, prev;
1809 kmp_hw_t types[KMP_HW_LAST];
1810 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1812 hwloc_topology_t tp = __kmp_hwloc_topology;
1813 *msg_id = kmp_i18n_null;
1814 if (__kmp_affinity.flags.verbose) {
1815 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1818 if (!KMP_AFFINITY_CAPABLE()) {
1821 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1823 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1825 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1828 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1830 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1832 __kmp_nThreadsPerCore = 1;
1833 if (__kmp_nThreadsPerCore == 0)
1834 __kmp_nThreadsPerCore = 1;
1835 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1836 if (nCoresPerPkg == 0)
1838 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1842 #if HWLOC_API_VERSION >= 0x00020400
1844 int nr_cpu_kinds = hwloc_cpukinds_get_nr(tp, 0);
1846 typedef struct kmp_hwloc_cpukinds_info_t {
1848 kmp_hw_core_type_t core_type;
1849 hwloc_bitmap_t mask;
1850 } kmp_hwloc_cpukinds_info_t;
1851 kmp_hwloc_cpukinds_info_t *cpukinds =
nullptr;
1853 if (nr_cpu_kinds > 0) {
1855 struct hwloc_info_s *infos;
1856 cpukinds = (kmp_hwloc_cpukinds_info_t *)__kmp_allocate(
1857 sizeof(kmp_hwloc_cpukinds_info_t) * nr_cpu_kinds);
1858 for (
unsigned idx = 0; idx < (unsigned)nr_cpu_kinds; ++idx) {
1859 cpukinds[idx].efficiency = -1;
1860 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_UNKNOWN;
1861 cpukinds[idx].mask = hwloc_bitmap_alloc();
1862 if (hwloc_cpukinds_get_info(tp, idx, cpukinds[idx].mask,
1863 &cpukinds[idx].efficiency, &nr_infos, &infos,
1865 for (
unsigned i = 0; i < nr_infos; ++i) {
1866 if (__kmp_str_match(
"CoreType", 8, infos[i].name)) {
1867 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1868 if (__kmp_str_match(
"IntelAtom", 9, infos[i].value)) {
1869 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_ATOM;
1871 }
else if (__kmp_str_match(
"IntelCore", 9, infos[i].value)) {
1872 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_CORE;
1883 root = hwloc_get_root_obj(tp);
1887 obj = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1888 while (obj && obj != root) {
1889 #if HWLOC_API_VERSION >= 0x00020000
1890 if (obj->memory_arity) {
1892 for (memory = obj->memory_first_child; memory;
1893 memory = hwloc_get_next_child(tp, obj, memory)) {
1894 if (memory->type == HWLOC_OBJ_NUMANODE)
1897 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1898 types[depth] = KMP_HW_NUMA;
1899 hwloc_types[depth] = memory->type;
1904 type = __kmp_hwloc_type_2_topology_type(obj);
1905 if (type != KMP_HW_UNKNOWN) {
1906 types[depth] = type;
1907 hwloc_types[depth] = obj->type;
1912 KMP_ASSERT(depth > 0);
1915 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1916 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1917 kmp_hw_t temp = types[i];
1918 types[i] = types[j];
1920 hwloc_types[i] = hwloc_types[j];
1921 hwloc_types[j] = hwloc_temp;
1925 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1927 hw_thread_index = 0;
1929 while ((pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu))) {
1930 int index = depth - 1;
1931 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1932 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1935 hw_thread.ids[index] = pu->logical_index;
1936 hw_thread.os_id = pu->os_index;
1937 hw_thread.original_idx = hw_thread_index;
1939 #if HWLOC_API_VERSION >= 0x00020400
1941 int cpukind_index = -1;
1942 for (
int i = 0; i < nr_cpu_kinds; ++i) {
1943 if (hwloc_bitmap_isset(cpukinds[i].mask, hw_thread.os_id)) {
1948 if (cpukind_index >= 0) {
1949 hw_thread.attrs.set_core_type(cpukinds[cpukind_index].core_type);
1950 hw_thread.attrs.set_core_eff(cpukinds[cpukind_index].efficiency);
1958 while (obj != root && obj != NULL) {
1960 #if HWLOC_API_VERSION >= 0x00020000
1964 if (obj->memory_arity) {
1966 for (memory = obj->memory_first_child; memory;
1967 memory = hwloc_get_next_child(tp, obj, memory)) {
1968 if (memory->type == HWLOC_OBJ_NUMANODE)
1971 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1972 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1974 hw_thread.ids[index] = memory->logical_index;
1975 hw_thread.ids[index + 1] = sub_id;
1982 type = __kmp_hwloc_type_2_topology_type(obj);
1983 if (type != KMP_HW_UNKNOWN) {
1984 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1986 hw_thread.ids[index] = obj->logical_index;
1987 hw_thread.ids[index + 1] = sub_id;
1997 #if HWLOC_API_VERSION >= 0x00020400
2000 for (
int idx = 0; idx < nr_cpu_kinds; ++idx)
2001 hwloc_bitmap_free(cpukinds[idx].mask);
2002 __kmp_free(cpukinds);
2005 __kmp_topology->sort_ids();
2013 static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
2014 *msg_id = kmp_i18n_null;
2016 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
2018 if (__kmp_affinity.flags.verbose) {
2019 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
2025 if (!KMP_AFFINITY_CAPABLE()) {
2026 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2027 __kmp_ncores = nPackages = __kmp_xproc;
2028 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
2036 __kmp_ncores = nPackages = __kmp_avail_proc;
2037 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
2040 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2043 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2045 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2048 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
2050 hw_thread.os_id = i;
2051 hw_thread.original_idx = avail_ct;
2052 hw_thread.ids[0] = i;
2053 hw_thread.ids[1] = 0;
2054 hw_thread.ids[2] = 0;
2057 if (__kmp_affinity.flags.verbose) {
2058 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
2063 #if KMP_GROUP_AFFINITY
2068 static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
2069 *msg_id = kmp_i18n_null;
2071 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
2072 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
2074 if (__kmp_affinity.flags.verbose) {
2075 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
2079 if (!KMP_AFFINITY_CAPABLE()) {
2080 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2081 nPackages = __kmp_num_proc_groups;
2082 __kmp_nThreadsPerCore = 1;
2083 __kmp_ncores = __kmp_xproc;
2084 nCoresPerPkg = nPackages / __kmp_ncores;
2089 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2092 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2094 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2097 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
2099 hw_thread.os_id = i;
2100 hw_thread.original_idx = avail_ct;
2101 hw_thread.ids[0] = i / BITS_PER_GROUP;
2102 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
2109 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2111 template <kmp_u
int32 LSB, kmp_u
int32 MSB>
2112 static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
2113 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
2114 const kmp_uint32 SHIFT_RIGHT = LSB;
2115 kmp_uint32 retval = v;
2116 retval <<= SHIFT_LEFT;
2117 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
2121 static int __kmp_cpuid_mask_width(
int count) {
2124 while ((1 << r) < count)
2129 class apicThreadInfo {
2133 unsigned maxCoresPerPkg;
2134 unsigned maxThreadsPerPkg;
2140 static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
2142 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
2143 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
2144 if (aa->pkgId < bb->pkgId)
2146 if (aa->pkgId > bb->pkgId)
2148 if (aa->coreId < bb->coreId)
2150 if (aa->coreId > bb->coreId)
2152 if (aa->threadId < bb->threadId)
2154 if (aa->threadId > bb->threadId)
2159 class cpuid_cache_info_t {
2164 bool operator==(
const info_t &rhs)
const {
2165 return level == rhs.level && mask == rhs.mask;
2167 bool operator!=(
const info_t &rhs)
const {
return !operator==(rhs); }
2169 cpuid_cache_info_t() : depth(0) {
2170 table[MAX_CACHE_LEVEL].level = 0;
2171 table[MAX_CACHE_LEVEL].mask = 0;
2173 size_t get_depth()
const {
return depth; }
2174 info_t &operator[](
size_t index) {
return table[index]; }
2175 const info_t &operator[](
size_t index)
const {
return table[index]; }
2176 bool operator==(
const cpuid_cache_info_t &rhs)
const {
2177 if (rhs.depth != depth)
2179 for (
size_t i = 0; i < depth; ++i)
2180 if (table[i] != rhs.table[i])
2184 bool operator!=(
const cpuid_cache_info_t &rhs)
const {
2185 return !operator==(rhs);
2189 const info_t &get_level(
unsigned level)
const {
2190 for (
size_t i = 0; i < depth; ++i) {
2191 if (table[i].level == level)
2194 return table[MAX_CACHE_LEVEL];
2197 static kmp_hw_t get_topology_type(
unsigned level) {
2198 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
2207 return KMP_HW_UNKNOWN;
2209 void get_leaf4_levels() {
2211 while (depth < MAX_CACHE_LEVEL) {
2212 unsigned cache_type, max_threads_sharing;
2213 unsigned cache_level, cache_mask_width;
2215 __kmp_x86_cpuid(4, level, &buf2);
2216 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
2220 if (cache_type == 2) {
2224 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
2225 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
2226 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
2227 table[depth].level = cache_level;
2228 table[depth].mask = ((-1) << cache_mask_width);
2233 static const int MAX_CACHE_LEVEL = 3;
2237 info_t table[MAX_CACHE_LEVEL + 1];
2244 static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
2246 *msg_id = kmp_i18n_null;
2248 if (__kmp_affinity.flags.verbose) {
2249 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
2253 __kmp_x86_cpuid(0, 0, &buf);
2255 *msg_id = kmp_i18n_str_NoLeaf4Support;
2264 if (!KMP_AFFINITY_CAPABLE()) {
2267 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2273 __kmp_x86_cpuid(1, 0, &buf);
2274 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2275 if (maxThreadsPerPkg == 0) {
2276 maxThreadsPerPkg = 1;
2290 __kmp_x86_cpuid(0, 0, &buf);
2292 __kmp_x86_cpuid(4, 0, &buf);
2293 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2311 __kmp_ncores = __kmp_xproc;
2312 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2313 __kmp_nThreadsPerCore = 1;
2322 kmp_affinity_raii_t previous_affinity;
2350 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
2351 __kmp_avail_proc *
sizeof(apicThreadInfo));
2352 unsigned nApics = 0;
2353 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2355 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2358 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
2360 __kmp_affinity_dispatch->bind_thread(i);
2361 threadInfo[nApics].osId = i;
2364 __kmp_x86_cpuid(1, 0, &buf);
2365 if (((buf.edx >> 9) & 1) == 0) {
2366 __kmp_free(threadInfo);
2367 *msg_id = kmp_i18n_str_ApicNotPresent;
2370 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
2371 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2372 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
2373 threadInfo[nApics].maxThreadsPerPkg = 1;
2382 __kmp_x86_cpuid(0, 0, &buf);
2384 __kmp_x86_cpuid(4, 0, &buf);
2385 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2387 threadInfo[nApics].maxCoresPerPkg = 1;
2391 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
2392 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
2394 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
2395 int widthT = widthCT - widthC;
2400 __kmp_free(threadInfo);
2401 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2405 int maskC = (1 << widthC) - 1;
2406 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
2408 int maskT = (1 << widthT) - 1;
2409 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
2416 previous_affinity.restore();
2419 qsort(threadInfo, nApics,
sizeof(*threadInfo),
2420 __kmp_affinity_cmp_apicThreadInfo_phys_id);
2437 __kmp_nThreadsPerCore = 1;
2438 unsigned nCores = 1;
2441 unsigned lastPkgId = threadInfo[0].pkgId;
2442 unsigned coreCt = 1;
2443 unsigned lastCoreId = threadInfo[0].coreId;
2444 unsigned threadCt = 1;
2445 unsigned lastThreadId = threadInfo[0].threadId;
2448 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
2449 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
2451 for (i = 1; i < nApics; i++) {
2452 if (threadInfo[i].pkgId != lastPkgId) {
2455 lastPkgId = threadInfo[i].pkgId;
2456 if ((
int)coreCt > nCoresPerPkg)
2457 nCoresPerPkg = coreCt;
2459 lastCoreId = threadInfo[i].coreId;
2460 if ((
int)threadCt > __kmp_nThreadsPerCore)
2461 __kmp_nThreadsPerCore = threadCt;
2463 lastThreadId = threadInfo[i].threadId;
2467 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
2468 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
2472 if (threadInfo[i].coreId != lastCoreId) {
2475 lastCoreId = threadInfo[i].coreId;
2476 if ((
int)threadCt > __kmp_nThreadsPerCore)
2477 __kmp_nThreadsPerCore = threadCt;
2479 lastThreadId = threadInfo[i].threadId;
2480 }
else if (threadInfo[i].threadId != lastThreadId) {
2482 lastThreadId = threadInfo[i].threadId;
2484 __kmp_free(threadInfo);
2485 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2491 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
2492 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
2493 __kmp_free(threadInfo);
2494 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
2502 if ((
int)coreCt > nCoresPerPkg)
2503 nCoresPerPkg = coreCt;
2504 if ((
int)threadCt > __kmp_nThreadsPerCore)
2505 __kmp_nThreadsPerCore = threadCt;
2506 __kmp_ncores = nCores;
2507 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
2515 int threadLevel = 2;
2517 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
2520 types[idx++] = KMP_HW_SOCKET;
2522 types[idx++] = KMP_HW_CORE;
2523 if (threadLevel >= 0)
2524 types[idx++] = KMP_HW_THREAD;
2526 KMP_ASSERT(depth > 0);
2527 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
2529 for (i = 0; i < nApics; ++i) {
2531 unsigned os = threadInfo[i].osId;
2532 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2535 if (pkgLevel >= 0) {
2536 hw_thread.ids[idx++] = threadInfo[i].pkgId;
2538 if (coreLevel >= 0) {
2539 hw_thread.ids[idx++] = threadInfo[i].coreId;
2541 if (threadLevel >= 0) {
2542 hw_thread.ids[idx++] = threadInfo[i].threadId;
2544 hw_thread.os_id = os;
2545 hw_thread.original_idx = i;
2548 __kmp_free(threadInfo);
2549 __kmp_topology->sort_ids();
2550 if (!__kmp_topology->check_ids()) {
2551 kmp_topology_t::deallocate(__kmp_topology);
2552 __kmp_topology =
nullptr;
2553 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2561 static void __kmp_get_hybrid_info(kmp_hw_core_type_t *type,
int *efficiency,
2562 unsigned *native_model_id) {
2564 __kmp_x86_cpuid(0x1a, 0, &buf);
2565 *type = (kmp_hw_core_type_t)__kmp_extract_bits<24, 31>(buf.eax);
2567 case KMP_HW_CORE_TYPE_ATOM:
2570 case KMP_HW_CORE_TYPE_CORE:
2576 *native_model_id = __kmp_extract_bits<0, 23>(buf.eax);
2598 INTEL_LEVEL_TYPE_INVALID = 0,
2599 INTEL_LEVEL_TYPE_SMT = 1,
2600 INTEL_LEVEL_TYPE_CORE = 2,
2601 INTEL_LEVEL_TYPE_MODULE = 3,
2602 INTEL_LEVEL_TYPE_TILE = 4,
2603 INTEL_LEVEL_TYPE_DIE = 5,
2604 INTEL_LEVEL_TYPE_LAST = 6,
2606 KMP_BUILD_ASSERT(INTEL_LEVEL_TYPE_LAST <
sizeof(
unsigned) * CHAR_BIT);
2607 #define KMP_LEAF_1F_KNOWN_LEVELS ((1u << INTEL_LEVEL_TYPE_LAST) - 1u)
2609 static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
2610 switch (intel_type) {
2611 case INTEL_LEVEL_TYPE_INVALID:
2612 return KMP_HW_SOCKET;
2613 case INTEL_LEVEL_TYPE_SMT:
2614 return KMP_HW_THREAD;
2615 case INTEL_LEVEL_TYPE_CORE:
2617 case INTEL_LEVEL_TYPE_TILE:
2619 case INTEL_LEVEL_TYPE_MODULE:
2620 return KMP_HW_MODULE;
2621 case INTEL_LEVEL_TYPE_DIE:
2624 return KMP_HW_UNKNOWN;
2627 static int __kmp_topology_type_2_intel_type(kmp_hw_t type) {
2630 return INTEL_LEVEL_TYPE_INVALID;
2632 return INTEL_LEVEL_TYPE_SMT;
2634 return INTEL_LEVEL_TYPE_CORE;
2636 return INTEL_LEVEL_TYPE_TILE;
2638 return INTEL_LEVEL_TYPE_MODULE;
2640 return INTEL_LEVEL_TYPE_DIE;
2642 return INTEL_LEVEL_TYPE_INVALID;
2646 struct cpuid_level_info_t {
2647 unsigned level_type, mask, mask_width, nitems, cache_mask;
2650 class cpuid_topo_desc_t {
2654 void clear() { desc = 0; }
2655 bool contains(
int intel_type)
const {
2656 KMP_DEBUG_ASSERT(intel_type >= 0 && intel_type < INTEL_LEVEL_TYPE_LAST);
2657 if ((1u << intel_type) & desc)
2661 bool contains_topology_type(kmp_hw_t type)
const {
2662 KMP_DEBUG_ASSERT(type >= 0 && type < KMP_HW_LAST);
2663 int intel_type = __kmp_topology_type_2_intel_type(type);
2664 return contains(intel_type);
2666 bool contains(cpuid_topo_desc_t rhs)
const {
2667 return ((desc | rhs.desc) == desc);
2669 void add(
int intel_type) { desc |= (1u << intel_type); }
2670 void add(cpuid_topo_desc_t rhs) { desc |= rhs.desc; }
2673 struct cpuid_proc_info_t {
2679 unsigned native_model_id;
2681 kmp_hw_core_type_t type;
2682 cpuid_topo_desc_t description;
2684 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
2690 static bool __kmp_x2apicid_get_levels(
int leaf, cpuid_proc_info_t *info,
2691 kmp_hw_t total_types[KMP_HW_LAST],
2693 cpuid_topo_desc_t *total_description) {
2694 unsigned level, levels_index;
2695 unsigned level_type, mask_width, nitems;
2697 cpuid_level_info_t(&levels)[INTEL_LEVEL_TYPE_LAST] = info->levels;
2698 bool retval =
false;
2707 level = levels_index = 0;
2709 __kmp_x86_cpuid(leaf, level, &buf);
2710 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
2711 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
2712 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
2713 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0) {
2718 if (KMP_LEAF_1F_KNOWN_LEVELS & (1u << level_type)) {
2720 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
2721 levels[levels_index].level_type = level_type;
2722 levels[levels_index].mask_width = mask_width;
2723 levels[levels_index].nitems = nitems;
2727 if (levels_index > 0) {
2728 levels[levels_index - 1].mask_width = mask_width;
2729 levels[levels_index - 1].nitems = nitems;
2733 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
2734 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
2735 info->description.clear();
2736 info->depth = levels_index;
2740 if (*total_depth == 0) {
2741 *total_depth = info->depth;
2742 total_description->clear();
2743 for (
int i = *total_depth - 1, j = 0; i >= 0; --i, ++j) {
2745 __kmp_intel_type_2_topology_type(info->levels[i].level_type);
2746 total_description->add(info->levels[i].level_type);
2752 if (levels_index == 0 || levels[0].level_type == INTEL_LEVEL_TYPE_INVALID)
2756 for (
unsigned i = 0; i < levels_index; ++i) {
2757 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
2758 levels[i].mask = ~((-1) << levels[i].mask_width);
2759 levels[i].cache_mask = (-1) << levels[i].mask_width;
2760 for (
unsigned j = 0; j < i; ++j)
2761 levels[i].mask ^= levels[j].mask;
2763 KMP_DEBUG_ASSERT(i > 0);
2764 levels[i].mask = (-1) << levels[i - 1].mask_width;
2765 levels[i].cache_mask = 0;
2767 info->description.add(info->levels[i].level_type);
2775 if (!total_description->contains(info->description)) {
2776 for (
int i = info->depth - 1, j = 0; i >= 0; --i, ++j) {
2778 if (total_description->contains(levels[i].level_type))
2781 kmp_hw_t curr_type =
2782 __kmp_intel_type_2_topology_type(levels[i].level_type);
2783 KMP_ASSERT(j != 0 &&
"Bad APIC Id information");
2785 for (
int k = info->depth - 1; k >= j; --k) {
2786 KMP_DEBUG_ASSERT(k + 1 < KMP_HW_LAST);
2787 total_types[k + 1] = total_types[k];
2790 total_types[j] = curr_type;
2793 total_description->add(info->description);
2799 static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
2801 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
2803 int topology_leaf, highest_leaf;
2806 cpuid_topo_desc_t total_description;
2807 static int leaves[] = {0, 0};
2810 int ninfos = (__kmp_avail_proc > 0 ? __kmp_avail_proc : 1);
2811 cpuid_proc_info_t *proc_info = (cpuid_proc_info_t *)__kmp_allocate(
2812 (
sizeof(cpuid_proc_info_t) +
sizeof(cpuid_cache_info_t)) * ninfos);
2813 cpuid_cache_info_t *cache_info = (cpuid_cache_info_t *)(proc_info + ninfos);
2815 kmp_i18n_id_t leaf_message_id;
2817 *msg_id = kmp_i18n_null;
2818 if (__kmp_affinity.flags.verbose) {
2819 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
2823 __kmp_x86_cpuid(0, 0, &buf);
2824 highest_leaf = buf.eax;
2829 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
2832 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2833 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
2836 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
2841 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2845 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2847 for (
int i = 0; i < num_leaves; ++i) {
2848 int leaf = leaves[i];
2849 if (highest_leaf < leaf)
2851 __kmp_x86_cpuid(leaf, 0, &buf);
2854 topology_leaf = leaf;
2855 __kmp_x2apicid_get_levels(leaf, &proc_info[0], types, &depth,
2856 &total_description);
2861 if (topology_leaf == -1 || depth == 0) {
2862 *msg_id = leaf_message_id;
2863 __kmp_free(proc_info);
2866 KMP_ASSERT(depth <= INTEL_LEVEL_TYPE_LAST);
2873 if (!KMP_AFFINITY_CAPABLE()) {
2876 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2877 for (
int i = 0; i < depth; ++i) {
2878 if (proc_info[0].levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
2879 __kmp_nThreadsPerCore = proc_info[0].levels[i].nitems;
2880 }
else if (proc_info[0].levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
2881 nCoresPerPkg = proc_info[0].levels[i].nitems;
2884 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
2885 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2886 __kmp_free(proc_info);
2895 kmp_affinity_raii_t previous_affinity;
2900 int hw_thread_index = 0;
2901 bool uniform_caches =
true;
2903 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2905 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2908 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2911 __kmp_affinity_dispatch->bind_thread(proc);
2912 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2913 proc_info[hw_thread_index].os_id = proc;
2914 proc_info[hw_thread_index].apic_id = buf.edx;
2915 __kmp_x2apicid_get_levels(topology_leaf, &proc_info[hw_thread_index], types,
2916 &depth, &total_description);
2917 if (proc_info[hw_thread_index].depth == 0) {
2918 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2919 __kmp_free(proc_info);
2923 cache_info[hw_thread_index].get_leaf4_levels();
2924 if (uniform_caches && hw_thread_index > 0)
2925 if (cache_info[0] != cache_info[hw_thread_index])
2926 uniform_caches =
false;
2928 if (__kmp_is_hybrid_cpu() && highest_leaf >= 0x1a) {
2929 __kmp_get_hybrid_info(&proc_info[hw_thread_index].type,
2930 &proc_info[hw_thread_index].efficiency,
2931 &proc_info[hw_thread_index].native_model_id);
2935 KMP_ASSERT(hw_thread_index > 0);
2936 previous_affinity.restore();
2939 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2942 for (
int i = 0; i < __kmp_topology->get_num_hw_threads(); ++i) {
2943 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2945 hw_thread.os_id = proc_info[i].os_id;
2946 hw_thread.original_idx = i;
2947 unsigned apic_id = proc_info[i].apic_id;
2949 for (
int j = 0, idx = depth - 1; j < depth; ++j, --idx) {
2950 if (!(proc_info[i].description.contains_topology_type(
2951 __kmp_topology->get_type(j)))) {
2952 hw_thread.ids[idx] = kmp_hw_thread_t::UNKNOWN_ID;
2954 hw_thread.ids[idx] = apic_id & proc_info[i].levels[j].mask;
2956 hw_thread.ids[idx] >>= proc_info[i].levels[j - 1].mask_width;
2960 hw_thread.attrs.set_core_type(proc_info[i].type);
2961 hw_thread.attrs.set_core_eff(proc_info[i].efficiency);
2964 __kmp_topology->sort_ids();
2967 for (
int j = 0; j < depth - 1; ++j) {
2969 int prev_id = __kmp_topology->at(0).ids[j];
2970 int curr_id = __kmp_topology->at(0).ids[j + 1];
2971 __kmp_topology->at(0).ids[j + 1] = new_id;
2972 for (
int i = 1; i < __kmp_topology->get_num_hw_threads(); ++i) {
2973 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2974 if (hw_thread.ids[j] == prev_id && hw_thread.ids[j + 1] == curr_id) {
2975 hw_thread.ids[j + 1] = new_id;
2976 }
else if (hw_thread.ids[j] == prev_id &&
2977 hw_thread.ids[j + 1] != curr_id) {
2978 curr_id = hw_thread.ids[j + 1];
2979 hw_thread.ids[j + 1] = ++new_id;
2981 prev_id = hw_thread.ids[j];
2982 curr_id = hw_thread.ids[j + 1];
2983 hw_thread.ids[j + 1] = ++new_id;
2990 if (uniform_caches) {
2991 for (
size_t i = 0; i < cache_info[0].get_depth(); ++i) {
2992 unsigned cache_mask = cache_info[0][i].mask;
2993 unsigned cache_level = cache_info[0][i].level;
2994 KMP_ASSERT(cache_level <= cpuid_cache_info_t::MAX_CACHE_LEVEL);
2995 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(cache_level);
2996 __kmp_topology->set_equivalent_type(cache_type, cache_type);
2997 for (
int j = 0; j < depth; ++j) {
2998 unsigned hw_cache_mask = proc_info[0].levels[j].cache_mask;
2999 if (hw_cache_mask == cache_mask && j < depth - 1) {
3000 kmp_hw_t type = __kmp_intel_type_2_topology_type(
3001 proc_info[0].levels[j + 1].level_type);
3002 __kmp_topology->set_equivalent_type(cache_type, type);
3008 for (
int i = 0; i < __kmp_topology->get_num_hw_threads(); ++i) {
3009 for (
size_t j = 0; j < cache_info[i].get_depth(); ++j) {
3010 unsigned cache_level = cache_info[i][j].level;
3011 kmp_hw_t cache_type =
3012 cpuid_cache_info_t::get_topology_type(cache_level);
3013 if (__kmp_topology->get_equivalent_type(cache_type) == KMP_HW_UNKNOWN)
3014 __kmp_topology->set_equivalent_type(cache_type, cache_type);
3020 bool unresolved_cache_levels =
false;
3021 for (
unsigned level = 1; level <= cpuid_cache_info_t::MAX_CACHE_LEVEL;
3023 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(level);
3026 if (__kmp_topology->get_equivalent_type(cache_type) == cache_type) {
3027 unresolved_cache_levels =
true;
3033 if (unresolved_cache_levels) {
3034 int num_hw_threads = __kmp_topology->get_num_hw_threads();
3035 int *ids = (
int *)__kmp_allocate(
sizeof(
int) * num_hw_threads);
3036 for (
unsigned l = 1; l <= cpuid_cache_info_t::MAX_CACHE_LEVEL; ++l) {
3037 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(l);
3038 if (__kmp_topology->get_equivalent_type(cache_type) != cache_type)
3040 for (
int i = 0; i < num_hw_threads; ++i) {
3041 int original_idx = __kmp_topology->at(i).original_idx;
3042 ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
3043 const cpuid_cache_info_t::info_t &info =
3044 cache_info[original_idx].get_level(l);
3046 if (info.level == 0)
3048 ids[i] = info.mask & proc_info[original_idx].apic_id;
3050 __kmp_topology->insert_layer(cache_type, ids);
3054 if (!__kmp_topology->check_ids()) {
3055 kmp_topology_t::deallocate(__kmp_topology);
3056 __kmp_topology =
nullptr;
3057 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
3058 __kmp_free(proc_info);
3061 __kmp_free(proc_info);
3067 #define threadIdIndex 1
3068 #define coreIdIndex 2
3069 #define pkgIdIndex 3
3070 #define nodeIdIndex 4
3072 typedef unsigned *ProcCpuInfo;
3073 static unsigned maxIndex = pkgIdIndex;
3075 static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
3078 const unsigned *aa = *(
unsigned *
const *)a;
3079 const unsigned *bb = *(
unsigned *
const *)b;
3080 for (i = maxIndex;; i--) {
3091 #if KMP_USE_HIER_SCHED
3093 static void __kmp_dispatch_set_hierarchy_values() {
3099 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
3100 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
3101 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
3102 #if KMP_ARCH_X86_64 && \
3103 (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
3104 KMP_OS_WINDOWS) && \
3106 if (__kmp_mic_type >= mic3)
3107 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
3110 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
3111 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
3112 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
3113 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
3116 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
3117 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
3118 __kmp_nThreadsPerCore;
3119 #if KMP_ARCH_X86_64 && \
3120 (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
3121 KMP_OS_WINDOWS) && \
3123 if (__kmp_mic_type >= mic3)
3124 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
3125 2 * __kmp_nThreadsPerCore;
3128 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
3129 __kmp_nThreadsPerCore;
3130 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
3131 nCoresPerPkg * __kmp_nThreadsPerCore;
3132 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
3133 nCoresPerPkg * __kmp_nThreadsPerCore;
3134 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
3135 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
3140 int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
3141 int index = type + 1;
3142 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
3143 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
3144 if (type == kmp_hier_layer_e::LAYER_THREAD)
3146 else if (type == kmp_hier_layer_e::LAYER_LOOP)
3148 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
3149 if (tid >= num_hw_threads)
3150 tid = tid % num_hw_threads;
3151 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
3155 int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
3158 KMP_DEBUG_ASSERT(i1 <= i2);
3159 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
3160 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
3161 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
3163 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
3167 static inline const char *__kmp_cpuinfo_get_filename() {
3168 const char *filename;
3169 if (__kmp_cpuinfo_file !=
nullptr)
3170 filename = __kmp_cpuinfo_file;
3172 filename =
"/proc/cpuinfo";
3176 static inline const char *__kmp_cpuinfo_get_envvar() {
3177 const char *envvar =
nullptr;
3178 if (__kmp_cpuinfo_file !=
nullptr)
3179 envvar =
"KMP_CPUINFO_FILE";
3183 static bool __kmp_package_id_from_core_siblings_list(
unsigned **threadInfo,
3186 if (!KMP_AFFINITY_CAPABLE())
3190 KMP_SNPRINTF(path,
sizeof(path),
3191 "/sys/devices/system/cpu/cpu%u/topology/core_siblings_list",
3192 threadInfo[idx][osIdIndex]);
3193 kmp_affin_mask_t *siblings = __kmp_parse_cpu_list(path);
3194 for (
unsigned i = 0; i < num_avail; ++i) {
3195 unsigned cpu_id = threadInfo[i][osIdIndex];
3196 KMP_ASSERT(cpu_id < __kmp_affin_mask_size * CHAR_BIT);
3197 if (!KMP_CPU_ISSET(cpu_id, siblings))
3199 if (threadInfo[i][pkgIdIndex] == UINT_MAX) {
3202 threadInfo[i][pkgIdIndex] = idx;
3203 }
else if (threadInfo[i][pkgIdIndex] != idx) {
3205 KMP_CPU_FREE(siblings);
3209 KMP_ASSERT(threadInfo[idx][pkgIdIndex] != UINT_MAX);
3210 KMP_CPU_FREE(siblings);
3217 static bool __kmp_affinity_create_cpuinfo_map(
int *line,
3218 kmp_i18n_id_t *
const msg_id) {
3219 *msg_id = kmp_i18n_null;
3222 unsigned num_records = __kmp_xproc;
3224 const char *filename = __kmp_cpuinfo_get_filename();
3225 const char *envvar = __kmp_cpuinfo_get_envvar();
3227 if (__kmp_affinity.flags.verbose) {
3228 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
3236 unsigned num_records = 0;
3238 buf[
sizeof(buf) - 1] = 1;
3239 if (!fgets(buf,
sizeof(buf), f)) {
3244 char s1[] =
"processor";
3245 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
3252 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3254 if (level > (
unsigned)__kmp_xproc) {
3255 level = __kmp_xproc;
3257 if (nodeIdIndex + level >= maxIndex) {
3258 maxIndex = nodeIdIndex + level;
3266 if (num_records == 0) {
3267 *msg_id = kmp_i18n_str_NoProcRecords;
3270 if (num_records > (
unsigned)__kmp_xproc) {
3271 *msg_id = kmp_i18n_str_TooManyProcRecords;
3280 if (fseek(f, 0, SEEK_SET) != 0) {
3281 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
3288 unsigned **threadInfo =
3289 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
3291 for (i = 0; i <= num_records; i++) {
3293 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3296 #define CLEANUP_THREAD_INFO \
3297 for (i = 0; i <= num_records; i++) { \
3298 __kmp_free(threadInfo[i]); \
3300 __kmp_free(threadInfo);
3305 #define INIT_PROC_INFO(p) \
3306 for (__index = 0; __index <= maxIndex; __index++) { \
3307 (p)[__index] = UINT_MAX; \
3310 for (i = 0; i <= num_records; i++) {
3311 INIT_PROC_INFO(threadInfo[i]);
3316 lpar_info_format1_t cpuinfo;
3317 unsigned num_avail = __kmp_xproc;
3319 if (__kmp_affinity.flags.verbose)
3320 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"system info for topology");
3323 smt_threads = syssmt(GET_NUMBER_SMT_SETS, 0, 0, NULL);
3326 rsethandle_t sys_rset = rs_alloc(RS_SYSTEM);
3327 if (sys_rset == NULL) {
3328 CLEANUP_THREAD_INFO;
3329 *msg_id = kmp_i18n_str_UnknownTopology;
3333 rsethandle_t srad = rs_alloc(RS_EMPTY);
3336 CLEANUP_THREAD_INFO;
3337 *msg_id = kmp_i18n_str_UnknownTopology;
3342 int sradsdl = rs_getinfo(NULL, R_SRADSDL, 0);
3346 CLEANUP_THREAD_INFO;
3347 *msg_id = kmp_i18n_str_UnknownTopology;
3351 int num_rads = rs_numrads(sys_rset, sradsdl, 0);
3355 CLEANUP_THREAD_INFO;
3356 *msg_id = kmp_i18n_str_UnknownTopology;
3361 int max_procs = rs_getinfo(NULL, R_MAXPROCS, 0);
3362 if (max_procs < 0) {
3365 CLEANUP_THREAD_INFO;
3366 *msg_id = kmp_i18n_str_UnknownTopology;
3372 for (
int srad_idx = 0; cur_rad < num_rads && srad_idx < VMI_MAXRADS;
3375 if (rs_getrad(sys_rset, srad, sradsdl, srad_idx, 0) < 0)
3378 for (
int cpu = 0; cpu < max_procs; cpu++) {
3380 if (rs_op(RS_TESTRESOURCE, srad, NULL, R_PROCS, cpu)) {
3381 threadInfo[cpu][osIdIndex] = cpu;
3382 threadInfo[cpu][pkgIdIndex] = cur_rad;
3383 threadInfo[cpu][coreIdIndex] = cpu / smt_threads;
3385 if (num_set >= num_avail) {
3399 unsigned num_avail = 0;
3402 bool reading_s390x_sys_info =
true;
3409 buf[
sizeof(buf) - 1] = 1;
3410 bool long_line =
false;
3411 if (!fgets(buf,
sizeof(buf), f)) {
3416 for (i = 0; i <= maxIndex; i++) {
3417 if (threadInfo[num_avail][i] != UINT_MAX) {
3425 }
else if (!buf[
sizeof(buf) - 1]) {
3430 #define CHECK_LINE \
3432 CLEANUP_THREAD_INFO; \
3433 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
3439 #if KMP_ARCH_LOONGARCH64
3446 if (*buf ==
'\n' && *line == 2)
3452 if (reading_s390x_sys_info) {
3454 reading_s390x_sys_info =
false;
3460 char s1[] =
"cpu number";
3462 char s1[] =
"processor";
3464 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
3466 char *p = strchr(buf +
sizeof(s1) - 1,
':');
3468 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3470 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
3471 #if KMP_ARCH_AARCH64
3480 threadInfo[num_avail][osIdIndex] = val;
3481 #if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
3485 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
3486 threadInfo[num_avail][osIdIndex]);
3487 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
3492 KMP_SNPRINTF(path,
sizeof(path),
3493 "/sys/devices/system/cpu/cpu%u/topology/book_id",
3494 threadInfo[num_avail][osIdIndex]);
3495 __kmp_read_from_file(path,
"%u", &book_id);
3496 threadInfo[num_avail][pkgIdIndex] |= (book_id << 8);
3499 KMP_SNPRINTF(path,
sizeof(path),
3500 "/sys/devices/system/cpu/cpu%u/topology/drawer_id",
3501 threadInfo[num_avail][osIdIndex]);
3502 __kmp_read_from_file(path,
"%u", &drawer_id);
3503 threadInfo[num_avail][pkgIdIndex] |= (drawer_id << 16);
3506 KMP_SNPRINTF(path,
sizeof(path),
3507 "/sys/devices/system/cpu/cpu%u/topology/core_id",
3508 threadInfo[num_avail][osIdIndex]);
3509 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
3513 char s2[] =
"physical id";
3514 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
3516 char *p = strchr(buf +
sizeof(s2) - 1,
':');
3518 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3520 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
3522 threadInfo[num_avail][pkgIdIndex] = val;
3525 char s3[] =
"core id";
3526 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
3528 char *p = strchr(buf +
sizeof(s3) - 1,
':');
3530 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3532 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
3534 threadInfo[num_avail][coreIdIndex] = val;
3538 char s4[] =
"thread id";
3539 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
3541 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3543 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3545 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
3547 threadInfo[num_avail][threadIdIndex] = val;
3551 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3553 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3555 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3558 if (level > (
unsigned)__kmp_xproc) {
3559 level = __kmp_xproc;
3561 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
3563 threadInfo[num_avail][nodeIdIndex + level] = val;
3570 if ((*buf != 0) && (*buf !=
'\n')) {
3575 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
3583 if ((
int)num_avail == __kmp_xproc) {
3584 CLEANUP_THREAD_INFO;
3585 *msg_id = kmp_i18n_str_TooManyEntries;
3591 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
3592 CLEANUP_THREAD_INFO;
3593 *msg_id = kmp_i18n_str_MissingProcField;
3598 if (KMP_AFFINITY_CAPABLE() &&
3599 !KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
3600 __kmp_affin_fullMask)) {
3601 INIT_PROC_INFO(threadInfo[num_avail]);
3608 KMP_ASSERT(num_avail <= num_records);
3609 INIT_PROC_INFO(threadInfo[num_avail]);
3614 CLEANUP_THREAD_INFO;
3615 *msg_id = kmp_i18n_str_MissingValCpuinfo;
3619 CLEANUP_THREAD_INFO;
3620 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
3627 for (i = 0; i < num_avail; ++i) {
3628 if (threadInfo[i][pkgIdIndex] == UINT_MAX) {
3629 if (!__kmp_package_id_from_core_siblings_list(threadInfo, num_avail, i)) {
3630 CLEANUP_THREAD_INFO;
3631 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
3637 #if KMP_MIC && REDUCE_TEAM_SIZE
3638 unsigned teamSize = 0;
3646 KMP_ASSERT(num_avail > 0);
3647 KMP_ASSERT(num_avail <= num_records);
3650 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
3651 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
3665 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3667 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3669 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3671 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3673 bool assign_thread_ids =
false;
3674 unsigned threadIdCt;
3677 restart_radix_check:
3681 if (assign_thread_ids) {
3682 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
3683 threadInfo[0][threadIdIndex] = threadIdCt++;
3684 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
3685 threadIdCt = threadInfo[0][threadIdIndex] + 1;
3688 for (index = 0; index <= maxIndex; index++) {
3692 lastId[index] = threadInfo[0][index];
3697 for (i = 1; i < num_avail; i++) {
3700 for (index = maxIndex; index >= threadIdIndex; index--) {
3701 if (assign_thread_ids && (index == threadIdIndex)) {
3703 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3704 threadInfo[i][threadIdIndex] = threadIdCt++;
3708 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3709 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3712 if (threadInfo[i][index] != lastId[index]) {
3717 for (index2 = threadIdIndex; index2 < index; index2++) {
3719 if (counts[index2] > maxCt[index2]) {
3720 maxCt[index2] = counts[index2];
3723 lastId[index2] = threadInfo[i][index2];
3727 lastId[index] = threadInfo[i][index];
3729 if (assign_thread_ids && (index > threadIdIndex)) {
3731 #if KMP_MIC && REDUCE_TEAM_SIZE
3734 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3741 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3742 threadInfo[i][threadIdIndex] = threadIdCt++;
3748 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3749 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3755 if (index < threadIdIndex) {
3759 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
3764 CLEANUP_THREAD_INFO;
3765 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3771 assign_thread_ids =
true;
3772 goto restart_radix_check;
3776 #if KMP_MIC && REDUCE_TEAM_SIZE
3779 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3782 for (index = threadIdIndex; index <= maxIndex; index++) {
3783 if (counts[index] > maxCt[index]) {
3784 maxCt[index] = counts[index];
3788 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
3789 nCoresPerPkg = maxCt[coreIdIndex];
3790 nPackages = totals[pkgIdIndex];
3796 __kmp_ncores = totals[coreIdIndex];
3797 if (!KMP_AFFINITY_CAPABLE()) {
3798 KMP_ASSERT(__kmp_affinity.type == affinity_none);
3802 #if KMP_MIC && REDUCE_TEAM_SIZE
3804 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
3805 __kmp_dflt_team_nth = teamSize;
3806 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
3807 "__kmp_dflt_team_nth = %d\n",
3808 __kmp_dflt_team_nth));
3812 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
3819 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
3820 for (index = threadIdIndex; index < maxIndex; index++) {
3821 KMP_ASSERT(totals[index] >= totals[index + 1]);
3822 inMap[index] = (totals[index] > totals[index + 1]);
3824 inMap[maxIndex] = (totals[maxIndex] > 1);
3825 inMap[pkgIdIndex] =
true;
3826 inMap[coreIdIndex] =
true;
3827 inMap[threadIdIndex] =
true;
3831 kmp_hw_t types[KMP_HW_LAST];
3834 int threadLevel = -1;
3835 for (index = threadIdIndex; index <= maxIndex; index++) {
3840 if (inMap[pkgIdIndex]) {
3842 types[idx++] = KMP_HW_SOCKET;
3844 if (inMap[coreIdIndex]) {
3846 types[idx++] = KMP_HW_CORE;
3848 if (inMap[threadIdIndex]) {
3850 types[idx++] = KMP_HW_THREAD;
3852 KMP_ASSERT(depth > 0);
3855 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
3857 for (i = 0; i < num_avail; ++i) {
3858 unsigned os = threadInfo[i][osIdIndex];
3860 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3862 hw_thread.os_id = os;
3863 hw_thread.original_idx = i;
3866 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
3867 if (!inMap[src_index]) {
3870 if (src_index == pkgIdIndex) {
3871 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
3872 }
else if (src_index == coreIdIndex) {
3873 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
3874 }
else if (src_index == threadIdIndex) {
3875 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
3885 CLEANUP_THREAD_INFO;
3886 __kmp_topology->sort_ids();
3888 int tlevel = __kmp_topology->get_level(KMP_HW_THREAD);
3891 if (__kmp_topology->at(0).ids[tlevel] == kmp_hw_thread_t::UNKNOWN_ID) {
3892 __kmp_topology->at(0).ids[tlevel] = 0;
3894 for (
int i = 1; i < __kmp_topology->get_num_hw_threads(); ++i) {
3895 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3896 if (hw_thread.ids[tlevel] != kmp_hw_thread_t::UNKNOWN_ID)
3898 kmp_hw_thread_t &prev_hw_thread = __kmp_topology->at(i - 1);
3902 for (
int j = 0; j < tlevel; ++j) {
3903 if (hw_thread.ids[j] != prev_hw_thread.ids[j]) {
3904 hw_thread.ids[tlevel] = 0;
3908 if (hw_thread.ids[tlevel] == kmp_hw_thread_t::UNKNOWN_ID)
3909 hw_thread.ids[tlevel] = prev_hw_thread.ids[tlevel] + 1;
3913 if (!__kmp_topology->check_ids()) {
3914 kmp_topology_t::deallocate(__kmp_topology);
3915 __kmp_topology =
nullptr;
3916 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3925 template <
typename FindNextFunctionType>
3926 static void __kmp_create_os_id_masks(
unsigned *numUnique,
3927 kmp_affinity_t &affinity,
3928 FindNextFunctionType find_next) {
3932 int numAddrs = __kmp_topology->get_num_hw_threads();
3933 int depth = __kmp_topology->get_depth();
3934 const char *env_var = __kmp_get_affinity_env_var(affinity);
3935 KMP_ASSERT(numAddrs);
3945 for (i = numAddrs - 1;; --i) {
3946 int osId = __kmp_topology->at(i).os_id;
3947 if (osId > maxOsId) {
3953 affinity.num_os_id_masks = maxOsId + 1;
3954 KMP_CPU_ALLOC_ARRAY(affinity.os_id_masks, affinity.num_os_id_masks);
3955 KMP_ASSERT(affinity.gran_levels >= 0);
3956 if (affinity.flags.verbose && (affinity.gran_levels > 0)) {
3957 KMP_INFORM(ThreadsMigrate, env_var, affinity.gran_levels);
3959 if (affinity.gran_levels >= (
int)depth) {
3960 KMP_AFF_WARNING(affinity, AffThreadsMayMigrate);
3970 kmp_affin_mask_t *sum;
3971 KMP_CPU_ALLOC_ON_STACK(sum);
3974 i = j = leader = find_next(-1);
3975 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3976 kmp_full_mask_modifier_t full_mask;
3977 for (i = find_next(i); i < numAddrs; i = find_next(i)) {
3981 if (__kmp_topology->is_close(leader, i, affinity)) {
3982 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3988 for (; j < i; j = find_next(j)) {
3989 int osId = __kmp_topology->at(j).os_id;
3990 KMP_DEBUG_ASSERT(osId <= maxOsId);
3991 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3992 KMP_CPU_COPY(mask, sum);
3993 __kmp_topology->at(j).leader = (j == leader);
3999 full_mask.include(sum);
4001 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
4006 for (; j < i; j = find_next(j)) {
4007 int osId = __kmp_topology->at(j).os_id;
4008 KMP_DEBUG_ASSERT(osId <= maxOsId);
4009 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
4010 KMP_CPU_COPY(mask, sum);
4011 __kmp_topology->at(j).leader = (j == leader);
4013 full_mask.include(sum);
4015 KMP_CPU_FREE_FROM_STACK(sum);
4018 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
4019 __kmp_topology->print(env_var);
4022 *numUnique = unique;
4028 static kmp_affin_mask_t *newMasks;
4029 static int numNewMasks;
4030 static int nextNewMask;
4032 #define ADD_MASK(_mask) \
4034 if (nextNewMask >= numNewMasks) { \
4037 kmp_affin_mask_t *temp; \
4038 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
4039 for (i = 0; i < numNewMasks / 2; i++) { \
4040 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
4041 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
4042 KMP_CPU_COPY(dest, src); \
4044 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
4047 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
4051 #define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
4053 if (((_osId) > _maxOsId) || \
4054 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
4055 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, _osId); \
4057 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
4063 static void __kmp_affinity_process_proclist(kmp_affinity_t &affinity) {
4065 kmp_affin_mask_t **out_masks = &affinity.masks;
4066 unsigned *out_numMasks = &affinity.num_masks;
4067 const char *proclist = affinity.proclist;
4068 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4069 int maxOsId = affinity.num_os_id_masks - 1;
4070 const char *scan = proclist;
4071 const char *next = proclist;
4076 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
4078 kmp_affin_mask_t *sumMask;
4079 KMP_CPU_ALLOC(sumMask);
4083 int start, end, stride;
4087 if (*next ==
'\0') {
4099 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
4101 num = __kmp_str_to_int(scan, *next);
4102 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
4105 if ((num > maxOsId) ||
4106 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4107 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4108 KMP_CPU_ZERO(sumMask);
4110 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
4130 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4133 num = __kmp_str_to_int(scan, *next);
4134 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
4137 if ((num > maxOsId) ||
4138 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4139 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4141 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
4158 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4160 start = __kmp_str_to_int(scan, *next);
4161 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
4166 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4180 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4182 end = __kmp_str_to_int(scan, *next);
4183 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
4200 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4202 stride = __kmp_str_to_int(scan, *next);
4203 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
4208 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
4210 KMP_ASSERT2(start <= end,
"bad explicit proc list");
4212 KMP_ASSERT2(start >= end,
"bad explicit proc list");
4214 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
4219 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4221 }
while (start <= end);
4224 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4226 }
while (start >= end);
4237 *out_numMasks = nextNewMask;
4238 if (nextNewMask == 0) {
4240 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4243 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
4244 for (i = 0; i < nextNewMask; i++) {
4245 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
4246 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
4247 KMP_CPU_COPY(dest, src);
4249 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4250 KMP_CPU_FREE(sumMask);
4273 static void __kmp_process_subplace_list(
const char **scan,
4274 kmp_affinity_t &affinity,
int maxOsId,
4275 kmp_affin_mask_t *tempMask,
4278 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4281 int start, count, stride, i;
4285 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4288 start = __kmp_str_to_int(*scan, *next);
4289 KMP_ASSERT(start >= 0);
4294 if (**scan ==
'}' || **scan ==
',') {
4295 if ((start > maxOsId) ||
4296 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4297 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4299 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4302 if (**scan ==
'}') {
4308 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
4313 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4316 count = __kmp_str_to_int(*scan, *next);
4317 KMP_ASSERT(count >= 0);
4322 if (**scan ==
'}' || **scan ==
',') {
4323 for (i = 0; i < count; i++) {
4324 if ((start > maxOsId) ||
4325 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4326 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4329 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4334 if (**scan ==
'}') {
4340 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
4347 if (**scan ==
'+') {
4351 if (**scan ==
'-') {
4359 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4362 stride = __kmp_str_to_int(*scan, *next);
4363 KMP_ASSERT(stride >= 0);
4369 if (**scan ==
'}' || **scan ==
',') {
4370 for (i = 0; i < count; i++) {
4371 if ((start > maxOsId) ||
4372 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4373 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4376 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4381 if (**scan ==
'}') {
4388 KMP_ASSERT2(0,
"bad explicit places list");
4392 static void __kmp_process_place(
const char **scan, kmp_affinity_t &affinity,
4393 int maxOsId, kmp_affin_mask_t *tempMask,
4396 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4400 if (**scan ==
'{') {
4402 __kmp_process_subplace_list(scan, affinity, maxOsId, tempMask, setSize);
4403 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
4405 }
else if (**scan ==
'!') {
4407 __kmp_process_place(scan, affinity, maxOsId, tempMask, setSize);
4408 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
4409 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
4412 int num = __kmp_str_to_int(*scan, *next);
4413 KMP_ASSERT(num >= 0);
4414 if ((num > maxOsId) ||
4415 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4416 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4418 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
4423 KMP_ASSERT2(0,
"bad explicit places list");
4428 void __kmp_affinity_process_placelist(kmp_affinity_t &affinity) {
4429 int i, j, count, stride, sign;
4430 kmp_affin_mask_t **out_masks = &affinity.masks;
4431 unsigned *out_numMasks = &affinity.num_masks;
4432 const char *placelist = affinity.proclist;
4433 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4434 int maxOsId = affinity.num_os_id_masks - 1;
4435 const char *scan = placelist;
4436 const char *next = placelist;
4439 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
4445 kmp_affin_mask_t *tempMask;
4446 kmp_affin_mask_t *previousMask;
4447 KMP_CPU_ALLOC(tempMask);
4448 KMP_CPU_ZERO(tempMask);
4449 KMP_CPU_ALLOC(previousMask);
4450 KMP_CPU_ZERO(previousMask);
4454 __kmp_process_place(&scan, affinity, maxOsId, tempMask, &setSize);
4458 if (*scan ==
'\0' || *scan ==
',') {
4462 KMP_CPU_ZERO(tempMask);
4464 if (*scan ==
'\0') {
4471 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4476 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4479 count = __kmp_str_to_int(scan, *next);
4480 KMP_ASSERT(count >= 0);
4485 if (*scan ==
'\0' || *scan ==
',') {
4488 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4507 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4510 stride = __kmp_str_to_int(scan, *next);
4511 KMP_DEBUG_ASSERT(stride >= 0);
4517 for (i = 0; i < count; i++) {
4522 KMP_CPU_COPY(previousMask, tempMask);
4523 ADD_MASK(previousMask);
4524 KMP_CPU_ZERO(tempMask);
4526 KMP_CPU_SET_ITERATE(j, previousMask) {
4527 if (!KMP_CPU_ISSET(j, previousMask)) {
4530 if ((j + stride > maxOsId) || (j + stride < 0) ||
4531 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
4532 (!KMP_CPU_ISSET(j + stride,
4533 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
4534 if (i < count - 1) {
4535 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, j + stride);
4539 KMP_CPU_SET(j + stride, tempMask);
4543 KMP_CPU_ZERO(tempMask);
4548 if (*scan ==
'\0') {
4556 KMP_ASSERT2(0,
"bad explicit places list");
4559 *out_numMasks = nextNewMask;
4560 if (nextNewMask == 0) {
4562 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4565 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
4566 KMP_CPU_FREE(tempMask);
4567 KMP_CPU_FREE(previousMask);
4568 for (i = 0; i < nextNewMask; i++) {
4569 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
4570 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
4571 KMP_CPU_COPY(dest, src);
4573 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4577 #undef ADD_MASK_OSID
4581 static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
4584 for (
int i = 0; i < nprocs; i++) {
4585 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
4586 for (
int j = bottom_level; j > 0; j--) {
4587 if (hw_thread.ids[j] > 0) {
4588 if (core_level < (j - 1)) {
4598 static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
4600 return __kmp_topology->get_count(core_level);
4603 static int __kmp_affinity_find_core(
int proc,
int bottom_level,
4606 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
4607 for (
int i = 0; i <= proc; ++i) {
4608 if (i + 1 <= proc) {
4609 for (
int j = 0; j <= core_level; ++j) {
4610 if (__kmp_topology->at(i + 1).sub_ids[j] !=
4611 __kmp_topology->at(i).sub_ids[j]) {
4623 static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
4625 if (core_level >= bottom_level)
4627 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
4628 return __kmp_topology->calculate_ratio(thread_level, core_level);
4631 static int *procarr = NULL;
4632 static int __kmp_aff_depth = 0;
4633 static int *__kmp_osid_to_hwthread_map = NULL;
4635 static void __kmp_affinity_get_mask_topology_info(
const kmp_affin_mask_t *mask,
4636 kmp_affinity_ids_t &ids,
4637 kmp_affinity_attrs_t &attrs) {
4638 if (!KMP_AFFINITY_CAPABLE())
4642 for (
int i = 0; i < KMP_HW_LAST; ++i)
4643 ids.ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
4644 attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
4649 int depth = __kmp_topology->get_depth();
4650 KMP_CPU_SET_ITERATE(cpu, mask) {
4651 int osid_idx = __kmp_osid_to_hwthread_map[cpu];
4653 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(osid_idx);
4654 for (
int level = 0; level < depth; ++level) {
4655 kmp_hw_t type = __kmp_topology->get_type(level);
4656 int id = hw_thread.sub_ids[level];
4657 if (ids.ids[type] == kmp_hw_thread_t::UNKNOWN_ID || ids.ids[type] ==
id) {
4662 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4663 for (; level < depth; ++level) {
4664 kmp_hw_t type = __kmp_topology->get_type(level);
4665 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4670 attrs.core_type = hw_thread.attrs.get_core_type();
4671 attrs.core_eff = hw_thread.attrs.get_core_eff();
4675 if (attrs.core_type != hw_thread.attrs.get_core_type())
4676 attrs.core_type = KMP_HW_CORE_TYPE_UNKNOWN;
4677 if (attrs.core_eff != hw_thread.attrs.get_core_eff())
4678 attrs.core_eff = kmp_hw_attr_t::UNKNOWN_CORE_EFF;
4683 static void __kmp_affinity_get_thread_topology_info(kmp_info_t *th) {
4684 if (!KMP_AFFINITY_CAPABLE())
4686 const kmp_affin_mask_t *mask = th->th.th_affin_mask;
4687 kmp_affinity_ids_t &ids = th->th.th_topology_ids;
4688 kmp_affinity_attrs_t &attrs = th->th.th_topology_attrs;
4689 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4695 static void __kmp_affinity_get_topology_info(kmp_affinity_t &affinity) {
4696 if (!KMP_AFFINITY_CAPABLE())
4698 if (affinity.type != affinity_none) {
4699 KMP_ASSERT(affinity.num_os_id_masks);
4700 KMP_ASSERT(affinity.os_id_masks);
4702 KMP_ASSERT(affinity.num_masks);
4703 KMP_ASSERT(affinity.masks);
4704 KMP_ASSERT(__kmp_affin_fullMask);
4706 int max_cpu = __kmp_affin_fullMask->get_max_cpu();
4707 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4710 if (!affinity.ids) {
4711 affinity.ids = (kmp_affinity_ids_t *)__kmp_allocate(
4712 sizeof(kmp_affinity_ids_t) * affinity.num_masks);
4714 if (!affinity.attrs) {
4715 affinity.attrs = (kmp_affinity_attrs_t *)__kmp_allocate(
4716 sizeof(kmp_affinity_attrs_t) * affinity.num_masks);
4718 if (!__kmp_osid_to_hwthread_map) {
4720 __kmp_osid_to_hwthread_map =
4721 (
int *)__kmp_allocate(
sizeof(
int) * (max_cpu + 1));
4725 for (
int hw_thread = 0; hw_thread < num_hw_threads; ++hw_thread) {
4726 int os_id = __kmp_topology->at(hw_thread).os_id;
4727 if (KMP_CPU_ISSET(os_id, __kmp_affin_fullMask))
4728 __kmp_osid_to_hwthread_map[os_id] = hw_thread;
4731 for (
unsigned i = 0; i < affinity.num_masks; ++i) {
4732 kmp_affinity_ids_t &ids = affinity.ids[i];
4733 kmp_affinity_attrs_t &attrs = affinity.attrs[i];
4734 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.masks, i);
4735 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4740 static void __kmp_aux_affinity_initialize_other_data(kmp_affinity_t &affinity) {
4742 if (__kmp_topology && __kmp_topology->get_num_hw_threads()) {
4743 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
4744 __kmp_affinity_get_topology_info(affinity);
4745 #if KMP_WEIGHTED_ITERATIONS_SUPPORTED
4746 __kmp_first_osid_with_ecore = __kmp_get_first_osid_with_ecore();
4753 static void __kmp_create_affinity_none_places(kmp_affinity_t &affinity) {
4754 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4755 KMP_ASSERT(affinity.type == affinity_none);
4756 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4757 affinity.num_masks = 1;
4758 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4759 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, 0);
4760 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4761 __kmp_aux_affinity_initialize_other_data(affinity);
4764 static void __kmp_aux_affinity_initialize_masks(kmp_affinity_t &affinity) {
4769 int verbose = affinity.flags.verbose;
4770 const char *env_var = affinity.env_var;
4773 if (__kmp_affin_fullMask && __kmp_affin_origMask)
4776 if (__kmp_affin_fullMask == NULL) {
4777 KMP_CPU_ALLOC(__kmp_affin_fullMask);
4779 if (__kmp_affin_origMask == NULL) {
4780 KMP_CPU_ALLOC(__kmp_affin_origMask);
4782 if (KMP_AFFINITY_CAPABLE()) {
4783 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4785 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4786 if (affinity.flags.respect) {
4789 __kmp_avail_proc = 0;
4790 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4791 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4796 if (__kmp_avail_proc > __kmp_xproc) {
4797 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4798 affinity.type = affinity_none;
4799 KMP_AFFINITY_DISABLE();
4804 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4805 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4806 __kmp_affin_fullMask);
4807 KMP_INFORM(InitOSProcSetRespect, env_var, buf);
4811 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4812 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4813 __kmp_affin_fullMask);
4814 KMP_INFORM(InitOSProcSetNotRespect, env_var, buf);
4817 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4819 if (__kmp_num_proc_groups <= 1) {
4821 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4825 __kmp_affin_fullMask->set_process_affinity(
true);
4831 static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
4832 bool success =
false;
4833 const char *env_var = affinity.env_var;
4834 kmp_i18n_id_t msg_id = kmp_i18n_null;
4835 int verbose = affinity.flags.verbose;
4839 if ((__kmp_cpuinfo_file != NULL) &&
4840 (__kmp_affinity_top_method == affinity_top_method_all)) {
4841 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4844 if (__kmp_affinity_top_method == affinity_top_method_all) {
4850 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4851 if (!__kmp_hwloc_error) {
4852 success = __kmp_affinity_create_hwloc_map(&msg_id);
4853 if (!success && verbose) {
4854 KMP_INFORM(AffIgnoringHwloc, env_var);
4856 }
else if (verbose) {
4857 KMP_INFORM(AffIgnoringHwloc, env_var);
4862 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
4864 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4865 if (!success && verbose && msg_id != kmp_i18n_null) {
4866 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4870 success = __kmp_affinity_create_apicid_map(&msg_id);
4871 if (!success && verbose && msg_id != kmp_i18n_null) {
4872 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4877 #if KMP_OS_LINUX || KMP_OS_AIX
4880 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4881 if (!success && verbose && msg_id != kmp_i18n_null) {
4882 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4887 #if KMP_GROUP_AFFINITY
4888 if (!success && (__kmp_num_proc_groups > 1)) {
4889 success = __kmp_affinity_create_proc_group_map(&msg_id);
4890 if (!success && verbose && msg_id != kmp_i18n_null) {
4891 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4897 success = __kmp_affinity_create_flat_map(&msg_id);
4898 if (!success && verbose && msg_id != kmp_i18n_null) {
4899 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4901 KMP_ASSERT(success);
4909 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4910 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4911 success = __kmp_affinity_create_hwloc_map(&msg_id);
4913 KMP_ASSERT(msg_id != kmp_i18n_null);
4914 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4919 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
4920 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
4921 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
4922 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4924 KMP_ASSERT(msg_id != kmp_i18n_null);
4925 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4927 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4928 success = __kmp_affinity_create_apicid_map(&msg_id);
4930 KMP_ASSERT(msg_id != kmp_i18n_null);
4931 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4936 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4938 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4940 KMP_ASSERT(msg_id != kmp_i18n_null);
4941 const char *filename = __kmp_cpuinfo_get_filename();
4943 KMP_FATAL(FileLineMsgExiting, filename, line,
4944 __kmp_i18n_catgets(msg_id));
4946 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4951 #if KMP_GROUP_AFFINITY
4952 else if (__kmp_affinity_top_method == affinity_top_method_group) {
4953 success = __kmp_affinity_create_proc_group_map(&msg_id);
4954 KMP_ASSERT(success);
4956 KMP_ASSERT(msg_id != kmp_i18n_null);
4957 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4962 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4963 success = __kmp_affinity_create_flat_map(&msg_id);
4965 KMP_ASSERT(success);
4969 if (!__kmp_topology) {
4970 if (KMP_AFFINITY_CAPABLE()) {
4971 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4973 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
4975 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
4976 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
4977 __kmp_nThreadsPerCore, __kmp_ncores);
4979 __kmp_topology->print(env_var);
4986 __kmp_topology->canonicalize();
4988 __kmp_topology->print(env_var);
4989 bool filtered = __kmp_topology->filter_hw_subset();
4990 if (filtered && verbose)
4991 __kmp_topology->print(
"KMP_HW_SUBSET");
4995 static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
4996 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4997 bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
4998 const char *env_var = __kmp_get_affinity_env_var(affinity);
5000 if (affinity.flags.initialized) {
5001 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5005 if (is_regular_affinity && (!__kmp_affin_fullMask || !__kmp_affin_origMask))
5006 __kmp_aux_affinity_initialize_masks(affinity);
5008 if (is_regular_affinity && !__kmp_topology) {
5009 bool success = __kmp_aux_affinity_initialize_topology(affinity);
5011 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
5013 affinity.type = affinity_none;
5014 KMP_AFFINITY_DISABLE();
5021 if (affinity.type == affinity_none) {
5022 __kmp_create_affinity_none_places(affinity);
5023 #if KMP_USE_HIER_SCHED
5024 __kmp_dispatch_set_hierarchy_values();
5026 affinity.flags.initialized = TRUE;
5030 __kmp_topology->set_granularity(affinity);
5031 int depth = __kmp_topology->get_depth();
5034 unsigned numUnique = 0;
5035 int numAddrs = __kmp_topology->get_num_hw_threads();
5038 if (affinity.core_attr_gran.valid) {
5039 __kmp_create_os_id_masks(&numUnique, affinity, [&](
int idx) {
5040 KMP_ASSERT(idx >= -1);
5041 for (
int i = idx + 1; i < numAddrs; ++i)
5042 if (__kmp_topology->at(i).attrs.contains(affinity.core_attr_gran))
5046 if (!affinity.os_id_masks) {
5047 const char *core_attribute;
5048 if (affinity.core_attr_gran.core_eff != kmp_hw_attr_t::UNKNOWN_CORE_EFF)
5049 core_attribute =
"core_efficiency";
5051 core_attribute =
"core_type";
5052 KMP_AFF_WARNING(affinity, AffIgnoringNotAvailable, env_var,
5054 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true))
5060 if (!affinity.os_id_masks) {
5061 int gran = affinity.gran_levels;
5062 int gran_level = depth - 1 - affinity.gran_levels;
5063 if (gran >= 0 && gran_level >= 0 && gran_level < depth) {
5064 __kmp_create_os_id_masks(
5065 &numUnique, affinity, [depth, numAddrs, &affinity](
int idx) {
5066 KMP_ASSERT(idx >= -1);
5067 int gran = affinity.gran_levels;
5068 int gran_level = depth - 1 - affinity.gran_levels;
5069 for (
int i = idx + 1; i < numAddrs; ++i)
5070 if ((gran >= depth) ||
5071 (gran < depth && __kmp_topology->at(i).ids[gran_level] !=
5072 kmp_hw_thread_t::UNKNOWN_ID))
5079 if (!affinity.os_id_masks) {
5080 __kmp_create_os_id_masks(&numUnique, affinity, [](
int idx) {
5081 KMP_ASSERT(idx >= -1);
5086 switch (affinity.type) {
5088 case affinity_explicit:
5089 KMP_DEBUG_ASSERT(affinity.proclist != NULL);
5090 if (is_hidden_helper_affinity ||
5091 __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
5092 __kmp_affinity_process_proclist(affinity);
5094 __kmp_affinity_process_placelist(affinity);
5096 if (affinity.num_masks == 0) {
5097 KMP_AFF_WARNING(affinity, AffNoValidProcID);
5098 affinity.type = affinity_none;
5099 __kmp_create_affinity_none_places(affinity);
5100 affinity.flags.initialized = TRUE;
5109 case affinity_logical:
5110 affinity.compact = 0;
5111 if (affinity.offset) {
5113 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
5117 case affinity_physical:
5118 if (__kmp_nThreadsPerCore > 1) {
5119 affinity.compact = 1;
5120 if (affinity.compact >= depth) {
5121 affinity.compact = 0;
5124 affinity.compact = 0;
5126 if (affinity.offset) {
5128 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
5132 case affinity_scatter:
5133 if (affinity.compact >= depth) {
5134 affinity.compact = 0;
5136 affinity.compact = depth - 1 - affinity.compact;
5140 case affinity_compact:
5141 if (affinity.compact >= depth) {
5142 affinity.compact = depth - 1;
5146 case affinity_balanced:
5147 if (depth <= 1 || is_hidden_helper_affinity) {
5148 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
5149 affinity.type = affinity_none;
5150 __kmp_create_affinity_none_places(affinity);
5151 affinity.flags.initialized = TRUE;
5153 }
else if (!__kmp_topology->is_uniform()) {
5155 __kmp_aff_depth = depth;
5158 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
5159 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
5161 int maxprocpercore = __kmp_affinity_max_proc_per_core(
5162 __kmp_avail_proc, depth - 1, core_level);
5164 int nproc = ncores * maxprocpercore;
5165 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
5166 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
5167 affinity.type = affinity_none;
5168 __kmp_create_affinity_none_places(affinity);
5169 affinity.flags.initialized = TRUE;
5173 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5174 for (
int i = 0; i < nproc; i++) {
5180 for (
int i = 0; i < __kmp_avail_proc; i++) {
5181 int proc = __kmp_topology->at(i).os_id;
5182 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
5184 if (core == lastcore) {
5191 procarr[core * maxprocpercore + inlastcore] = proc;
5194 if (affinity.compact >= depth) {
5195 affinity.compact = depth - 1;
5200 if (affinity.flags.dups) {
5201 affinity.num_masks = __kmp_avail_proc;
5203 affinity.num_masks = numUnique;
5206 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
5207 (__kmp_affinity_num_places > 0) &&
5208 ((
unsigned)__kmp_affinity_num_places < affinity.num_masks) &&
5209 !is_hidden_helper_affinity) {
5210 affinity.num_masks = __kmp_affinity_num_places;
5213 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
5217 __kmp_topology->sort_compact(affinity);
5221 int num_hw_threads = __kmp_topology->get_num_hw_threads();
5222 kmp_full_mask_modifier_t full_mask;
5223 for (i = 0, j = 0; i < num_hw_threads; i++) {
5224 if ((!affinity.flags.dups) && (!__kmp_topology->at(i).leader)) {
5227 int osId = __kmp_topology->at(i).os_id;
5229 kmp_affin_mask_t *src = KMP_CPU_INDEX(affinity.os_id_masks, osId);
5230 if (KMP_CPU_ISEMPTY(src))
5232 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, j);
5233 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
5234 KMP_CPU_COPY(dest, src);
5235 full_mask.include(src);
5236 if (++j >= affinity.num_masks) {
5240 KMP_DEBUG_ASSERT(j == affinity.num_masks);
5242 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
5243 __kmp_topology->print(env_var);
5247 __kmp_topology->sort_ids();
5251 KMP_ASSERT2(0,
"Unexpected affinity setting");
5253 __kmp_aux_affinity_initialize_other_data(affinity);
5254 affinity.flags.initialized = TRUE;
5257 void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
5266 int disabled = (affinity.type == affinity_disabled);
5267 if (!KMP_AFFINITY_CAPABLE())
5268 KMP_ASSERT(disabled);
5270 affinity.type = affinity_none;
5271 __kmp_aux_affinity_initialize(affinity);
5273 affinity.type = affinity_disabled;
5276 void __kmp_affinity_uninitialize(
void) {
5277 for (kmp_affinity_t *affinity : __kmp_affinities) {
5278 if (affinity->masks != NULL)
5279 KMP_CPU_FREE_ARRAY(affinity->masks, affinity->num_masks);
5280 if (affinity->os_id_masks != NULL)
5281 KMP_CPU_FREE_ARRAY(affinity->os_id_masks, affinity->num_os_id_masks);
5282 if (affinity->proclist != NULL)
5283 __kmp_free(affinity->proclist);
5284 if (affinity->ids != NULL)
5285 __kmp_free(affinity->ids);
5286 if (affinity->attrs != NULL)
5287 __kmp_free(affinity->attrs);
5288 *affinity = KMP_AFFINITY_INIT(affinity->env_var);
5290 if (__kmp_affin_origMask != NULL) {
5291 if (KMP_AFFINITY_CAPABLE()) {
5294 bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
5296 __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
5299 KMP_CPU_FREE(__kmp_affin_origMask);
5300 __kmp_affin_origMask = NULL;
5302 __kmp_affinity_num_places = 0;
5303 if (procarr != NULL) {
5304 __kmp_free(procarr);
5307 if (__kmp_osid_to_hwthread_map) {
5308 __kmp_free(__kmp_osid_to_hwthread_map);
5309 __kmp_osid_to_hwthread_map = NULL;
5312 if (__kmp_hwloc_topology != NULL) {
5313 hwloc_topology_destroy(__kmp_hwloc_topology);
5314 __kmp_hwloc_topology = NULL;
5317 if (__kmp_hw_subset) {
5318 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
5319 __kmp_hw_subset =
nullptr;
5321 if (__kmp_topology) {
5322 kmp_topology_t::deallocate(__kmp_topology);
5323 __kmp_topology =
nullptr;
5325 KMPAffinity::destroy_api();
5328 static void __kmp_select_mask_by_gtid(
int gtid,
const kmp_affinity_t *affinity,
5329 int *place, kmp_affin_mask_t **mask) {
5331 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5332 if (is_hidden_helper)
5335 mask_idx = gtid - 2;
5337 mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
5338 KMP_DEBUG_ASSERT(affinity->num_masks > 0);
5339 *place = (mask_idx + affinity->offset) % affinity->num_masks;
5340 *mask = KMP_CPU_INDEX(affinity->masks, *place);
5345 void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
5347 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5350 for (
int id = 0;
id < KMP_HW_LAST; ++id)
5351 th->th.th_topology_ids.ids[
id] = kmp_hw_thread_t::UNKNOWN_ID;
5352 th->th.th_topology_attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
5354 if (!KMP_AFFINITY_CAPABLE()) {
5358 if (th->th.th_affin_mask == NULL) {
5359 KMP_CPU_ALLOC(th->th.th_affin_mask);
5361 KMP_CPU_ZERO(th->th.th_affin_mask);
5369 kmp_affin_mask_t *mask;
5371 const kmp_affinity_t *affinity;
5372 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5374 if (is_hidden_helper)
5375 affinity = &__kmp_hh_affinity;
5377 affinity = &__kmp_affinity;
5379 if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
5380 if ((affinity->type == affinity_none) ||
5381 (affinity->type == affinity_balanced) ||
5382 KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5383 #if KMP_GROUP_AFFINITY
5384 if (__kmp_num_proc_groups > 1) {
5388 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5390 mask = __kmp_affin_fullMask;
5392 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5395 if (!isa_root || __kmp_nested_proc_bind.bind_types[0] == proc_bind_false) {
5396 #if KMP_GROUP_AFFINITY
5397 if (__kmp_num_proc_groups > 1) {
5401 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5403 mask = __kmp_affin_fullMask;
5405 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5409 th->th.th_current_place = i;
5410 if (isa_root && !is_hidden_helper) {
5411 th->th.th_new_place = i;
5412 th->th.th_first_place = 0;
5413 th->th.th_last_place = affinity->num_masks - 1;
5414 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
5417 th->th.th_first_place = 0;
5418 th->th.th_last_place = affinity->num_masks - 1;
5422 th->th.th_topology_ids = __kmp_affinity.ids[i];
5423 th->th.th_topology_attrs = __kmp_affinity.attrs[i];
5426 if (i == KMP_PLACE_ALL) {
5427 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to all places\n",
5430 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to place %d\n",
5434 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5437 void __kmp_affinity_bind_init_mask(
int gtid) {
5438 if (!KMP_AFFINITY_CAPABLE()) {
5441 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5442 const kmp_affinity_t *affinity;
5443 const char *env_var;
5444 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5446 if (is_hidden_helper)
5447 affinity = &__kmp_hh_affinity;
5449 affinity = &__kmp_affinity;
5450 env_var = __kmp_get_affinity_env_var(*affinity,
true);
5452 if (affinity->flags.verbose && (affinity->type == affinity_none ||
5453 (th->th.th_current_place != KMP_PLACE_ALL &&
5454 affinity->type != affinity_balanced)) &&
5455 !KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5456 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5457 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5458 th->th.th_affin_mask);
5459 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5467 if (affinity->type == affinity_none) {
5468 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
5473 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5477 void __kmp_affinity_bind_place(
int gtid) {
5479 if (!KMP_AFFINITY_CAPABLE() || KMP_HIDDEN_HELPER_THREAD(gtid)) {
5483 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5485 KA_TRACE(100, (
"__kmp_affinity_bind_place: binding T#%d to place %d (current "
5487 gtid, th->th.th_new_place, th->th.th_current_place));
5490 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5491 KMP_ASSERT(th->th.th_new_place >= 0);
5492 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity.num_masks);
5493 if (th->th.th_first_place <= th->th.th_last_place) {
5494 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
5495 (th->th.th_new_place <= th->th.th_last_place));
5497 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
5498 (th->th.th_new_place >= th->th.th_last_place));
5503 kmp_affin_mask_t *mask =
5504 KMP_CPU_INDEX(__kmp_affinity.masks, th->th.th_new_place);
5505 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5506 th->th.th_current_place = th->th.th_new_place;
5508 if (__kmp_affinity.flags.verbose) {
5509 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5510 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5511 th->th.th_affin_mask);
5512 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
5513 __kmp_gettid(), gtid, buf);
5515 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5518 int __kmp_aux_set_affinity(
void **mask) {
5523 if (!KMP_AFFINITY_CAPABLE()) {
5527 gtid = __kmp_entry_gtid();
5530 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5531 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5532 (kmp_affin_mask_t *)(*mask));
5534 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
5538 if (__kmp_env_consistency_check) {
5539 if ((mask == NULL) || (*mask == NULL)) {
5540 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5545 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
5546 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5547 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5549 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
5554 if (num_procs == 0) {
5555 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5558 #if KMP_GROUP_AFFINITY
5559 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
5560 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5566 th = __kmp_threads[gtid];
5567 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5568 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5570 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
5573 th->th.th_current_place = KMP_PLACE_UNDEFINED;
5574 th->th.th_new_place = KMP_PLACE_UNDEFINED;
5575 th->th.th_first_place = 0;
5576 th->th.th_last_place = __kmp_affinity.num_masks - 1;
5579 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
5584 int __kmp_aux_get_affinity(
void **mask) {
5587 #if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5590 if (!KMP_AFFINITY_CAPABLE()) {
5594 gtid = __kmp_entry_gtid();
5595 #if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5596 th = __kmp_threads[gtid];
5600 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5604 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5605 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5606 th->th.th_affin_mask);
5608 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
5612 if (__kmp_env_consistency_check) {
5613 if ((mask == NULL) || (*mask == NULL)) {
5614 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
5618 #if !KMP_OS_WINDOWS && !KMP_OS_AIX
5620 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5623 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5624 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5625 (kmp_affin_mask_t *)(*mask));
5627 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
5635 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
5641 int __kmp_aux_get_affinity_max_proc() {
5642 if (!KMP_AFFINITY_CAPABLE()) {
5645 #if KMP_GROUP_AFFINITY
5646 if (__kmp_num_proc_groups > 1) {
5647 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
5653 int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
5654 if (!KMP_AFFINITY_CAPABLE()) {
5660 int gtid = __kmp_entry_gtid();
5661 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5662 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5663 (kmp_affin_mask_t *)(*mask));
5664 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
5665 "affinity mask for thread %d = %s\n",
5669 if (__kmp_env_consistency_check) {
5670 if ((mask == NULL) || (*mask == NULL)) {
5671 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
5675 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5678 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5682 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
5686 int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
5687 if (!KMP_AFFINITY_CAPABLE()) {
5693 int gtid = __kmp_entry_gtid();
5694 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5695 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5696 (kmp_affin_mask_t *)(*mask));
5697 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
5698 "affinity mask for thread %d = %s\n",
5702 if (__kmp_env_consistency_check) {
5703 if ((mask == NULL) || (*mask == NULL)) {
5704 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
5708 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5711 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5715 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
5719 int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
5720 if (!KMP_AFFINITY_CAPABLE()) {
5726 int gtid = __kmp_entry_gtid();
5727 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5728 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5729 (kmp_affin_mask_t *)(*mask));
5730 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
5731 "affinity mask for thread %d = %s\n",
5735 if (__kmp_env_consistency_check) {
5736 if ((mask == NULL) || (*mask == NULL)) {
5737 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
5741 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5744 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5748 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
5751 #if KMP_WEIGHTED_ITERATIONS_SUPPORTED
5753 int __kmp_get_first_osid_with_ecore(
void) {
5755 int high = __kmp_topology->get_num_hw_threads() - 1;
5757 while (high - low > 1) {
5758 mid = (high + low) / 2;
5759 if (__kmp_topology->at(mid).attrs.get_core_type() ==
5760 KMP_HW_CORE_TYPE_CORE) {
5766 if (__kmp_topology->at(mid).attrs.get_core_type() == KMP_HW_CORE_TYPE_ATOM) {
5774 void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
5775 KMP_DEBUG_ASSERT(th);
5776 bool fine_gran =
true;
5777 int tid = th->th.th_info.ds.ds_tid;
5778 const char *env_var =
"KMP_AFFINITY";
5781 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
5784 switch (__kmp_affinity.gran) {
5788 if (__kmp_nThreadsPerCore > 1) {
5793 if (nCoresPerPkg > 1) {
5801 if (__kmp_topology->is_uniform()) {
5805 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
5807 int ncores = __kmp_ncores;
5808 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
5809 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
5813 int chunk = nthreads / ncores;
5815 int big_cores = nthreads % ncores;
5817 int big_nth = (chunk + 1) * big_cores;
5818 if (tid < big_nth) {
5819 coreID = tid / (chunk + 1);
5820 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5822 coreID = (tid - big_cores) / chunk;
5823 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5825 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5826 "Illegal set affinity operation when not capable");
5828 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5833 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
5834 KMP_CPU_SET(osID, mask);
5836 for (
int i = 0; i < __kmp_nth_per_core; i++) {
5838 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
5839 KMP_CPU_SET(osID, mask);
5842 if (__kmp_affinity.flags.verbose) {
5843 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5844 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5845 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5848 __kmp_affinity_get_thread_topology_info(th);
5849 __kmp_set_system_affinity(mask, TRUE);
5852 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5856 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
5857 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
5858 __kmp_aff_depth - 1, core_level);
5859 int nth_per_core = __kmp_affinity_max_proc_per_core(
5860 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5864 if (nthreads == __kmp_avail_proc) {
5866 int osID = __kmp_topology->at(tid).os_id;
5867 KMP_CPU_SET(osID, mask);
5870 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
5871 for (
int i = 0; i < __kmp_avail_proc; i++) {
5872 int osID = __kmp_topology->at(i).os_id;
5873 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
5875 KMP_CPU_SET(osID, mask);
5879 }
else if (nthreads <= ncores) {
5882 for (
int i = 0; i < ncores; i++) {
5885 for (
int j = 0; j < nth_per_core; j++) {
5886 if (procarr[i * nth_per_core + j] != -1) {
5893 for (
int j = 0; j < nth_per_core; j++) {
5894 int osID = procarr[i * nth_per_core + j];
5896 KMP_CPU_SET(osID, mask);
5912 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
5914 int *ncores_with_x_procs =
5915 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5917 int *ncores_with_x_to_max_procs =
5918 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5920 for (
int i = 0; i <= nth_per_core; i++) {
5921 ncores_with_x_procs[i] = 0;
5922 ncores_with_x_to_max_procs[i] = 0;
5925 for (
int i = 0; i < ncores; i++) {
5927 for (
int j = 0; j < nth_per_core; j++) {
5928 if (procarr[i * nth_per_core + j] != -1) {
5932 nproc_at_core[i] = cnt;
5933 ncores_with_x_procs[cnt]++;
5936 for (
int i = 0; i <= nth_per_core; i++) {
5937 for (
int j = i; j <= nth_per_core; j++) {
5938 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5943 int nproc = nth_per_core * ncores;
5945 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5946 for (
int i = 0; i < nproc; i++) {
5953 for (
int j = 1; j <= nth_per_core; j++) {
5954 int cnt = ncores_with_x_to_max_procs[j];
5955 for (
int i = 0; i < ncores; i++) {
5957 if (nproc_at_core[i] == 0) {
5960 for (
int k = 0; k < nth_per_core; k++) {
5961 if (procarr[i * nth_per_core + k] != -1) {
5962 if (newarr[i * nth_per_core + k] == 0) {
5963 newarr[i * nth_per_core + k] = 1;
5969 newarr[i * nth_per_core + k]++;
5977 if (cnt == 0 || nth == 0) {
5988 for (
int i = 0; i < nproc; i++) {
5992 int osID = procarr[i];
5993 KMP_CPU_SET(osID, mask);
5995 int coreID = i / nth_per_core;
5996 for (
int ii = 0; ii < nth_per_core; ii++) {
5997 int osID = procarr[coreID * nth_per_core + ii];
5999 KMP_CPU_SET(osID, mask);
6009 if (__kmp_affinity.flags.verbose) {
6010 char buf[KMP_AFFIN_MASK_PRINT_LEN];
6011 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
6012 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
6015 __kmp_affinity_get_thread_topology_info(th);
6016 __kmp_set_system_affinity(mask, TRUE);
6020 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
6035 kmp_set_thread_affinity_mask_initial()
6040 int gtid = __kmp_get_gtid();
6043 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6044 "non-omp thread, returning\n"));
6047 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
6048 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6049 "affinity not initialized, returning\n"));
6052 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6053 "set full mask for thread %d\n",
6055 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
6057 return bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
6059 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
int try_open(const char *filename, const char *mode)