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) {
206 if (ahwthread->ids[level] < bhwthread->ids[level])
208 else if (ahwthread->ids[level] > bhwthread->ids[level])
211 if (ahwthread->os_id < bhwthread->os_id)
213 else if (ahwthread->os_id > bhwthread->os_id)
218#if KMP_AFFINITY_SUPPORTED
219int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b) {
221 const kmp_hw_thread_t *aa = (
const kmp_hw_thread_t *)a;
222 const kmp_hw_thread_t *bb = (
const kmp_hw_thread_t *)b;
223 int depth = __kmp_topology->get_depth();
224 int compact = __kmp_topology->compact;
225 KMP_DEBUG_ASSERT(compact >= 0);
226 KMP_DEBUG_ASSERT(compact <= depth);
227 for (i = 0; i < compact; i++) {
228 int j = depth - i - 1;
229 if (aa->sub_ids[j] < bb->sub_ids[j])
231 if (aa->sub_ids[j] > bb->sub_ids[j])
234 for (; i < depth; i++) {
236 if (aa->sub_ids[j] < bb->sub_ids[j])
238 if (aa->sub_ids[j] > bb->sub_ids[j])
245void kmp_hw_thread_t::print()
const {
246 int depth = __kmp_topology->get_depth();
247 printf(
"%4d ", os_id);
248 for (
int i = 0; i < depth; ++i) {
249 printf(
"%4d ", ids[i]);
252 if (attrs.is_core_type_valid())
253 printf(
" (%s)", __kmp_hw_get_core_type_string(attrs.get_core_type()));
254 if (attrs.is_core_eff_valid())
255 printf(
" (eff=%d)", attrs.get_core_eff());
267void kmp_topology_t::_insert_layer(kmp_hw_t type,
const int *ids) {
271 int previous_id = kmp_hw_thread_t::UNKNOWN_ID;
272 int previous_new_id = kmp_hw_thread_t::UNKNOWN_ID;
276 for (target_layer = 0; target_layer < depth; ++target_layer) {
277 bool layers_equal =
true;
278 bool strictly_above_target_layer =
false;
279 for (
int i = 0; i < num_hw_threads; ++i) {
280 int id = hw_threads[i].ids[target_layer];
282 if (
id != previous_id && new_id == previous_new_id) {
284 strictly_above_target_layer =
true;
285 layers_equal =
false;
287 }
else if (
id == previous_id && new_id != previous_new_id) {
289 layers_equal =
false;
293 previous_new_id = new_id;
295 if (strictly_above_target_layer || layers_equal)
301 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
303 types[target_layer] = type;
304 for (
int k = 0; k < num_hw_threads; ++k) {
305 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
306 hw_threads[k].ids[j] = hw_threads[k].ids[i];
307 hw_threads[k].ids[target_layer] = ids[k];
309 equivalent[type] = type;
313#if KMP_GROUP_AFFINITY
315void kmp_topology_t::_insert_windows_proc_groups() {
317 if (__kmp_num_proc_groups == 1)
319 kmp_affin_mask_t *mask;
320 int *ids = (
int *)__kmp_allocate(
sizeof(
int) * num_hw_threads);
322 for (
int i = 0; i < num_hw_threads; ++i) {
324 KMP_CPU_SET(hw_threads[i].os_id, mask);
325 ids[i] = __kmp_get_proc_group(mask);
328 _insert_layer(KMP_HW_PROC_GROUP, ids);
335void kmp_topology_t::_remove_radix1_layers() {
336 int preference[KMP_HW_LAST];
337 int top_index1, top_index2;
339 preference[KMP_HW_SOCKET] = 110;
340 preference[KMP_HW_PROC_GROUP] = 100;
341 preference[KMP_HW_CORE] = 95;
342 preference[KMP_HW_THREAD] = 90;
343 preference[KMP_HW_NUMA] = 85;
344 preference[KMP_HW_DIE] = 80;
345 preference[KMP_HW_TILE] = 75;
346 preference[KMP_HW_MODULE] = 73;
347 preference[KMP_HW_L3] = 70;
348 preference[KMP_HW_L2] = 65;
349 preference[KMP_HW_L1] = 60;
350 preference[KMP_HW_LLC] = 5;
353 while (top_index1 < depth - 1 && top_index2 < depth) {
354 kmp_hw_t type1 = types[top_index1];
355 kmp_hw_t type2 = types[top_index2];
356 KMP_ASSERT_VALID_HW_TYPE(type1);
357 KMP_ASSERT_VALID_HW_TYPE(type2);
360 if ((type1 == KMP_HW_THREAD || type1 == KMP_HW_CORE ||
361 type1 == KMP_HW_SOCKET) &&
362 (type2 == KMP_HW_THREAD || type2 == KMP_HW_CORE ||
363 type2 == KMP_HW_SOCKET)) {
364 top_index1 = top_index2++;
368 bool all_same =
true;
369 int id1 = hw_threads[0].ids[top_index1];
370 int id2 = hw_threads[0].ids[top_index2];
371 int pref1 = preference[type1];
372 int pref2 = preference[type2];
373 for (
int hwidx = 1; hwidx < num_hw_threads; ++hwidx) {
374 if (hw_threads[hwidx].ids[top_index1] == id1 &&
375 hw_threads[hwidx].ids[top_index2] != id2) {
379 if (hw_threads[hwidx].ids[top_index2] != id2)
381 id1 = hw_threads[hwidx].ids[top_index1];
382 id2 = hw_threads[hwidx].ids[top_index2];
386 kmp_hw_t remove_type, keep_type;
387 int remove_layer, remove_layer_ids;
390 remove_layer = remove_layer_ids = top_index2;
394 remove_layer = remove_layer_ids = top_index1;
400 remove_layer_ids = top_index2;
403 set_equivalent_type(remove_type, keep_type);
404 for (
int idx = 0; idx < num_hw_threads; ++idx) {
405 kmp_hw_thread_t &hw_thread = hw_threads[idx];
406 for (
int d = remove_layer_ids; d < depth - 1; ++d)
407 hw_thread.ids[d] = hw_thread.ids[d + 1];
409 for (
int idx = remove_layer; idx < depth - 1; ++idx)
410 types[idx] = types[idx + 1];
413 top_index1 = top_index2++;
416 KMP_ASSERT(depth > 0);
419void kmp_topology_t::_set_last_level_cache() {
420 if (get_equivalent_type(KMP_HW_L3) != KMP_HW_UNKNOWN)
421 set_equivalent_type(KMP_HW_LLC, KMP_HW_L3);
422 else if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
423 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
425 else if (__kmp_mic_type == mic3) {
426 if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
427 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
428 else if (get_equivalent_type(KMP_HW_TILE) != KMP_HW_UNKNOWN)
429 set_equivalent_type(KMP_HW_LLC, KMP_HW_TILE);
432 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
435 else if (get_equivalent_type(KMP_HW_L1) != KMP_HW_UNKNOWN)
436 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
438 if (get_equivalent_type(KMP_HW_LLC) == KMP_HW_UNKNOWN) {
439 if (get_equivalent_type(KMP_HW_SOCKET) != KMP_HW_UNKNOWN)
440 set_equivalent_type(KMP_HW_LLC, KMP_HW_SOCKET);
441 else if (get_equivalent_type(KMP_HW_CORE) != KMP_HW_UNKNOWN)
442 set_equivalent_type(KMP_HW_LLC, KMP_HW_CORE);
444 KMP_ASSERT(get_equivalent_type(KMP_HW_LLC) != KMP_HW_UNKNOWN);
448void kmp_topology_t::_gather_enumeration_information() {
449 int previous_id[KMP_HW_LAST];
450 int max[KMP_HW_LAST];
452 for (
int i = 0; i < depth; ++i) {
453 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
458 int core_level = get_level(KMP_HW_CORE);
459 for (
int i = 0; i < num_hw_threads; ++i) {
460 kmp_hw_thread_t &hw_thread = hw_threads[i];
461 for (
int layer = 0; layer < depth; ++layer) {
462 int id = hw_thread.ids[layer];
463 if (
id != previous_id[layer]) {
465 for (
int l = layer; l < depth; ++l)
469 for (
int l = layer + 1; l < depth; ++l) {
470 if (max[l] > ratio[l])
476 if (__kmp_is_hybrid_cpu() && core_level >= 0 && layer <= core_level) {
477 if (hw_thread.attrs.is_core_eff_valid() &&
478 hw_thread.attrs.core_eff >= num_core_efficiencies) {
481 num_core_efficiencies = hw_thread.attrs.core_eff + 1;
483 if (hw_thread.attrs.is_core_type_valid()) {
485 for (
int j = 0; j < num_core_types; ++j) {
486 if (hw_thread.attrs.get_core_type() == core_types[j]) {
492 KMP_ASSERT(num_core_types < KMP_HW_MAX_NUM_CORE_TYPES);
493 core_types[num_core_types++] = hw_thread.attrs.get_core_type();
500 for (
int layer = 0; layer < depth; ++layer) {
501 previous_id[layer] = hw_thread.ids[layer];
504 for (
int layer = 0; layer < depth; ++layer) {
505 if (max[layer] > ratio[layer])
506 ratio[layer] = max[layer];
510int kmp_topology_t::_get_ncores_with_attr(
const kmp_hw_attr_t &attr,
512 bool find_all)
const {
513 int current, current_max;
514 int previous_id[KMP_HW_LAST];
515 for (
int i = 0; i < depth; ++i)
516 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
517 int core_level = get_level(KMP_HW_CORE);
520 KMP_ASSERT(above_level < core_level);
523 for (
int i = 0; i < num_hw_threads; ++i) {
524 kmp_hw_thread_t &hw_thread = hw_threads[i];
525 if (!find_all && hw_thread.ids[above_level] != previous_id[above_level]) {
526 if (current > current_max)
527 current_max = current;
528 current = hw_thread.attrs.contains(attr);
530 for (
int level = above_level + 1; level <= core_level; ++level) {
531 if (hw_thread.ids[level] != previous_id[level]) {
532 if (hw_thread.attrs.contains(attr))
538 for (
int level = 0; level < depth; ++level)
539 previous_id[level] = hw_thread.ids[level];
541 if (current > current_max)
542 current_max = current;
547void kmp_topology_t::_discover_uniformity() {
549 for (
int level = 0; level < depth; ++level)
551 flags.uniform = (num == count[depth - 1]);
555void kmp_topology_t::_set_sub_ids() {
556 int previous_id[KMP_HW_LAST];
557 int sub_id[KMP_HW_LAST];
559 for (
int i = 0; i < depth; ++i) {
563 for (
int i = 0; i < num_hw_threads; ++i) {
564 kmp_hw_thread_t &hw_thread = hw_threads[i];
566 for (
int j = 0; j < depth; ++j) {
567 if (hw_thread.ids[j] != previous_id[j]) {
569 for (
int k = j + 1; k < depth; ++k) {
576 for (
int j = 0; j < depth; ++j) {
577 previous_id[j] = hw_thread.ids[j];
580 for (
int j = 0; j < depth; ++j) {
581 hw_thread.sub_ids[j] = sub_id[j];
586void kmp_topology_t::_set_globals() {
588 int core_level, thread_level, package_level;
589 package_level = get_level(KMP_HW_SOCKET);
590#if KMP_GROUP_AFFINITY
591 if (package_level == -1)
592 package_level = get_level(KMP_HW_PROC_GROUP);
594 core_level = get_level(KMP_HW_CORE);
595 thread_level = get_level(KMP_HW_THREAD);
597 KMP_ASSERT(core_level != -1);
598 KMP_ASSERT(thread_level != -1);
600 __kmp_nThreadsPerCore = calculate_ratio(thread_level, core_level);
601 if (package_level != -1) {
602 nCoresPerPkg = calculate_ratio(core_level, package_level);
603 nPackages = get_count(package_level);
606 nCoresPerPkg = get_count(core_level);
609#ifndef KMP_DFLT_NTH_CORES
610 __kmp_ncores = get_count(core_level);
614kmp_topology_t *kmp_topology_t::allocate(
int nproc,
int ndepth,
615 const kmp_hw_t *types) {
616 kmp_topology_t *retval;
618 size_t size =
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc +
619 sizeof(int) * (
size_t)KMP_HW_LAST * 3;
620 char *bytes = (
char *)__kmp_allocate(size);
621 retval = (kmp_topology_t *)bytes;
623 retval->hw_threads = (kmp_hw_thread_t *)(bytes +
sizeof(kmp_topology_t));
625 retval->hw_threads =
nullptr;
627 retval->num_hw_threads = nproc;
628 retval->depth = ndepth;
630 (
int *)(bytes +
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc);
631 retval->types = (kmp_hw_t *)arr;
632 retval->ratio = arr + (size_t)KMP_HW_LAST;
633 retval->count = arr + 2 * (size_t)KMP_HW_LAST;
634 retval->num_core_efficiencies = 0;
635 retval->num_core_types = 0;
637 for (
int i = 0; i < KMP_HW_MAX_NUM_CORE_TYPES; ++i)
638 retval->core_types[i] = KMP_HW_CORE_TYPE_UNKNOWN;
639 KMP_FOREACH_HW_TYPE(type) { retval->equivalent[type] = KMP_HW_UNKNOWN; }
640 for (
int i = 0; i < ndepth; ++i) {
641 retval->types[i] = types[i];
642 retval->equivalent[types[i]] = types[i];
647void kmp_topology_t::deallocate(kmp_topology_t *topology) {
649 __kmp_free(topology);
652bool kmp_topology_t::check_ids()
const {
654 if (num_hw_threads == 0)
656 for (
int i = 1; i < num_hw_threads; ++i) {
657 kmp_hw_thread_t ¤t_thread = hw_threads[i];
658 kmp_hw_thread_t &previous_thread = hw_threads[i - 1];
660 for (
int j = 0; j < depth; ++j) {
661 if (previous_thread.ids[j] != current_thread.ids[j]) {
673void kmp_topology_t::dump()
const {
674 printf(
"***********************\n");
675 printf(
"*** __kmp_topology: ***\n");
676 printf(
"***********************\n");
677 printf(
"* depth: %d\n", depth);
680 for (
int i = 0; i < depth; ++i)
681 printf(
"%15s ", __kmp_hw_get_keyword(types[i]));
685 for (
int i = 0; i < depth; ++i) {
686 printf(
"%15d ", ratio[i]);
691 for (
int i = 0; i < depth; ++i) {
692 printf(
"%15d ", count[i]);
696 printf(
"* num_core_eff: %d\n", num_core_efficiencies);
697 printf(
"* num_core_types: %d\n", num_core_types);
698 printf(
"* core_types: ");
699 for (
int i = 0; i < num_core_types; ++i)
700 printf(
"%3d ", core_types[i]);
703 printf(
"* equivalent map:\n");
704 KMP_FOREACH_HW_TYPE(i) {
705 const char *key = __kmp_hw_get_keyword(i);
706 const char *value = __kmp_hw_get_keyword(equivalent[i]);
707 printf(
"%-15s -> %-15s\n", key, value);
710 printf(
"* uniform: %s\n", (is_uniform() ?
"Yes" :
"No"));
712 printf(
"* num_hw_threads: %d\n", num_hw_threads);
713 printf(
"* hw_threads:\n");
714 for (
int i = 0; i < num_hw_threads; ++i) {
715 hw_threads[i].print();
717 printf(
"***********************\n");
720void kmp_topology_t::print(
const char *env_var)
const {
722 int print_types_depth;
723 __kmp_str_buf_init(&buf);
724 kmp_hw_t print_types[KMP_HW_LAST + 2];
727 if (num_hw_threads) {
728 KMP_INFORM(AvailableOSProc, env_var, num_hw_threads);
730 KMP_INFORM(AvailableOSProc, env_var, __kmp_xproc);
735 KMP_INFORM(Uniform, env_var);
737 KMP_INFORM(NonUniform, env_var);
741 KMP_FOREACH_HW_TYPE(type) {
742 kmp_hw_t eq_type = equivalent[type];
743 if (eq_type != KMP_HW_UNKNOWN && eq_type != type) {
744 KMP_INFORM(AffEqualTopologyTypes, env_var,
745 __kmp_hw_get_catalog_string(type),
746 __kmp_hw_get_catalog_string(eq_type));
751 KMP_ASSERT(depth > 0 && depth <= (
int)KMP_HW_LAST);
754 print_types_depth = 0;
755 for (
int level = 0; level < depth; ++level)
756 print_types[print_types_depth++] = types[level];
757 if (equivalent[KMP_HW_CORE] != KMP_HW_CORE) {
759 if (print_types[print_types_depth - 1] == KMP_HW_THREAD) {
762 print_types[print_types_depth - 1] = KMP_HW_CORE;
763 print_types[print_types_depth++] = KMP_HW_THREAD;
765 print_types[print_types_depth++] = KMP_HW_CORE;
769 if (equivalent[KMP_HW_THREAD] != KMP_HW_THREAD)
770 print_types[print_types_depth++] = KMP_HW_THREAD;
772 __kmp_str_buf_clear(&buf);
773 kmp_hw_t numerator_type;
774 kmp_hw_t denominator_type = KMP_HW_UNKNOWN;
775 int core_level = get_level(KMP_HW_CORE);
776 int ncores = get_count(core_level);
778 for (
int plevel = 0, level = 0; plevel < print_types_depth; ++plevel) {
781 numerator_type = print_types[plevel];
782 KMP_ASSERT_VALID_HW_TYPE(numerator_type);
783 if (equivalent[numerator_type] != numerator_type)
786 c = get_ratio(level++);
789 __kmp_str_buf_print(&buf,
"%d %s", c,
790 __kmp_hw_get_catalog_string(numerator_type, plural));
792 __kmp_str_buf_print(&buf,
" x %d %s/%s", c,
793 __kmp_hw_get_catalog_string(numerator_type, plural),
794 __kmp_hw_get_catalog_string(denominator_type));
796 denominator_type = numerator_type;
798 KMP_INFORM(TopologyGeneric, env_var, buf.str, ncores);
801 if (__kmp_is_hybrid_cpu()) {
802 for (
int i = 0; i < num_core_types; ++i) {
803 kmp_hw_core_type_t core_type = core_types[i];
806 attr.set_core_type(core_type);
807 int ncores = get_ncores_with_attr(attr);
809 KMP_INFORM(TopologyHybrid, env_var, ncores,
810 __kmp_hw_get_core_type_string(core_type));
811 KMP_ASSERT(num_core_efficiencies <= KMP_HW_MAX_NUM_CORE_EFFS)
812 for (
int eff = 0; eff < num_core_efficiencies; ++eff) {
813 attr.set_core_eff(eff);
814 int ncores_with_eff = get_ncores_with_attr(attr);
815 if (ncores_with_eff > 0) {
816 KMP_INFORM(TopologyHybridCoreEff, env_var, ncores_with_eff, eff);
823 if (num_hw_threads <= 0) {
824 __kmp_str_buf_free(&buf);
829 KMP_INFORM(OSProcToPhysicalThreadMap, env_var);
830 for (
int i = 0; i < num_hw_threads; i++) {
831 __kmp_str_buf_clear(&buf);
832 for (
int level = 0; level < depth; ++level) {
833 kmp_hw_t type = types[level];
834 __kmp_str_buf_print(&buf,
"%s ", __kmp_hw_get_catalog_string(type));
835 __kmp_str_buf_print(&buf,
"%d ", hw_threads[i].ids[level]);
837 if (__kmp_is_hybrid_cpu())
840 __kmp_hw_get_core_type_string(hw_threads[i].attrs.get_core_type()));
841 KMP_INFORM(OSProcMapToPack, env_var, hw_threads[i].os_id, buf.str);
844 __kmp_str_buf_free(&buf);
847#if KMP_AFFINITY_SUPPORTED
848void kmp_topology_t::set_granularity(kmp_affinity_t &affinity)
const {
849 const char *env_var = __kmp_get_affinity_env_var(affinity);
853 if (!__kmp_is_hybrid_cpu()) {
854 if (affinity.core_attr_gran.valid) {
858 affinity, AffIgnoringNonHybrid, env_var,
859 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true));
860 affinity.gran = KMP_HW_CORE;
861 affinity.gran_levels = -1;
862 affinity.core_attr_gran = KMP_AFFINITY_ATTRS_UNKNOWN;
863 affinity.flags.core_types_gran = affinity.flags.core_effs_gran = 0;
864 }
else if (affinity.flags.core_types_gran ||
865 affinity.flags.core_effs_gran) {
867 if (affinity.flags.omp_places) {
869 affinity, AffIgnoringNonHybrid, env_var,
870 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true));
873 KMP_AFF_WARNING(affinity, AffGranularityBad, env_var,
874 "Intel(R) Hybrid Technology core attribute",
875 __kmp_hw_get_catalog_string(KMP_HW_CORE));
877 affinity.gran = KMP_HW_CORE;
878 affinity.gran_levels = -1;
879 affinity.core_attr_gran = KMP_AFFINITY_ATTRS_UNKNOWN;
880 affinity.flags.core_types_gran = affinity.flags.core_effs_gran = 0;
884 if (affinity.gran_levels < 0) {
885 kmp_hw_t gran_type = get_equivalent_type(affinity.gran);
887 if (gran_type == KMP_HW_UNKNOWN) {
889 kmp_hw_t gran_types[3] = {KMP_HW_CORE, KMP_HW_THREAD, KMP_HW_SOCKET};
890 for (
auto g : gran_types) {
891 if (get_equivalent_type(g) != KMP_HW_UNKNOWN) {
896 KMP_ASSERT(gran_type != KMP_HW_UNKNOWN);
898 KMP_AFF_WARNING(affinity, AffGranularityBad, env_var,
899 __kmp_hw_get_catalog_string(affinity.gran),
900 __kmp_hw_get_catalog_string(gran_type));
901 affinity.gran = gran_type;
903#if KMP_GROUP_AFFINITY
911 if (__kmp_num_proc_groups > 1) {
912 int gran_depth = get_level(gran_type);
913 int proc_group_depth = get_level(KMP_HW_PROC_GROUP);
914 if (gran_depth >= 0 && proc_group_depth >= 0 &&
915 gran_depth < proc_group_depth) {
916 KMP_AFF_WARNING(affinity, AffGranTooCoarseProcGroup, env_var,
917 __kmp_hw_get_catalog_string(affinity.gran));
918 affinity.gran = gran_type = KMP_HW_PROC_GROUP;
922 affinity.gran_levels = 0;
923 for (
int i = depth - 1; i >= 0 && get_type(i) != gran_type; --i)
924 affinity.gran_levels++;
929void kmp_topology_t::canonicalize() {
930#if KMP_GROUP_AFFINITY
931 _insert_windows_proc_groups();
933 _remove_radix1_layers();
934 _gather_enumeration_information();
935 _discover_uniformity();
938 _set_last_level_cache();
942 if (__kmp_mic_type == mic3) {
943 if (get_level(KMP_HW_L2) != -1)
944 set_equivalent_type(KMP_HW_TILE, KMP_HW_L2);
945 else if (get_level(KMP_HW_TILE) != -1)
946 set_equivalent_type(KMP_HW_L2, KMP_HW_TILE);
951 KMP_ASSERT(depth > 0);
952 for (
int level = 0; level < depth; ++level) {
954 KMP_ASSERT(count[level] > 0 && ratio[level] > 0);
955 KMP_ASSERT_VALID_HW_TYPE(types[level]);
957 KMP_ASSERT(equivalent[types[level]] == types[level]);
962void kmp_topology_t::canonicalize(
int npackages,
int ncores_per_pkg,
963 int nthreads_per_core,
int ncores) {
966 KMP_FOREACH_HW_TYPE(i) { equivalent[i] = KMP_HW_UNKNOWN; }
967 for (
int level = 0; level < depth; ++level) {
971 count[0] = npackages;
973 count[2] = __kmp_xproc;
974 ratio[0] = npackages;
975 ratio[1] = ncores_per_pkg;
976 ratio[2] = nthreads_per_core;
977 equivalent[KMP_HW_SOCKET] = KMP_HW_SOCKET;
978 equivalent[KMP_HW_CORE] = KMP_HW_CORE;
979 equivalent[KMP_HW_THREAD] = KMP_HW_THREAD;
980 types[0] = KMP_HW_SOCKET;
981 types[1] = KMP_HW_CORE;
982 types[2] = KMP_HW_THREAD;
984 _discover_uniformity();
989template <
size_t SIZE,
typename IndexFunc>
struct kmp_sub_ids_t {
992 int prev_sub_id[KMP_HW_LAST];
996 kmp_sub_ids_t(
int last_level) : last_level(last_level) {
997 KMP_ASSERT(last_level < KMP_HW_LAST);
998 for (
size_t i = 0; i < SIZE; ++i)
1000 for (
size_t i = 0; i < KMP_HW_LAST; ++i)
1001 prev_sub_id[i] = -1;
1003 void update(
const kmp_hw_thread_t &hw_thread) {
1004 int idx = indexer(hw_thread);
1005 KMP_ASSERT(idx < (
int)SIZE);
1006 for (
int level = 0; level <= last_level; ++level) {
1007 if (hw_thread.sub_ids[level] != prev_sub_id[level]) {
1008 if (level < last_level)
1014 for (
int level = 0; level <= last_level; ++level)
1015 prev_sub_id[level] = hw_thread.sub_ids[level];
1017 int get_sub_id(
const kmp_hw_thread_t &hw_thread)
const {
1018 return sub_id[indexer(hw_thread)];
1022#if KMP_AFFINITY_SUPPORTED
1023static kmp_str_buf_t *
1024__kmp_hw_get_catalog_core_string(
const kmp_hw_attr_t &attr, kmp_str_buf_t *buf,
1026 __kmp_str_buf_init(buf);
1027 if (attr.is_core_type_valid())
1028 __kmp_str_buf_print(buf,
"%s %s",
1029 __kmp_hw_get_core_type_string(attr.get_core_type()),
1030 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural));
1032 __kmp_str_buf_print(buf,
"%s eff=%d",
1033 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural),
1034 attr.get_core_eff());
1038bool kmp_topology_t::restrict_to_mask(
const kmp_affin_mask_t *mask) {
1042 for (
int i = 0; i < num_hw_threads; ++i) {
1043 int os_id = hw_threads[i].os_id;
1044 if (KMP_CPU_ISSET(os_id, mask)) {
1046 hw_threads[new_index] = hw_threads[i];
1049 KMP_CPU_CLR(os_id, __kmp_affin_fullMask);
1054 KMP_DEBUG_ASSERT(new_index <= num_hw_threads);
1055 affected = (num_hw_threads != new_index);
1056 num_hw_threads = new_index;
1060 _gather_enumeration_information();
1061 _discover_uniformity();
1063 _set_last_level_cache();
1066 if (__kmp_num_proc_groups <= 1)
1068 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
1076bool kmp_topology_t::filter_hw_subset() {
1078 if (!__kmp_hw_subset)
1082 __kmp_hw_subset->sort();
1085 bool using_core_types =
false;
1086 bool using_core_effs =
false;
1087 int hw_subset_depth = __kmp_hw_subset->get_depth();
1088 kmp_hw_t specified[KMP_HW_LAST];
1089 int *topology_levels = (
int *)KMP_ALLOCA(
sizeof(
int) * hw_subset_depth);
1090 KMP_ASSERT(hw_subset_depth > 0);
1091 KMP_FOREACH_HW_TYPE(i) { specified[i] = KMP_HW_UNKNOWN; }
1092 int core_level = get_level(KMP_HW_CORE);
1093 for (
int i = 0; i < hw_subset_depth; ++i) {
1095 const kmp_hw_subset_t::item_t &item = __kmp_hw_subset->at(i);
1096 int num = item.num[0];
1097 int offset = item.offset[0];
1098 kmp_hw_t type = item.type;
1099 kmp_hw_t equivalent_type = equivalent[type];
1100 int level = get_level(type);
1101 topology_levels[i] = level;
1104 if (equivalent_type != KMP_HW_UNKNOWN) {
1105 __kmp_hw_subset->at(i).type = equivalent_type;
1107 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetNotExistGeneric,
1108 __kmp_hw_get_catalog_string(type));
1114 if (specified[equivalent_type] != KMP_HW_UNKNOWN) {
1115 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetEqvLayers,
1116 __kmp_hw_get_catalog_string(type),
1117 __kmp_hw_get_catalog_string(specified[equivalent_type]));
1120 specified[equivalent_type] = type;
1123 max_count = get_ratio(level);
1124 if (max_count < 0 ||
1125 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1126 bool plural = (num > 1);
1127 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric,
1128 __kmp_hw_get_catalog_string(type, plural));
1133 if (core_level == level) {
1135 for (
int j = 0; j < item.num_attrs; ++j) {
1136 if (item.attr[j].is_core_type_valid())
1137 using_core_types =
true;
1138 if (item.attr[j].is_core_eff_valid())
1139 using_core_effs =
true;
1147 if ((using_core_effs || using_core_types) && !__kmp_is_hybrid_cpu()) {
1148 if (item.num_attrs == 1) {
1149 if (using_core_effs) {
1150 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1153 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1156 using_core_effs =
false;
1157 using_core_types =
false;
1159 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrsNonHybrid);
1165 if (using_core_types && using_core_effs) {
1166 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
"core_type",
1172 if (using_core_effs) {
1173 for (
int j = 0; j < item.num_attrs; ++j) {
1174 if (item.attr[j].is_core_eff_valid()) {
1175 int core_eff = item.attr[j].get_core_eff();
1176 if (core_eff < 0 || core_eff >= num_core_efficiencies) {
1178 __kmp_str_buf_init(&buf);
1179 __kmp_str_buf_print(&buf,
"%d", item.attr[j].get_core_eff());
1180 __kmp_msg(kmp_ms_warning,
1181 KMP_MSG(AffHWSubsetAttrInvalid,
"efficiency", buf.str),
1182 KMP_HNT(ValidValuesRange, 0, num_core_efficiencies - 1),
1184 __kmp_str_buf_free(&buf);
1192 if (using_core_types || using_core_effs) {
1193 for (
int j = 0; j < item.num_attrs; ++j) {
1194 int num = item.num[j];
1195 int offset = item.offset[j];
1196 int level_above = core_level - 1;
1197 if (level_above >= 0) {
1198 max_count = get_ncores_with_attr_per(item.attr[j], level_above);
1199 if (max_count <= 0 ||
1200 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1202 __kmp_hw_get_catalog_core_string(item.attr[j], &buf, num > 0);
1203 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric, buf.str);
1204 __kmp_str_buf_free(&buf);
1211 if ((using_core_types || using_core_effs) && item.num_attrs > 1) {
1212 for (
int j = 0; j < item.num_attrs; ++j) {
1215 if (!item.attr[j]) {
1216 kmp_hw_attr_t other_attr;
1217 for (
int k = 0; k < item.num_attrs; ++k) {
1218 if (item.attr[k] != item.attr[j]) {
1219 other_attr = item.attr[k];
1224 __kmp_hw_get_catalog_core_string(other_attr, &buf, item.num[j] > 0);
1225 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
1226 __kmp_hw_get_catalog_string(KMP_HW_CORE), buf.str);
1227 __kmp_str_buf_free(&buf);
1231 for (
int k = 0; k < j; ++k) {
1232 if (!item.attr[j] || !item.attr[k])
1234 if (item.attr[k] == item.attr[j]) {
1236 __kmp_hw_get_catalog_core_string(item.attr[j], &buf,
1238 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrRepeat, buf.str);
1239 __kmp_str_buf_free(&buf);
1248 struct core_type_indexer {
1249 int operator()(
const kmp_hw_thread_t &t)
const {
1250 switch (t.attrs.get_core_type()) {
1251 case KMP_HW_CORE_TYPE_UNKNOWN:
1252 case KMP_HW_MAX_NUM_CORE_TYPES:
1254#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1255 case KMP_HW_CORE_TYPE_ATOM:
1257 case KMP_HW_CORE_TYPE_CORE:
1261 KMP_ASSERT2(
false,
"Unhandled kmp_hw_thread_t enumeration");
1262 KMP_BUILTIN_UNREACHABLE;
1265 struct core_eff_indexer {
1266 int operator()(
const kmp_hw_thread_t &t)
const {
1267 return t.attrs.get_core_eff();
1271 kmp_sub_ids_t<KMP_HW_MAX_NUM_CORE_TYPES, core_type_indexer> core_type_sub_ids(
1273 kmp_sub_ids_t<KMP_HW_MAX_NUM_CORE_EFFS, core_eff_indexer> core_eff_sub_ids(
1277 int num_filtered = 0;
1278 kmp_affin_mask_t *filtered_mask;
1279 KMP_CPU_ALLOC(filtered_mask);
1280 KMP_CPU_COPY(filtered_mask, __kmp_affin_fullMask);
1281 for (
int i = 0; i < num_hw_threads; ++i) {
1282 kmp_hw_thread_t &hw_thread = hw_threads[i];
1284 if (using_core_types)
1285 core_type_sub_ids.update(hw_thread);
1286 if (using_core_effs)
1287 core_eff_sub_ids.update(hw_thread);
1290 bool should_be_filtered =
false;
1291 for (
int hw_subset_index = 0; hw_subset_index < hw_subset_depth;
1292 ++hw_subset_index) {
1293 const auto &hw_subset_item = __kmp_hw_subset->at(hw_subset_index);
1294 int level = topology_levels[hw_subset_index];
1297 if ((using_core_effs || using_core_types) && level == core_level) {
1303 kmp_hw_core_type_t core_type = hw_thread.attrs.get_core_type();
1304 int core_eff = hw_thread.attrs.get_core_eff();
1305 for (attr_idx = 0; attr_idx < hw_subset_item.num_attrs; ++attr_idx) {
1306 if (using_core_types &&
1307 hw_subset_item.attr[attr_idx].get_core_type() == core_type)
1309 if (using_core_effs &&
1310 hw_subset_item.attr[attr_idx].get_core_eff() == core_eff)
1314 if (attr_idx == hw_subset_item.num_attrs) {
1315 should_be_filtered =
true;
1319 int num = hw_subset_item.num[attr_idx];
1320 int offset = hw_subset_item.offset[attr_idx];
1321 if (using_core_types)
1322 sub_id = core_type_sub_ids.get_sub_id(hw_thread);
1324 sub_id = core_eff_sub_ids.get_sub_id(hw_thread);
1325 if (sub_id < offset ||
1326 (num != kmp_hw_subset_t::USE_ALL && sub_id >= offset + num)) {
1327 should_be_filtered =
true;
1331 int num = hw_subset_item.num[0];
1332 int offset = hw_subset_item.offset[0];
1333 if (hw_thread.sub_ids[level] < offset ||
1334 (num != kmp_hw_subset_t::USE_ALL &&
1335 hw_thread.sub_ids[level] >= offset + num)) {
1336 should_be_filtered =
true;
1342 if (should_be_filtered) {
1343 KMP_CPU_CLR(hw_thread.os_id, filtered_mask);
1349 if (num_filtered == num_hw_threads) {
1350 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAllFiltered);
1355 restrict_to_mask(filtered_mask);
1359bool kmp_topology_t::is_close(
int hwt1,
int hwt2,
1360 const kmp_affinity_t &stgs)
const {
1361 int hw_level = stgs.gran_levels;
1362 if (hw_level >= depth)
1365 const kmp_hw_thread_t &t1 = hw_threads[hwt1];
1366 const kmp_hw_thread_t &t2 = hw_threads[hwt2];
1367 if (stgs.flags.core_types_gran)
1368 return t1.attrs.get_core_type() == t2.attrs.get_core_type();
1369 if (stgs.flags.core_effs_gran)
1370 return t1.attrs.get_core_eff() == t2.attrs.get_core_eff();
1371 for (
int i = 0; i < (depth - hw_level); ++i) {
1372 if (t1.ids[i] != t2.ids[i])
1380bool KMPAffinity::picked_api =
false;
1382void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
1383void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
1384void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
1385void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
1386void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
1387void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
1389void KMPAffinity::pick_api() {
1390 KMPAffinity *affinity_dispatch;
1396 if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
1397 __kmp_affinity.type != affinity_disabled) {
1398 affinity_dispatch =
new KMPHwlocAffinity();
1402 affinity_dispatch =
new KMPNativeAffinity();
1404 __kmp_affinity_dispatch = affinity_dispatch;
1408void KMPAffinity::destroy_api() {
1409 if (__kmp_affinity_dispatch != NULL) {
1410 delete __kmp_affinity_dispatch;
1411 __kmp_affinity_dispatch = NULL;
1416#define KMP_ADVANCE_SCAN(scan) \
1417 while (*scan != '\0') { \
1425char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
1426 kmp_affin_mask_t *mask) {
1427 int start = 0, finish = 0, previous = 0;
1430 KMP_ASSERT(buf_len >= 40);
1433 char *end = buf + buf_len - 1;
1436 if (mask->begin() == mask->end()) {
1437 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
1438 KMP_ADVANCE_SCAN(scan);
1439 KMP_ASSERT(scan <= end);
1444 start = mask->begin();
1448 for (finish = mask->next(start), previous = start;
1449 finish == previous + 1 && finish != mask->end();
1450 finish = mask->next(finish)) {
1457 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
1458 KMP_ADVANCE_SCAN(scan);
1460 first_range =
false;
1463 if (previous - start > 1) {
1464 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
1467 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
1468 KMP_ADVANCE_SCAN(scan);
1469 if (previous - start > 0) {
1470 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
1473 KMP_ADVANCE_SCAN(scan);
1476 if (start == mask->end())
1484 KMP_ASSERT(scan <= end);
1487#undef KMP_ADVANCE_SCAN
1493kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
1494 kmp_affin_mask_t *mask) {
1495 int start = 0, finish = 0, previous = 0;
1500 __kmp_str_buf_clear(buf);
1503 if (mask->begin() == mask->end()) {
1504 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
1509 start = mask->begin();
1513 for (finish = mask->next(start), previous = start;
1514 finish == previous + 1 && finish != mask->end();
1515 finish = mask->next(finish)) {
1522 __kmp_str_buf_print(buf,
"%s",
",");
1524 first_range =
false;
1527 if (previous - start > 1) {
1528 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
1531 __kmp_str_buf_print(buf,
"%u", start);
1532 if (previous - start > 0) {
1533 __kmp_str_buf_print(buf,
",%u", previous);
1538 if (start == mask->end())
1546kmp_affin_mask_t *__kmp_affinity_get_offline_cpus() {
1547 kmp_affin_mask_t *offline;
1548 KMP_CPU_ALLOC(offline);
1549 KMP_CPU_ZERO(offline);
1551 int n, begin_cpu, end_cpu;
1553 auto skip_ws = [](FILE *f) {
1557 }
while (isspace(c));
1563 int status = offline_file.
try_open(
"/sys/devices/system/cpu/offline",
"r");
1566 while (!feof(offline_file)) {
1567 skip_ws(offline_file);
1568 n = fscanf(offline_file,
"%d", &begin_cpu);
1571 skip_ws(offline_file);
1572 int c = fgetc(offline_file);
1573 if (c == EOF || c ==
',') {
1575 end_cpu = begin_cpu;
1576 }
else if (c ==
'-') {
1578 skip_ws(offline_file);
1579 n = fscanf(offline_file,
"%d", &end_cpu);
1582 skip_ws(offline_file);
1583 c = fgetc(offline_file);
1589 if (begin_cpu < 0 || begin_cpu >= __kmp_xproc || end_cpu < 0 ||
1590 end_cpu >= __kmp_xproc || begin_cpu > end_cpu) {
1594 for (
int cpu = begin_cpu; cpu <= end_cpu; ++cpu) {
1595 KMP_CPU_SET(cpu, offline);
1603int __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
1607#if KMP_GROUP_AFFINITY
1609 if (__kmp_num_proc_groups > 1) {
1611 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
1612 for (group = 0; group < __kmp_num_proc_groups; group++) {
1614 int num = __kmp_GetActiveProcessorCount(group);
1615 for (i = 0; i < num; i++) {
1616 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1626 kmp_affin_mask_t *offline_cpus = __kmp_affinity_get_offline_cpus();
1627 for (proc = 0; proc < __kmp_xproc; proc++) {
1629 if (KMP_CPU_ISSET(proc, offline_cpus))
1631 KMP_CPU_SET(proc, mask);
1634 KMP_CPU_FREE(offline_cpus);
1643kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1645kmp_affin_mask_t *__kmp_affin_origMask = NULL;
1648static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1649#if HWLOC_API_VERSION >= 0x00020000
1650 return hwloc_obj_type_is_cache(obj->type);
1652 return obj->type == HWLOC_OBJ_CACHE;
1657static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1659 if (__kmp_hwloc_is_cache_type(obj)) {
1660 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1661 return KMP_HW_UNKNOWN;
1662 switch (obj->attr->cache.depth) {
1666#if KMP_MIC_SUPPORTED
1667 if (__kmp_mic_type == mic3) {
1675 return KMP_HW_UNKNOWN;
1678 switch (obj->type) {
1679 case HWLOC_OBJ_PACKAGE:
1680 return KMP_HW_SOCKET;
1681 case HWLOC_OBJ_NUMANODE:
1683 case HWLOC_OBJ_CORE:
1686 return KMP_HW_THREAD;
1687 case HWLOC_OBJ_GROUP:
1688#if HWLOC_API_VERSION >= 0x00020000
1689 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1691 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1693 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1694 return KMP_HW_MODULE;
1695 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1696 return KMP_HW_PROC_GROUP;
1698 return KMP_HW_UNKNOWN;
1699#if HWLOC_API_VERSION >= 0x00020100
1704 return KMP_HW_UNKNOWN;
1711static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1712 hwloc_obj_type_t type) {
1715 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1716 obj->logical_index, type, 0);
1717 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1718 obj->type, first) == obj;
1719 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1728static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1729 hwloc_obj_t lower) {
1731 hwloc_obj_type_t ltype = lower->type;
1732 int lindex = lower->logical_index - 1;
1735 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1736 while (obj && lindex >= 0 &&
1737 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1738 if (obj->userdata) {
1739 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1744 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1747 lower->userdata = RCAST(
void *, sub_id + 1);
1751static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1753 int hw_thread_index, sub_id;
1755 hwloc_obj_t pu, obj, root, prev;
1756 kmp_hw_t types[KMP_HW_LAST];
1757 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1759 hwloc_topology_t tp = __kmp_hwloc_topology;
1760 *msg_id = kmp_i18n_null;
1761 if (__kmp_affinity.flags.verbose) {
1762 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1765 if (!KMP_AFFINITY_CAPABLE()) {
1768 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1770 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1772 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1775 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1777 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1779 __kmp_nThreadsPerCore = 1;
1780 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1781 if (nCoresPerPkg == 0)
1783 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1787#if HWLOC_API_VERSION >= 0x00020400
1789 int nr_cpu_kinds = hwloc_cpukinds_get_nr(tp, 0);
1791 typedef struct kmp_hwloc_cpukinds_info_t {
1793 kmp_hw_core_type_t core_type;
1794 hwloc_bitmap_t mask;
1795 } kmp_hwloc_cpukinds_info_t;
1796 kmp_hwloc_cpukinds_info_t *cpukinds =
nullptr;
1798 if (nr_cpu_kinds > 0) {
1800 struct hwloc_info_s *infos;
1801 cpukinds = (kmp_hwloc_cpukinds_info_t *)__kmp_allocate(
1802 sizeof(kmp_hwloc_cpukinds_info_t) * nr_cpu_kinds);
1803 for (
unsigned idx = 0; idx < (unsigned)nr_cpu_kinds; ++idx) {
1804 cpukinds[idx].efficiency = -1;
1805 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_UNKNOWN;
1806 cpukinds[idx].mask = hwloc_bitmap_alloc();
1807 if (hwloc_cpukinds_get_info(tp, idx, cpukinds[idx].mask,
1808 &cpukinds[idx].efficiency, &nr_infos, &infos,
1810 for (
unsigned i = 0; i < nr_infos; ++i) {
1811 if (__kmp_str_match(
"CoreType", 8, infos[i].name)) {
1812#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1813 if (__kmp_str_match(
"IntelAtom", 9, infos[i].value)) {
1814 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_ATOM;
1816 }
else if (__kmp_str_match(
"IntelCore", 9, infos[i].value)) {
1817 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_CORE;
1828 root = hwloc_get_root_obj(tp);
1832 pu = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1835 types[depth] = KMP_HW_THREAD;
1836 hwloc_types[depth] = obj->type;
1838 while (obj != root && obj != NULL) {
1840#if HWLOC_API_VERSION >= 0x00020000
1841 if (obj->memory_arity) {
1843 for (memory = obj->memory_first_child; memory;
1844 memory = hwloc_get_next_child(tp, obj, memory)) {
1845 if (memory->type == HWLOC_OBJ_NUMANODE)
1848 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1849 types[depth] = KMP_HW_NUMA;
1850 hwloc_types[depth] = memory->type;
1855 type = __kmp_hwloc_type_2_topology_type(obj);
1856 if (type != KMP_HW_UNKNOWN) {
1857 types[depth] = type;
1858 hwloc_types[depth] = obj->type;
1862 KMP_ASSERT(depth > 0);
1865 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1866 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1867 kmp_hw_t temp = types[i];
1868 types[i] = types[j];
1870 hwloc_types[i] = hwloc_types[j];
1871 hwloc_types[j] = hwloc_temp;
1875 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1877 hw_thread_index = 0;
1879 while ((pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu))) {
1880 int index = depth - 1;
1881 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1882 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1885 hw_thread.ids[index] = pu->logical_index;
1886 hw_thread.os_id = pu->os_index;
1888#if HWLOC_API_VERSION >= 0x00020400
1890 int cpukind_index = -1;
1891 for (
int i = 0; i < nr_cpu_kinds; ++i) {
1892 if (hwloc_bitmap_isset(cpukinds[i].mask, hw_thread.os_id)) {
1897 if (cpukind_index >= 0) {
1898 hw_thread.attrs.set_core_type(cpukinds[cpukind_index].core_type);
1899 hw_thread.attrs.set_core_eff(cpukinds[cpukind_index].efficiency);
1907 while (obj != root && obj != NULL) {
1909#if HWLOC_API_VERSION >= 0x00020000
1913 if (obj->memory_arity) {
1915 for (memory = obj->memory_first_child; memory;
1916 memory = hwloc_get_next_child(tp, obj, memory)) {
1917 if (memory->type == HWLOC_OBJ_NUMANODE)
1920 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1921 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1923 hw_thread.ids[index] = memory->logical_index;
1924 hw_thread.ids[index + 1] = sub_id;
1932 type = __kmp_hwloc_type_2_topology_type(obj);
1933 if (type != KMP_HW_UNKNOWN) {
1934 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1936 hw_thread.ids[index] = obj->logical_index;
1937 hw_thread.ids[index + 1] = sub_id;
1947#if HWLOC_API_VERSION >= 0x00020400
1950 for (
int idx = 0; idx < nr_cpu_kinds; ++idx)
1951 hwloc_bitmap_free(cpukinds[idx].mask);
1952 __kmp_free(cpukinds);
1955 __kmp_topology->sort_ids();
1963static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
1964 *msg_id = kmp_i18n_null;
1966 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1968 if (__kmp_affinity.flags.verbose) {
1969 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
1975 if (!KMP_AFFINITY_CAPABLE()) {
1976 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1977 __kmp_ncores = nPackages = __kmp_xproc;
1978 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1986 __kmp_ncores = nPackages = __kmp_avail_proc;
1987 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1990 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1993 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1995 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1998 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
2000 hw_thread.os_id = i;
2001 hw_thread.ids[0] = i;
2002 hw_thread.ids[1] = 0;
2003 hw_thread.ids[2] = 0;
2006 if (__kmp_affinity.flags.verbose) {
2007 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
2012#if KMP_GROUP_AFFINITY
2017static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
2018 *msg_id = kmp_i18n_null;
2020 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
2021 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
2023 if (__kmp_affinity.flags.verbose) {
2024 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
2028 if (!KMP_AFFINITY_CAPABLE()) {
2029 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2030 nPackages = __kmp_num_proc_groups;
2031 __kmp_nThreadsPerCore = 1;
2032 __kmp_ncores = __kmp_xproc;
2033 nCoresPerPkg = nPackages / __kmp_ncores;
2038 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
2041 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2043 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2046 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct++);
2048 hw_thread.os_id = i;
2049 hw_thread.ids[0] = i / BITS_PER_GROUP;
2050 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
2056#if KMP_ARCH_X86 || KMP_ARCH_X86_64
2058template <kmp_u
int32 LSB, kmp_u
int32 MSB>
2059static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
2060 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
2061 const kmp_uint32 SHIFT_RIGHT = LSB;
2062 kmp_uint32 retval = v;
2063 retval <<= SHIFT_LEFT;
2064 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
2068static int __kmp_cpuid_mask_width(
int count) {
2071 while ((1 << r) < count)
2076class apicThreadInfo {
2080 unsigned maxCoresPerPkg;
2081 unsigned maxThreadsPerPkg;
2087static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
2089 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
2090 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
2091 if (aa->pkgId < bb->pkgId)
2093 if (aa->pkgId > bb->pkgId)
2095 if (aa->coreId < bb->coreId)
2097 if (aa->coreId > bb->coreId)
2099 if (aa->threadId < bb->threadId)
2101 if (aa->threadId > bb->threadId)
2106class kmp_cache_info_t {
2109 unsigned level, mask;
2111 kmp_cache_info_t() : depth(0) { get_leaf4_levels(); }
2112 size_t get_depth()
const {
return depth; }
2113 info_t &operator[](
size_t index) {
return table[index]; }
2114 const info_t &operator[](
size_t index)
const {
return table[index]; }
2116 static kmp_hw_t get_topology_type(
unsigned level) {
2117 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
2126 return KMP_HW_UNKNOWN;
2130 static const int MAX_CACHE_LEVEL = 3;
2133 info_t table[MAX_CACHE_LEVEL];
2135 void get_leaf4_levels() {
2137 while (depth < MAX_CACHE_LEVEL) {
2138 unsigned cache_type, max_threads_sharing;
2139 unsigned cache_level, cache_mask_width;
2141 __kmp_x86_cpuid(4, level, &buf2);
2142 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
2146 if (cache_type == 2) {
2150 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
2151 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
2152 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
2153 table[depth].level = cache_level;
2154 table[depth].mask = ((-1) << cache_mask_width);
2165static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
2167 *msg_id = kmp_i18n_null;
2169 if (__kmp_affinity.flags.verbose) {
2170 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
2174 __kmp_x86_cpuid(0, 0, &buf);
2176 *msg_id = kmp_i18n_str_NoLeaf4Support;
2185 if (!KMP_AFFINITY_CAPABLE()) {
2188 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2194 __kmp_x86_cpuid(1, 0, &buf);
2195 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2196 if (maxThreadsPerPkg == 0) {
2197 maxThreadsPerPkg = 1;
2211 __kmp_x86_cpuid(0, 0, &buf);
2213 __kmp_x86_cpuid(4, 0, &buf);
2214 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2232 __kmp_ncores = __kmp_xproc;
2233 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2234 __kmp_nThreadsPerCore = 1;
2243 kmp_affinity_raii_t previous_affinity;
2271 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
2272 __kmp_avail_proc *
sizeof(apicThreadInfo));
2273 unsigned nApics = 0;
2274 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2276 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2279 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
2281 __kmp_affinity_dispatch->bind_thread(i);
2282 threadInfo[nApics].osId = i;
2285 __kmp_x86_cpuid(1, 0, &buf);
2286 if (((buf.edx >> 9) & 1) == 0) {
2287 __kmp_free(threadInfo);
2288 *msg_id = kmp_i18n_str_ApicNotPresent;
2291 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
2292 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2293 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
2294 threadInfo[nApics].maxThreadsPerPkg = 1;
2303 __kmp_x86_cpuid(0, 0, &buf);
2305 __kmp_x86_cpuid(4, 0, &buf);
2306 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2308 threadInfo[nApics].maxCoresPerPkg = 1;
2312 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
2313 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
2315 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
2316 int widthT = widthCT - widthC;
2321 __kmp_free(threadInfo);
2322 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2326 int maskC = (1 << widthC) - 1;
2327 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
2329 int maskT = (1 << widthT) - 1;
2330 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
2337 previous_affinity.restore();
2340 qsort(threadInfo, nApics,
sizeof(*threadInfo),
2341 __kmp_affinity_cmp_apicThreadInfo_phys_id);
2358 __kmp_nThreadsPerCore = 1;
2359 unsigned nCores = 1;
2362 unsigned lastPkgId = threadInfo[0].pkgId;
2363 unsigned coreCt = 1;
2364 unsigned lastCoreId = threadInfo[0].coreId;
2365 unsigned threadCt = 1;
2366 unsigned lastThreadId = threadInfo[0].threadId;
2369 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
2370 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
2372 for (i = 1; i < nApics; i++) {
2373 if (threadInfo[i].pkgId != lastPkgId) {
2376 lastPkgId = threadInfo[i].pkgId;
2377 if ((
int)coreCt > nCoresPerPkg)
2378 nCoresPerPkg = coreCt;
2380 lastCoreId = threadInfo[i].coreId;
2381 if ((
int)threadCt > __kmp_nThreadsPerCore)
2382 __kmp_nThreadsPerCore = threadCt;
2384 lastThreadId = threadInfo[i].threadId;
2388 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
2389 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
2393 if (threadInfo[i].coreId != lastCoreId) {
2396 lastCoreId = threadInfo[i].coreId;
2397 if ((
int)threadCt > __kmp_nThreadsPerCore)
2398 __kmp_nThreadsPerCore = threadCt;
2400 lastThreadId = threadInfo[i].threadId;
2401 }
else if (threadInfo[i].threadId != lastThreadId) {
2403 lastThreadId = threadInfo[i].threadId;
2405 __kmp_free(threadInfo);
2406 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2412 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
2413 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
2414 __kmp_free(threadInfo);
2415 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
2423 if ((
int)coreCt > nCoresPerPkg)
2424 nCoresPerPkg = coreCt;
2425 if ((
int)threadCt > __kmp_nThreadsPerCore)
2426 __kmp_nThreadsPerCore = threadCt;
2427 __kmp_ncores = nCores;
2428 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
2436 int threadLevel = 2;
2438 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
2441 types[idx++] = KMP_HW_SOCKET;
2443 types[idx++] = KMP_HW_CORE;
2444 if (threadLevel >= 0)
2445 types[idx++] = KMP_HW_THREAD;
2447 KMP_ASSERT(depth > 0);
2448 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
2450 for (i = 0; i < nApics; ++i) {
2452 unsigned os = threadInfo[i].osId;
2453 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2456 if (pkgLevel >= 0) {
2457 hw_thread.ids[idx++] = threadInfo[i].pkgId;
2459 if (coreLevel >= 0) {
2460 hw_thread.ids[idx++] = threadInfo[i].coreId;
2462 if (threadLevel >= 0) {
2463 hw_thread.ids[idx++] = threadInfo[i].threadId;
2465 hw_thread.os_id = os;
2468 __kmp_free(threadInfo);
2469 __kmp_topology->sort_ids();
2470 if (!__kmp_topology->check_ids()) {
2471 kmp_topology_t::deallocate(__kmp_topology);
2472 __kmp_topology =
nullptr;
2473 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2481static void __kmp_get_hybrid_info(kmp_hw_core_type_t *type,
int *efficiency,
2482 unsigned *native_model_id) {
2484 __kmp_x86_cpuid(0x1a, 0, &buf);
2485 *type = (kmp_hw_core_type_t)__kmp_extract_bits<24, 31>(buf.eax);
2487 case KMP_HW_CORE_TYPE_ATOM:
2490 case KMP_HW_CORE_TYPE_CORE:
2496 *native_model_id = __kmp_extract_bits<0, 23>(buf.eax);
2518 INTEL_LEVEL_TYPE_INVALID = 0,
2519 INTEL_LEVEL_TYPE_SMT = 1,
2520 INTEL_LEVEL_TYPE_CORE = 2,
2521 INTEL_LEVEL_TYPE_MODULE = 3,
2522 INTEL_LEVEL_TYPE_TILE = 4,
2523 INTEL_LEVEL_TYPE_DIE = 5,
2524 INTEL_LEVEL_TYPE_LAST = 6,
2527struct cpuid_level_info_t {
2528 unsigned level_type, mask, mask_width, nitems, cache_mask;
2531static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
2532 switch (intel_type) {
2533 case INTEL_LEVEL_TYPE_INVALID:
2534 return KMP_HW_SOCKET;
2535 case INTEL_LEVEL_TYPE_SMT:
2536 return KMP_HW_THREAD;
2537 case INTEL_LEVEL_TYPE_CORE:
2539 case INTEL_LEVEL_TYPE_TILE:
2541 case INTEL_LEVEL_TYPE_MODULE:
2542 return KMP_HW_MODULE;
2543 case INTEL_LEVEL_TYPE_DIE:
2546 return KMP_HW_UNKNOWN;
2553__kmp_x2apicid_get_levels(
int leaf,
2554 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST],
2555 kmp_uint64 known_levels) {
2556 unsigned level, levels_index;
2557 unsigned level_type, mask_width, nitems;
2567 level = levels_index = 0;
2569 __kmp_x86_cpuid(leaf, level, &buf);
2570 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
2571 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
2572 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
2573 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0)
2576 if (known_levels & (1ull << level_type)) {
2578 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
2579 levels[levels_index].level_type = level_type;
2580 levels[levels_index].mask_width = mask_width;
2581 levels[levels_index].nitems = nitems;
2585 if (levels_index > 0) {
2586 levels[levels_index - 1].mask_width = mask_width;
2587 levels[levels_index - 1].nitems = nitems;
2591 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
2594 if (levels_index == 0 || levels[0].level_type == INTEL_LEVEL_TYPE_INVALID)
2598 for (
unsigned i = 0; i < levels_index; ++i) {
2599 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
2600 levels[i].mask = ~((-1) << levels[i].mask_width);
2601 levels[i].cache_mask = (-1) << levels[i].mask_width;
2602 for (
unsigned j = 0; j < i; ++j)
2603 levels[i].mask ^= levels[j].mask;
2605 KMP_DEBUG_ASSERT(i > 0);
2606 levels[i].mask = (-1) << levels[i - 1].mask_width;
2607 levels[i].cache_mask = 0;
2610 return levels_index;
2613static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
2615 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
2616 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
2617 unsigned levels_index;
2619 kmp_uint64 known_levels;
2620 int topology_leaf, highest_leaf, apic_id;
2622 static int leaves[] = {0, 0};
2624 kmp_i18n_id_t leaf_message_id;
2626 KMP_BUILD_ASSERT(
sizeof(known_levels) * CHAR_BIT > KMP_HW_LAST);
2628 *msg_id = kmp_i18n_null;
2629 if (__kmp_affinity.flags.verbose) {
2630 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
2634 known_levels = 0ull;
2635 for (
int i = 0; i < INTEL_LEVEL_TYPE_LAST; ++i) {
2636 if (__kmp_intel_type_2_topology_type(i) != KMP_HW_UNKNOWN) {
2637 known_levels |= (1ull << i);
2642 __kmp_x86_cpuid(0, 0, &buf);
2643 highest_leaf = buf.eax;
2648 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
2651 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2652 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
2655 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
2660 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2664 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2666 for (
int i = 0; i < num_leaves; ++i) {
2667 int leaf = leaves[i];
2668 if (highest_leaf < leaf)
2670 __kmp_x86_cpuid(leaf, 0, &buf);
2673 topology_leaf = leaf;
2674 levels_index = __kmp_x2apicid_get_levels(leaf, levels, known_levels);
2675 if (levels_index == 0)
2679 if (topology_leaf == -1 || levels_index == 0) {
2680 *msg_id = leaf_message_id;
2683 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
2690 if (!KMP_AFFINITY_CAPABLE()) {
2693 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2694 for (
unsigned i = 0; i < levels_index; ++i) {
2695 if (levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
2696 __kmp_nThreadsPerCore = levels[i].nitems;
2697 }
else if (levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
2698 nCoresPerPkg = levels[i].nitems;
2701 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
2702 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2707 int depth = levels_index;
2708 for (
int i = depth - 1, j = 0; i >= 0; --i, ++j)
2709 types[j] = __kmp_intel_type_2_topology_type(levels[i].level_type);
2711 kmp_topology_t::allocate(__kmp_avail_proc, levels_index, types);
2714 kmp_cache_info_t cache_info;
2715 for (
size_t i = 0; i < cache_info.get_depth(); ++i) {
2716 const kmp_cache_info_t::info_t &info = cache_info[i];
2717 unsigned cache_mask = info.mask;
2718 unsigned cache_level = info.level;
2719 for (
unsigned j = 0; j < levels_index; ++j) {
2720 unsigned hw_cache_mask = levels[j].cache_mask;
2721 kmp_hw_t cache_type = kmp_cache_info_t::get_topology_type(cache_level);
2722 if (hw_cache_mask == cache_mask && j < levels_index - 1) {
2724 __kmp_intel_type_2_topology_type(levels[j + 1].level_type);
2725 __kmp_topology->set_equivalent_type(cache_type, type);
2735 kmp_affinity_raii_t previous_affinity;
2740 int hw_thread_index = 0;
2741 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2742 cpuid_level_info_t my_levels[INTEL_LEVEL_TYPE_LAST];
2743 unsigned my_levels_index;
2746 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2749 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2751 __kmp_affinity_dispatch->bind_thread(proc);
2754 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2756 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
2758 __kmp_x2apicid_get_levels(topology_leaf, my_levels, known_levels);
2759 if (my_levels_index == 0 || my_levels_index != levels_index) {
2760 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2764 hw_thread.os_id = proc;
2766 for (
unsigned j = 0, idx = depth - 1; j < my_levels_index; ++j, --idx) {
2767 hw_thread.ids[idx] = apic_id & my_levels[j].mask;
2769 hw_thread.ids[idx] >>= my_levels[j - 1].mask_width;
2773 if (__kmp_is_hybrid_cpu() && highest_leaf >= 0x1a) {
2774 kmp_hw_core_type_t type;
2775 unsigned native_model_id;
2777 __kmp_get_hybrid_info(&type, &efficiency, &native_model_id);
2778 hw_thread.attrs.set_core_type(type);
2779 hw_thread.attrs.set_core_eff(efficiency);
2783 KMP_ASSERT(hw_thread_index > 0);
2784 __kmp_topology->sort_ids();
2785 if (!__kmp_topology->check_ids()) {
2786 kmp_topology_t::deallocate(__kmp_topology);
2787 __kmp_topology =
nullptr;
2788 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
2796#define threadIdIndex 1
2797#define coreIdIndex 2
2799#define nodeIdIndex 4
2801typedef unsigned *ProcCpuInfo;
2802static unsigned maxIndex = pkgIdIndex;
2804static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
2807 const unsigned *aa = *(
unsigned *
const *)a;
2808 const unsigned *bb = *(
unsigned *
const *)b;
2809 for (i = maxIndex;; i--) {
2820#if KMP_USE_HIER_SCHED
2822static void __kmp_dispatch_set_hierarchy_values() {
2828 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
2829 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2830 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
2831#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2833 if (__kmp_mic_type >= mic3)
2834 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
2837 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
2838 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
2839 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
2840 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
2843 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
2844 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
2845 __kmp_nThreadsPerCore;
2846#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2848 if (__kmp_mic_type >= mic3)
2849 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2850 2 * __kmp_nThreadsPerCore;
2853 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2854 __kmp_nThreadsPerCore;
2855 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
2856 nCoresPerPkg * __kmp_nThreadsPerCore;
2857 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
2858 nCoresPerPkg * __kmp_nThreadsPerCore;
2859 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
2860 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2865int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
2866 int index = type + 1;
2867 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
2868 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
2869 if (type == kmp_hier_layer_e::LAYER_THREAD)
2871 else if (type == kmp_hier_layer_e::LAYER_LOOP)
2873 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
2874 if (tid >= num_hw_threads)
2875 tid = tid % num_hw_threads;
2876 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
2880int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
2883 KMP_DEBUG_ASSERT(i1 <= i2);
2884 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
2885 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
2886 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
2888 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
2892static inline const char *__kmp_cpuinfo_get_filename() {
2893 const char *filename;
2894 if (__kmp_cpuinfo_file !=
nullptr)
2895 filename = __kmp_cpuinfo_file;
2897 filename =
"/proc/cpuinfo";
2901static inline const char *__kmp_cpuinfo_get_envvar() {
2902 const char *envvar =
nullptr;
2903 if (__kmp_cpuinfo_file !=
nullptr)
2904 envvar =
"KMP_CPUINFO_FILE";
2911static bool __kmp_affinity_create_cpuinfo_map(
int *line,
2912 kmp_i18n_id_t *
const msg_id) {
2913 *msg_id = kmp_i18n_null;
2916 unsigned num_records = __kmp_xproc;
2918 const char *filename = __kmp_cpuinfo_get_filename();
2919 const char *envvar = __kmp_cpuinfo_get_envvar();
2921 if (__kmp_affinity.flags.verbose) {
2922 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
2930 unsigned num_records = 0;
2932 buf[
sizeof(buf) - 1] = 1;
2933 if (!fgets(buf,
sizeof(buf), f)) {
2938 char s1[] =
"processor";
2939 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2946 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
2948 if (level > (
unsigned)__kmp_xproc) {
2949 level = __kmp_xproc;
2951 if (nodeIdIndex + level >= maxIndex) {
2952 maxIndex = nodeIdIndex + level;
2960 if (num_records == 0) {
2961 *msg_id = kmp_i18n_str_NoProcRecords;
2964 if (num_records > (
unsigned)__kmp_xproc) {
2965 *msg_id = kmp_i18n_str_TooManyProcRecords;
2974 if (fseek(f, 0, SEEK_SET) != 0) {
2975 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
2982 unsigned **threadInfo =
2983 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
2985 for (i = 0; i <= num_records; i++) {
2987 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2990#define CLEANUP_THREAD_INFO \
2991 for (i = 0; i <= num_records; i++) { \
2992 __kmp_free(threadInfo[i]); \
2994 __kmp_free(threadInfo);
2999#define INIT_PROC_INFO(p) \
3000 for (__index = 0; __index <= maxIndex; __index++) { \
3001 (p)[__index] = UINT_MAX; \
3004 for (i = 0; i <= num_records; i++) {
3005 INIT_PROC_INFO(threadInfo[i]);
3010 lpar_info_format1_t cpuinfo;
3011 unsigned num_avail = __kmp_xproc;
3013 if (__kmp_affinity.flags.verbose)
3014 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"system info for topology");
3018 lpar_get_info(LPAR_INFO_FORMAT1, &cpuinfo,
sizeof(lpar_info_format1_t));
3020 smt_threads = cpuinfo.smt_threads;
3022 CLEANUP_THREAD_INFO;
3023 *msg_id = kmp_i18n_str_UnknownTopology;
3028 rsethandle_t sys_rset = rs_alloc(RS_SYSTEM);
3029 if (sys_rset == NULL) {
3030 CLEANUP_THREAD_INFO;
3031 *msg_id = kmp_i18n_str_UnknownTopology;
3035 rsethandle_t srad = rs_alloc(RS_EMPTY);
3038 CLEANUP_THREAD_INFO;
3039 *msg_id = kmp_i18n_str_UnknownTopology;
3044 int sradsdl = rs_getinfo(NULL, R_SRADSDL, 0);
3048 CLEANUP_THREAD_INFO;
3049 *msg_id = kmp_i18n_str_UnknownTopology;
3053 int num_rads = rs_numrads(sys_rset, sradsdl, 0);
3057 CLEANUP_THREAD_INFO;
3058 *msg_id = kmp_i18n_str_UnknownTopology;
3063 int max_procs = rs_getinfo(NULL, R_MAXPROCS, 0);
3064 if (max_procs < 0) {
3067 CLEANUP_THREAD_INFO;
3068 *msg_id = kmp_i18n_str_UnknownTopology;
3074 for (
int srad_idx = 0; cur_rad < num_rads && srad_idx < VMI_MAXRADS;
3077 if (rs_getrad(sys_rset, srad, sradsdl, srad_idx, 0) < 0)
3080 for (
int cpu = 0; cpu < max_procs; cpu++) {
3082 if (rs_op(RS_TESTRESOURCE, srad, NULL, R_PROCS, cpu)) {
3083 threadInfo[cpu][osIdIndex] = cpu;
3084 threadInfo[cpu][pkgIdIndex] = cur_rad;
3085 threadInfo[cpu][coreIdIndex] = cpu / smt_threads;
3087 if (num_set >= num_avail) {
3101 unsigned num_avail = 0;
3104 bool reading_s390x_sys_info =
true;
3111 buf[
sizeof(buf) - 1] = 1;
3112 bool long_line =
false;
3113 if (!fgets(buf,
sizeof(buf), f)) {
3118 for (i = 0; i <= maxIndex; i++) {
3119 if (threadInfo[num_avail][i] != UINT_MAX) {
3127 }
else if (!buf[
sizeof(buf) - 1]) {
3134 CLEANUP_THREAD_INFO; \
3135 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
3141#if KMP_ARCH_LOONGARCH64
3148 if (*buf ==
'\n' && *line == 2)
3154 if (reading_s390x_sys_info) {
3156 reading_s390x_sys_info =
false;
3162 char s1[] =
"cpu number";
3164 char s1[] =
"processor";
3166 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
3168 char *p = strchr(buf +
sizeof(s1) - 1,
':');
3170 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3172 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
3182 threadInfo[num_avail][osIdIndex] = val;
3183#if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
3187 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
3188 threadInfo[num_avail][osIdIndex]);
3189 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
3194 KMP_SNPRINTF(path,
sizeof(path),
3195 "/sys/devices/system/cpu/cpu%u/topology/book_id",
3196 threadInfo[num_avail][osIdIndex]);
3197 __kmp_read_from_file(path,
"%u", &book_id);
3198 threadInfo[num_avail][pkgIdIndex] |= (book_id << 8);
3201 KMP_SNPRINTF(path,
sizeof(path),
3202 "/sys/devices/system/cpu/cpu%u/topology/drawer_id",
3203 threadInfo[num_avail][osIdIndex]);
3204 __kmp_read_from_file(path,
"%u", &drawer_id);
3205 threadInfo[num_avail][pkgIdIndex] |= (drawer_id << 16);
3208 KMP_SNPRINTF(path,
sizeof(path),
3209 "/sys/devices/system/cpu/cpu%u/topology/core_id",
3210 threadInfo[num_avail][osIdIndex]);
3211 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
3215 char s2[] =
"physical id";
3216 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
3218 char *p = strchr(buf +
sizeof(s2) - 1,
':');
3220 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3222 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
3224 threadInfo[num_avail][pkgIdIndex] = val;
3227 char s3[] =
"core id";
3228 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
3230 char *p = strchr(buf +
sizeof(s3) - 1,
':');
3232 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3234 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
3236 threadInfo[num_avail][coreIdIndex] = val;
3240 char s4[] =
"thread id";
3241 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
3243 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3245 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3247 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
3249 threadInfo[num_avail][threadIdIndex] = val;
3253 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3255 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3257 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3260 if (level > (
unsigned)__kmp_xproc) {
3261 level = __kmp_xproc;
3263 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
3265 threadInfo[num_avail][nodeIdIndex + level] = val;
3272 if ((*buf != 0) && (*buf !=
'\n')) {
3277 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
3285 if ((
int)num_avail == __kmp_xproc) {
3286 CLEANUP_THREAD_INFO;
3287 *msg_id = kmp_i18n_str_TooManyEntries;
3293 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
3294 CLEANUP_THREAD_INFO;
3295 *msg_id = kmp_i18n_str_MissingProcField;
3298 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
3299 CLEANUP_THREAD_INFO;
3300 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
3305 if (KMP_AFFINITY_CAPABLE() &&
3306 !KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
3307 __kmp_affin_fullMask)) {
3308 INIT_PROC_INFO(threadInfo[num_avail]);
3315 KMP_ASSERT(num_avail <= num_records);
3316 INIT_PROC_INFO(threadInfo[num_avail]);
3321 CLEANUP_THREAD_INFO;
3322 *msg_id = kmp_i18n_str_MissingValCpuinfo;
3326 CLEANUP_THREAD_INFO;
3327 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
3332#if KMP_MIC && REDUCE_TEAM_SIZE
3333 unsigned teamSize = 0;
3341 KMP_ASSERT(num_avail > 0);
3342 KMP_ASSERT(num_avail <= num_records);
3345 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
3346 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
3360 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3362 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3364 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3366 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3368 bool assign_thread_ids =
false;
3369 unsigned threadIdCt;
3376 if (assign_thread_ids) {
3377 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
3378 threadInfo[0][threadIdIndex] = threadIdCt++;
3379 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
3380 threadIdCt = threadInfo[0][threadIdIndex] + 1;
3383 for (index = 0; index <= maxIndex; index++) {
3387 lastId[index] = threadInfo[0][index];
3392 for (i = 1; i < num_avail; i++) {
3395 for (index = maxIndex; index >= threadIdIndex; index--) {
3396 if (assign_thread_ids && (index == threadIdIndex)) {
3398 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3399 threadInfo[i][threadIdIndex] = threadIdCt++;
3403 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3404 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3407 if (threadInfo[i][index] != lastId[index]) {
3412 for (index2 = threadIdIndex; index2 < index; index2++) {
3414 if (counts[index2] > maxCt[index2]) {
3415 maxCt[index2] = counts[index2];
3418 lastId[index2] = threadInfo[i][index2];
3422 lastId[index] = threadInfo[i][index];
3424 if (assign_thread_ids && (index > threadIdIndex)) {
3426#if KMP_MIC && REDUCE_TEAM_SIZE
3429 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3436 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3437 threadInfo[i][threadIdIndex] = threadIdCt++;
3443 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3444 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3450 if (index < threadIdIndex) {
3454 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
3459 CLEANUP_THREAD_INFO;
3460 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3466 assign_thread_ids =
true;
3467 goto restart_radix_check;
3471#if KMP_MIC && REDUCE_TEAM_SIZE
3474 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3477 for (index = threadIdIndex; index <= maxIndex; index++) {
3478 if (counts[index] > maxCt[index]) {
3479 maxCt[index] = counts[index];
3483 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
3484 nCoresPerPkg = maxCt[coreIdIndex];
3485 nPackages = totals[pkgIdIndex];
3491 __kmp_ncores = totals[coreIdIndex];
3492 if (!KMP_AFFINITY_CAPABLE()) {
3493 KMP_ASSERT(__kmp_affinity.type == affinity_none);
3497#if KMP_MIC && REDUCE_TEAM_SIZE
3499 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
3500 __kmp_dflt_team_nth = teamSize;
3501 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
3502 "__kmp_dflt_team_nth = %d\n",
3503 __kmp_dflt_team_nth));
3507 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
3514 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
3515 for (index = threadIdIndex; index < maxIndex; index++) {
3516 KMP_ASSERT(totals[index] >= totals[index + 1]);
3517 inMap[index] = (totals[index] > totals[index + 1]);
3519 inMap[maxIndex] = (totals[maxIndex] > 1);
3520 inMap[pkgIdIndex] =
true;
3521 inMap[coreIdIndex] =
true;
3522 inMap[threadIdIndex] =
true;
3526 kmp_hw_t types[KMP_HW_LAST];
3529 int threadLevel = -1;
3530 for (index = threadIdIndex; index <= maxIndex; index++) {
3535 if (inMap[pkgIdIndex]) {
3537 types[idx++] = KMP_HW_SOCKET;
3539 if (inMap[coreIdIndex]) {
3541 types[idx++] = KMP_HW_CORE;
3543 if (inMap[threadIdIndex]) {
3545 types[idx++] = KMP_HW_THREAD;
3547 KMP_ASSERT(depth > 0);
3550 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
3552 for (i = 0; i < num_avail; ++i) {
3553 unsigned os = threadInfo[i][osIdIndex];
3555 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3557 hw_thread.os_id = os;
3560 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
3561 if (!inMap[src_index]) {
3564 if (src_index == pkgIdIndex) {
3565 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
3566 }
else if (src_index == coreIdIndex) {
3567 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
3568 }
else if (src_index == threadIdIndex) {
3569 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
3579 CLEANUP_THREAD_INFO;
3580 __kmp_topology->sort_ids();
3581 if (!__kmp_topology->check_ids()) {
3582 kmp_topology_t::deallocate(__kmp_topology);
3583 __kmp_topology =
nullptr;
3584 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3593template <
typename FindNextFunctionType>
3594static void __kmp_create_os_id_masks(
unsigned *numUnique,
3595 kmp_affinity_t &affinity,
3596 FindNextFunctionType find_next) {
3600 int numAddrs = __kmp_topology->get_num_hw_threads();
3601 int depth = __kmp_topology->get_depth();
3602 const char *env_var = __kmp_get_affinity_env_var(affinity);
3603 KMP_ASSERT(numAddrs);
3613 for (i = numAddrs - 1;; --i) {
3614 int osId = __kmp_topology->at(i).os_id;
3615 if (osId > maxOsId) {
3621 affinity.num_os_id_masks = maxOsId + 1;
3622 KMP_CPU_ALLOC_ARRAY(affinity.os_id_masks, affinity.num_os_id_masks);
3623 KMP_ASSERT(affinity.gran_levels >= 0);
3624 if (affinity.flags.verbose && (affinity.gran_levels > 0)) {
3625 KMP_INFORM(ThreadsMigrate, env_var, affinity.gran_levels);
3627 if (affinity.gran_levels >= (
int)depth) {
3628 KMP_AFF_WARNING(affinity, AffThreadsMayMigrate);
3638 kmp_affin_mask_t *sum;
3639 KMP_CPU_ALLOC_ON_STACK(sum);
3642 i = j = leader = find_next(-1);
3643 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3644 kmp_full_mask_modifier_t full_mask;
3645 for (i = find_next(i); i < numAddrs; i = find_next(i)) {
3649 if (__kmp_topology->is_close(leader, i, affinity)) {
3650 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3656 for (; j < i; j = find_next(j)) {
3657 int osId = __kmp_topology->at(j).os_id;
3658 KMP_DEBUG_ASSERT(osId <= maxOsId);
3659 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3660 KMP_CPU_COPY(mask, sum);
3661 __kmp_topology->at(j).leader = (j == leader);
3667 full_mask.include(sum);
3669 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3674 for (; j < i; j = find_next(j)) {
3675 int osId = __kmp_topology->at(j).os_id;
3676 KMP_DEBUG_ASSERT(osId <= maxOsId);
3677 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3678 KMP_CPU_COPY(mask, sum);
3679 __kmp_topology->at(j).leader = (j == leader);
3681 full_mask.include(sum);
3683 KMP_CPU_FREE_FROM_STACK(sum);
3686 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
3687 __kmp_topology->print(env_var);
3690 *numUnique = unique;
3696static kmp_affin_mask_t *newMasks;
3697static int numNewMasks;
3698static int nextNewMask;
3700#define ADD_MASK(_mask) \
3702 if (nextNewMask >= numNewMasks) { \
3705 kmp_affin_mask_t *temp; \
3706 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
3707 for (i = 0; i < numNewMasks / 2; i++) { \
3708 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
3709 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
3710 KMP_CPU_COPY(dest, src); \
3712 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
3715 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
3719#define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
3721 if (((_osId) > _maxOsId) || \
3722 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
3723 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, _osId); \
3725 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
3731static void __kmp_affinity_process_proclist(kmp_affinity_t &affinity) {
3733 kmp_affin_mask_t **out_masks = &affinity.masks;
3734 unsigned *out_numMasks = &affinity.num_masks;
3735 const char *proclist = affinity.proclist;
3736 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3737 int maxOsId = affinity.num_os_id_masks - 1;
3738 const char *scan = proclist;
3739 const char *next = proclist;
3744 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3746 kmp_affin_mask_t *sumMask;
3747 KMP_CPU_ALLOC(sumMask);
3751 int start, end, stride;
3755 if (*next ==
'\0') {
3767 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
3769 num = __kmp_str_to_int(scan, *next);
3770 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3773 if ((num > maxOsId) ||
3774 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3775 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3776 KMP_CPU_ZERO(sumMask);
3778 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3798 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3801 num = __kmp_str_to_int(scan, *next);
3802 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3805 if ((num > maxOsId) ||
3806 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3807 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3809 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3826 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3828 start = __kmp_str_to_int(scan, *next);
3829 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
3834 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3848 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3850 end = __kmp_str_to_int(scan, *next);
3851 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
3868 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3870 stride = __kmp_str_to_int(scan, *next);
3871 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
3876 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
3878 KMP_ASSERT2(start <= end,
"bad explicit proc list");
3880 KMP_ASSERT2(start >= end,
"bad explicit proc list");
3882 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
3887 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3889 }
while (start <= end);
3892 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3894 }
while (start >= end);
3905 *out_numMasks = nextNewMask;
3906 if (nextNewMask == 0) {
3908 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3911 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3912 for (i = 0; i < nextNewMask; i++) {
3913 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3914 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3915 KMP_CPU_COPY(dest, src);
3917 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3918 KMP_CPU_FREE(sumMask);
3941static void __kmp_process_subplace_list(
const char **scan,
3942 kmp_affinity_t &affinity,
int maxOsId,
3943 kmp_affin_mask_t *tempMask,
3946 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3949 int start, count, stride, i;
3953 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3956 start = __kmp_str_to_int(*scan, *next);
3957 KMP_ASSERT(start >= 0);
3962 if (**scan ==
'}' || **scan ==
',') {
3963 if ((start > maxOsId) ||
3964 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3965 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3967 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3970 if (**scan ==
'}') {
3976 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3981 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3984 count = __kmp_str_to_int(*scan, *next);
3985 KMP_ASSERT(count >= 0);
3990 if (**scan ==
'}' || **scan ==
',') {
3991 for (i = 0; i < count; i++) {
3992 if ((start > maxOsId) ||
3993 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3994 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3997 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4002 if (**scan ==
'}') {
4008 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
4015 if (**scan ==
'+') {
4019 if (**scan ==
'-') {
4027 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
4030 stride = __kmp_str_to_int(*scan, *next);
4031 KMP_ASSERT(stride >= 0);
4037 if (**scan ==
'}' || **scan ==
',') {
4038 for (i = 0; i < count; i++) {
4039 if ((start > maxOsId) ||
4040 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
4041 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
4044 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
4049 if (**scan ==
'}') {
4056 KMP_ASSERT2(0,
"bad explicit places list");
4060static void __kmp_process_place(
const char **scan, kmp_affinity_t &affinity,
4061 int maxOsId, kmp_affin_mask_t *tempMask,
4064 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4068 if (**scan ==
'{') {
4070 __kmp_process_subplace_list(scan, affinity, maxOsId, tempMask, setSize);
4071 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
4073 }
else if (**scan ==
'!') {
4075 __kmp_process_place(scan, affinity, maxOsId, tempMask, setSize);
4076 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
4077 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
4080 int num = __kmp_str_to_int(*scan, *next);
4081 KMP_ASSERT(num >= 0);
4082 if ((num > maxOsId) ||
4083 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
4084 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
4086 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
4091 KMP_ASSERT2(0,
"bad explicit places list");
4096void __kmp_affinity_process_placelist(kmp_affinity_t &affinity) {
4097 int i, j, count, stride, sign;
4098 kmp_affin_mask_t **out_masks = &affinity.masks;
4099 unsigned *out_numMasks = &affinity.num_masks;
4100 const char *placelist = affinity.proclist;
4101 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
4102 int maxOsId = affinity.num_os_id_masks - 1;
4103 const char *scan = placelist;
4104 const char *next = placelist;
4107 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
4113 kmp_affin_mask_t *tempMask;
4114 kmp_affin_mask_t *previousMask;
4115 KMP_CPU_ALLOC(tempMask);
4116 KMP_CPU_ZERO(tempMask);
4117 KMP_CPU_ALLOC(previousMask);
4118 KMP_CPU_ZERO(previousMask);
4122 __kmp_process_place(&scan, affinity, maxOsId, tempMask, &setSize);
4126 if (*scan ==
'\0' || *scan ==
',') {
4130 KMP_CPU_ZERO(tempMask);
4132 if (*scan ==
'\0') {
4139 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4144 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4147 count = __kmp_str_to_int(scan, *next);
4148 KMP_ASSERT(count >= 0);
4153 if (*scan ==
'\0' || *scan ==
',') {
4156 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
4175 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
4178 stride = __kmp_str_to_int(scan, *next);
4179 KMP_DEBUG_ASSERT(stride >= 0);
4185 for (i = 0; i < count; i++) {
4190 KMP_CPU_COPY(previousMask, tempMask);
4191 ADD_MASK(previousMask);
4192 KMP_CPU_ZERO(tempMask);
4194 KMP_CPU_SET_ITERATE(j, previousMask) {
4195 if (!KMP_CPU_ISSET(j, previousMask)) {
4198 if ((j + stride > maxOsId) || (j + stride < 0) ||
4199 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
4200 (!KMP_CPU_ISSET(j + stride,
4201 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
4202 if (i < count - 1) {
4203 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, j + stride);
4207 KMP_CPU_SET(j + stride, tempMask);
4211 KMP_CPU_ZERO(tempMask);
4216 if (*scan ==
'\0') {
4224 KMP_ASSERT2(0,
"bad explicit places list");
4227 *out_numMasks = nextNewMask;
4228 if (nextNewMask == 0) {
4230 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4233 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
4234 KMP_CPU_FREE(tempMask);
4235 KMP_CPU_FREE(previousMask);
4236 for (i = 0; i < nextNewMask; i++) {
4237 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
4238 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
4239 KMP_CPU_COPY(dest, src);
4241 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
4249static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
4252 for (
int i = 0; i < nprocs; i++) {
4253 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
4254 for (
int j = bottom_level; j > 0; j--) {
4255 if (hw_thread.ids[j] > 0) {
4256 if (core_level < (j - 1)) {
4266static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
4268 return __kmp_topology->get_count(core_level);
4271static int __kmp_affinity_find_core(
int proc,
int bottom_level,
4274 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
4275 for (
int i = 0; i <= proc; ++i) {
4276 if (i + 1 <= proc) {
4277 for (
int j = 0; j <= core_level; ++j) {
4278 if (__kmp_topology->at(i + 1).sub_ids[j] !=
4279 __kmp_topology->at(i).sub_ids[j]) {
4291static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
4293 if (core_level >= bottom_level)
4295 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
4296 return __kmp_topology->calculate_ratio(thread_level, core_level);
4299static int *procarr = NULL;
4300static int __kmp_aff_depth = 0;
4301static int *__kmp_osid_to_hwthread_map = NULL;
4303static void __kmp_affinity_get_mask_topology_info(
const kmp_affin_mask_t *mask,
4304 kmp_affinity_ids_t &ids,
4305 kmp_affinity_attrs_t &attrs) {
4306 if (!KMP_AFFINITY_CAPABLE())
4310 for (
int i = 0; i < KMP_HW_LAST; ++i)
4311 ids.ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
4312 attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
4317 int depth = __kmp_topology->get_depth();
4318 KMP_CPU_SET_ITERATE(cpu, mask) {
4319 int osid_idx = __kmp_osid_to_hwthread_map[cpu];
4321 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(osid_idx);
4322 for (
int level = 0; level < depth; ++level) {
4323 kmp_hw_t type = __kmp_topology->get_type(level);
4324 int id = hw_thread.sub_ids[level];
4325 if (ids.ids[type] == kmp_hw_thread_t::UNKNOWN_ID || ids.ids[type] ==
id) {
4330 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4331 for (; level < depth; ++level) {
4332 kmp_hw_t type = __kmp_topology->get_type(level);
4333 ids.ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4338 attrs.core_type = hw_thread.attrs.get_core_type();
4339 attrs.core_eff = hw_thread.attrs.get_core_eff();
4343 if (attrs.core_type != hw_thread.attrs.get_core_type())
4344 attrs.core_type = KMP_HW_CORE_TYPE_UNKNOWN;
4345 if (attrs.core_eff != hw_thread.attrs.get_core_eff())
4346 attrs.core_eff = kmp_hw_attr_t::UNKNOWN_CORE_EFF;
4351static void __kmp_affinity_get_thread_topology_info(kmp_info_t *th) {
4352 if (!KMP_AFFINITY_CAPABLE())
4354 const kmp_affin_mask_t *mask = th->th.th_affin_mask;
4355 kmp_affinity_ids_t &ids = th->th.th_topology_ids;
4356 kmp_affinity_attrs_t &attrs = th->th.th_topology_attrs;
4357 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4363static void __kmp_affinity_get_topology_info(kmp_affinity_t &affinity) {
4364 if (!KMP_AFFINITY_CAPABLE())
4366 if (affinity.type != affinity_none) {
4367 KMP_ASSERT(affinity.num_os_id_masks);
4368 KMP_ASSERT(affinity.os_id_masks);
4370 KMP_ASSERT(affinity.num_masks);
4371 KMP_ASSERT(affinity.masks);
4372 KMP_ASSERT(__kmp_affin_fullMask);
4374 int max_cpu = __kmp_affin_fullMask->get_max_cpu();
4375 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4378 if (!affinity.ids) {
4379 affinity.ids = (kmp_affinity_ids_t *)__kmp_allocate(
4380 sizeof(kmp_affinity_ids_t) * affinity.num_masks);
4382 if (!affinity.attrs) {
4383 affinity.attrs = (kmp_affinity_attrs_t *)__kmp_allocate(
4384 sizeof(kmp_affinity_attrs_t) * affinity.num_masks);
4386 if (!__kmp_osid_to_hwthread_map) {
4388 __kmp_osid_to_hwthread_map =
4389 (
int *)__kmp_allocate(
sizeof(
int) * (max_cpu + 1));
4393 for (
int hw_thread = 0; hw_thread < num_hw_threads; ++hw_thread) {
4394 int os_id = __kmp_topology->at(hw_thread).os_id;
4395 if (KMP_CPU_ISSET(os_id, __kmp_affin_fullMask))
4396 __kmp_osid_to_hwthread_map[os_id] = hw_thread;
4399 for (
unsigned i = 0; i < affinity.num_masks; ++i) {
4400 kmp_affinity_ids_t &ids = affinity.ids[i];
4401 kmp_affinity_attrs_t &attrs = affinity.attrs[i];
4402 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.masks, i);
4403 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4408static void __kmp_aux_affinity_initialize_other_data(kmp_affinity_t &affinity) {
4410 if (__kmp_topology && __kmp_topology->get_num_hw_threads()) {
4411 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
4412 __kmp_affinity_get_topology_info(affinity);
4413#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
4414 __kmp_first_osid_with_ecore = __kmp_get_first_osid_with_ecore();
4421static void __kmp_create_affinity_none_places(kmp_affinity_t &affinity) {
4422 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4423 KMP_ASSERT(affinity.type == affinity_none);
4424 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4425 affinity.num_masks = 1;
4426 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4427 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, 0);
4428 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4429 __kmp_aux_affinity_initialize_other_data(affinity);
4432static void __kmp_aux_affinity_initialize_masks(kmp_affinity_t &affinity) {
4437 int verbose = affinity.flags.verbose;
4438 const char *env_var = affinity.env_var;
4441 if (__kmp_affin_fullMask && __kmp_affin_origMask)
4444 if (__kmp_affin_fullMask == NULL) {
4445 KMP_CPU_ALLOC(__kmp_affin_fullMask);
4447 if (__kmp_affin_origMask == NULL) {
4448 KMP_CPU_ALLOC(__kmp_affin_origMask);
4450 if (KMP_AFFINITY_CAPABLE()) {
4451 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4453 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4454 if (affinity.flags.respect) {
4457 __kmp_avail_proc = 0;
4458 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4459 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4464 if (__kmp_avail_proc > __kmp_xproc) {
4465 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4466 affinity.type = affinity_none;
4467 KMP_AFFINITY_DISABLE();
4472 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4473 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4474 __kmp_affin_fullMask);
4475 KMP_INFORM(InitOSProcSetRespect, env_var, buf);
4479 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4480 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4481 __kmp_affin_fullMask);
4482 KMP_INFORM(InitOSProcSetNotRespect, env_var, buf);
4485 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4487 if (__kmp_num_proc_groups <= 1) {
4489 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4493 __kmp_affin_fullMask->set_process_affinity(
true);
4499static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
4500 bool success =
false;
4501 const char *env_var = affinity.env_var;
4502 kmp_i18n_id_t msg_id = kmp_i18n_null;
4503 int verbose = affinity.flags.verbose;
4507 if ((__kmp_cpuinfo_file != NULL) &&
4508 (__kmp_affinity_top_method == affinity_top_method_all)) {
4509 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4512 if (__kmp_affinity_top_method == affinity_top_method_all) {
4518 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4519 if (!__kmp_hwloc_error) {
4520 success = __kmp_affinity_create_hwloc_map(&msg_id);
4521 if (!success && verbose) {
4522 KMP_INFORM(AffIgnoringHwloc, env_var);
4524 }
else if (verbose) {
4525 KMP_INFORM(AffIgnoringHwloc, env_var);
4530#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4532 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4533 if (!success && verbose && msg_id != kmp_i18n_null) {
4534 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4538 success = __kmp_affinity_create_apicid_map(&msg_id);
4539 if (!success && verbose && msg_id != kmp_i18n_null) {
4540 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4545#if KMP_OS_LINUX || KMP_OS_AIX
4548 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4549 if (!success && verbose && msg_id != kmp_i18n_null) {
4550 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4555#if KMP_GROUP_AFFINITY
4556 if (!success && (__kmp_num_proc_groups > 1)) {
4557 success = __kmp_affinity_create_proc_group_map(&msg_id);
4558 if (!success && verbose && msg_id != kmp_i18n_null) {
4559 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4565 success = __kmp_affinity_create_flat_map(&msg_id);
4566 if (!success && verbose && msg_id != kmp_i18n_null) {
4567 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4569 KMP_ASSERT(success);
4577 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4578 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4579 success = __kmp_affinity_create_hwloc_map(&msg_id);
4581 KMP_ASSERT(msg_id != kmp_i18n_null);
4582 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4587#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4588 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
4589 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
4590 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4592 KMP_ASSERT(msg_id != kmp_i18n_null);
4593 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4595 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4596 success = __kmp_affinity_create_apicid_map(&msg_id);
4598 KMP_ASSERT(msg_id != kmp_i18n_null);
4599 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4604 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4606 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4608 KMP_ASSERT(msg_id != kmp_i18n_null);
4609 const char *filename = __kmp_cpuinfo_get_filename();
4611 KMP_FATAL(FileLineMsgExiting, filename, line,
4612 __kmp_i18n_catgets(msg_id));
4614 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4619#if KMP_GROUP_AFFINITY
4620 else if (__kmp_affinity_top_method == affinity_top_method_group) {
4621 success = __kmp_affinity_create_proc_group_map(&msg_id);
4622 KMP_ASSERT(success);
4624 KMP_ASSERT(msg_id != kmp_i18n_null);
4625 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4630 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4631 success = __kmp_affinity_create_flat_map(&msg_id);
4633 KMP_ASSERT(success);
4637 if (!__kmp_topology) {
4638 if (KMP_AFFINITY_CAPABLE()) {
4639 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4641 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
4643 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
4644 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
4645 __kmp_nThreadsPerCore, __kmp_ncores);
4647 __kmp_topology->print(env_var);
4654 __kmp_topology->canonicalize();
4656 __kmp_topology->print(env_var);
4657 bool filtered = __kmp_topology->filter_hw_subset();
4658 if (filtered && verbose)
4659 __kmp_topology->print(
"KMP_HW_SUBSET");
4663static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
4664 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4665 bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
4666 const char *env_var = __kmp_get_affinity_env_var(affinity);
4668 if (affinity.flags.initialized) {
4669 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4673 if (is_regular_affinity && (!__kmp_affin_fullMask || !__kmp_affin_origMask))
4674 __kmp_aux_affinity_initialize_masks(affinity);
4676 if (is_regular_affinity && !__kmp_topology) {
4677 bool success = __kmp_aux_affinity_initialize_topology(affinity);
4679 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4681 affinity.type = affinity_none;
4682 KMP_AFFINITY_DISABLE();
4689 if (affinity.type == affinity_none) {
4690 __kmp_create_affinity_none_places(affinity);
4691#if KMP_USE_HIER_SCHED
4692 __kmp_dispatch_set_hierarchy_values();
4694 affinity.flags.initialized = TRUE;
4698 __kmp_topology->set_granularity(affinity);
4699 int depth = __kmp_topology->get_depth();
4703 int numAddrs = __kmp_topology->get_num_hw_threads();
4706 if (affinity.core_attr_gran.valid) {
4707 __kmp_create_os_id_masks(&numUnique, affinity, [&](
int idx) {
4708 KMP_ASSERT(idx >= -1);
4709 for (
int i = idx + 1; i < numAddrs; ++i)
4710 if (__kmp_topology->at(i).attrs.contains(affinity.core_attr_gran))
4714 if (!affinity.os_id_masks) {
4715 const char *core_attribute;
4716 if (affinity.core_attr_gran.core_eff != kmp_hw_attr_t::UNKNOWN_CORE_EFF)
4717 core_attribute =
"core_efficiency";
4719 core_attribute =
"core_type";
4720 KMP_AFF_WARNING(affinity, AffIgnoringNotAvailable, env_var,
4722 __kmp_hw_get_catalog_string(KMP_HW_CORE,
true))
4727 if (!affinity.os_id_masks) {
4728 __kmp_create_os_id_masks(&numUnique, affinity, [](
int idx) {
4729 KMP_ASSERT(idx >= -1);
4733 if (affinity.gran_levels == 0) {
4734 KMP_DEBUG_ASSERT((
int)numUnique == __kmp_avail_proc);
4737 switch (affinity.type) {
4739 case affinity_explicit:
4740 KMP_DEBUG_ASSERT(affinity.proclist != NULL);
4741 if (is_hidden_helper_affinity ||
4742 __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
4743 __kmp_affinity_process_proclist(affinity);
4745 __kmp_affinity_process_placelist(affinity);
4747 if (affinity.num_masks == 0) {
4748 KMP_AFF_WARNING(affinity, AffNoValidProcID);
4749 affinity.type = affinity_none;
4750 __kmp_create_affinity_none_places(affinity);
4751 affinity.flags.initialized = TRUE;
4760 case affinity_logical:
4761 affinity.compact = 0;
4762 if (affinity.offset) {
4764 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4768 case affinity_physical:
4769 if (__kmp_nThreadsPerCore > 1) {
4770 affinity.compact = 1;
4771 if (affinity.compact >= depth) {
4772 affinity.compact = 0;
4775 affinity.compact = 0;
4777 if (affinity.offset) {
4779 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4783 case affinity_scatter:
4784 if (affinity.compact >= depth) {
4785 affinity.compact = 0;
4787 affinity.compact = depth - 1 - affinity.compact;
4791 case affinity_compact:
4792 if (affinity.compact >= depth) {
4793 affinity.compact = depth - 1;
4797 case affinity_balanced:
4798 if (depth <= 1 || is_hidden_helper_affinity) {
4799 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4800 affinity.type = affinity_none;
4801 __kmp_create_affinity_none_places(affinity);
4802 affinity.flags.initialized = TRUE;
4804 }
else if (!__kmp_topology->is_uniform()) {
4806 __kmp_aff_depth = depth;
4809 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
4810 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
4812 int maxprocpercore = __kmp_affinity_max_proc_per_core(
4813 __kmp_avail_proc, depth - 1, core_level);
4815 int nproc = ncores * maxprocpercore;
4816 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
4817 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4818 affinity.type = affinity_none;
4819 __kmp_create_affinity_none_places(affinity);
4820 affinity.flags.initialized = TRUE;
4824 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
4825 for (
int i = 0; i < nproc; i++) {
4831 for (
int i = 0; i < __kmp_avail_proc; i++) {
4832 int proc = __kmp_topology->at(i).os_id;
4833 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
4835 if (core == lastcore) {
4842 procarr[core * maxprocpercore + inlastcore] = proc;
4845 if (affinity.compact >= depth) {
4846 affinity.compact = depth - 1;
4851 if (affinity.flags.dups) {
4852 affinity.num_masks = __kmp_avail_proc;
4854 affinity.num_masks = numUnique;
4857 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
4858 (__kmp_affinity_num_places > 0) &&
4859 ((
unsigned)__kmp_affinity_num_places < affinity.num_masks) &&
4860 !is_hidden_helper_affinity) {
4861 affinity.num_masks = __kmp_affinity_num_places;
4864 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4868 __kmp_topology->sort_compact(affinity);
4872 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4873 kmp_full_mask_modifier_t full_mask;
4874 for (i = 0, j = 0; i < num_hw_threads; i++) {
4875 if ((!affinity.flags.dups) && (!__kmp_topology->at(i).leader)) {
4878 int osId = __kmp_topology->at(i).os_id;
4880 kmp_affin_mask_t *src = KMP_CPU_INDEX(affinity.os_id_masks, osId);
4881 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, j);
4882 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
4883 KMP_CPU_COPY(dest, src);
4884 full_mask.include(src);
4885 if (++j >= affinity.num_masks) {
4889 KMP_DEBUG_ASSERT(j == affinity.num_masks);
4891 if (full_mask.restrict_to_mask() && affinity.flags.verbose) {
4892 __kmp_topology->print(env_var);
4896 __kmp_topology->sort_ids();
4900 KMP_ASSERT2(0,
"Unexpected affinity setting");
4902 __kmp_aux_affinity_initialize_other_data(affinity);
4903 affinity.flags.initialized = TRUE;
4906void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
4915 int disabled = (affinity.type == affinity_disabled);
4916 if (!KMP_AFFINITY_CAPABLE())
4917 KMP_ASSERT(disabled);
4919 affinity.type = affinity_none;
4920 __kmp_aux_affinity_initialize(affinity);
4922 affinity.type = affinity_disabled;
4925void __kmp_affinity_uninitialize(
void) {
4926 for (kmp_affinity_t *affinity : __kmp_affinities) {
4927 if (affinity->masks != NULL)
4928 KMP_CPU_FREE_ARRAY(affinity->masks, affinity->num_masks);
4929 if (affinity->os_id_masks != NULL)
4930 KMP_CPU_FREE_ARRAY(affinity->os_id_masks, affinity->num_os_id_masks);
4931 if (affinity->proclist != NULL)
4932 __kmp_free(affinity->proclist);
4933 if (affinity->ids != NULL)
4934 __kmp_free(affinity->ids);
4935 if (affinity->attrs != NULL)
4936 __kmp_free(affinity->attrs);
4937 *affinity = KMP_AFFINITY_INIT(affinity->env_var);
4939 if (__kmp_affin_origMask != NULL) {
4940 if (KMP_AFFINITY_CAPABLE()) {
4943 bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
4945 __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
4948 KMP_CPU_FREE(__kmp_affin_origMask);
4949 __kmp_affin_origMask = NULL;
4951 __kmp_affinity_num_places = 0;
4952 if (procarr != NULL) {
4953 __kmp_free(procarr);
4956 if (__kmp_osid_to_hwthread_map) {
4957 __kmp_free(__kmp_osid_to_hwthread_map);
4958 __kmp_osid_to_hwthread_map = NULL;
4961 if (__kmp_hwloc_topology != NULL) {
4962 hwloc_topology_destroy(__kmp_hwloc_topology);
4963 __kmp_hwloc_topology = NULL;
4966 if (__kmp_hw_subset) {
4967 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
4968 __kmp_hw_subset =
nullptr;
4970 if (__kmp_topology) {
4971 kmp_topology_t::deallocate(__kmp_topology);
4972 __kmp_topology =
nullptr;
4974 KMPAffinity::destroy_api();
4977static void __kmp_select_mask_by_gtid(
int gtid,
const kmp_affinity_t *affinity,
4978 int *place, kmp_affin_mask_t **mask) {
4980 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
4981 if (is_hidden_helper)
4984 mask_idx = gtid - 2;
4986 mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
4987 KMP_DEBUG_ASSERT(affinity->num_masks > 0);
4988 *place = (mask_idx + affinity->offset) % affinity->num_masks;
4989 *mask = KMP_CPU_INDEX(affinity->masks, *place);
4994void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
4996 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4999 for (
int id = 0;
id < KMP_HW_LAST; ++id)
5000 th->th.th_topology_ids.ids[
id] = kmp_hw_thread_t::UNKNOWN_ID;
5001 th->th.th_topology_attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
5003 if (!KMP_AFFINITY_CAPABLE()) {
5007 if (th->th.th_affin_mask == NULL) {
5008 KMP_CPU_ALLOC(th->th.th_affin_mask);
5010 KMP_CPU_ZERO(th->th.th_affin_mask);
5018 kmp_affin_mask_t *mask;
5020 const kmp_affinity_t *affinity;
5021 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5023 if (is_hidden_helper)
5024 affinity = &__kmp_hh_affinity;
5026 affinity = &__kmp_affinity;
5028 if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
5029 if ((affinity->type == affinity_none) ||
5030 (affinity->type == affinity_balanced) ||
5031 KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5032#if KMP_GROUP_AFFINITY
5033 if (__kmp_num_proc_groups > 1) {
5037 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5039 mask = __kmp_affin_fullMask;
5041 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5044 if (!isa_root || __kmp_nested_proc_bind.bind_types[0] == proc_bind_false) {
5045#if KMP_GROUP_AFFINITY
5046 if (__kmp_num_proc_groups > 1) {
5050 KMP_ASSERT(__kmp_affin_fullMask != NULL);
5052 mask = __kmp_affin_fullMask;
5054 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
5058 th->th.th_current_place = i;
5059 if (isa_root && !is_hidden_helper) {
5060 th->th.th_new_place = i;
5061 th->th.th_first_place = 0;
5062 th->th.th_last_place = affinity->num_masks - 1;
5063 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
5066 th->th.th_first_place = 0;
5067 th->th.th_last_place = affinity->num_masks - 1;
5071 th->th.th_topology_ids = __kmp_affinity.ids[i];
5072 th->th.th_topology_attrs = __kmp_affinity.attrs[i];
5075 if (i == KMP_PLACE_ALL) {
5076 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to all places\n",
5079 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: setting T#%d to place %d\n",
5083 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5086void __kmp_affinity_bind_init_mask(
int gtid) {
5087 if (!KMP_AFFINITY_CAPABLE()) {
5090 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5091 const kmp_affinity_t *affinity;
5092 const char *env_var;
5093 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
5095 if (is_hidden_helper)
5096 affinity = &__kmp_hh_affinity;
5098 affinity = &__kmp_affinity;
5099 env_var = __kmp_get_affinity_env_var(*affinity,
true);
5101 if (affinity->flags.verbose && (affinity->type == affinity_none ||
5102 (th->th.th_current_place != KMP_PLACE_ALL &&
5103 affinity->type != affinity_balanced)) &&
5104 !KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
5105 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5106 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5107 th->th.th_affin_mask);
5108 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5116 if (affinity->type == affinity_none) {
5117 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
5122 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5126void __kmp_affinity_bind_place(
int gtid) {
5128 if (!KMP_AFFINITY_CAPABLE() || KMP_HIDDEN_HELPER_THREAD(gtid)) {
5132 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
5134 KA_TRACE(100, (
"__kmp_affinity_bind_place: binding T#%d to place %d (current "
5136 gtid, th->th.th_new_place, th->th.th_current_place));
5139 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5140 KMP_ASSERT(th->th.th_new_place >= 0);
5141 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity.num_masks);
5142 if (th->th.th_first_place <= th->th.th_last_place) {
5143 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
5144 (th->th.th_new_place <= th->th.th_last_place));
5146 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
5147 (th->th.th_new_place >= th->th.th_last_place));
5152 kmp_affin_mask_t *mask =
5153 KMP_CPU_INDEX(__kmp_affinity.masks, th->th.th_new_place);
5154 KMP_CPU_COPY(th->th.th_affin_mask, mask);
5155 th->th.th_current_place = th->th.th_new_place;
5157 if (__kmp_affinity.flags.verbose) {
5158 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5159 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5160 th->th.th_affin_mask);
5161 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
5162 __kmp_gettid(), gtid, buf);
5164 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
5167int __kmp_aux_set_affinity(
void **mask) {
5172 if (!KMP_AFFINITY_CAPABLE()) {
5176 gtid = __kmp_entry_gtid();
5179 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5180 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5181 (kmp_affin_mask_t *)(*mask));
5183 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
5187 if (__kmp_env_consistency_check) {
5188 if ((mask == NULL) || (*mask == NULL)) {
5189 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5194 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
5195 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5196 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5198 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
5203 if (num_procs == 0) {
5204 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5207#if KMP_GROUP_AFFINITY
5208 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
5209 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
5215 th = __kmp_threads[gtid];
5216 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5217 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5219 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
5222 th->th.th_current_place = KMP_PLACE_UNDEFINED;
5223 th->th.th_new_place = KMP_PLACE_UNDEFINED;
5224 th->th.th_first_place = 0;
5225 th->th.th_last_place = __kmp_affinity.num_masks - 1;
5228 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
5233int __kmp_aux_get_affinity(
void **mask) {
5236#if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5239 if (!KMP_AFFINITY_CAPABLE()) {
5243 gtid = __kmp_entry_gtid();
5244#if KMP_OS_WINDOWS || KMP_OS_AIX || KMP_DEBUG
5245 th = __kmp_threads[gtid];
5249 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
5253 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5254 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5255 th->th.th_affin_mask);
5257 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
5261 if (__kmp_env_consistency_check) {
5262 if ((mask == NULL) || (*mask == NULL)) {
5263 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
5267#if !KMP_OS_WINDOWS && !KMP_OS_AIX
5269 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
5272 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5273 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5274 (kmp_affin_mask_t *)(*mask));
5276 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
5284 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
5290int __kmp_aux_get_affinity_max_proc() {
5291 if (!KMP_AFFINITY_CAPABLE()) {
5294#if KMP_GROUP_AFFINITY
5295 if (__kmp_num_proc_groups > 1) {
5296 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
5302int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
5303 if (!KMP_AFFINITY_CAPABLE()) {
5309 int gtid = __kmp_entry_gtid();
5310 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5311 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5312 (kmp_affin_mask_t *)(*mask));
5313 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
5314 "affinity mask for thread %d = %s\n",
5318 if (__kmp_env_consistency_check) {
5319 if ((mask == NULL) || (*mask == NULL)) {
5320 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
5324 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5327 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5331 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
5335int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
5336 if (!KMP_AFFINITY_CAPABLE()) {
5342 int gtid = __kmp_entry_gtid();
5343 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5344 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5345 (kmp_affin_mask_t *)(*mask));
5346 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
5347 "affinity mask for thread %d = %s\n",
5351 if (__kmp_env_consistency_check) {
5352 if ((mask == NULL) || (*mask == NULL)) {
5353 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
5357 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5360 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5364 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
5368int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
5369 if (!KMP_AFFINITY_CAPABLE()) {
5375 int gtid = __kmp_entry_gtid();
5376 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5377 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5378 (kmp_affin_mask_t *)(*mask));
5379 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
5380 "affinity mask for thread %d = %s\n",
5384 if (__kmp_env_consistency_check) {
5385 if ((mask == NULL) || (*mask == NULL)) {
5386 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
5390 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5393 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5397 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
5400#if KMP_WEIGHTED_ITERATIONS_SUPPORTED
5402int __kmp_get_first_osid_with_ecore(
void) {
5404 int high = __kmp_topology->get_num_hw_threads() - 1;
5406 while (high - low > 1) {
5407 mid = (high + low) / 2;
5408 if (__kmp_topology->at(mid).attrs.get_core_type() ==
5409 KMP_HW_CORE_TYPE_CORE) {
5415 if (__kmp_topology->at(mid).attrs.get_core_type() == KMP_HW_CORE_TYPE_ATOM) {
5423void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
5424 KMP_DEBUG_ASSERT(th);
5425 bool fine_gran =
true;
5426 int tid = th->th.th_info.ds.ds_tid;
5427 const char *env_var =
"KMP_AFFINITY";
5430 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
5433 switch (__kmp_affinity.gran) {
5437 if (__kmp_nThreadsPerCore > 1) {
5442 if (nCoresPerPkg > 1) {
5450 if (__kmp_topology->is_uniform()) {
5454 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
5456 int ncores = __kmp_ncores;
5457 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
5458 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
5462 int chunk = nthreads / ncores;
5464 int big_cores = nthreads % ncores;
5466 int big_nth = (chunk + 1) * big_cores;
5467 if (tid < big_nth) {
5468 coreID = tid / (chunk + 1);
5469 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5471 coreID = (tid - big_cores) / chunk;
5472 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5474 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5475 "Illegal set affinity operation when not capable");
5477 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5482 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
5483 KMP_CPU_SET(osID, mask);
5485 for (
int i = 0; i < __kmp_nth_per_core; i++) {
5487 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
5488 KMP_CPU_SET(osID, mask);
5491 if (__kmp_affinity.flags.verbose) {
5492 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5493 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5494 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5497 __kmp_affinity_get_thread_topology_info(th);
5498 __kmp_set_system_affinity(mask, TRUE);
5501 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5505 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
5506 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
5507 __kmp_aff_depth - 1, core_level);
5508 int nth_per_core = __kmp_affinity_max_proc_per_core(
5509 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5513 if (nthreads == __kmp_avail_proc) {
5515 int osID = __kmp_topology->at(tid).os_id;
5516 KMP_CPU_SET(osID, mask);
5519 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
5520 for (
int i = 0; i < __kmp_avail_proc; i++) {
5521 int osID = __kmp_topology->at(i).os_id;
5522 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
5524 KMP_CPU_SET(osID, mask);
5528 }
else if (nthreads <= ncores) {
5531 for (
int i = 0; i < ncores; i++) {
5534 for (
int j = 0; j < nth_per_core; j++) {
5535 if (procarr[i * nth_per_core + j] != -1) {
5542 for (
int j = 0; j < nth_per_core; j++) {
5543 int osID = procarr[i * nth_per_core + j];
5545 KMP_CPU_SET(osID, mask);
5561 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
5563 int *ncores_with_x_procs =
5564 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5566 int *ncores_with_x_to_max_procs =
5567 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5569 for (
int i = 0; i <= nth_per_core; i++) {
5570 ncores_with_x_procs[i] = 0;
5571 ncores_with_x_to_max_procs[i] = 0;
5574 for (
int i = 0; i < ncores; i++) {
5576 for (
int j = 0; j < nth_per_core; j++) {
5577 if (procarr[i * nth_per_core + j] != -1) {
5581 nproc_at_core[i] = cnt;
5582 ncores_with_x_procs[cnt]++;
5585 for (
int i = 0; i <= nth_per_core; i++) {
5586 for (
int j = i; j <= nth_per_core; j++) {
5587 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5592 int nproc = nth_per_core * ncores;
5594 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5595 for (
int i = 0; i < nproc; i++) {
5602 for (
int j = 1; j <= nth_per_core; j++) {
5603 int cnt = ncores_with_x_to_max_procs[j];
5604 for (
int i = 0; i < ncores; i++) {
5606 if (nproc_at_core[i] == 0) {
5609 for (
int k = 0; k < nth_per_core; k++) {
5610 if (procarr[i * nth_per_core + k] != -1) {
5611 if (newarr[i * nth_per_core + k] == 0) {
5612 newarr[i * nth_per_core + k] = 1;
5618 newarr[i * nth_per_core + k]++;
5626 if (cnt == 0 || nth == 0) {
5637 for (
int i = 0; i < nproc; i++) {
5641 int osID = procarr[i];
5642 KMP_CPU_SET(osID, mask);
5644 int coreID = i / nth_per_core;
5645 for (
int ii = 0; ii < nth_per_core; ii++) {
5646 int osID = procarr[coreID * nth_per_core + ii];
5648 KMP_CPU_SET(osID, mask);
5658 if (__kmp_affinity.flags.verbose) {
5659 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5660 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5661 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5664 __kmp_affinity_get_thread_topology_info(th);
5665 __kmp_set_system_affinity(mask, TRUE);
5669#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_AIX
5683 kmp_set_thread_affinity_mask_initial()
5688 int gtid = __kmp_get_gtid();
5691 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5692 "non-omp thread, returning\n"));
5695 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
5696 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5697 "affinity not initialized, returning\n"));
5700 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5701 "set full mask for thread %d\n",
5703 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
5705 return bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
5707 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
int try_open(const char *filename, const char *mode)