14#include "kmp_affinity.h"
18#include "kmp_wrapper_getpid.h"
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
32kmp_topology_t *__kmp_topology =
nullptr;
34kmp_hw_subset_t *__kmp_hw_subset =
nullptr;
37static hierarchy_info machine_hierarchy;
39void __kmp_cleanup_hierarchy() { machine_hierarchy.fini(); }
41#if KMP_AFFINITY_SUPPORTED
43class 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);
66static 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;
78void __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;
98static int nCoresPerPkg, nPackages;
99static int __kmp_nThreadsPerCore;
100#ifndef KMP_DFLT_NTH_CORES
101static int __kmp_ncores;
104const 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;
138const 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;
172const 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__)
201int 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
238int 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])
264void 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());
286void 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
334void 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();
357void 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);
441void 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);
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);
470void 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];
535int 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;
572void kmp_topology_t::_discover_uniformity() {
574 for (
int level = 0; level < depth; ++level)
576 flags.uniform = (num == count[depth - 1]);
580void 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];
611void 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);
639kmp_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];
672void kmp_topology_t::deallocate(kmp_topology_t *topology) {
674 __kmp_free(topology);
677bool 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]) {
698void 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");
745void 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
875void 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++;
956void 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();
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]);
989void 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
1015static 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());
1030bool 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);
1068bool 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);
1407bool 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])
1428bool KMPAffinity::picked_api =
false;
1430void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
1431void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
1432void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
1433void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
1434void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
1435void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
1437void 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();
1450 affinity_dispatch =
new KMPNativeAffinity();
1452 __kmp_affinity_dispatch = affinity_dispatch;
1456void KMPAffinity::destroy_api() {
1457 if (__kmp_affinity_dispatch != NULL) {
1458 delete __kmp_affinity_dispatch;
1459 __kmp_affinity_dispatch = NULL;
1464#define KMP_ADVANCE_SCAN(scan) \
1465 while (*scan != '\0') { \
1473char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
1474 kmp_affin_mask_t *mask) {
1475 int start = 0, finish = 0, previous = 0;
1478 KMP_ASSERT(buf_len >= 40);
1481 char *end = buf + buf_len - 1;
1484 if (mask->begin() == mask->end()) {
1485 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
1486 KMP_ADVANCE_SCAN(scan);
1487 KMP_ASSERT(scan <= end);
1492 start = mask->begin();
1496 for (finish = mask->next(start), previous = start;
1497 finish == previous + 1 && finish != mask->end();
1498 finish = mask->next(finish)) {
1505 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
1506 KMP_ADVANCE_SCAN(scan);
1508 first_range =
false;
1511 if (previous - start > 1) {
1512 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
1515 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
1516 KMP_ADVANCE_SCAN(scan);
1517 if (previous - start > 0) {
1518 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
1521 KMP_ADVANCE_SCAN(scan);
1524 if (start == mask->end())
1532 KMP_ASSERT(scan <= end);
1535#undef KMP_ADVANCE_SCAN
1541kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
1542 kmp_affin_mask_t *mask) {
1543 int start = 0, finish = 0, previous = 0;
1548 __kmp_str_buf_clear(buf);
1551 if (mask->begin() == mask->end()) {
1552 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
1557 start = mask->begin();
1561 for (finish = mask->next(start), previous = start;
1562 finish == previous + 1 && finish != mask->end();
1563 finish = mask->next(finish)) {
1570 __kmp_str_buf_print(buf,
"%s",
",");
1572 first_range =
false;
1575 if (previous - start > 1) {
1576 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
1579 __kmp_str_buf_print(buf,
"%u", start);
1580 if (previous - start > 0) {
1581 __kmp_str_buf_print(buf,
",%u", previous);
1586 if (start == mask->end())
1594kmp_affin_mask_t *__kmp_affinity_get_offline_cpus() {
1595 kmp_affin_mask_t *offline;
1596 KMP_CPU_ALLOC(offline);
1597 KMP_CPU_ZERO(offline);
1599 int n, begin_cpu, end_cpu;
1601 auto skip_ws = [](FILE *f) {
1605 }
while (isspace(c));
1611 int status = offline_file.
try_open(
"/sys/devices/system/cpu/offline",
"r");
1614 while (!feof(offline_file)) {
1615 skip_ws(offline_file);
1616 n = fscanf(offline_file,
"%d", &begin_cpu);
1619 skip_ws(offline_file);
1620 int c = fgetc(offline_file);
1621 if (c == EOF || c ==
',') {
1623 end_cpu = begin_cpu;
1624 }
else if (c ==
'-') {
1626 skip_ws(offline_file);
1627 n = fscanf(offline_file,
"%d", &end_cpu);
1630 skip_ws(offline_file);
1631 c = fgetc(offline_file);
1637 if (begin_cpu < 0 || begin_cpu >= __kmp_xproc || end_cpu < 0 ||
1638 end_cpu >= __kmp_xproc || begin_cpu > end_cpu) {
1642 for (
int cpu = begin_cpu; cpu <= end_cpu; ++cpu) {
1643 KMP_CPU_SET(cpu, offline);
1651int __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
1655#if KMP_GROUP_AFFINITY
1657 if (__kmp_num_proc_groups > 1) {
1659 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
1660 for (group = 0; group < __kmp_num_proc_groups; group++) {
1662 int num = __kmp_GetActiveProcessorCount(group);
1663 for (i = 0; i < num; i++) {
1664 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1674 kmp_affin_mask_t *offline_cpus = __kmp_affinity_get_offline_cpus();
1675 for (proc = 0; proc < __kmp_xproc; proc++) {
1677 if (KMP_CPU_ISSET(proc, offline_cpus))
1679 KMP_CPU_SET(proc, mask);
1682 KMP_CPU_FREE(offline_cpus);
1691kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1693kmp_affin_mask_t *__kmp_affin_origMask = NULL;
1696static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1697#if HWLOC_API_VERSION >= 0x00020000
1698 return hwloc_obj_type_is_cache(obj->type);
1700 return obj->type == HWLOC_OBJ_CACHE;
1705static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1707 if (__kmp_hwloc_is_cache_type(obj)) {
1708 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1709 return KMP_HW_UNKNOWN;
1710 switch (obj->attr->cache.depth) {
1714#if KMP_MIC_SUPPORTED
1715 if (__kmp_mic_type == mic3) {
1723 return KMP_HW_UNKNOWN;
1726 switch (obj->type) {
1727 case HWLOC_OBJ_PACKAGE:
1728 return KMP_HW_SOCKET;
1729 case HWLOC_OBJ_NUMANODE:
1731 case HWLOC_OBJ_CORE:
1734 return KMP_HW_THREAD;
1735 case HWLOC_OBJ_GROUP:
1736#if HWLOC_API_VERSION >= 0x00020000
1737 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1739 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1741 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1742 return KMP_HW_MODULE;
1743 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1744 return KMP_HW_PROC_GROUP;
1746 return KMP_HW_UNKNOWN;
1747#if HWLOC_API_VERSION >= 0x00020100
1752 return KMP_HW_UNKNOWN;
1759static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1760 hwloc_obj_type_t type) {
1763 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1764 obj->logical_index, type, 0);
1765 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1766 obj->type, first) == obj;
1767 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1776static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1777 hwloc_obj_t lower) {
1779 hwloc_obj_type_t ltype = lower->type;
1780 int lindex = lower->logical_index - 1;
1783 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1784 while (obj && lindex >= 0 &&
1785 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1786 if (obj->userdata) {
1787 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1792 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1795 lower->userdata = RCAST(
void *, sub_id + 1);
1799static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1801 int hw_thread_index, sub_id;
1803 hwloc_obj_t pu, obj, root, prev;
1804 kmp_hw_t types[KMP_HW_LAST];
1805 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1807 hwloc_topology_t tp = __kmp_hwloc_topology;
1808 *msg_id = kmp_i18n_null;
1809 if (__kmp_affinity.flags.verbose) {
1810 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1813 if (!KMP_AFFINITY_CAPABLE()) {
1816 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1818 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1820 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1823 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1825 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1827 __kmp_nThreadsPerCore = 1;
1828 if (__kmp_nThreadsPerCore == 0)
1829 __kmp_nThreadsPerCore = 1;
1830 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1831 if (nCoresPerPkg == 0)
1833 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1837#if HWLOC_API_VERSION >= 0x00020400
1839 int nr_cpu_kinds = hwloc_cpukinds_get_nr(tp, 0);
1841 typedef struct kmp_hwloc_cpukinds_info_t {
1843 kmp_hw_core_type_t core_type;
1844 hwloc_bitmap_t mask;
1845 } kmp_hwloc_cpukinds_info_t;
1846 kmp_hwloc_cpukinds_info_t *cpukinds =
nullptr;
1848 if (nr_cpu_kinds > 0) {
1850 struct hwloc_info_s *infos;
1851 cpukinds = (kmp_hwloc_cpukinds_info_t *)__kmp_allocate(
1852 sizeof(kmp_hwloc_cpukinds_info_t) * nr_cpu_kinds);
1853 for (
unsigned idx = 0; idx < (unsigned)nr_cpu_kinds; ++idx) {
1854 cpukinds[idx].efficiency = -1;
1855 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_UNKNOWN;
1856 cpukinds[idx].mask = hwloc_bitmap_alloc();
1857 if (hwloc_cpukinds_get_info(tp, idx, cpukinds[idx].mask,
1858 &cpukinds[idx].efficiency, &nr_infos, &infos,
1860 for (
unsigned i = 0; i < nr_infos; ++i) {
1861 if (__kmp_str_match(
"CoreType", 8, infos[i].name)) {
1862#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1863 if (__kmp_str_match(
"IntelAtom", 9, infos[i].value)) {
1864 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_ATOM;
1866 }
else if (__kmp_str_match(
"IntelCore", 9, infos[i].value)) {
1867 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_CORE;
1878 root = hwloc_get_root_obj(tp);
1882 obj = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1883 while (obj && obj != root) {
1884#if HWLOC_API_VERSION >= 0x00020000
1885 if (obj->memory_arity) {
1887 for (memory = obj->memory_first_child; memory;
1888 memory = hwloc_get_next_child(tp, obj, memory)) {
1889 if (memory->type == HWLOC_OBJ_NUMANODE)
1892 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1893 types[depth] = KMP_HW_NUMA;
1894 hwloc_types[depth] = memory->type;
1899 type = __kmp_hwloc_type_2_topology_type(obj);
1900 if (type != KMP_HW_UNKNOWN) {
1901 types[depth] = type;
1902 hwloc_types[depth] = obj->type;
1907 KMP_ASSERT(depth > 0);
1910 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1911 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1912 kmp_hw_t temp = types[i];
1913 types[i] = types[j];
1915 hwloc_types[i] = hwloc_types[j];
1916 hwloc_types[j] = hwloc_temp;
1920 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1922 hw_thread_index = 0;
1924 while ((pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu))) {
1925 int index = depth - 1;
1926 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1927 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1930 hw_thread.ids[index] = pu->logical_index;
1931 hw_thread.os_id = pu->os_index;
1932 hw_thread.original_idx = hw_thread_index;
1934#if HWLOC_API_VERSION >= 0x00020400
1936 int cpukind_index = -1;
1937 for (
int i = 0; i < nr_cpu_kinds; ++i) {
1938 if (hwloc_bitmap_isset(cpukinds[i].mask, hw_thread.os_id)) {
1943 if (cpukind_index >= 0) {
1944 hw_thread.attrs.set_core_type(cpukinds[cpukind_index].core_type);
1945 hw_thread.attrs.set_core_eff(cpukinds[cpukind_index].efficiency);
1953 while (obj != root && obj != NULL) {
1955#if HWLOC_API_VERSION >= 0x00020000
1959 if (obj->memory_arity) {
1961 for (memory = obj->memory_first_child; memory;
1962 memory = hwloc_get_next_child(tp, obj, memory)) {
1963 if (memory->type == HWLOC_OBJ_NUMANODE)
1966 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1967 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1969 hw_thread.ids[index] = memory->logical_index;
1970 hw_thread.ids[index + 1] = sub_id;
1977 type = __kmp_hwloc_type_2_topology_type(obj);
1978 if (type != KMP_HW_UNKNOWN) {
1979 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1981 hw_thread.ids[index] = obj->logical_index;
1982 hw_thread.ids[index + 1] = sub_id;
1992#if HWLOC_API_VERSION >= 0x00020400
1995 for (
int idx = 0; idx < nr_cpu_kinds; ++idx)
1996 hwloc_bitmap_free(cpukinds[idx].mask);
1997 __kmp_free(cpukinds);
2000 __kmp_topology->sort_ids();
2008static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
2009 *msg_id = kmp_i18n_null;
2011 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
2013 if (__kmp_affinity.flags.verbose) {
2014 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
2020 if (!KMP_AFFINITY_CAPABLE()) {
2021 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2022 __kmp_ncores = nPackages = __kmp_xproc;
2023 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
2031 __kmp_ncores = nPackages = __kmp_avail_proc;
2032 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
2035 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2038 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2040 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2043 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
2045 hw_thread.os_id = i;
2046 hw_thread.original_idx = avail_ct;
2047 hw_thread.ids[0] = i;
2048 hw_thread.ids[1] = 0;
2049 hw_thread.ids[2] = 0;
2052 if (__kmp_affinity.flags.verbose) {
2053 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
2058#if KMP_GROUP_AFFINITY
2063static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
2064 *msg_id = kmp_i18n_null;
2066 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
2067 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
2069 if (__kmp_affinity.flags.verbose) {
2070 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
2074 if (!KMP_AFFINITY_CAPABLE()) {
2075 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2076 nPackages = __kmp_num_proc_groups;
2077 __kmp_nThreadsPerCore = 1;
2078 __kmp_ncores = __kmp_xproc;
2079 nCoresPerPkg = nPackages / __kmp_ncores;
2084 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2087 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2089 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2092 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
2094 hw_thread.os_id = i;
2095 hw_thread.original_idx = avail_ct;
2096 hw_thread.ids[0] = i / BITS_PER_GROUP;
2097 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
2104#if KMP_ARCH_X86 || KMP_ARCH_X86_64
2106template <kmp_u
int32 LSB, kmp_u
int32 MSB>
2107static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
2108 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
2109 const kmp_uint32 SHIFT_RIGHT = LSB;
2110 kmp_uint32 retval = v;
2111 retval <<= SHIFT_LEFT;
2112 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
2116static int __kmp_cpuid_mask_width(
int count) {
2119 while ((1 << r) < count)
2124class apicThreadInfo {
2128 unsigned maxCoresPerPkg;
2129 unsigned maxThreadsPerPkg;
2135static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
2137 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
2138 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
2139 if (aa->pkgId < bb->pkgId)
2141 if (aa->pkgId > bb->pkgId)
2143 if (aa->coreId < bb->coreId)
2145 if (aa->coreId > bb->coreId)
2147 if (aa->threadId < bb->threadId)
2149 if (aa->threadId > bb->threadId)
2154class cpuid_cache_info_t {
2159 bool operator==(
const info_t &rhs)
const {
2160 return level == rhs.level && mask == rhs.mask;
2162 bool operator!=(
const info_t &rhs)
const {
return !operator==(rhs); }
2164 cpuid_cache_info_t() : depth(0) {
2165 table[MAX_CACHE_LEVEL].level = 0;
2166 table[MAX_CACHE_LEVEL].mask = 0;
2168 size_t get_depth()
const {
return depth; }
2169 info_t &operator[](
size_t index) {
return table[index]; }
2170 const info_t &operator[](
size_t index)
const {
return table[index]; }
2171 bool operator==(
const cpuid_cache_info_t &rhs)
const {
2172 if (rhs.depth != depth)
2174 for (
size_t i = 0; i < depth; ++i)
2175 if (table[i] != rhs.table[i])
2179 bool operator!=(
const cpuid_cache_info_t &rhs)
const {
2180 return !operator==(rhs);
2184 const info_t &get_level(
unsigned level)
const {
2185 for (
size_t i = 0; i < depth; ++i) {
2186 if (table[i].level == level)
2189 return table[MAX_CACHE_LEVEL];
2192 static kmp_hw_t get_topology_type(
unsigned level) {
2193 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
2202 return KMP_HW_UNKNOWN;
2204 void get_leaf4_levels() {
2206 while (depth < MAX_CACHE_LEVEL) {
2207 unsigned cache_type, max_threads_sharing;
2208 unsigned cache_level, cache_mask_width;
2210 __kmp_x86_cpuid(4, level, &buf2);
2211 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
2215 if (cache_type == 2) {
2219 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
2220 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
2221 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
2222 table[depth].level = cache_level;
2223 table[depth].mask = ((-1) << cache_mask_width);
2228 static const int MAX_CACHE_LEVEL = 3;
2232 info_t table[MAX_CACHE_LEVEL + 1];
2239static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
2241 *msg_id = kmp_i18n_null;
2243 if (__kmp_affinity.flags.verbose) {
2244 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
2248 __kmp_x86_cpuid(0, 0, &buf);
2250 *msg_id = kmp_i18n_str_NoLeaf4Support;
2259 if (!KMP_AFFINITY_CAPABLE()) {
2262 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2268 __kmp_x86_cpuid(1, 0, &buf);
2269 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2270 if (maxThreadsPerPkg == 0) {
2271 maxThreadsPerPkg = 1;
2285 __kmp_x86_cpuid(0, 0, &buf);
2287 __kmp_x86_cpuid(4, 0, &buf);
2288 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2306 __kmp_ncores = __kmp_xproc;
2307 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2308 __kmp_nThreadsPerCore = 1;
2317 kmp_affinity_raii_t previous_affinity;
2345 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
2346 __kmp_avail_proc *
sizeof(apicThreadInfo));
2347 unsigned nApics = 0;
2348 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2350 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2353 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
2355 __kmp_affinity_dispatch->bind_thread(i);
2356 threadInfo[nApics].osId = i;
2359 __kmp_x86_cpuid(1, 0, &buf);
2360 if (((buf.edx >> 9) & 1) == 0) {
2361 __kmp_free(threadInfo);
2362 *msg_id = kmp_i18n_str_ApicNotPresent;
2365 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
2366 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2367 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
2368 threadInfo[nApics].maxThreadsPerPkg = 1;
2377 __kmp_x86_cpuid(0, 0, &buf);
2379 __kmp_x86_cpuid(4, 0, &buf);
2380 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2382 threadInfo[nApics].maxCoresPerPkg = 1;
2386 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
2387 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
2389 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
2390 int widthT = widthCT - widthC;
2395 __kmp_free(threadInfo);
2396 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2400 int maskC = (1 << widthC) - 1;
2401 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
2403 int maskT = (1 << widthT) - 1;
2404 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
2411 previous_affinity.restore();
2414 qsort(threadInfo, nApics,
sizeof(*threadInfo),
2415 __kmp_affinity_cmp_apicThreadInfo_phys_id);
2432 __kmp_nThreadsPerCore = 1;
2433 unsigned nCores = 1;
2436 unsigned lastPkgId = threadInfo[0].pkgId;
2437 unsigned coreCt = 1;
2438 unsigned lastCoreId = threadInfo[0].coreId;
2439 unsigned threadCt = 1;
2440 unsigned lastThreadId = threadInfo[0].threadId;
2443 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
2444 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
2446 for (i = 1; i < nApics; i++) {
2447 if (threadInfo[i].pkgId != lastPkgId) {
2450 lastPkgId = threadInfo[i].pkgId;
2451 if ((
int)coreCt > nCoresPerPkg)
2452 nCoresPerPkg = coreCt;
2454 lastCoreId = threadInfo[i].coreId;
2455 if ((
int)threadCt > __kmp_nThreadsPerCore)
2456 __kmp_nThreadsPerCore = threadCt;
2458 lastThreadId = threadInfo[i].threadId;
2462 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
2463 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
2467 if (threadInfo[i].coreId != lastCoreId) {
2470 lastCoreId = threadInfo[i].coreId;
2471 if ((
int)threadCt > __kmp_nThreadsPerCore)
2472 __kmp_nThreadsPerCore = threadCt;
2474 lastThreadId = threadInfo[i].threadId;
2475 }
else if (threadInfo[i].threadId != lastThreadId) {
2477 lastThreadId = threadInfo[i].threadId;
2479 __kmp_free(threadInfo);
2480 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2486 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
2487 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
2488 __kmp_free(threadInfo);
2489 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
2497 if ((
int)coreCt > nCoresPerPkg)
2498 nCoresPerPkg = coreCt;
2499 if ((
int)threadCt > __kmp_nThreadsPerCore)
2500 __kmp_nThreadsPerCore = threadCt;
2501 __kmp_ncores = nCores;
2502 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
2510 int threadLevel = 2;
2512 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
2515 types[idx++] = KMP_HW_SOCKET;
2517 types[idx++] = KMP_HW_CORE;
2518 if (threadLevel >= 0)
2519 types[idx++] = KMP_HW_THREAD;
2521 KMP_ASSERT(depth > 0);
2522 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
2524 for (i = 0; i < nApics; ++i) {
2526 unsigned os = threadInfo[i].osId;
2527 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2530 if (pkgLevel >= 0) {
2531 hw_thread.ids[idx++] = threadInfo[i].pkgId;
2533 if (coreLevel >= 0) {
2534 hw_thread.ids[idx++] = threadInfo[i].coreId;
2536 if (threadLevel >= 0) {
2537 hw_thread.ids[idx++] = threadInfo[i].threadId;
2539 hw_thread.os_id = os;
2540 hw_thread.original_idx = i;
2543 __kmp_free(threadInfo);
2544 __kmp_topology->sort_ids();
2545 if (!__kmp_topology->check_ids()) {
2546 kmp_topology_t::deallocate(__kmp_topology);
2547 __kmp_topology =
nullptr;
2548 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2556static void __kmp_get_hybrid_info(kmp_hw_core_type_t *type,
int *efficiency,
2557 unsigned *native_model_id) {
2559 __kmp_x86_cpuid(0x1a, 0, &buf);
2560 *type = (kmp_hw_core_type_t)__kmp_extract_bits<24, 31>(buf.eax);
2562 case KMP_HW_CORE_TYPE_ATOM:
2565 case KMP_HW_CORE_TYPE_CORE:
2571 *native_model_id = __kmp_extract_bits<0, 23>(buf.eax);
2593 INTEL_LEVEL_TYPE_INVALID = 0,
2594 INTEL_LEVEL_TYPE_SMT = 1,
2595 INTEL_LEVEL_TYPE_CORE = 2,
2596 INTEL_LEVEL_TYPE_MODULE = 3,
2597 INTEL_LEVEL_TYPE_TILE = 4,
2598 INTEL_LEVEL_TYPE_DIE = 5,
2599 INTEL_LEVEL_TYPE_LAST = 6,
2601KMP_BUILD_ASSERT(INTEL_LEVEL_TYPE_LAST <
sizeof(
unsigned) * CHAR_BIT);
2602#define KMP_LEAF_1F_KNOWN_LEVELS ((1u << INTEL_LEVEL_TYPE_LAST) - 1u)
2604static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
2605 switch (intel_type) {
2606 case INTEL_LEVEL_TYPE_INVALID:
2607 return KMP_HW_SOCKET;
2608 case INTEL_LEVEL_TYPE_SMT:
2609 return KMP_HW_THREAD;
2610 case INTEL_LEVEL_TYPE_CORE:
2612 case INTEL_LEVEL_TYPE_TILE:
2614 case INTEL_LEVEL_TYPE_MODULE:
2615 return KMP_HW_MODULE;
2616 case INTEL_LEVEL_TYPE_DIE:
2619 return KMP_HW_UNKNOWN;
2622static int __kmp_topology_type_2_intel_type(kmp_hw_t type) {
2625 return INTEL_LEVEL_TYPE_INVALID;
2627 return INTEL_LEVEL_TYPE_SMT;
2629 return INTEL_LEVEL_TYPE_CORE;
2631 return INTEL_LEVEL_TYPE_TILE;
2633 return INTEL_LEVEL_TYPE_MODULE;
2635 return INTEL_LEVEL_TYPE_DIE;
2637 return INTEL_LEVEL_TYPE_INVALID;
2641struct cpuid_level_info_t {
2642 unsigned level_type, mask, mask_width, nitems, cache_mask;
2645class cpuid_topo_desc_t {
2649 void clear() { desc = 0; }
2650 bool contains(
int intel_type)
const {
2651 KMP_DEBUG_ASSERT(intel_type >= 0 && intel_type < INTEL_LEVEL_TYPE_LAST);
2652 if ((1u << intel_type) & desc)
2656 bool contains_topology_type(kmp_hw_t type)
const {
2657 KMP_DEBUG_ASSERT(type >= 0 && type < KMP_HW_LAST);
2658 int intel_type = __kmp_topology_type_2_intel_type(type);
2659 return contains(intel_type);
2661 bool contains(cpuid_topo_desc_t rhs)
const {
2662 return ((desc | rhs.desc) == desc);
2664 void add(
int intel_type) { desc |= (1u << intel_type); }
2665 void add(cpuid_topo_desc_t rhs) { desc |= rhs.desc; }
2668struct cpuid_proc_info_t {
2674 unsigned native_model_id;
2676 kmp_hw_core_type_t type;
2677 cpuid_topo_desc_t description;
2679 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
2685static bool __kmp_x2apicid_get_levels(
int leaf, cpuid_proc_info_t *info,
2686 kmp_hw_t total_types[KMP_HW_LAST],
2688 cpuid_topo_desc_t *total_description) {
2689 unsigned level, levels_index;
2690 unsigned level_type, mask_width, nitems;
2692 cpuid_level_info_t(&levels)[INTEL_LEVEL_TYPE_LAST] = info->levels;
2693 bool retval =
false;
2702 level = levels_index = 0;
2704 __kmp_x86_cpuid(leaf, level, &buf);
2705 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
2706 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
2707 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
2708 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0) {
2713 if (KMP_LEAF_1F_KNOWN_LEVELS & (1u << level_type)) {
2715 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
2716 levels[levels_index].level_type = level_type;
2717 levels[levels_index].mask_width = mask_width;
2718 levels[levels_index].nitems = nitems;
2722 if (levels_index > 0) {
2723 levels[levels_index - 1].mask_width = mask_width;
2724 levels[levels_index - 1].nitems = nitems;
2728 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
2729 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
2730 info->description.clear();
2731 info->depth = levels_index;
2735 if (*total_depth == 0) {
2736 *total_depth = info->depth;
2737 total_description->clear();
2738 for (
int i = *total_depth - 1, j = 0; i >= 0; --i, ++j) {
2740 __kmp_intel_type_2_topology_type(info->levels[i].level_type);
2741 total_description->add(info->levels[i].level_type);
2747 if (levels_index == 0 || levels[0].level_type == INTEL_LEVEL_TYPE_INVALID)
2751 for (
unsigned i = 0; i < levels_index; ++i) {
2752 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
2753 levels[i].mask = ~((-1) << levels[i].mask_width);
2754 levels[i].cache_mask = (-1) << levels[i].mask_width;
2755 for (
unsigned j = 0; j < i; ++j)
2756 levels[i].mask ^= levels[j].mask;
2758 KMP_DEBUG_ASSERT(i > 0);
2759 levels[i].mask = (-1) << levels[i - 1].mask_width;
2760 levels[i].cache_mask = 0;
2762 info->description.add(info->levels[i].level_type);
2770 if (!total_description->contains(info->description)) {
2771 for (
int i = info->depth - 1, j = 0; i >= 0; --i, ++j) {
2773 if (total_description->contains(levels[i].level_type))
2776 kmp_hw_t curr_type =
2777 __kmp_intel_type_2_topology_type(levels[i].level_type);
2778 KMP_ASSERT(j != 0 &&
"Bad APIC Id information");
2780 for (
int k = info->depth - 1; k >= j; --k) {
2781 KMP_DEBUG_ASSERT(k + 1 < KMP_HW_LAST);
2782 total_types[k + 1] = total_types[k];
2785 total_types[j] = curr_type;
2788 total_description->add(info->description);
2794static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
2796 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
2798 int topology_leaf, highest_leaf;
2801 cpuid_topo_desc_t total_description;
2802 static int leaves[] = {0, 0};
2805 int ninfos = (__kmp_avail_proc > 0 ? __kmp_avail_proc : 1);
2806 cpuid_proc_info_t *proc_info = (cpuid_proc_info_t *)__kmp_allocate(
2807 (
sizeof(cpuid_proc_info_t) +
sizeof(cpuid_cache_info_t)) * ninfos);
2808 cpuid_cache_info_t *cache_info = (cpuid_cache_info_t *)(proc_info + ninfos);
2810 kmp_i18n_id_t leaf_message_id;
2812 *msg_id = kmp_i18n_null;
2813 if (__kmp_affinity.flags.verbose) {
2814 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
2818 __kmp_x86_cpuid(0, 0, &buf);
2819 highest_leaf = buf.eax;
2824 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
2827 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2828 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
2831 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
2836 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2840 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2842 for (
int i = 0; i < num_leaves; ++i) {
2843 int leaf = leaves[i];
2844 if (highest_leaf < leaf)
2846 __kmp_x86_cpuid(leaf, 0, &buf);
2849 topology_leaf = leaf;
2850 __kmp_x2apicid_get_levels(leaf, &proc_info[0], types, &depth,
2851 &total_description);
2856 if (topology_leaf == -1 || depth == 0) {
2857 *msg_id = leaf_message_id;
2858 __kmp_free(proc_info);
2861 KMP_ASSERT(depth <= INTEL_LEVEL_TYPE_LAST);
2868 if (!KMP_AFFINITY_CAPABLE()) {
2871 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2872 for (
int i = 0; i < depth; ++i) {
2873 if (proc_info[0].levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
2874 __kmp_nThreadsPerCore = proc_info[0].levels[i].nitems;
2875 }
else if (proc_info[0].levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
2876 nCoresPerPkg = proc_info[0].levels[i].nitems;
2879 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
2880 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2881 __kmp_free(proc_info);
2890 kmp_affinity_raii_t previous_affinity;
2895 int hw_thread_index = 0;
2896 bool uniform_caches =
true;
2898 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2900 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2903 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2906 __kmp_affinity_dispatch->bind_thread(proc);
2907 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2908 proc_info[hw_thread_index].os_id = proc;
2909 proc_info[hw_thread_index].apic_id = buf.edx;
2910 __kmp_x2apicid_get_levels(topology_leaf, &proc_info[hw_thread_index], types,
2911 &depth, &total_description);
2912 if (proc_info[hw_thread_index].depth == 0) {
2913 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2914 __kmp_free(proc_info);
2918 cache_info[hw_thread_index].get_leaf4_levels();
2919 if (uniform_caches && hw_thread_index > 0)
2920 if (cache_info[0] != cache_info[hw_thread_index])
2921 uniform_caches =
false;
2923 if (__kmp_is_hybrid_cpu() && highest_leaf >= 0x1a) {
2924 __kmp_get_hybrid_info(&proc_info[hw_thread_index].type,
2925 &proc_info[hw_thread_index].efficiency,
2926 &proc_info[hw_thread_index].native_model_id);
2930 KMP_ASSERT(hw_thread_index > 0);
2931 previous_affinity.restore();
2934 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2937 for (
int i = 0; i < __kmp_topology->get_num_hw_threads(); ++i) {
2938 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2940 hw_thread.os_id = proc_info[i].os_id;
2941 hw_thread.original_idx = i;
2942 unsigned apic_id = proc_info[i].apic_id;
2944 for (
int j = 0, idx = depth - 1; j < depth; ++j, --idx) {
2945 if (!(proc_info[i].description.contains_topology_type(
2946 __kmp_topology->get_type(j)))) {
2947 hw_thread.ids[idx] = kmp_hw_thread_t::UNKNOWN_ID;
2949 hw_thread.ids[idx] = apic_id & proc_info[i].levels[j].mask;
2951 hw_thread.ids[idx] >>= proc_info[i].levels[j - 1].mask_width;
2955 hw_thread.attrs.set_core_type(proc_info[i].type);
2956 hw_thread.attrs.set_core_eff(proc_info[i].efficiency);
2959 __kmp_topology->sort_ids();
2962 for (
int j = 0; j < depth - 1; ++j) {
2964 int prev_id = __kmp_topology->at(0).ids[j];
2965 int curr_id = __kmp_topology->at(0).ids[j + 1];
2966 __kmp_topology->at(0).ids[j + 1] = new_id;
2967 for (
int i = 1; i < __kmp_topology->get_num_hw_threads(); ++i) {
2968 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2969 if (hw_thread.ids[j] == prev_id && hw_thread.ids[j + 1] == curr_id) {
2970 hw_thread.ids[j + 1] = new_id;
2971 }
else if (hw_thread.ids[j] == prev_id &&
2972 hw_thread.ids[j + 1] != curr_id) {
2973 curr_id = hw_thread.ids[j + 1];
2974 hw_thread.ids[j + 1] = ++new_id;
2976 prev_id = hw_thread.ids[j];
2977 curr_id = hw_thread.ids[j + 1];
2978 hw_thread.ids[j + 1] = ++new_id;
2985 if (uniform_caches) {
2986 for (
size_t i = 0; i < cache_info[0].get_depth(); ++i) {
2987 unsigned cache_mask = cache_info[0][i].mask;
2988 unsigned cache_level = cache_info[0][i].level;
2989 KMP_ASSERT(cache_level <= cpuid_cache_info_t::MAX_CACHE_LEVEL);
2990 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(cache_level);
2991 __kmp_topology->set_equivalent_type(cache_type, cache_type);
2992 for (
int j = 0; j < depth; ++j) {
2993 unsigned hw_cache_mask = proc_info[0].levels[j].cache_mask;
2994 if (hw_cache_mask == cache_mask && j < depth - 1) {
2995 kmp_hw_t type = __kmp_intel_type_2_topology_type(
2996 proc_info[0].levels[j + 1].level_type);
2997 __kmp_topology->set_equivalent_type(cache_type, type);
3003 for (
int i = 0; i < __kmp_topology->get_num_hw_threads(); ++i) {
3004 for (
size_t j = 0; j < cache_info[i].get_depth(); ++j) {
3005 unsigned cache_level = cache_info[i][j].level;
3006 kmp_hw_t cache_type =
3007 cpuid_cache_info_t::get_topology_type(cache_level);
3008 if (__kmp_topology->get_equivalent_type(cache_type) == KMP_HW_UNKNOWN)
3009 __kmp_topology->set_equivalent_type(cache_type, cache_type);
3015 bool unresolved_cache_levels =
false;
3016 for (
unsigned level = 1; level <= cpuid_cache_info_t::MAX_CACHE_LEVEL;
3018 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(level);
3021 if (__kmp_topology->get_equivalent_type(cache_type) == cache_type) {
3022 unresolved_cache_levels =
true;
3028 if (unresolved_cache_levels) {
3029 int num_hw_threads = __kmp_topology->get_num_hw_threads();
3030 int *ids = (
int *)__kmp_allocate(
sizeof(
int) * num_hw_threads);
3031 for (
unsigned l = 1; l <= cpuid_cache_info_t::MAX_CACHE_LEVEL; ++l) {
3032 kmp_hw_t cache_type = cpuid_cache_info_t::get_topology_type(l);
3033 if (__kmp_topology->get_equivalent_type(cache_type) != cache_type)
3035 for (
int i = 0; i < num_hw_threads; ++i) {
3036 int original_idx = __kmp_topology->at(i).original_idx;
3037 ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
3038 const cpuid_cache_info_t::info_t &info =
3039 cache_info[original_idx].get_level(l);
3041 if (info.level == 0)
3043 ids[i] = info.mask & proc_info[original_idx].apic_id;
3045 __kmp_topology->insert_layer(cache_type, ids);
3049 if (!__kmp_topology->check_ids()) {
3050 kmp_topology_t::deallocate(__kmp_topology);
3051 __kmp_topology =
nullptr;
3052 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
3053 __kmp_free(proc_info);
3056 __kmp_free(proc_info);
3062#define threadIdIndex 1
3063#define coreIdIndex 2
3065#define nodeIdIndex 4
3067typedef unsigned *ProcCpuInfo;
3068static unsigned maxIndex = pkgIdIndex;
3070static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
3073 const unsigned *aa = *(
unsigned *
const *)a;
3074 const unsigned *bb = *(
unsigned *
const *)b;
3075 for (i = maxIndex;; i--) {
3086#if KMP_USE_HIER_SCHED
3088static void __kmp_dispatch_set_hierarchy_values() {
3094 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
3095 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
3096 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
3097#if KMP_ARCH_X86_64 && \
3098 (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
3099 KMP_OS_WINDOWS) && \
3101 if (__kmp_mic_type >= mic3)
3102 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
3105 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
3106 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
3107 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
3108 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
3111 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
3112 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
3113 __kmp_nThreadsPerCore;
3114#if KMP_ARCH_X86_64 && \
3115 (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
3116 KMP_OS_WINDOWS) && \
3118 if (__kmp_mic_type >= mic3)
3119 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
3120 2 * __kmp_nThreadsPerCore;
3123 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
3124 __kmp_nThreadsPerCore;
3125 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
3126 nCoresPerPkg * __kmp_nThreadsPerCore;
3127 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
3128 nCoresPerPkg * __kmp_nThreadsPerCore;
3129 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
3130 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
3135int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
3136 int index = type + 1;
3137 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
3138 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
3139 if (type == kmp_hier_layer_e::LAYER_THREAD)
3141 else if (type == kmp_hier_layer_e::LAYER_LOOP)
3143 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
3144 if (tid >= num_hw_threads)
3145 tid = tid % num_hw_threads;
3146 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
3150int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
3153 KMP_DEBUG_ASSERT(i1 <= i2);
3154 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
3155 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
3156 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
3158 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
3162static inline const char *__kmp_cpuinfo_get_filename() {
3163 const char *filename;
3164 if (__kmp_cpuinfo_file !=
nullptr)
3165 filename = __kmp_cpuinfo_file;
3167 filename =
"/proc/cpuinfo";
3171static inline const char *__kmp_cpuinfo_get_envvar() {
3172 const char *envvar =
nullptr;
3173 if (__kmp_cpuinfo_file !=
nullptr)
3174 envvar =
"KMP_CPUINFO_FILE";
3181static bool __kmp_affinity_create_cpuinfo_map(
int *line,
3182 kmp_i18n_id_t *
const msg_id) {
3183 *msg_id = kmp_i18n_null;
3186 unsigned num_records = __kmp_xproc;
3188 const char *filename = __kmp_cpuinfo_get_filename();
3189 const char *envvar = __kmp_cpuinfo_get_envvar();
3191 if (__kmp_affinity.flags.verbose) {
3192 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
3200 unsigned num_records = 0;
3202 buf[
sizeof(buf) - 1] = 1;
3203 if (!fgets(buf,
sizeof(buf), f)) {
3208 char s1[] =
"processor";
3209 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
3216 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3218 if (level > (
unsigned)__kmp_xproc) {
3219 level = __kmp_xproc;
3221 if (nodeIdIndex + level >= maxIndex) {
3222 maxIndex = nodeIdIndex + level;
3230 if (num_records == 0) {
3231 *msg_id = kmp_i18n_str_NoProcRecords;
3234 if (num_records > (
unsigned)__kmp_xproc) {
3235 *msg_id = kmp_i18n_str_TooManyProcRecords;
3244 if (fseek(f, 0, SEEK_SET) != 0) {
3245 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
3252 unsigned **threadInfo =
3253 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
3255 for (i = 0; i <= num_records; i++) {
3257 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3260#define CLEANUP_THREAD_INFO \
3261 for (i = 0; i <= num_records; i++) { \
3262 __kmp_free(threadInfo[i]); \
3264 __kmp_free(threadInfo);
3269#define INIT_PROC_INFO(p) \
3270 for (__index = 0; __index <= maxIndex; __index++) { \
3271 (p)[__index] = UINT_MAX; \
3274 for (i = 0; i <= num_records; i++) {
3275 INIT_PROC_INFO(threadInfo[i]);
3280 lpar_info_format1_t cpuinfo;
3281 unsigned num_avail = __kmp_xproc;
3283 if (__kmp_affinity.flags.verbose)
3284 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"system info for topology");
3287 smt_threads = syssmt(GET_NUMBER_SMT_SETS, 0, 0, NULL);
3290 rsethandle_t sys_rset = rs_alloc(RS_SYSTEM);
3291 if (sys_rset == NULL) {
3292 CLEANUP_THREAD_INFO;
3293 *msg_id = kmp_i18n_str_UnknownTopology;
3297 rsethandle_t srad = rs_alloc(RS_EMPTY);
3300 CLEANUP_THREAD_INFO;
3301 *msg_id = kmp_i18n_str_UnknownTopology;
3306 int sradsdl = rs_getinfo(NULL, R_SRADSDL, 0);
3310 CLEANUP_THREAD_INFO;
3311 *msg_id = kmp_i18n_str_UnknownTopology;
3315 int num_rads = rs_numrads(sys_rset, sradsdl, 0);
3319 CLEANUP_THREAD_INFO;
3320 *msg_id = kmp_i18n_str_UnknownTopology;
3325 int max_procs = rs_getinfo(NULL, R_MAXPROCS, 0);
3326 if (max_procs < 0) {
3329 CLEANUP_THREAD_INFO;
3330 *msg_id = kmp_i18n_str_UnknownTopology;
3336 for (
int srad_idx = 0; cur_rad < num_rads && srad_idx < VMI_MAXRADS;
3339 if (rs_getrad(sys_rset, srad, sradsdl, srad_idx, 0) < 0)
3342 for (
int cpu = 0; cpu < max_procs; cpu++) {
3344 if (rs_op(RS_TESTRESOURCE, srad, NULL, R_PROCS, cpu)) {
3345 threadInfo[cpu][osIdIndex] = cpu;
3346 threadInfo[cpu][pkgIdIndex] = cur_rad;
3347 threadInfo[cpu][coreIdIndex] = cpu / smt_threads;
3349 if (num_set >= num_avail) {
3363 unsigned num_avail = 0;
3366 bool reading_s390x_sys_info =
true;
3373 buf[
sizeof(buf) - 1] = 1;
3374 bool long_line =
false;
3375 if (!fgets(buf,
sizeof(buf), f)) {
3380 for (i = 0; i <= maxIndex; i++) {
3381 if (threadInfo[num_avail][i] != UINT_MAX) {
3389 }
else if (!buf[
sizeof(buf) - 1]) {
3396 CLEANUP_THREAD_INFO; \
3397 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
3403#if KMP_ARCH_LOONGARCH64
3410 if (*buf ==
'\n' && *line == 2)
3416 if (reading_s390x_sys_info) {
3418 reading_s390x_sys_info =
false;
3424 char s1[] =
"cpu number";
3426 char s1[] =
"processor";
3428 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
3430 char *p = strchr(buf +
sizeof(s1) - 1,
':');
3432 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3434 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
3444 threadInfo[num_avail][osIdIndex] = val;
3445#if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
3449 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
3450 threadInfo[num_avail][osIdIndex]);
3451 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
3456 KMP_SNPRINTF(path,
sizeof(path),
3457 "/sys/devices/system/cpu/cpu%u/topology/book_id",
3458 threadInfo[num_avail][osIdIndex]);
3459 __kmp_read_from_file(path,
"%u", &book_id);
3460 threadInfo[num_avail][pkgIdIndex] |= (book_id << 8);
3463 KMP_SNPRINTF(path,
sizeof(path),
3464 "/sys/devices/system/cpu/cpu%u/topology/drawer_id",
3465 threadInfo[num_avail][osIdIndex]);
3466 __kmp_read_from_file(path,
"%u", &drawer_id);
3467 threadInfo[num_avail][pkgIdIndex] |= (drawer_id << 16);
3470 KMP_SNPRINTF(path,
sizeof(path),
3471 "/sys/devices/system/cpu/cpu%u/topology/core_id",
3472 threadInfo[num_avail][osIdIndex]);
3473 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
3477 char s2[] =
"physical id";
3478 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
3480 char *p = strchr(buf +
sizeof(s2) - 1,
':');
3482 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3484 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
3486 threadInfo[num_avail][pkgIdIndex] = val;
3489 char s3[] =
"core id";
3490 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
3492 char *p = strchr(buf +
sizeof(s3) - 1,
':');
3494 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3496 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
3498 threadInfo[num_avail][coreIdIndex] = val;
3502 char s4[] =
"thread id";
3503 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
3505 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3507 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3509 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
3511 threadInfo[num_avail][threadIdIndex] = val;
3515 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3517 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3519 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3522 if (level > (
unsigned)__kmp_xproc) {
3523 level = __kmp_xproc;
3525 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
3527 threadInfo[num_avail][nodeIdIndex + level] = val;
3534 if ((*buf != 0) && (*buf !=
'\n')) {
3539 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
3547 if ((
int)num_avail == __kmp_xproc) {
3548 CLEANUP_THREAD_INFO;
3549 *msg_id = kmp_i18n_str_TooManyEntries;
3555 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
3556 CLEANUP_THREAD_INFO;
3557 *msg_id = kmp_i18n_str_MissingProcField;
3560 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
3561 CLEANUP_THREAD_INFO;
3562 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
3567 if (KMP_AFFINITY_CAPABLE() &&
3568 !KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
3569 __kmp_affin_fullMask)) {
3570 INIT_PROC_INFO(threadInfo[num_avail]);
3577 KMP_ASSERT(num_avail <= num_records);
3578 INIT_PROC_INFO(threadInfo[num_avail]);
3583 CLEANUP_THREAD_INFO;
3584 *msg_id = kmp_i18n_str_MissingValCpuinfo;
3588 CLEANUP_THREAD_INFO;
3589 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
3594#if KMP_MIC && REDUCE_TEAM_SIZE
3595 unsigned teamSize = 0;
3603 KMP_ASSERT(num_avail > 0);
3604 KMP_ASSERT(num_avail <= num_records);
3607 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
3608 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
3622 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3624 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3626 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3628 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3630 bool assign_thread_ids =
false;
3631 unsigned threadIdCt;
3638 if (assign_thread_ids) {
3639 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
3640 threadInfo[0][threadIdIndex] = threadIdCt++;
3641 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
3642 threadIdCt = threadInfo[0][threadIdIndex] + 1;
3645 for (index = 0; index <= maxIndex; index++) {
3649 lastId[index] = threadInfo[0][index];
3654 for (i = 1; i < num_avail; i++) {
3657 for (index = maxIndex; index >= threadIdIndex; index--) {
3658 if (assign_thread_ids && (index == threadIdIndex)) {
3660 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3661 threadInfo[i][threadIdIndex] = threadIdCt++;
3665 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3666 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3669 if (threadInfo[i][index] != lastId[index]) {
3674 for (index2 = threadIdIndex; index2 < index; index2++) {
3676 if (counts[index2] > maxCt[index2]) {
3677 maxCt[index2] = counts[index2];
3680 lastId[index2] = threadInfo[i][index2];
3684 lastId[index] = threadInfo[i][index];
3686 if (assign_thread_ids && (index > threadIdIndex)) {
3688#if KMP_MIC && REDUCE_TEAM_SIZE
3691 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3698 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3699 threadInfo[i][threadIdIndex] = threadIdCt++;
3705 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3706 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3712 if (index < threadIdIndex) {
3716 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
3721 CLEANUP_THREAD_INFO;
3722 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3728 assign_thread_ids =
true;
3729 goto restart_radix_check;
3733#if KMP_MIC && REDUCE_TEAM_SIZE
3736 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3739 for (index = threadIdIndex; index <= maxIndex; index++) {
3740 if (counts[index] > maxCt[index]) {
3741 maxCt[index] = counts[index];
3745 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
3746 nCoresPerPkg = maxCt[coreIdIndex];
3747 nPackages = totals[pkgIdIndex];
3753 __kmp_ncores = totals[coreIdIndex];
3754 if (!KMP_AFFINITY_CAPABLE()) {
3755 KMP_ASSERT(__kmp_affinity.type == affinity_none);
3759#if KMP_MIC && REDUCE_TEAM_SIZE
3761 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
3762 __kmp_dflt_team_nth = teamSize;
3763 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
3764 "__kmp_dflt_team_nth = %d\n",
3765 __kmp_dflt_team_nth));
3769 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
3776 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
3777 for (index = threadIdIndex; index < maxIndex; index++) {
3778 KMP_ASSERT(totals[index] >= totals[index + 1]);
3779 inMap[index] = (totals[index] > totals[index + 1]);
3781 inMap[maxIndex] = (totals[maxIndex] > 1);
3782 inMap[pkgIdIndex] =
true;
3783 inMap[coreIdIndex] =
true;
3784 inMap[threadIdIndex] =
true;
3788 kmp_hw_t types[KMP_HW_LAST];
3791 int threadLevel = -1;
3792 for (index = threadIdIndex; index <= maxIndex; index++) {
3797 if (inMap[pkgIdIndex]) {
3799 types[idx++] = KMP_HW_SOCKET;
3801 if (inMap[coreIdIndex]) {
3803 types[idx++] = KMP_HW_CORE;
3805 if (inMap[threadIdIndex]) {
3807 types[idx++] = KMP_HW_THREAD;
3809 KMP_ASSERT(depth > 0);
3812 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
3814 for (i = 0; i < num_avail; ++i) {
3815 unsigned os = threadInfo[i][osIdIndex];
3817 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3819 hw_thread.os_id = os;
3820 hw_thread.original_idx = i;
3823 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
3824 if (!inMap[src_index]) {
3827 if (src_index == pkgIdIndex) {
3828 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
3829 }
else if (src_index == coreIdIndex) {
3830 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
3831 }
else if (src_index == threadIdIndex) {
3832 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
3842 CLEANUP_THREAD_INFO;
3843 __kmp_topology->sort_ids();
3845 int tlevel = __kmp_topology->get_level(KMP_HW_THREAD);
3848 if (__kmp_topology->at(0).ids[tlevel] == kmp_hw_thread_t::UNKNOWN_ID) {
3849 __kmp_topology->at(0).ids[tlevel] = 0;
3851 for (
int i = 1; i < __kmp_topology->get_num_hw_threads(); ++i) {
3852 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3853 if (hw_thread.ids[tlevel] != kmp_hw_thread_t::UNKNOWN_ID)
3855 kmp_hw_thread_t &prev_hw_thread = __kmp_topology->at(i - 1);
3859 for (
int j = 0; j < tlevel; ++j) {
3860 if (hw_thread.ids[j] != prev_hw_thread.ids[j]) {
3861 hw_thread.ids[tlevel] = 0;
3865 if (hw_thread.ids[tlevel] == kmp_hw_thread_t::UNKNOWN_ID)
3866 hw_thread.ids[tlevel] = prev_hw_thread.ids[tlevel] + 1;
3870 if (!__kmp_topology->check_ids()) {
3871 kmp_topology_t::deallocate(__kmp_topology);
3872 __kmp_topology =
nullptr;
3873 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3882template <
typename FindNextFunctionType>
3883static void __kmp_create_os_id_masks(
unsigned *numUnique,
3884 kmp_affinity_t &affinity,
3885 FindNextFunctionType find_next) {
3889 int numAddrs = __kmp_topology->get_num_hw_threads();
3890 int depth = __kmp_topology->get_depth();
3891 const char *env_var = __kmp_get_affinity_env_var(affinity);
3892 KMP_ASSERT(numAddrs);
3902 for (i = numAddrs - 1;; --i) {
3903 int osId = __kmp_topology->at(i).os_id;
3904 if (osId > maxOsId) {
3910 affinity.num_os_id_masks = maxOsId + 1;
3911 KMP_CPU_ALLOC_ARRAY(affinity.os_id_masks, affinity.num_os_id_masks);
3912 KMP_ASSERT(affinity.gran_levels >= 0);
3913 if (affinity.flags.verbose && (affinity.gran_levels > 0)) {
3914 KMP_INFORM(ThreadsMigrate, env_var, affinity.gran_levels);
3916 if (affinity.gran_levels >= (
int)depth) {
3917 KMP_AFF_WARNING(affinity, AffThreadsMayMigrate);
3927 kmp_affin_mask_t *sum;
3928 KMP_CPU_ALLOC_ON_STACK(sum);
3931 i = j = leader = find_next(-1);
3932 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3933 kmp_full_mask_modifier_t full_mask;
3934 for (i = find_next(i); i < numAddrs; i = find_next(i)) {
3938 if (__kmp_topology->is_close(leader, i, affinity)) {
3939 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3945 for (; j < i; j = find_next(j)) {
3946 int osId = __kmp_topology->at(j).os_id;
3947 KMP_DEBUG_ASSERT(osId <= maxOsId);
3948 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3949 KMP_CPU_COPY(mask, sum);
3950 __kmp_topology->at(j).leader = (j == leader);
3956 full_mask.include(sum);
3958 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3963 for (; j < i; j = find_next(j)) {
3964 int osId = __kmp_topology->at(j).os_id;
3965 KMP_DEBUG_ASSERT(osId <= maxOsId);
3966 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3967 KMP_CPU_COPY(mask, sum);
3968 __kmp_topology->at(j).leader = (j == leader);
3970 full_mask.include(sum);
3972 KMP_CPU_FREE_FROM_STACK(sum);
3975 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
3976 __kmp_topology->print(env_var);
3979 *numUnique = unique;
3985static kmp_affin_mask_t *newMasks;
3986static int numNewMasks;
3987static int nextNewMask;
3989#define ADD_MASK(_mask) \
3991 if (nextNewMask >= numNewMasks) { \
3994 kmp_affin_mask_t *temp; \
3995 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
3996 for (i = 0; i < numNewMasks / 2; i++) { \
3997 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
3998 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
3999 KMP_CPU_COPY(dest, src); \
4001 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
4004 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
4008#define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
4010 if (((_osId) > _maxOsId) || \
4011 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
4012 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, _osId); \
4014 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
4020static void __kmp_affinity_process_proclist(kmp_affinity_t &affinity) {
4022 kmp_affin_mask_t **out_masks = &affinity.masks;
4023 unsigned *out_numMasks = &affinity.num_masks;
4024 const char *proclist = affinity.proclist;
4025 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4026 int maxOsId = affinity.num_os_id_masks - 1;
4027 const char *scan = proclist;
4028 const char *next = proclist;
4033 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
4035 kmp_affin_mask_t *sumMask;
4036 KMP_CPU_ALLOC(sumMask);
4040 int start, end, stride;
4044 if (*next ==
'\0') {
4056 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
4058 num = __kmp_str_to_int(scan, *next);
4059 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
4062 if ((num > maxOsId) ||
4063 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4064 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4065 KMP_CPU_ZERO(sumMask);
4067 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
4087 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4090 num = __kmp_str_to_int(scan, *next);
4091 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
4094 if ((num > maxOsId) ||
4095 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4096 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4098 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
4115 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4117 start = __kmp_str_to_int(scan, *next);
4118 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
4123 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4137 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4139 end = __kmp_str_to_int(scan, *next);
4140 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
4157 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
4159 stride = __kmp_str_to_int(scan, *next);
4160 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
4165 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
4167 KMP_ASSERT2(start <= end,
"bad explicit proc list");
4169 KMP_ASSERT2(start >= end,
"bad explicit proc list");
4171 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
4176 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4178 }
while (start <= end);
4181 ADD_MASK_OSID(start, osId2Mask, maxOsId);
4183 }
while (start >= end);
4194 *out_numMasks = nextNewMask;
4195 if (nextNewMask == 0) {
4197 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4200 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
4201 for (i = 0; i < nextNewMask; i++) {
4202 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
4203 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
4204 KMP_CPU_COPY(dest, src);
4206 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4207 KMP_CPU_FREE(sumMask);
4230static void __kmp_process_subplace_list(
const char **scan,
4231 kmp_affinity_t &affinity,
int maxOsId,
4232 kmp_affin_mask_t *tempMask,
4235 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4238 int start, count, stride, i;
4242 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4245 start = __kmp_str_to_int(*scan, *next);
4246 KMP_ASSERT(start >= 0);
4251 if (**scan ==
'}' || **scan ==
',') {
4252 if ((start > maxOsId) ||
4253 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4254 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4256 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4259 if (**scan ==
'}') {
4265 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
4270 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4273 count = __kmp_str_to_int(*scan, *next);
4274 KMP_ASSERT(count >= 0);
4279 if (**scan ==
'}' || **scan ==
',') {
4280 for (i = 0; i < count; i++) {
4281 if ((start > maxOsId) ||
4282 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4283 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4286 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4291 if (**scan ==
'}') {
4297 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
4304 if (**scan ==
'+') {
4308 if (**scan ==
'-') {
4316 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4319 stride = __kmp_str_to_int(*scan, *next);
4320 KMP_ASSERT(stride >= 0);
4326 if (**scan ==
'}' || **scan ==
',') {
4327 for (i = 0; i < count; i++) {
4328 if ((start > maxOsId) ||
4329 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4330 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4333 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4338 if (**scan ==
'}') {
4345 KMP_ASSERT2(0,
"bad explicit places list");
4349static void __kmp_process_place(
const char **scan, kmp_affinity_t &affinity,
4350 int maxOsId, kmp_affin_mask_t *tempMask,
4353 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4357 if (**scan ==
'{') {
4359 __kmp_process_subplace_list(scan, affinity, maxOsId, tempMask, setSize);
4360 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
4362 }
else if (**scan ==
'!') {
4364 __kmp_process_place(scan, affinity, maxOsId, tempMask, setSize);
4365 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
4366 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
4369 int num = __kmp_str_to_int(*scan, *next);
4370 KMP_ASSERT(num >= 0);
4371 if ((num > maxOsId) ||
4372 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4373 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4375 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
4380 KMP_ASSERT2(0,
"bad explicit places list");
4385void __kmp_affinity_process_placelist(kmp_affinity_t &affinity) {
4386 int i, j, count, stride, sign;
4387 kmp_affin_mask_t **out_masks = &affinity.masks;
4388 unsigned *out_numMasks = &affinity.num_masks;
4389 const char *placelist = affinity.proclist;
4390 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4391 int maxOsId = affinity.num_os_id_masks - 1;
4392 const char *scan = placelist;
4393 const char *next = placelist;
4396 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
4402 kmp_affin_mask_t *tempMask;
4403 kmp_affin_mask_t *previousMask;
4404 KMP_CPU_ALLOC(tempMask);
4405 KMP_CPU_ZERO(tempMask);
4406 KMP_CPU_ALLOC(previousMask);
4407 KMP_CPU_ZERO(previousMask);
4411 __kmp_process_place(&scan, affinity, maxOsId, tempMask, &setSize);
4415 if (*scan ==
'\0' || *scan ==
',') {
4419 KMP_CPU_ZERO(tempMask);
4421 if (*scan ==
'\0') {
4428 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4433 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4436 count = __kmp_str_to_int(scan, *next);
4437 KMP_ASSERT(count >= 0);
4442 if (*scan ==
'\0' || *scan ==
',') {
4445 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4464 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4467 stride = __kmp_str_to_int(scan, *next);
4468 KMP_DEBUG_ASSERT(stride >= 0);
4474 for (i = 0; i < count; i++) {
4479 KMP_CPU_COPY(previousMask, tempMask);
4480 ADD_MASK(previousMask);
4481 KMP_CPU_ZERO(tempMask);
4483 KMP_CPU_SET_ITERATE(j, previousMask) {
4484 if (!KMP_CPU_ISSET(j, previousMask)) {
4487 if ((j + stride > maxOsId) || (j + stride < 0) ||
4488 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
4489 (!KMP_CPU_ISSET(j + stride,
4490 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
4491 if (i < count - 1) {
4492 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, j + stride);
4496 KMP_CPU_SET(j + stride, tempMask);
4500 KMP_CPU_ZERO(tempMask);
4505 if (*scan ==
'\0') {
4513 KMP_ASSERT2(0,
"bad explicit places list");
4516 *out_numMasks = nextNewMask;
4517 if (nextNewMask == 0) {
4519 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4522 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
4523 KMP_CPU_FREE(tempMask);
4524 KMP_CPU_FREE(previousMask);
4525 for (i = 0; i < nextNewMask; i++) {
4526 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
4527 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
4528 KMP_CPU_COPY(dest, src);
4530 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4538static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
4541 for (
int i = 0; i < nprocs; i++) {
4542 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
4543 for (
int j = bottom_level; j > 0; j--) {
4544 if (hw_thread.ids[j] > 0) {
4545 if (core_level < (j - 1)) {
4555static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
4557 return __kmp_topology->get_count(core_level);
4560static int __kmp_affinity_find_core(
int proc,
int bottom_level,
4563 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
4564 for (
int i = 0; i <= proc; ++i) {
4565 if (i + 1 <= proc) {
4566 for (
int j = 0; j <= core_level; ++j) {
4567 if (__kmp_topology->at(i + 1).sub_ids[j] !=
4568 __kmp_topology->at(i).sub_ids[j]) {
4580static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
4582 if (core_level >= bottom_level)
4584 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
4585 return __kmp_topology->calculate_ratio(thread_level, core_level);
4588static int *procarr = NULL;
4589static int __kmp_aff_depth = 0;
4590static int *__kmp_osid_to_hwthread_map = NULL;
4592static void __kmp_affinity_get_mask_topology_info(
const kmp_affin_mask_t *mask,
4593 kmp_affinity_ids_t &ids,
4594 kmp_affinity_attrs_t &attrs) {
4595 if (!KMP_AFFINITY_CAPABLE())
4599 for (
int i = 0; i < KMP_HW_LAST; ++i)
4600 ids.ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
4601 attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
4606 int depth = __kmp_topology->get_depth();
4607 KMP_CPU_SET_ITERATE(cpu, mask) {
4608 int osid_idx = __kmp_osid_to_hwthread_map[cpu];
4610 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(osid_idx);
4611 for (
int level = 0; level < depth; ++level) {
4612 kmp_hw_t type = __kmp_topology->get_type(level);
4613 int id = hw_thread.sub_ids[level];
4614 if (ids.ids[type] == kmp_hw_thread_t::UNKNOWN_ID || ids.ids[type] ==
id) {
4619 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4620 for (; level < depth; ++level) {
4621 kmp_hw_t type = __kmp_topology->get_type(level);
4622 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4627 attrs.core_type = hw_thread.attrs.get_core_type();
4628 attrs.core_eff = hw_thread.attrs.get_core_eff();
4632 if (attrs.core_type != hw_thread.attrs.get_core_type())
4633 attrs.core_type = KMP_HW_CORE_TYPE_UNKNOWN;
4634 if (attrs.core_eff != hw_thread.attrs.get_core_eff())
4635 attrs.core_eff = kmp_hw_attr_t::UNKNOWN_CORE_EFF;
4640static void __kmp_affinity_get_thread_topology_info(kmp_info_t *th) {
4641 if (!KMP_AFFINITY_CAPABLE())
4643 const kmp_affin_mask_t *mask = th->th.th_affin_mask;
4644 kmp_affinity_ids_t &ids = th->th.th_topology_ids;
4645 kmp_affinity_attrs_t &attrs = th->th.th_topology_attrs;
4646 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4652static void __kmp_affinity_get_topology_info(kmp_affinity_t &affinity) {
4653 if (!KMP_AFFINITY_CAPABLE())
4655 if (affinity.type != affinity_none) {
4656 KMP_ASSERT(affinity.num_os_id_masks);
4657 KMP_ASSERT(affinity.os_id_masks);
4659 KMP_ASSERT(affinity.num_masks);
4660 KMP_ASSERT(affinity.masks);
4661 KMP_ASSERT(__kmp_affin_fullMask);
4663 int max_cpu = __kmp_affin_fullMask->get_max_cpu();
4664 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4667 if (!affinity.ids) {
4668 affinity.ids = (kmp_affinity_ids_t *)__kmp_allocate(
4669 sizeof(kmp_affinity_ids_t) * affinity.num_masks);
4671 if (!affinity.attrs) {
4672 affinity.attrs = (kmp_affinity_attrs_t *)__kmp_allocate(
4673 sizeof(kmp_affinity_attrs_t) * affinity.num_masks);
4675 if (!__kmp_osid_to_hwthread_map) {
4677 __kmp_osid_to_hwthread_map =
4678 (
int *)__kmp_allocate(
sizeof(
int) * (max_cpu + 1));
4682 for (
int hw_thread = 0; hw_thread < num_hw_threads; ++hw_thread) {
4683 int os_id = __kmp_topology->at(hw_thread).os_id;
4684 if (KMP_CPU_ISSET(os_id, __kmp_affin_fullMask))
4685 __kmp_osid_to_hwthread_map[os_id] = hw_thread;
4688 for (
unsigned i = 0; i < affinity.num_masks; ++i) {
4689 kmp_affinity_ids_t &ids = affinity.ids[i];
4690 kmp_affinity_attrs_t &attrs = affinity.attrs[i];
4691 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.masks, i);
4692 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4697static void __kmp_aux_affinity_initialize_other_data(kmp_affinity_t &affinity) {
4699 if (__kmp_topology && __kmp_topology->get_num_hw_threads()) {
4700 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
4701 __kmp_affinity_get_topology_info(affinity);
4702#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
4703 __kmp_first_osid_with_ecore = __kmp_get_first_osid_with_ecore();
4710static void __kmp_create_affinity_none_places(kmp_affinity_t &affinity) {
4711 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4712 KMP_ASSERT(affinity.type == affinity_none);
4713 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4714 affinity.num_masks = 1;
4715 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4716 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, 0);
4717 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4718 __kmp_aux_affinity_initialize_other_data(affinity);
4721static void __kmp_aux_affinity_initialize_masks(kmp_affinity_t &affinity) {
4726 int verbose = affinity.flags.verbose;
4727 const char *env_var = affinity.env_var;
4730 if (__kmp_affin_fullMask && __kmp_affin_origMask)
4733 if (__kmp_affin_fullMask == NULL) {
4734 KMP_CPU_ALLOC(__kmp_affin_fullMask);
4736 if (__kmp_affin_origMask == NULL) {
4737 KMP_CPU_ALLOC(__kmp_affin_origMask);
4739 if (KMP_AFFINITY_CAPABLE()) {
4740 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4742 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4743 if (affinity.flags.respect) {
4746 __kmp_avail_proc = 0;
4747 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4748 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4753 if (__kmp_avail_proc > __kmp_xproc) {
4754 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4755 affinity.type = affinity_none;
4756 KMP_AFFINITY_DISABLE();
4761 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4762 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4763 __kmp_affin_fullMask);
4764 KMP_INFORM(InitOSProcSetRespect, env_var, buf);
4768 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4769 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4770 __kmp_affin_fullMask);
4771 KMP_INFORM(InitOSProcSetNotRespect, env_var, buf);
4774 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4776 if (__kmp_num_proc_groups <= 1) {
4778 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4782 __kmp_affin_fullMask->set_process_affinity(
true);
4788static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
4789 bool success =
false;
4790 const char *env_var = affinity.env_var;
4791 kmp_i18n_id_t msg_id = kmp_i18n_null;
4792 int verbose = affinity.flags.verbose;
4796 if ((__kmp_cpuinfo_file != NULL) &&
4797 (__kmp_affinity_top_method == affinity_top_method_all)) {
4798 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4801 if (__kmp_affinity_top_method == affinity_top_method_all) {
4807 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4808 if (!__kmp_hwloc_error) {
4809 success = __kmp_affinity_create_hwloc_map(&msg_id);
4810 if (!success && verbose) {
4811 KMP_INFORM(AffIgnoringHwloc, env_var);
4813 }
else if (verbose) {
4814 KMP_INFORM(AffIgnoringHwloc, env_var);
4819#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4821 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4822 if (!success && verbose && msg_id != kmp_i18n_null) {
4823 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4827 success = __kmp_affinity_create_apicid_map(&msg_id);
4828 if (!success && verbose && msg_id != kmp_i18n_null) {
4829 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4834#if KMP_OS_LINUX || KMP_OS_AIX
4837 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4838 if (!success && verbose && msg_id != kmp_i18n_null) {
4839 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4844#if KMP_GROUP_AFFINITY
4845 if (!success && (__kmp_num_proc_groups > 1)) {
4846 success = __kmp_affinity_create_proc_group_map(&msg_id);
4847 if (!success && verbose && msg_id != kmp_i18n_null) {
4848 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4854 success = __kmp_affinity_create_flat_map(&msg_id);
4855 if (!success && verbose && msg_id != kmp_i18n_null) {
4856 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4858 KMP_ASSERT(success);
4866 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4867 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4868 success = __kmp_affinity_create_hwloc_map(&msg_id);
4870 KMP_ASSERT(msg_id != kmp_i18n_null);
4871 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4876#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4877 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
4878 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
4879 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4881 KMP_ASSERT(msg_id != kmp_i18n_null);
4882 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4884 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4885 success = __kmp_affinity_create_apicid_map(&msg_id);
4887 KMP_ASSERT(msg_id != kmp_i18n_null);
4888 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4893 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4895 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4897 KMP_ASSERT(msg_id != kmp_i18n_null);
4898 const char *filename = __kmp_cpuinfo_get_filename();
4900 KMP_FATAL(FileLineMsgExiting, filename, line,
4901 __kmp_i18n_catgets(msg_id));
4903 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4908#if KMP_GROUP_AFFINITY
4909 else if (__kmp_affinity_top_method == affinity_top_method_group) {
4910 success = __kmp_affinity_create_proc_group_map(&msg_id);
4911 KMP_ASSERT(success);
4913 KMP_ASSERT(msg_id != kmp_i18n_null);
4914 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4919 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4920 success = __kmp_affinity_create_flat_map(&msg_id);
4922 KMP_ASSERT(success);
4926 if (!__kmp_topology) {
4927 if (KMP_AFFINITY_CAPABLE()) {
4928 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4930 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
4932 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
4933 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
4934 __kmp_nThreadsPerCore, __kmp_ncores);
4936 __kmp_topology->print(env_var);
4943 __kmp_topology->canonicalize();
4945 __kmp_topology->print(env_var);
4946 bool filtered = __kmp_topology->filter_hw_subset();
4947 if (filtered && verbose)
4948 __kmp_topology->print(
"KMP_HW_SUBSET");
4952static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
4953 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4954 bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
4955 const char *env_var = __kmp_get_affinity_env_var(affinity);
4957 if (affinity.flags.initialized) {
4958 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4962 if (is_regular_affinity && (!__kmp_affin_fullMask || !__kmp_affin_origMask))
4963 __kmp_aux_affinity_initialize_masks(affinity);
4965 if (is_regular_affinity && !__kmp_topology) {
4966 bool success = __kmp_aux_affinity_initialize_topology(affinity);
4968 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4970 affinity.type = affinity_none;
4971 KMP_AFFINITY_DISABLE();
4978 if (affinity.type == affinity_none) {
4979 __kmp_create_affinity_none_places(affinity);
4980#if KMP_USE_HIER_SCHED
4981 __kmp_dispatch_set_hierarchy_values();
4983 affinity.flags.initialized = TRUE;
4987 __kmp_topology->set_granularity(affinity);
4988 int depth = __kmp_topology->get_depth();
4991 unsigned numUnique = 0;
4992 int numAddrs = __kmp_topology->get_num_hw_threads();
4995 if (affinity.core_attr_gran.valid) {
4996 __kmp_create_os_id_masks(&numUnique, affinity, [&](
int idx) {
4997 KMP_ASSERT(idx >= -1);
4998 for (
int i = idx + 1; i < numAddrs; ++i)
4999 if (__kmp_topology->at(i).attrs.contains(affinity.core_attr_gran))
5003 if (!affinity.os_id_masks) {
5004 const char *core_attribute;
5005 if (affinity.core_attr_gran.core_eff != kmp_hw_attr_t::UNKNOWN_CORE_EFF)
5006 core_attribute =
"core_efficiency";
5008 core_attribute =
"core_type";
5009 KMP_AFF_WARNING(affinity, AffIgnoringNotAvailable, env_var,
5011 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true))
5017 if (!affinity.os_id_masks) {
5018 int gran = affinity.gran_levels;
5019 int gran_level = depth - 1 - affinity.gran_levels;
5020 if (gran >= 0 && gran_level >= 0 && gran_level < depth) {
5021 __kmp_create_os_id_masks(
5022 &numUnique, affinity, [depth, numAddrs, &affinity](
int idx) {
5023 KMP_ASSERT(idx >= -1);
5024 int gran = affinity.gran_levels;
5025 int gran_level = depth - 1 - affinity.gran_levels;
5026 for (
int i = idx + 1; i < numAddrs; ++i)
5027 if ((gran >= depth) ||
5028 (gran < depth && __kmp_topology->at(i).ids[gran_level] !=
5029 kmp_hw_thread_t::UNKNOWN_ID))
5036 if (!affinity.os_id_masks) {
5037 __kmp_create_os_id_masks(&numUnique, affinity, [](
int idx) {
5038 KMP_ASSERT(idx >= -1);
5043 switch (affinity.type) {
5045 case affinity_explicit:
5046 KMP_DEBUG_ASSERT(affinity.proclist != NULL);
5047 if (is_hidden_helper_affinity ||
5048 __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
5049 __kmp_affinity_process_proclist(affinity);
5051 __kmp_affinity_process_placelist(affinity);
5053 if (affinity.num_masks == 0) {
5054 KMP_AFF_WARNING(affinity, AffNoValidProcID);
5055 affinity.type = affinity_none;
5056 __kmp_create_affinity_none_places(affinity);
5057 affinity.flags.initialized = TRUE;
5066 case affinity_logical:
5067 affinity.compact = 0;
5068 if (affinity.offset) {
5070 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
5074 case affinity_physical:
5075 if (__kmp_nThreadsPerCore > 1) {
5076 affinity.compact = 1;
5077 if (affinity.compact >= depth) {
5078 affinity.compact = 0;
5081 affinity.compact = 0;
5083 if (affinity.offset) {
5085 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
5089 case affinity_scatter:
5090 if (affinity.compact >= depth) {
5091 affinity.compact = 0;
5093 affinity.compact = depth - 1 - affinity.compact;
5097 case affinity_compact:
5098 if (affinity.compact >= depth) {
5099 affinity.compact = depth - 1;
5103 case affinity_balanced:
5104 if (depth <= 1 || is_hidden_helper_affinity) {
5105 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
5106 affinity.type = affinity_none;
5107 __kmp_create_affinity_none_places(affinity);
5108 affinity.flags.initialized = TRUE;
5110 }
else if (!__kmp_topology->is_uniform()) {
5112 __kmp_aff_depth = depth;
5115 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
5116 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
5118 int maxprocpercore = __kmp_affinity_max_proc_per_core(
5119 __kmp_avail_proc, depth - 1, core_level);
5121 int nproc = ncores * maxprocpercore;
5122 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
5123 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
5124 affinity.type = affinity_none;
5125 __kmp_create_affinity_none_places(affinity);
5126 affinity.flags.initialized = TRUE;
5130 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5131 for (
int i = 0; i < nproc; i++) {
5137 for (
int i = 0; i < __kmp_avail_proc; i++) {
5138 int proc = __kmp_topology->at(i).os_id;
5139 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
5141 if (core == lastcore) {
5148 procarr[core * maxprocpercore + inlastcore] = proc;
5151 if (affinity.compact >= depth) {
5152 affinity.compact = depth - 1;
5157 if (affinity.flags.dups) {
5158 affinity.num_masks = __kmp_avail_proc;
5160 affinity.num_masks = numUnique;
5163 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
5164 (__kmp_affinity_num_places > 0) &&
5165 ((
unsigned)__kmp_affinity_num_places < affinity.num_masks) &&
5166 !is_hidden_helper_affinity) {
5167 affinity.num_masks = __kmp_affinity_num_places;
5170 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
5174 __kmp_topology->sort_compact(affinity);
5178 int num_hw_threads = __kmp_topology->get_num_hw_threads();
5179 kmp_full_mask_modifier_t full_mask;
5180 for (i = 0, j = 0; i < num_hw_threads; i++) {
5181 if ((!affinity.flags.dups) && (!__kmp_topology->at(i).leader)) {
5184 int osId = __kmp_topology->at(i).os_id;
5186 kmp_affin_mask_t *src = KMP_CPU_INDEX(affinity.os_id_masks, osId);
5187 if (KMP_CPU_ISEMPTY(src))
5189 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, j);
5190 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
5191 KMP_CPU_COPY(dest, src);
5192 full_mask.include(src);
5193 if (++j >= affinity.num_masks) {
5197 KMP_DEBUG_ASSERT(j == affinity.num_masks);
5199 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
5200 __kmp_topology->print(env_var);
5204 __kmp_topology->sort_ids();
5208 KMP_ASSERT2(0,
"Unexpected affinity setting");
5210 __kmp_aux_affinity_initialize_other_data(affinity);
5211 affinity.flags.initialized = TRUE;
5214void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
5223 int disabled = (affinity.type == affinity_disabled);
5224 if (!KMP_AFFINITY_CAPABLE())
5225 KMP_ASSERT(disabled);
5227 affinity.type = affinity_none;
5228 __kmp_aux_affinity_initialize(affinity);
5230 affinity.type = affinity_disabled;
5233void __kmp_affinity_uninitialize(
void) {
5234 for (kmp_affinity_t *affinity : __kmp_affinities) {
5235 if (affinity->masks != NULL)
5236 KMP_CPU_FREE_ARRAY(affinity->masks, affinity->num_masks);
5237 if (affinity->os_id_masks != NULL)
5238 KMP_CPU_FREE_ARRAY(affinity->os_id_masks, affinity->num_os_id_masks);
5239 if (affinity->proclist != NULL)
5240 __kmp_free(affinity->proclist);
5241 if (affinity->ids != NULL)
5242 __kmp_free(affinity->ids);
5243 if (affinity->attrs != NULL)
5244 __kmp_free(affinity->attrs);
5245 *affinity = KMP_AFFINITY_INIT(affinity->env_var);
5247 if (__kmp_affin_origMask != NULL) {
5248 if (KMP_AFFINITY_CAPABLE()) {
5251 bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
5253 __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
5256 KMP_CPU_FREE(__kmp_affin_origMask);
5257 __kmp_affin_origMask = NULL;
5259 __kmp_affinity_num_places = 0;
5260 if (procarr != NULL) {
5261 __kmp_free(procarr);
5264 if (__kmp_osid_to_hwthread_map) {
5265 __kmp_free(__kmp_osid_to_hwthread_map);
5266 __kmp_osid_to_hwthread_map = NULL;
5269 if (__kmp_hwloc_topology != NULL) {
5270 hwloc_topology_destroy(__kmp_hwloc_topology);
5271 __kmp_hwloc_topology = NULL;
5274 if (__kmp_hw_subset) {
5275 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
5276 __kmp_hw_subset =
nullptr;
5278 if (__kmp_topology) {
5279 kmp_topology_t::deallocate(__kmp_topology);
5280 __kmp_topology =
nullptr;
5282 KMPAffinity::destroy_api();
5285static void __kmp_select_mask_by_gtid(
int gtid,
const kmp_affinity_t *affinity,
5286 int *place, kmp_affin_mask_t **mask) {
5288 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5289 if (is_hidden_helper)
5292 mask_idx = gtid - 2;
5294 mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
5295 KMP_DEBUG_ASSERT(affinity->num_masks > 0);
5296 *place = (mask_idx + affinity->offset) % affinity->num_masks;
5297 *mask = KMP_CPU_INDEX(affinity->masks, *place);
5302void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
5304 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5307 for (
int id = 0;
id < KMP_HW_LAST; ++id)
5308 th->th.th_topology_ids.ids[
id] = kmp_hw_thread_t::UNKNOWN_ID;
5309 th->th.th_topology_attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
5311 if (!KMP_AFFINITY_CAPABLE()) {
5315 if (th->th.th_affin_mask == NULL) {
5316 KMP_CPU_ALLOC(th->th.th_affin_mask);
5318 KMP_CPU_ZERO(th->th.th_affin_mask);
5326 kmp_affin_mask_t *mask;
5328 const kmp_affinity_t *affinity;
5329 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5331 if (is_hidden_helper)
5332 affinity = &__kmp_hh_affinity;
5334 affinity = &__kmp_affinity;
5336 if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
5337 if ((affinity->type == affinity_none) ||
5338 (affinity->type == affinity_balanced) ||
5339 KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5340#if KMP_GROUP_AFFINITY
5341 if (__kmp_num_proc_groups > 1) {
5345 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5347 mask = __kmp_affin_fullMask;
5349 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5352 if (!isa_root || __kmp_nested_proc_bind.bind_types[0] == proc_bind_false) {
5353#if KMP_GROUP_AFFINITY
5354 if (__kmp_num_proc_groups > 1) {
5358 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5360 mask = __kmp_affin_fullMask;
5362 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5366 th->th.th_current_place = i;
5367 if (isa_root && !is_hidden_helper) {
5368 th->th.th_new_place = i;
5369 th->th.th_first_place = 0;
5370 th->th.th_last_place = affinity->num_masks - 1;
5371 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
5374 th->th.th_first_place = 0;
5375 th->th.th_last_place = affinity->num_masks - 1;
5379 th->th.th_topology_ids = __kmp_affinity.ids[i];
5380 th->th.th_topology_attrs = __kmp_affinity.attrs[i];
5383 if (i == KMP_PLACE_ALL) {
5384 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to all places\n",
5387 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to place %d\n",
5391 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5394void __kmp_affinity_bind_init_mask(
int gtid) {
5395 if (!KMP_AFFINITY_CAPABLE()) {
5398 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5399 const kmp_affinity_t *affinity;
5400 const char *env_var;
5401 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5403 if (is_hidden_helper)
5404 affinity = &__kmp_hh_affinity;
5406 affinity = &__kmp_affinity;
5407 env_var = __kmp_get_affinity_env_var(*affinity,
true);
5409 if (affinity->flags.verbose && (affinity->type == affinity_none ||
5410 (th->th.th_current_place != KMP_PLACE_ALL &&
5411 affinity->type != affinity_balanced)) &&
5412 !KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5413 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5414 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5415 th->th.th_affin_mask);
5416 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5424 if (affinity->type == affinity_none) {
5425 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
5430 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5434void __kmp_affinity_bind_place(
int gtid) {
5436 if (!KMP_AFFINITY_CAPABLE() || KMP_HIDDEN_HELPER_THREAD(gtid)) {
5440 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5442 KA_TRACE(100, (
"__kmp_affinity_bind_place: binding T#%d to place %d (current "
5444 gtid, th->th.th_new_place, th->th.th_current_place));
5447 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5448 KMP_ASSERT(th->th.th_new_place >= 0);
5449 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity.num_masks);
5450 if (th->th.th_first_place <= th->th.th_last_place) {
5451 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
5452 (th->th.th_new_place <= th->th.th_last_place));
5454 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
5455 (th->th.th_new_place >= th->th.th_last_place));
5460 kmp_affin_mask_t *mask =
5461 KMP_CPU_INDEX(__kmp_affinity.masks, th->th.th_new_place);
5462 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5463 th->th.th_current_place = th->th.th_new_place;
5465 if (__kmp_affinity.flags.verbose) {
5466 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5467 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5468 th->th.th_affin_mask);
5469 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
5470 __kmp_gettid(), gtid, buf);
5472 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5475int __kmp_aux_set_affinity(
void **mask) {
5480 if (!KMP_AFFINITY_CAPABLE()) {
5484 gtid = __kmp_entry_gtid();
5487 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5488 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5489 (kmp_affin_mask_t *)(*mask));
5491 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
5495 if (__kmp_env_consistency_check) {
5496 if ((mask == NULL) || (*mask == NULL)) {
5497 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5502 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
5503 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5504 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5506 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
5511 if (num_procs == 0) {
5512 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5515#if KMP_GROUP_AFFINITY
5516 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
5517 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5523 th = __kmp_threads[gtid];
5524 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5525 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5527 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
5530 th->th.th_current_place = KMP_PLACE_UNDEFINED;
5531 th->th.th_new_place = KMP_PLACE_UNDEFINED;
5532 th->th.th_first_place = 0;
5533 th->th.th_last_place = __kmp_affinity.num_masks - 1;
5536 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
5541int __kmp_aux_get_affinity(
void **mask) {
5544#if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5547 if (!KMP_AFFINITY_CAPABLE()) {
5551 gtid = __kmp_entry_gtid();
5552#if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5553 th = __kmp_threads[gtid];
5557 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5561 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5562 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5563 th->th.th_affin_mask);
5565 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
5569 if (__kmp_env_consistency_check) {
5570 if ((mask == NULL) || (*mask == NULL)) {
5571 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
5575#if !KMP_OS_WINDOWS && !KMP_OS_AIX
5577 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5580 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5581 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5582 (kmp_affin_mask_t *)(*mask));
5584 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
5592 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
5598int __kmp_aux_get_affinity_max_proc() {
5599 if (!KMP_AFFINITY_CAPABLE()) {
5602#if KMP_GROUP_AFFINITY
5603 if (__kmp_num_proc_groups > 1) {
5604 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
5610int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
5611 if (!KMP_AFFINITY_CAPABLE()) {
5617 int gtid = __kmp_entry_gtid();
5618 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5619 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5620 (kmp_affin_mask_t *)(*mask));
5621 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
5622 "affinity mask for thread %d = %s\n",
5626 if (__kmp_env_consistency_check) {
5627 if ((mask == NULL) || (*mask == NULL)) {
5628 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
5632 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5635 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5639 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
5643int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
5644 if (!KMP_AFFINITY_CAPABLE()) {
5650 int gtid = __kmp_entry_gtid();
5651 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5652 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5653 (kmp_affin_mask_t *)(*mask));
5654 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
5655 "affinity mask for thread %d = %s\n",
5659 if (__kmp_env_consistency_check) {
5660 if ((mask == NULL) || (*mask == NULL)) {
5661 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
5665 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5668 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5672 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
5676int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
5677 if (!KMP_AFFINITY_CAPABLE()) {
5683 int gtid = __kmp_entry_gtid();
5684 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5685 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5686 (kmp_affin_mask_t *)(*mask));
5687 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
5688 "affinity mask for thread %d = %s\n",
5692 if (__kmp_env_consistency_check) {
5693 if ((mask == NULL) || (*mask == NULL)) {
5694 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
5698 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5701 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5705 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
5708#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
5710int __kmp_get_first_osid_with_ecore(
void) {
5712 int high = __kmp_topology->get_num_hw_threads() - 1;
5714 while (high - low > 1) {
5715 mid = (high + low) / 2;
5716 if (__kmp_topology->at(mid).attrs.get_core_type() ==
5717 KMP_HW_CORE_TYPE_CORE) {
5723 if (__kmp_topology->at(mid).attrs.get_core_type() == KMP_HW_CORE_TYPE_ATOM) {
5731void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
5732 KMP_DEBUG_ASSERT(th);
5733 bool fine_gran =
true;
5734 int tid = th->th.th_info.ds.ds_tid;
5735 const char *env_var =
"KMP_AFFINITY";
5738 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
5741 switch (__kmp_affinity.gran) {
5745 if (__kmp_nThreadsPerCore > 1) {
5750 if (nCoresPerPkg > 1) {
5758 if (__kmp_topology->is_uniform()) {
5762 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
5764 int ncores = __kmp_ncores;
5765 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
5766 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
5770 int chunk = nthreads / ncores;
5772 int big_cores = nthreads % ncores;
5774 int big_nth = (chunk + 1) * big_cores;
5775 if (tid < big_nth) {
5776 coreID = tid / (chunk + 1);
5777 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5779 coreID = (tid - big_cores) / chunk;
5780 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5782 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5783 "Illegal set affinity operation when not capable");
5785 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5790 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
5791 KMP_CPU_SET(osID, mask);
5793 for (
int i = 0; i < __kmp_nth_per_core; i++) {
5795 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
5796 KMP_CPU_SET(osID, mask);
5799 if (__kmp_affinity.flags.verbose) {
5800 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5801 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5802 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5805 __kmp_affinity_get_thread_topology_info(th);
5806 __kmp_set_system_affinity(mask, TRUE);
5809 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5813 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
5814 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
5815 __kmp_aff_depth - 1, core_level);
5816 int nth_per_core = __kmp_affinity_max_proc_per_core(
5817 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5821 if (nthreads == __kmp_avail_proc) {
5823 int osID = __kmp_topology->at(tid).os_id;
5824 KMP_CPU_SET(osID, mask);
5827 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
5828 for (
int i = 0; i < __kmp_avail_proc; i++) {
5829 int osID = __kmp_topology->at(i).os_id;
5830 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
5832 KMP_CPU_SET(osID, mask);
5836 }
else if (nthreads <= ncores) {
5839 for (
int i = 0; i < ncores; i++) {
5842 for (
int j = 0; j < nth_per_core; j++) {
5843 if (procarr[i * nth_per_core + j] != -1) {
5850 for (
int j = 0; j < nth_per_core; j++) {
5851 int osID = procarr[i * nth_per_core + j];
5853 KMP_CPU_SET(osID, mask);
5869 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
5871 int *ncores_with_x_procs =
5872 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5874 int *ncores_with_x_to_max_procs =
5875 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5877 for (
int i = 0; i <= nth_per_core; i++) {
5878 ncores_with_x_procs[i] = 0;
5879 ncores_with_x_to_max_procs[i] = 0;
5882 for (
int i = 0; i < ncores; i++) {
5884 for (
int j = 0; j < nth_per_core; j++) {
5885 if (procarr[i * nth_per_core + j] != -1) {
5889 nproc_at_core[i] = cnt;
5890 ncores_with_x_procs[cnt]++;
5893 for (
int i = 0; i <= nth_per_core; i++) {
5894 for (
int j = i; j <= nth_per_core; j++) {
5895 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5900 int nproc = nth_per_core * ncores;
5902 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5903 for (
int i = 0; i < nproc; i++) {
5910 for (
int j = 1; j <= nth_per_core; j++) {
5911 int cnt = ncores_with_x_to_max_procs[j];
5912 for (
int i = 0; i < ncores; i++) {
5914 if (nproc_at_core[i] == 0) {
5917 for (
int k = 0; k < nth_per_core; k++) {
5918 if (procarr[i * nth_per_core + k] != -1) {
5919 if (newarr[i * nth_per_core + k] == 0) {
5920 newarr[i * nth_per_core + k] = 1;
5926 newarr[i * nth_per_core + k]++;
5934 if (cnt == 0 || nth == 0) {
5945 for (
int i = 0; i < nproc; i++) {
5949 int osID = procarr[i];
5950 KMP_CPU_SET(osID, mask);
5952 int coreID = i / nth_per_core;
5953 for (
int ii = 0; ii < nth_per_core; ii++) {
5954 int osID = procarr[coreID * nth_per_core + ii];
5956 KMP_CPU_SET(osID, mask);
5966 if (__kmp_affinity.flags.verbose) {
5967 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5968 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5969 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5972 __kmp_affinity_get_thread_topology_info(th);
5973 __kmp_set_system_affinity(mask, TRUE);
5977#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_DRAGONFLY || \
5992 kmp_set_thread_affinity_mask_initial()
5997 int gtid = __kmp_get_gtid();
6000 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6001 "non-omp thread, returning\n"));
6004 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
6005 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6006 "affinity not initialized, returning\n"));
6009 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
6010 "set full mask for thread %d\n",
6012 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
6014 return bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
6016 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
int try_open(const char *filename, const char *mode)