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(); }
41void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar) {
45 if (TCR_1(machine_hierarchy.uninitialized))
46 machine_hierarchy.init(nproc);
49 if (nproc > machine_hierarchy.base_num_threads)
50 machine_hierarchy.resize(nproc);
52 depth = machine_hierarchy.depth;
53 KMP_DEBUG_ASSERT(depth > 0);
55 thr_bar->depth = depth;
56 __kmp_type_convert(machine_hierarchy.numPerLevel[0] - 1,
57 &(thr_bar->base_leaf_kids));
58 thr_bar->skip_per_level = machine_hierarchy.skipPerLevel;
61static int nCoresPerPkg, nPackages;
62static int __kmp_nThreadsPerCore;
63#ifndef KMP_DFLT_NTH_CORES
64static int __kmp_ncores;
67const char *__kmp_hw_get_catalog_string(kmp_hw_t type,
bool plural) {
70 return ((plural) ? KMP_I18N_STR(Sockets) : KMP_I18N_STR(Socket));
72 return ((plural) ? KMP_I18N_STR(Dice) : KMP_I18N_STR(Die));
74 return ((plural) ? KMP_I18N_STR(Modules) : KMP_I18N_STR(Module));
76 return ((plural) ? KMP_I18N_STR(Tiles) : KMP_I18N_STR(Tile));
78 return ((plural) ? KMP_I18N_STR(NumaDomains) : KMP_I18N_STR(NumaDomain));
80 return ((plural) ? KMP_I18N_STR(L3Caches) : KMP_I18N_STR(L3Cache));
82 return ((plural) ? KMP_I18N_STR(L2Caches) : KMP_I18N_STR(L2Cache));
84 return ((plural) ? KMP_I18N_STR(L1Caches) : KMP_I18N_STR(L1Cache));
86 return ((plural) ? KMP_I18N_STR(LLCaches) : KMP_I18N_STR(LLCache));
88 return ((plural) ? KMP_I18N_STR(Cores) : KMP_I18N_STR(Core));
90 return ((plural) ? KMP_I18N_STR(Threads) : KMP_I18N_STR(Thread));
91 case KMP_HW_PROC_GROUP:
92 return ((plural) ? KMP_I18N_STR(ProcGroups) : KMP_I18N_STR(ProcGroup));
94 return KMP_I18N_STR(Unknown);
97const char *__kmp_hw_get_keyword(kmp_hw_t type,
bool plural) {
100 return ((plural) ?
"sockets" :
"socket");
102 return ((plural) ?
"dice" :
"die");
104 return ((plural) ?
"modules" :
"module");
106 return ((plural) ?
"tiles" :
"tile");
108 return ((plural) ?
"numa_domains" :
"numa_domain");
110 return ((plural) ?
"l3_caches" :
"l3_cache");
112 return ((plural) ?
"l2_caches" :
"l2_cache");
114 return ((plural) ?
"l1_caches" :
"l1_cache");
116 return ((plural) ?
"ll_caches" :
"ll_cache");
118 return ((plural) ?
"cores" :
"core");
120 return ((plural) ?
"threads" :
"thread");
121 case KMP_HW_PROC_GROUP:
122 return ((plural) ?
"proc_groups" :
"proc_group");
124 return ((plural) ?
"unknowns" :
"unknown");
127const char *__kmp_hw_get_core_type_string(kmp_hw_core_type_t type) {
129 case KMP_HW_CORE_TYPE_UNKNOWN:
131#if KMP_ARCH_X86 || KMP_ARCH_X86_64
132 case KMP_HW_CORE_TYPE_ATOM:
133 return "Intel Atom(R) processor";
134 case KMP_HW_CORE_TYPE_CORE:
135 return "Intel(R) Core(TM) processor";
141#if KMP_AFFINITY_SUPPORTED
144#define KMP_AFF_WARNING(s, ...) \
145 if (s.flags.verbose || (s.flags.warnings && (s.type != affinity_none))) { \
146 KMP_WARNING(__VA_ARGS__); \
149#define KMP_AFF_WARNING(s, ...) KMP_WARNING(__VA_ARGS__)
154int kmp_hw_thread_t::compare_ids(
const void *a,
const void *b) {
155 const kmp_hw_thread_t *ahwthread = (
const kmp_hw_thread_t *)a;
156 const kmp_hw_thread_t *bhwthread = (
const kmp_hw_thread_t *)b;
157 int depth = __kmp_topology->get_depth();
158 for (
int level = 0; level < depth; ++level) {
159 if (ahwthread->ids[level] < bhwthread->ids[level])
161 else if (ahwthread->ids[level] > bhwthread->ids[level])
164 if (ahwthread->os_id < bhwthread->os_id)
166 else if (ahwthread->os_id > bhwthread->os_id)
171#if KMP_AFFINITY_SUPPORTED
172int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b) {
174 const kmp_hw_thread_t *aa = (
const kmp_hw_thread_t *)a;
175 const kmp_hw_thread_t *bb = (
const kmp_hw_thread_t *)b;
176 int depth = __kmp_topology->get_depth();
177 int compact = __kmp_topology->compact;
178 KMP_DEBUG_ASSERT(compact >= 0);
179 KMP_DEBUG_ASSERT(compact <= depth);
180 for (i = 0; i < compact; i++) {
181 int j = depth - i - 1;
182 if (aa->sub_ids[j] < bb->sub_ids[j])
184 if (aa->sub_ids[j] > bb->sub_ids[j])
187 for (; i < depth; i++) {
189 if (aa->sub_ids[j] < bb->sub_ids[j])
191 if (aa->sub_ids[j] > bb->sub_ids[j])
198void kmp_hw_thread_t::print()
const {
199 int depth = __kmp_topology->get_depth();
200 printf(
"%4d ", os_id);
201 for (
int i = 0; i < depth; ++i) {
202 printf(
"%4d ", ids[i]);
205 if (attrs.is_core_type_valid())
206 printf(
" (%s)", __kmp_hw_get_core_type_string(attrs.get_core_type()));
207 if (attrs.is_core_eff_valid())
208 printf(
" (eff=%d)", attrs.get_core_eff());
218void kmp_topology_t::_insert_layer(kmp_hw_t type,
const int *ids) {
222 int previous_id = kmp_hw_thread_t::UNKNOWN_ID;
223 int previous_new_id = kmp_hw_thread_t::UNKNOWN_ID;
227 for (target_layer = 0; target_layer < depth; ++target_layer) {
228 bool layers_equal =
true;
229 bool strictly_above_target_layer =
false;
230 for (
int i = 0; i < num_hw_threads; ++i) {
231 int id = hw_threads[i].ids[target_layer];
233 if (
id != previous_id && new_id == previous_new_id) {
235 strictly_above_target_layer =
true;
236 layers_equal =
false;
238 }
else if (
id == previous_id && new_id != previous_new_id) {
240 layers_equal =
false;
244 previous_new_id = new_id;
246 if (strictly_above_target_layer || layers_equal)
252 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
254 types[target_layer] = type;
255 for (
int k = 0; k < num_hw_threads; ++k) {
256 for (
int i = depth - 1, j = depth; i >= target_layer; --i, --j)
257 hw_threads[k].ids[j] = hw_threads[k].ids[i];
258 hw_threads[k].ids[target_layer] = ids[k];
260 equivalent[type] = type;
264#if KMP_GROUP_AFFINITY
266void kmp_topology_t::_insert_windows_proc_groups() {
268 if (__kmp_num_proc_groups == 1)
270 kmp_affin_mask_t *mask;
271 int *ids = (
int *)__kmp_allocate(
sizeof(
int) * num_hw_threads);
273 for (
int i = 0; i < num_hw_threads; ++i) {
275 KMP_CPU_SET(hw_threads[i].os_id, mask);
276 ids[i] = __kmp_get_proc_group(mask);
279 _insert_layer(KMP_HW_PROC_GROUP, ids);
286void kmp_topology_t::_remove_radix1_layers() {
287 int preference[KMP_HW_LAST];
288 int top_index1, top_index2;
290 preference[KMP_HW_SOCKET] = 110;
291 preference[KMP_HW_PROC_GROUP] = 100;
292 preference[KMP_HW_CORE] = 95;
293 preference[KMP_HW_THREAD] = 90;
294 preference[KMP_HW_NUMA] = 85;
295 preference[KMP_HW_DIE] = 80;
296 preference[KMP_HW_TILE] = 75;
297 preference[KMP_HW_MODULE] = 73;
298 preference[KMP_HW_L3] = 70;
299 preference[KMP_HW_L2] = 65;
300 preference[KMP_HW_L1] = 60;
301 preference[KMP_HW_LLC] = 5;
304 while (top_index1 < depth - 1 && top_index2 < depth) {
305 kmp_hw_t type1 = types[top_index1];
306 kmp_hw_t type2 = types[top_index2];
307 KMP_ASSERT_VALID_HW_TYPE(type1);
308 KMP_ASSERT_VALID_HW_TYPE(type2);
311 if ((type1 == KMP_HW_THREAD || type1 == KMP_HW_CORE ||
312 type1 == KMP_HW_SOCKET) &&
313 (type2 == KMP_HW_THREAD || type2 == KMP_HW_CORE ||
314 type2 == KMP_HW_SOCKET)) {
315 top_index1 = top_index2++;
319 bool all_same =
true;
320 int id1 = hw_threads[0].ids[top_index1];
321 int id2 = hw_threads[0].ids[top_index2];
322 int pref1 = preference[type1];
323 int pref2 = preference[type2];
324 for (
int hwidx = 1; hwidx < num_hw_threads; ++hwidx) {
325 if (hw_threads[hwidx].ids[top_index1] == id1 &&
326 hw_threads[hwidx].ids[top_index2] != id2) {
330 if (hw_threads[hwidx].ids[top_index2] != id2)
332 id1 = hw_threads[hwidx].ids[top_index1];
333 id2 = hw_threads[hwidx].ids[top_index2];
337 kmp_hw_t remove_type, keep_type;
338 int remove_layer, remove_layer_ids;
341 remove_layer = remove_layer_ids = top_index2;
345 remove_layer = remove_layer_ids = top_index1;
351 remove_layer_ids = top_index2;
354 set_equivalent_type(remove_type, keep_type);
355 for (
int idx = 0; idx < num_hw_threads; ++idx) {
356 kmp_hw_thread_t &hw_thread = hw_threads[idx];
357 for (
int d = remove_layer_ids; d < depth - 1; ++d)
358 hw_thread.ids[d] = hw_thread.ids[d + 1];
360 for (
int idx = remove_layer; idx < depth - 1; ++idx)
361 types[idx] = types[idx + 1];
364 top_index1 = top_index2++;
367 KMP_ASSERT(depth > 0);
370void kmp_topology_t::_set_last_level_cache() {
371 if (get_equivalent_type(KMP_HW_L3) != KMP_HW_UNKNOWN)
372 set_equivalent_type(KMP_HW_LLC, KMP_HW_L3);
373 else if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
374 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
376 else if (__kmp_mic_type == mic3) {
377 if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
378 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
379 else if (get_equivalent_type(KMP_HW_TILE) != KMP_HW_UNKNOWN)
380 set_equivalent_type(KMP_HW_LLC, KMP_HW_TILE);
383 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
386 else if (get_equivalent_type(KMP_HW_L1) != KMP_HW_UNKNOWN)
387 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
389 if (get_equivalent_type(KMP_HW_LLC) == KMP_HW_UNKNOWN) {
390 if (get_equivalent_type(KMP_HW_SOCKET) != KMP_HW_UNKNOWN)
391 set_equivalent_type(KMP_HW_LLC, KMP_HW_SOCKET);
392 else if (get_equivalent_type(KMP_HW_CORE) != KMP_HW_UNKNOWN)
393 set_equivalent_type(KMP_HW_LLC, KMP_HW_CORE);
395 KMP_ASSERT(get_equivalent_type(KMP_HW_LLC) != KMP_HW_UNKNOWN);
399void kmp_topology_t::_gather_enumeration_information() {
400 int previous_id[KMP_HW_LAST];
401 int max[KMP_HW_LAST];
403 for (
int i = 0; i < depth; ++i) {
404 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
409 int core_level = get_level(KMP_HW_CORE);
410 for (
int i = 0; i < num_hw_threads; ++i) {
411 kmp_hw_thread_t &hw_thread = hw_threads[i];
412 for (
int layer = 0; layer < depth; ++layer) {
413 int id = hw_thread.ids[layer];
414 if (
id != previous_id[layer]) {
416 for (
int l = layer; l < depth; ++l)
420 for (
int l = layer + 1; l < depth; ++l) {
421 if (max[l] > ratio[l])
427 if (__kmp_is_hybrid_cpu() && core_level >= 0 && layer <= core_level) {
428 if (hw_thread.attrs.is_core_eff_valid() &&
429 hw_thread.attrs.core_eff >= num_core_efficiencies) {
432 num_core_efficiencies = hw_thread.attrs.core_eff + 1;
434 if (hw_thread.attrs.is_core_type_valid()) {
436 for (
int j = 0; j < num_core_types; ++j) {
437 if (hw_thread.attrs.get_core_type() == core_types[j]) {
443 KMP_ASSERT(num_core_types < KMP_HW_MAX_NUM_CORE_TYPES);
444 core_types[num_core_types++] = hw_thread.attrs.get_core_type();
451 for (
int layer = 0; layer < depth; ++layer) {
452 previous_id[layer] = hw_thread.ids[layer];
455 for (
int layer = 0; layer < depth; ++layer) {
456 if (max[layer] > ratio[layer])
457 ratio[layer] = max[layer];
461int kmp_topology_t::_get_ncores_with_attr(
const kmp_hw_attr_t &attr,
463 bool find_all)
const {
464 int current, current_max;
465 int previous_id[KMP_HW_LAST];
466 for (
int i = 0; i < depth; ++i)
467 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
468 int core_level = get_level(KMP_HW_CORE);
471 KMP_ASSERT(above_level < core_level);
474 for (
int i = 0; i < num_hw_threads; ++i) {
475 kmp_hw_thread_t &hw_thread = hw_threads[i];
476 if (!find_all && hw_thread.ids[above_level] != previous_id[above_level]) {
477 if (current > current_max)
478 current_max = current;
479 current = hw_thread.attrs.contains(attr);
481 for (
int level = above_level + 1; level <= core_level; ++level) {
482 if (hw_thread.ids[level] != previous_id[level]) {
483 if (hw_thread.attrs.contains(attr))
489 for (
int level = 0; level < depth; ++level)
490 previous_id[level] = hw_thread.ids[level];
492 if (current > current_max)
493 current_max = current;
498void kmp_topology_t::_discover_uniformity() {
500 for (
int level = 0; level < depth; ++level)
502 flags.uniform = (num == count[depth - 1]);
506void kmp_topology_t::_set_sub_ids() {
507 int previous_id[KMP_HW_LAST];
508 int sub_id[KMP_HW_LAST];
510 for (
int i = 0; i < depth; ++i) {
514 for (
int i = 0; i < num_hw_threads; ++i) {
515 kmp_hw_thread_t &hw_thread = hw_threads[i];
517 for (
int j = 0; j < depth; ++j) {
518 if (hw_thread.ids[j] != previous_id[j]) {
520 for (
int k = j + 1; k < depth; ++k) {
527 for (
int j = 0; j < depth; ++j) {
528 previous_id[j] = hw_thread.ids[j];
531 for (
int j = 0; j < depth; ++j) {
532 hw_thread.sub_ids[j] = sub_id[j];
537void kmp_topology_t::_set_globals() {
539 int core_level, thread_level, package_level;
540 package_level = get_level(KMP_HW_SOCKET);
541#if KMP_GROUP_AFFINITY
542 if (package_level == -1)
543 package_level = get_level(KMP_HW_PROC_GROUP);
545 core_level = get_level(KMP_HW_CORE);
546 thread_level = get_level(KMP_HW_THREAD);
548 KMP_ASSERT(core_level != -1);
549 KMP_ASSERT(thread_level != -1);
551 __kmp_nThreadsPerCore = calculate_ratio(thread_level, core_level);
552 if (package_level != -1) {
553 nCoresPerPkg = calculate_ratio(core_level, package_level);
554 nPackages = get_count(package_level);
557 nCoresPerPkg = get_count(core_level);
560#ifndef KMP_DFLT_NTH_CORES
561 __kmp_ncores = get_count(core_level);
565kmp_topology_t *kmp_topology_t::allocate(
int nproc,
int ndepth,
566 const kmp_hw_t *types) {
567 kmp_topology_t *retval;
569 size_t size =
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc +
570 sizeof(int) * (
size_t)KMP_HW_LAST * 3;
571 char *bytes = (
char *)__kmp_allocate(size);
572 retval = (kmp_topology_t *)bytes;
574 retval->hw_threads = (kmp_hw_thread_t *)(bytes +
sizeof(kmp_topology_t));
576 retval->hw_threads =
nullptr;
578 retval->num_hw_threads = nproc;
579 retval->depth = ndepth;
581 (
int *)(bytes +
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc);
582 retval->types = (kmp_hw_t *)arr;
583 retval->ratio = arr + (size_t)KMP_HW_LAST;
584 retval->count = arr + 2 * (size_t)KMP_HW_LAST;
585 retval->num_core_efficiencies = 0;
586 retval->num_core_types = 0;
588 for (
int i = 0; i < KMP_HW_MAX_NUM_CORE_TYPES; ++i)
589 retval->core_types[i] = KMP_HW_CORE_TYPE_UNKNOWN;
590 KMP_FOREACH_HW_TYPE(type) { retval->equivalent[type] = KMP_HW_UNKNOWN; }
591 for (
int i = 0; i < ndepth; ++i) {
592 retval->types[i] = types[i];
593 retval->equivalent[types[i]] = types[i];
598void kmp_topology_t::deallocate(kmp_topology_t *topology) {
600 __kmp_free(topology);
603bool kmp_topology_t::check_ids()
const {
605 if (num_hw_threads == 0)
607 for (
int i = 1; i < num_hw_threads; ++i) {
608 kmp_hw_thread_t ¤t_thread = hw_threads[i];
609 kmp_hw_thread_t &previous_thread = hw_threads[i - 1];
611 for (
int j = 0; j < depth; ++j) {
612 if (previous_thread.ids[j] != current_thread.ids[j]) {
624void kmp_topology_t::dump()
const {
625 printf(
"***********************\n");
626 printf(
"*** __kmp_topology: ***\n");
627 printf(
"***********************\n");
628 printf(
"* depth: %d\n", depth);
631 for (
int i = 0; i < depth; ++i)
632 printf(
"%15s ", __kmp_hw_get_keyword(types[i]));
636 for (
int i = 0; i < depth; ++i) {
637 printf(
"%15d ", ratio[i]);
642 for (
int i = 0; i < depth; ++i) {
643 printf(
"%15d ", count[i]);
647 printf(
"* num_core_eff: %d\n", num_core_efficiencies);
648 printf(
"* num_core_types: %d\n", num_core_types);
649 printf(
"* core_types: ");
650 for (
int i = 0; i < num_core_types; ++i)
651 printf(
"%3d ", core_types[i]);
654 printf(
"* equivalent map:\n");
655 KMP_FOREACH_HW_TYPE(i) {
656 const char *key = __kmp_hw_get_keyword(i);
657 const char *value = __kmp_hw_get_keyword(equivalent[i]);
658 printf(
"%-15s -> %-15s\n", key, value);
661 printf(
"* uniform: %s\n", (is_uniform() ?
"Yes" :
"No"));
663 printf(
"* num_hw_threads: %d\n", num_hw_threads);
664 printf(
"* hw_threads:\n");
665 for (
int i = 0; i < num_hw_threads; ++i) {
666 hw_threads[i].print();
668 printf(
"***********************\n");
671void kmp_topology_t::print(
const char *env_var)
const {
673 int print_types_depth;
674 __kmp_str_buf_init(&buf);
675 kmp_hw_t print_types[KMP_HW_LAST + 2];
678 if (num_hw_threads) {
679 KMP_INFORM(AvailableOSProc, env_var, num_hw_threads);
681 KMP_INFORM(AvailableOSProc, env_var, __kmp_xproc);
686 KMP_INFORM(Uniform, env_var);
688 KMP_INFORM(NonUniform, env_var);
692 KMP_FOREACH_HW_TYPE(type) {
693 kmp_hw_t eq_type = equivalent[type];
694 if (eq_type != KMP_HW_UNKNOWN && eq_type != type) {
695 KMP_INFORM(AffEqualTopologyTypes, env_var,
696 __kmp_hw_get_catalog_string(type),
697 __kmp_hw_get_catalog_string(eq_type));
702 KMP_ASSERT(depth > 0 && depth <= (
int)KMP_HW_LAST);
705 print_types_depth = 0;
706 for (
int level = 0; level < depth; ++level)
707 print_types[print_types_depth++] = types[level];
708 if (equivalent[KMP_HW_CORE] != KMP_HW_CORE) {
710 if (print_types[print_types_depth - 1] == KMP_HW_THREAD) {
713 print_types[print_types_depth - 1] = KMP_HW_CORE;
714 print_types[print_types_depth++] = KMP_HW_THREAD;
716 print_types[print_types_depth++] = KMP_HW_CORE;
720 if (equivalent[KMP_HW_THREAD] != KMP_HW_THREAD)
721 print_types[print_types_depth++] = KMP_HW_THREAD;
723 __kmp_str_buf_clear(&buf);
724 kmp_hw_t numerator_type;
725 kmp_hw_t denominator_type = KMP_HW_UNKNOWN;
726 int core_level = get_level(KMP_HW_CORE);
727 int ncores = get_count(core_level);
729 for (
int plevel = 0, level = 0; plevel < print_types_depth; ++plevel) {
732 numerator_type = print_types[plevel];
733 KMP_ASSERT_VALID_HW_TYPE(numerator_type);
734 if (equivalent[numerator_type] != numerator_type)
737 c = get_ratio(level++);
740 __kmp_str_buf_print(&buf,
"%d %s", c,
741 __kmp_hw_get_catalog_string(numerator_type, plural));
743 __kmp_str_buf_print(&buf,
" x %d %s/%s", c,
744 __kmp_hw_get_catalog_string(numerator_type, plural),
745 __kmp_hw_get_catalog_string(denominator_type));
747 denominator_type = numerator_type;
749 KMP_INFORM(TopologyGeneric, env_var, buf.str, ncores);
752 if (__kmp_is_hybrid_cpu()) {
753 for (
int i = 0; i < num_core_types; ++i) {
754 kmp_hw_core_type_t core_type = core_types[i];
757 attr.set_core_type(core_type);
758 int ncores = get_ncores_with_attr(attr);
760 KMP_INFORM(TopologyHybrid, env_var, ncores,
761 __kmp_hw_get_core_type_string(core_type));
762 KMP_ASSERT(num_core_efficiencies <= KMP_HW_MAX_NUM_CORE_EFFS)
763 for (
int eff = 0; eff < num_core_efficiencies; ++eff) {
764 attr.set_core_eff(eff);
765 int ncores_with_eff = get_ncores_with_attr(attr);
766 if (ncores_with_eff > 0) {
767 KMP_INFORM(TopologyHybridCoreEff, env_var, ncores_with_eff, eff);
774 if (num_hw_threads <= 0) {
775 __kmp_str_buf_free(&buf);
780 KMP_INFORM(OSProcToPhysicalThreadMap, env_var);
781 for (
int i = 0; i < num_hw_threads; i++) {
782 __kmp_str_buf_clear(&buf);
783 for (
int level = 0; level < depth; ++level) {
784 kmp_hw_t type = types[level];
785 __kmp_str_buf_print(&buf,
"%s ", __kmp_hw_get_catalog_string(type));
786 __kmp_str_buf_print(&buf,
"%d ", hw_threads[i].ids[level]);
788 if (__kmp_is_hybrid_cpu())
791 __kmp_hw_get_core_type_string(hw_threads[i].attrs.get_core_type()));
792 KMP_INFORM(OSProcMapToPack, env_var, hw_threads[i].os_id, buf.str);
795 __kmp_str_buf_free(&buf);
798#if KMP_AFFINITY_SUPPORTED
799void kmp_topology_t::set_granularity(kmp_affinity_t &affinity)
const {
800 const char *env_var = affinity.env_var;
802 if (affinity.gran_levels < 0) {
803 kmp_hw_t gran_type = get_equivalent_type(affinity.gran);
805 if (gran_type == KMP_HW_UNKNOWN) {
807 kmp_hw_t gran_types[3] = {KMP_HW_CORE, KMP_HW_THREAD, KMP_HW_SOCKET};
808 for (
auto g : gran_types) {
809 if (get_equivalent_type(g) != KMP_HW_UNKNOWN) {
814 KMP_ASSERT(gran_type != KMP_HW_UNKNOWN);
816 KMP_AFF_WARNING(affinity, AffGranularityBad, env_var,
817 __kmp_hw_get_catalog_string(affinity.gran),
818 __kmp_hw_get_catalog_string(gran_type));
819 affinity.gran = gran_type;
821#if KMP_GROUP_AFFINITY
829 if (__kmp_num_proc_groups > 1) {
830 int gran_depth = get_level(gran_type);
831 int proc_group_depth = get_level(KMP_HW_PROC_GROUP);
832 if (gran_depth >= 0 && proc_group_depth >= 0 &&
833 gran_depth < proc_group_depth) {
834 KMP_AFF_WARNING(affinity, AffGranTooCoarseProcGroup, env_var,
835 __kmp_hw_get_catalog_string(affinity.gran));
836 affinity.gran = gran_type = KMP_HW_PROC_GROUP;
840 affinity.gran_levels = 0;
841 for (
int i = depth - 1; i >= 0 && get_type(i) != gran_type; --i)
842 affinity.gran_levels++;
847void kmp_topology_t::canonicalize() {
848#if KMP_GROUP_AFFINITY
849 _insert_windows_proc_groups();
851 _remove_radix1_layers();
852 _gather_enumeration_information();
853 _discover_uniformity();
856 _set_last_level_cache();
860 if (__kmp_mic_type == mic3) {
861 if (get_level(KMP_HW_L2) != -1)
862 set_equivalent_type(KMP_HW_TILE, KMP_HW_L2);
863 else if (get_level(KMP_HW_TILE) != -1)
864 set_equivalent_type(KMP_HW_L2, KMP_HW_TILE);
869 KMP_ASSERT(depth > 0);
870 for (
int level = 0; level < depth; ++level) {
872 KMP_ASSERT(count[level] > 0 && ratio[level] > 0);
873 KMP_ASSERT_VALID_HW_TYPE(types[level]);
875 KMP_ASSERT(equivalent[types[level]] == types[level]);
880void kmp_topology_t::canonicalize(
int npackages,
int ncores_per_pkg,
881 int nthreads_per_core,
int ncores) {
884 KMP_FOREACH_HW_TYPE(i) { equivalent[i] = KMP_HW_UNKNOWN; }
885 for (
int level = 0; level < depth; ++level) {
889 count[0] = npackages;
891 count[2] = __kmp_xproc;
892 ratio[0] = npackages;
893 ratio[1] = ncores_per_pkg;
894 ratio[2] = nthreads_per_core;
895 equivalent[KMP_HW_SOCKET] = KMP_HW_SOCKET;
896 equivalent[KMP_HW_CORE] = KMP_HW_CORE;
897 equivalent[KMP_HW_THREAD] = KMP_HW_THREAD;
898 types[0] = KMP_HW_SOCKET;
899 types[1] = KMP_HW_CORE;
900 types[2] = KMP_HW_THREAD;
902 _discover_uniformity();
907template <
size_t SIZE,
typename IndexFunc>
struct kmp_sub_ids_t {
910 int prev_sub_id[KMP_HW_LAST];
914 kmp_sub_ids_t(
int last_level) : last_level(last_level) {
915 KMP_ASSERT(last_level < KMP_HW_LAST);
916 for (
size_t i = 0; i < SIZE; ++i)
918 for (
size_t i = 0; i < KMP_HW_LAST; ++i)
921 void update(
const kmp_hw_thread_t &hw_thread) {
922 int idx = indexer(hw_thread);
923 KMP_ASSERT(idx < (
int)SIZE);
924 for (
int level = 0; level <= last_level; ++level) {
925 if (hw_thread.sub_ids[level] != prev_sub_id[level]) {
926 if (level < last_level)
932 for (
int level = 0; level <= last_level; ++level)
933 prev_sub_id[level] = hw_thread.sub_ids[level];
935 int get_sub_id(
const kmp_hw_thread_t &hw_thread)
const {
936 return sub_id[indexer(hw_thread)];
940static kmp_str_buf_t *
941__kmp_hw_get_catalog_core_string(
const kmp_hw_attr_t &attr, kmp_str_buf_t *buf,
943 __kmp_str_buf_init(buf);
944 if (attr.is_core_type_valid())
945 __kmp_str_buf_print(buf,
"%s %s",
946 __kmp_hw_get_core_type_string(attr.get_core_type()),
947 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural));
949 __kmp_str_buf_print(buf,
"%s eff=%d",
950 __kmp_hw_get_catalog_string(KMP_HW_CORE, plural),
951 attr.get_core_eff());
958bool kmp_topology_t::filter_hw_subset() {
960 if (!__kmp_hw_subset)
964 __kmp_hw_subset->sort();
967 bool using_core_types =
false;
968 bool using_core_effs =
false;
969 int hw_subset_depth = __kmp_hw_subset->get_depth();
970 kmp_hw_t specified[KMP_HW_LAST];
971 int *topology_levels = (
int *)KMP_ALLOCA(
sizeof(
int) * hw_subset_depth);
972 KMP_ASSERT(hw_subset_depth > 0);
973 KMP_FOREACH_HW_TYPE(i) { specified[i] = KMP_HW_UNKNOWN; }
974 int core_level = get_level(KMP_HW_CORE);
975 for (
int i = 0; i < hw_subset_depth; ++i) {
977 const kmp_hw_subset_t::item_t &item = __kmp_hw_subset->at(i);
978 int num = item.num[0];
979 int offset = item.offset[0];
980 kmp_hw_t type = item.type;
981 kmp_hw_t equivalent_type = equivalent[type];
982 int level = get_level(type);
983 topology_levels[i] = level;
986 if (equivalent_type != KMP_HW_UNKNOWN) {
987 __kmp_hw_subset->at(i).type = equivalent_type;
989 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetNotExistGeneric,
990 __kmp_hw_get_catalog_string(type));
996 if (specified[equivalent_type] != KMP_HW_UNKNOWN) {
997 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetEqvLayers,
998 __kmp_hw_get_catalog_string(type),
999 __kmp_hw_get_catalog_string(specified[equivalent_type]));
1002 specified[equivalent_type] = type;
1005 max_count = get_ratio(level);
1006 if (max_count < 0 ||
1007 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1008 bool plural = (num > 1);
1009 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric,
1010 __kmp_hw_get_catalog_string(type, plural));
1015 if (core_level == level) {
1017 for (
int j = 0; j < item.num_attrs; ++j) {
1018 if (item.attr[j].is_core_type_valid())
1019 using_core_types =
true;
1020 if (item.attr[j].is_core_eff_valid())
1021 using_core_effs =
true;
1029 if ((using_core_effs || using_core_types) && !__kmp_is_hybrid_cpu()) {
1030 if (item.num_attrs == 1) {
1031 if (using_core_effs) {
1032 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1035 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIgnoringAttr,
1038 using_core_effs =
false;
1039 using_core_types =
false;
1041 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrsNonHybrid);
1047 if (using_core_types && using_core_effs) {
1048 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
"core_type",
1054 if (using_core_effs) {
1055 for (
int j = 0; j < item.num_attrs; ++j) {
1056 if (item.attr[j].is_core_eff_valid()) {
1057 int core_eff = item.attr[j].get_core_eff();
1058 if (core_eff < 0 || core_eff >= num_core_efficiencies) {
1060 __kmp_str_buf_init(&buf);
1061 __kmp_str_buf_print(&buf,
"%d", item.attr[j].get_core_eff());
1062 __kmp_msg(kmp_ms_warning,
1063 KMP_MSG(AffHWSubsetAttrInvalid,
"efficiency", buf.str),
1064 KMP_HNT(ValidValuesRange, 0, num_core_efficiencies - 1),
1066 __kmp_str_buf_free(&buf);
1074 if (using_core_types || using_core_effs) {
1075 for (
int j = 0; j < item.num_attrs; ++j) {
1076 int num = item.num[j];
1077 int offset = item.offset[j];
1078 int level_above = core_level - 1;
1079 if (level_above >= 0) {
1080 max_count = get_ncores_with_attr_per(item.attr[j], level_above);
1081 if (max_count <= 0 ||
1082 (num != kmp_hw_subset_t::USE_ALL && num + offset > max_count)) {
1084 __kmp_hw_get_catalog_core_string(item.attr[j], &buf, num > 0);
1085 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetManyGeneric, buf.str);
1086 __kmp_str_buf_free(&buf);
1093 if ((using_core_types || using_core_effs) && item.num_attrs > 1) {
1094 for (
int j = 0; j < item.num_attrs; ++j) {
1097 if (!item.attr[j]) {
1098 kmp_hw_attr_t other_attr;
1099 for (
int k = 0; k < item.num_attrs; ++k) {
1100 if (item.attr[k] != item.attr[j]) {
1101 other_attr = item.attr[k];
1106 __kmp_hw_get_catalog_core_string(other_attr, &buf, item.num[j] > 0);
1107 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetIncompat,
1108 __kmp_hw_get_catalog_string(KMP_HW_CORE), buf.str);
1109 __kmp_str_buf_free(&buf);
1113 for (
int k = 0; k < j; ++k) {
1114 if (!item.attr[j] || !item.attr[k])
1116 if (item.attr[k] == item.attr[j]) {
1118 __kmp_hw_get_catalog_core_string(item.attr[j], &buf,
1120 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAttrRepeat, buf.str);
1121 __kmp_str_buf_free(&buf);
1130 struct core_type_indexer {
1131 int operator()(
const kmp_hw_thread_t &t)
const {
1132 switch (t.attrs.get_core_type()) {
1133#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1134 case KMP_HW_CORE_TYPE_ATOM:
1136 case KMP_HW_CORE_TYPE_CORE:
1139 case KMP_HW_CORE_TYPE_UNKNOWN:
1146 struct core_eff_indexer {
1147 int operator()(
const kmp_hw_thread_t &t)
const {
1148 return t.attrs.get_core_eff();
1152 kmp_sub_ids_t<KMP_HW_MAX_NUM_CORE_TYPES, core_type_indexer> core_type_sub_ids(
1154 kmp_sub_ids_t<KMP_HW_MAX_NUM_CORE_EFFS, core_eff_indexer> core_eff_sub_ids(
1158 int num_filtered = 0;
1159 bool *filtered = (
bool *)__kmp_allocate(
sizeof(
bool) * num_hw_threads);
1160 for (
int i = 0; i < num_hw_threads; ++i) {
1161 kmp_hw_thread_t &hw_thread = hw_threads[i];
1163 if (using_core_types)
1164 core_type_sub_ids.update(hw_thread);
1165 if (using_core_effs)
1166 core_eff_sub_ids.update(hw_thread);
1169 bool should_be_filtered =
false;
1170 for (
int hw_subset_index = 0; hw_subset_index < hw_subset_depth;
1171 ++hw_subset_index) {
1172 const auto &hw_subset_item = __kmp_hw_subset->at(hw_subset_index);
1173 int level = topology_levels[hw_subset_index];
1176 if ((using_core_effs || using_core_types) && level == core_level) {
1182 kmp_hw_core_type_t core_type = hw_thread.attrs.get_core_type();
1183 int core_eff = hw_thread.attrs.get_core_eff();
1184 for (attr_idx = 0; attr_idx < hw_subset_item.num_attrs; ++attr_idx) {
1185 if (using_core_types &&
1186 hw_subset_item.attr[attr_idx].get_core_type() == core_type)
1188 if (using_core_effs &&
1189 hw_subset_item.attr[attr_idx].get_core_eff() == core_eff)
1193 if (attr_idx == hw_subset_item.num_attrs) {
1194 should_be_filtered =
true;
1198 int num = hw_subset_item.num[attr_idx];
1199 int offset = hw_subset_item.offset[attr_idx];
1200 if (using_core_types)
1201 sub_id = core_type_sub_ids.get_sub_id(hw_thread);
1203 sub_id = core_eff_sub_ids.get_sub_id(hw_thread);
1204 if (sub_id < offset ||
1205 (num != kmp_hw_subset_t::USE_ALL && sub_id >= offset + num)) {
1206 should_be_filtered =
true;
1210 int num = hw_subset_item.num[0];
1211 int offset = hw_subset_item.offset[0];
1212 if (hw_thread.sub_ids[level] < offset ||
1213 (num != kmp_hw_subset_t::USE_ALL &&
1214 hw_thread.sub_ids[level] >= offset + num)) {
1215 should_be_filtered =
true;
1221 filtered[i] = should_be_filtered;
1222 if (should_be_filtered)
1227 if (num_filtered == num_hw_threads) {
1228 KMP_AFF_WARNING(__kmp_affinity, AffHWSubsetAllFiltered);
1229 __kmp_free(filtered);
1235 for (
int i = 0; i < num_hw_threads; ++i) {
1238 hw_threads[new_index] = hw_threads[i];
1241#if KMP_AFFINITY_SUPPORTED
1242 KMP_CPU_CLR(hw_threads[i].os_id, __kmp_affin_fullMask);
1248 KMP_DEBUG_ASSERT(new_index <= num_hw_threads);
1249 num_hw_threads = new_index;
1252 _gather_enumeration_information();
1253 _discover_uniformity();
1255 _set_last_level_cache();
1256 __kmp_free(filtered);
1260bool kmp_topology_t::is_close(
int hwt1,
int hwt2,
int hw_level)
const {
1261 if (hw_level >= depth)
1264 const kmp_hw_thread_t &t1 = hw_threads[hwt1];
1265 const kmp_hw_thread_t &t2 = hw_threads[hwt2];
1266 for (
int i = 0; i < (depth - hw_level); ++i) {
1267 if (t1.ids[i] != t2.ids[i])
1275#if KMP_AFFINITY_SUPPORTED
1277bool KMPAffinity::picked_api =
false;
1279void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
1280void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
1281void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
1282void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
1283void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
1284void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
1286void KMPAffinity::pick_api() {
1287 KMPAffinity *affinity_dispatch;
1293 if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
1294 __kmp_affinity.type != affinity_disabled) {
1295 affinity_dispatch =
new KMPHwlocAffinity();
1299 affinity_dispatch =
new KMPNativeAffinity();
1301 __kmp_affinity_dispatch = affinity_dispatch;
1305void KMPAffinity::destroy_api() {
1306 if (__kmp_affinity_dispatch != NULL) {
1307 delete __kmp_affinity_dispatch;
1308 __kmp_affinity_dispatch = NULL;
1313#define KMP_ADVANCE_SCAN(scan) \
1314 while (*scan != '\0') { \
1322char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
1323 kmp_affin_mask_t *mask) {
1324 int start = 0, finish = 0, previous = 0;
1327 KMP_ASSERT(buf_len >= 40);
1330 char *end = buf + buf_len - 1;
1333 if (mask->begin() == mask->end()) {
1334 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
1335 KMP_ADVANCE_SCAN(scan);
1336 KMP_ASSERT(scan <= end);
1341 start = mask->begin();
1345 for (finish = mask->next(start), previous = start;
1346 finish == previous + 1 && finish != mask->end();
1347 finish = mask->next(finish)) {
1354 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
1355 KMP_ADVANCE_SCAN(scan);
1357 first_range =
false;
1360 if (previous - start > 1) {
1361 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
1364 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
1365 KMP_ADVANCE_SCAN(scan);
1366 if (previous - start > 0) {
1367 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
1370 KMP_ADVANCE_SCAN(scan);
1373 if (start == mask->end())
1381 KMP_ASSERT(scan <= end);
1384#undef KMP_ADVANCE_SCAN
1390kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
1391 kmp_affin_mask_t *mask) {
1392 int start = 0, finish = 0, previous = 0;
1397 __kmp_str_buf_clear(buf);
1400 if (mask->begin() == mask->end()) {
1401 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
1406 start = mask->begin();
1410 for (finish = mask->next(start), previous = start;
1411 finish == previous + 1 && finish != mask->end();
1412 finish = mask->next(finish)) {
1419 __kmp_str_buf_print(buf,
"%s",
",");
1421 first_range =
false;
1424 if (previous - start > 1) {
1425 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
1428 __kmp_str_buf_print(buf,
"%u", start);
1429 if (previous - start > 0) {
1430 __kmp_str_buf_print(buf,
",%u", previous);
1435 if (start == mask->end())
1443kmp_affin_mask_t *__kmp_affinity_get_offline_cpus() {
1444 kmp_affin_mask_t *offline;
1445 KMP_CPU_ALLOC(offline);
1446 KMP_CPU_ZERO(offline);
1448 int n, begin_cpu, end_cpu;
1450 auto skip_ws = [](FILE *f) {
1454 }
while (isspace(c));
1460 int status = offline_file.
try_open(
"/sys/devices/system/cpu/offline",
"r");
1463 while (!feof(offline_file)) {
1464 skip_ws(offline_file);
1465 n = fscanf(offline_file,
"%d", &begin_cpu);
1468 skip_ws(offline_file);
1469 int c = fgetc(offline_file);
1470 if (c == EOF || c ==
',') {
1472 end_cpu = begin_cpu;
1473 }
else if (c ==
'-') {
1475 skip_ws(offline_file);
1476 n = fscanf(offline_file,
"%d", &end_cpu);
1479 skip_ws(offline_file);
1480 c = fgetc(offline_file);
1486 if (begin_cpu < 0 || begin_cpu >= __kmp_xproc || end_cpu < 0 ||
1487 end_cpu >= __kmp_xproc || begin_cpu > end_cpu) {
1491 for (
int cpu = begin_cpu; cpu <= end_cpu; ++cpu) {
1492 KMP_CPU_SET(cpu, offline);
1500int __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
1504#if KMP_GROUP_AFFINITY
1506 if (__kmp_num_proc_groups > 1) {
1508 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
1509 for (group = 0; group < __kmp_num_proc_groups; group++) {
1511 int num = __kmp_GetActiveProcessorCount(group);
1512 for (i = 0; i < num; i++) {
1513 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1523 kmp_affin_mask_t *offline_cpus = __kmp_affinity_get_offline_cpus();
1524 for (proc = 0; proc < __kmp_xproc; proc++) {
1526 if (KMP_CPU_ISSET(proc, offline_cpus))
1528 KMP_CPU_SET(proc, mask);
1531 KMP_CPU_FREE(offline_cpus);
1540kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1542kmp_affin_mask_t *__kmp_affin_origMask = NULL;
1545static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1546#if HWLOC_API_VERSION >= 0x00020000
1547 return hwloc_obj_type_is_cache(obj->type);
1549 return obj->type == HWLOC_OBJ_CACHE;
1554static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1556 if (__kmp_hwloc_is_cache_type(obj)) {
1557 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1558 return KMP_HW_UNKNOWN;
1559 switch (obj->attr->cache.depth) {
1563#if KMP_MIC_SUPPORTED
1564 if (__kmp_mic_type == mic3) {
1572 return KMP_HW_UNKNOWN;
1575 switch (obj->type) {
1576 case HWLOC_OBJ_PACKAGE:
1577 return KMP_HW_SOCKET;
1578 case HWLOC_OBJ_NUMANODE:
1580 case HWLOC_OBJ_CORE:
1583 return KMP_HW_THREAD;
1584 case HWLOC_OBJ_GROUP:
1585#if HWLOC_API_VERSION >= 0x00020000
1586 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1588 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1590 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1591 return KMP_HW_MODULE;
1592 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1593 return KMP_HW_PROC_GROUP;
1595 return KMP_HW_UNKNOWN;
1596#if HWLOC_API_VERSION >= 0x00020100
1601 return KMP_HW_UNKNOWN;
1608static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1609 hwloc_obj_type_t type) {
1612 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1613 obj->logical_index, type, 0);
1614 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1615 obj->type, first) == obj;
1616 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1625static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1626 hwloc_obj_t lower) {
1628 hwloc_obj_type_t ltype = lower->type;
1629 int lindex = lower->logical_index - 1;
1632 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1633 while (obj && lindex >= 0 &&
1634 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1635 if (obj->userdata) {
1636 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1641 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1644 lower->userdata = RCAST(
void *, sub_id + 1);
1648static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1650 int hw_thread_index, sub_id;
1652 hwloc_obj_t pu, obj, root, prev;
1653 kmp_hw_t types[KMP_HW_LAST];
1654 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1656 hwloc_topology_t tp = __kmp_hwloc_topology;
1657 *msg_id = kmp_i18n_null;
1658 if (__kmp_affinity.flags.verbose) {
1659 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1662 if (!KMP_AFFINITY_CAPABLE()) {
1665 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1667 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1669 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1672 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1674 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1676 __kmp_nThreadsPerCore = 1;
1677 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1678 if (nCoresPerPkg == 0)
1680 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1684#if HWLOC_API_VERSION >= 0x00020400
1686 int nr_cpu_kinds = hwloc_cpukinds_get_nr(tp, 0);
1688 typedef struct kmp_hwloc_cpukinds_info_t {
1690 kmp_hw_core_type_t core_type;
1691 hwloc_bitmap_t mask;
1692 } kmp_hwloc_cpukinds_info_t;
1693 kmp_hwloc_cpukinds_info_t *cpukinds =
nullptr;
1695 if (nr_cpu_kinds > 0) {
1697 struct hwloc_info_s *infos;
1698 cpukinds = (kmp_hwloc_cpukinds_info_t *)__kmp_allocate(
1699 sizeof(kmp_hwloc_cpukinds_info_t) * nr_cpu_kinds);
1700 for (
unsigned idx = 0; idx < (unsigned)nr_cpu_kinds; ++idx) {
1701 cpukinds[idx].efficiency = -1;
1702 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_UNKNOWN;
1703 cpukinds[idx].mask = hwloc_bitmap_alloc();
1704 if (hwloc_cpukinds_get_info(tp, idx, cpukinds[idx].mask,
1705 &cpukinds[idx].efficiency, &nr_infos, &infos,
1707 for (
unsigned i = 0; i < nr_infos; ++i) {
1708 if (__kmp_str_match(
"CoreType", 8, infos[i].name)) {
1709#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1710 if (__kmp_str_match(
"IntelAtom", 9, infos[i].value)) {
1711 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_ATOM;
1713 }
else if (__kmp_str_match(
"IntelCore", 9, infos[i].value)) {
1714 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_CORE;
1725 root = hwloc_get_root_obj(tp);
1729 pu = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1732 types[depth] = KMP_HW_THREAD;
1733 hwloc_types[depth] = obj->type;
1735 while (obj != root && obj != NULL) {
1737#if HWLOC_API_VERSION >= 0x00020000
1738 if (obj->memory_arity) {
1740 for (memory = obj->memory_first_child; memory;
1741 memory = hwloc_get_next_child(tp, obj, memory)) {
1742 if (memory->type == HWLOC_OBJ_NUMANODE)
1745 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1746 types[depth] = KMP_HW_NUMA;
1747 hwloc_types[depth] = memory->type;
1752 type = __kmp_hwloc_type_2_topology_type(obj);
1753 if (type != KMP_HW_UNKNOWN) {
1754 types[depth] = type;
1755 hwloc_types[depth] = obj->type;
1759 KMP_ASSERT(depth > 0);
1762 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1763 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1764 kmp_hw_t temp = types[i];
1765 types[i] = types[j];
1767 hwloc_types[i] = hwloc_types[j];
1768 hwloc_types[j] = hwloc_temp;
1772 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1774 hw_thread_index = 0;
1776 while ((pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu))) {
1777 int index = depth - 1;
1778 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1779 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1782 hw_thread.ids[index] = pu->logical_index;
1783 hw_thread.os_id = pu->os_index;
1785#if HWLOC_API_VERSION >= 0x00020400
1787 int cpukind_index = -1;
1788 for (
int i = 0; i < nr_cpu_kinds; ++i) {
1789 if (hwloc_bitmap_isset(cpukinds[i].mask, hw_thread.os_id)) {
1794 if (cpukind_index >= 0) {
1795 hw_thread.attrs.set_core_type(cpukinds[cpukind_index].core_type);
1796 hw_thread.attrs.set_core_eff(cpukinds[cpukind_index].efficiency);
1804 while (obj != root && obj != NULL) {
1806#if HWLOC_API_VERSION >= 0x00020000
1810 if (obj->memory_arity) {
1812 for (memory = obj->memory_first_child; memory;
1813 memory = hwloc_get_next_child(tp, obj, memory)) {
1814 if (memory->type == HWLOC_OBJ_NUMANODE)
1817 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1818 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1820 hw_thread.ids[index] = memory->logical_index;
1821 hw_thread.ids[index + 1] = sub_id;
1829 type = __kmp_hwloc_type_2_topology_type(obj);
1830 if (type != KMP_HW_UNKNOWN) {
1831 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1833 hw_thread.ids[index] = obj->logical_index;
1834 hw_thread.ids[index + 1] = sub_id;
1844#if HWLOC_API_VERSION >= 0x00020400
1847 for (
int idx = 0; idx < nr_cpu_kinds; ++idx)
1848 hwloc_bitmap_free(cpukinds[idx].mask);
1849 __kmp_free(cpukinds);
1852 __kmp_topology->sort_ids();
1860static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
1861 *msg_id = kmp_i18n_null;
1863 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1865 if (__kmp_affinity.flags.verbose) {
1866 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
1872 if (!KMP_AFFINITY_CAPABLE()) {
1873 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1874 __kmp_ncores = nPackages = __kmp_xproc;
1875 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1883 __kmp_ncores = nPackages = __kmp_avail_proc;
1884 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1887 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1890 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1892 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1895 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
1897 hw_thread.os_id = i;
1898 hw_thread.ids[0] = i;
1899 hw_thread.ids[1] = 0;
1900 hw_thread.ids[2] = 0;
1903 if (__kmp_affinity.flags.verbose) {
1904 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
1909#if KMP_GROUP_AFFINITY
1914static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
1915 *msg_id = kmp_i18n_null;
1917 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
1918 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
1920 if (__kmp_affinity.flags.verbose) {
1921 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
1925 if (!KMP_AFFINITY_CAPABLE()) {
1926 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1927 nPackages = __kmp_num_proc_groups;
1928 __kmp_nThreadsPerCore = 1;
1929 __kmp_ncores = __kmp_xproc;
1930 nCoresPerPkg = nPackages / __kmp_ncores;
1935 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1938 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1940 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1943 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct++);
1945 hw_thread.os_id = i;
1946 hw_thread.ids[0] = i / BITS_PER_GROUP;
1947 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
1953#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1955template <kmp_u
int32 LSB, kmp_u
int32 MSB>
1956static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
1957 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
1958 const kmp_uint32 SHIFT_RIGHT = LSB;
1959 kmp_uint32 retval = v;
1960 retval <<= SHIFT_LEFT;
1961 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
1965static int __kmp_cpuid_mask_width(
int count) {
1968 while ((1 << r) < count)
1973class apicThreadInfo {
1977 unsigned maxCoresPerPkg;
1978 unsigned maxThreadsPerPkg;
1984static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
1986 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
1987 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
1988 if (aa->pkgId < bb->pkgId)
1990 if (aa->pkgId > bb->pkgId)
1992 if (aa->coreId < bb->coreId)
1994 if (aa->coreId > bb->coreId)
1996 if (aa->threadId < bb->threadId)
1998 if (aa->threadId > bb->threadId)
2003class kmp_cache_info_t {
2006 unsigned level, mask;
2008 kmp_cache_info_t() : depth(0) { get_leaf4_levels(); }
2009 size_t get_depth()
const {
return depth; }
2010 info_t &operator[](
size_t index) {
return table[index]; }
2011 const info_t &operator[](
size_t index)
const {
return table[index]; }
2013 static kmp_hw_t get_topology_type(
unsigned level) {
2014 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
2023 return KMP_HW_UNKNOWN;
2027 static const int MAX_CACHE_LEVEL = 3;
2030 info_t table[MAX_CACHE_LEVEL];
2032 void get_leaf4_levels() {
2034 while (depth < MAX_CACHE_LEVEL) {
2035 unsigned cache_type, max_threads_sharing;
2036 unsigned cache_level, cache_mask_width;
2038 __kmp_x86_cpuid(4, level, &buf2);
2039 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
2043 if (cache_type == 2) {
2047 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
2048 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
2049 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
2050 table[depth].level = cache_level;
2051 table[depth].mask = ((-1) << cache_mask_width);
2062static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
2064 *msg_id = kmp_i18n_null;
2066 if (__kmp_affinity.flags.verbose) {
2067 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
2071 __kmp_x86_cpuid(0, 0, &buf);
2073 *msg_id = kmp_i18n_str_NoLeaf4Support;
2082 if (!KMP_AFFINITY_CAPABLE()) {
2085 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2091 __kmp_x86_cpuid(1, 0, &buf);
2092 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2093 if (maxThreadsPerPkg == 0) {
2094 maxThreadsPerPkg = 1;
2108 __kmp_x86_cpuid(0, 0, &buf);
2110 __kmp_x86_cpuid(4, 0, &buf);
2111 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2129 __kmp_ncores = __kmp_xproc;
2130 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2131 __kmp_nThreadsPerCore = 1;
2140 kmp_affinity_raii_t previous_affinity;
2168 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
2169 __kmp_avail_proc *
sizeof(apicThreadInfo));
2170 unsigned nApics = 0;
2171 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2173 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2176 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
2178 __kmp_affinity_dispatch->bind_thread(i);
2179 threadInfo[nApics].osId = i;
2182 __kmp_x86_cpuid(1, 0, &buf);
2183 if (((buf.edx >> 9) & 1) == 0) {
2184 __kmp_free(threadInfo);
2185 *msg_id = kmp_i18n_str_ApicNotPresent;
2188 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
2189 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2190 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
2191 threadInfo[nApics].maxThreadsPerPkg = 1;
2200 __kmp_x86_cpuid(0, 0, &buf);
2202 __kmp_x86_cpuid(4, 0, &buf);
2203 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2205 threadInfo[nApics].maxCoresPerPkg = 1;
2209 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
2210 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
2212 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
2213 int widthT = widthCT - widthC;
2218 __kmp_free(threadInfo);
2219 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2223 int maskC = (1 << widthC) - 1;
2224 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
2226 int maskT = (1 << widthT) - 1;
2227 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
2234 previous_affinity.restore();
2237 qsort(threadInfo, nApics,
sizeof(*threadInfo),
2238 __kmp_affinity_cmp_apicThreadInfo_phys_id);
2255 __kmp_nThreadsPerCore = 1;
2256 unsigned nCores = 1;
2259 unsigned lastPkgId = threadInfo[0].pkgId;
2260 unsigned coreCt = 1;
2261 unsigned lastCoreId = threadInfo[0].coreId;
2262 unsigned threadCt = 1;
2263 unsigned lastThreadId = threadInfo[0].threadId;
2266 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
2267 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
2269 for (i = 1; i < nApics; i++) {
2270 if (threadInfo[i].pkgId != lastPkgId) {
2273 lastPkgId = threadInfo[i].pkgId;
2274 if ((
int)coreCt > nCoresPerPkg)
2275 nCoresPerPkg = coreCt;
2277 lastCoreId = threadInfo[i].coreId;
2278 if ((
int)threadCt > __kmp_nThreadsPerCore)
2279 __kmp_nThreadsPerCore = threadCt;
2281 lastThreadId = threadInfo[i].threadId;
2285 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
2286 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
2290 if (threadInfo[i].coreId != lastCoreId) {
2293 lastCoreId = threadInfo[i].coreId;
2294 if ((
int)threadCt > __kmp_nThreadsPerCore)
2295 __kmp_nThreadsPerCore = threadCt;
2297 lastThreadId = threadInfo[i].threadId;
2298 }
else if (threadInfo[i].threadId != lastThreadId) {
2300 lastThreadId = threadInfo[i].threadId;
2302 __kmp_free(threadInfo);
2303 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2309 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
2310 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
2311 __kmp_free(threadInfo);
2312 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
2320 if ((
int)coreCt > nCoresPerPkg)
2321 nCoresPerPkg = coreCt;
2322 if ((
int)threadCt > __kmp_nThreadsPerCore)
2323 __kmp_nThreadsPerCore = threadCt;
2324 __kmp_ncores = nCores;
2325 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
2333 int threadLevel = 2;
2335 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
2338 types[idx++] = KMP_HW_SOCKET;
2340 types[idx++] = KMP_HW_CORE;
2341 if (threadLevel >= 0)
2342 types[idx++] = KMP_HW_THREAD;
2344 KMP_ASSERT(depth > 0);
2345 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
2347 for (i = 0; i < nApics; ++i) {
2349 unsigned os = threadInfo[i].osId;
2350 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2353 if (pkgLevel >= 0) {
2354 hw_thread.ids[idx++] = threadInfo[i].pkgId;
2356 if (coreLevel >= 0) {
2357 hw_thread.ids[idx++] = threadInfo[i].coreId;
2359 if (threadLevel >= 0) {
2360 hw_thread.ids[idx++] = threadInfo[i].threadId;
2362 hw_thread.os_id = os;
2365 __kmp_free(threadInfo);
2366 __kmp_topology->sort_ids();
2367 if (!__kmp_topology->check_ids()) {
2368 kmp_topology_t::deallocate(__kmp_topology);
2369 __kmp_topology =
nullptr;
2370 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2378static void __kmp_get_hybrid_info(kmp_hw_core_type_t *type,
int *efficiency,
2379 unsigned *native_model_id) {
2381 __kmp_x86_cpuid(0x1a, 0, &buf);
2382 *type = (kmp_hw_core_type_t)__kmp_extract_bits<24, 31>(buf.eax);
2384 case KMP_HW_CORE_TYPE_ATOM:
2387 case KMP_HW_CORE_TYPE_CORE:
2393 *native_model_id = __kmp_extract_bits<0, 23>(buf.eax);
2415 INTEL_LEVEL_TYPE_INVALID = 0,
2416 INTEL_LEVEL_TYPE_SMT = 1,
2417 INTEL_LEVEL_TYPE_CORE = 2,
2418 INTEL_LEVEL_TYPE_MODULE = 3,
2419 INTEL_LEVEL_TYPE_TILE = 4,
2420 INTEL_LEVEL_TYPE_DIE = 5,
2421 INTEL_LEVEL_TYPE_LAST = 6,
2424struct cpuid_level_info_t {
2425 unsigned level_type, mask, mask_width, nitems, cache_mask;
2428static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
2429 switch (intel_type) {
2430 case INTEL_LEVEL_TYPE_INVALID:
2431 return KMP_HW_SOCKET;
2432 case INTEL_LEVEL_TYPE_SMT:
2433 return KMP_HW_THREAD;
2434 case INTEL_LEVEL_TYPE_CORE:
2436 case INTEL_LEVEL_TYPE_TILE:
2438 case INTEL_LEVEL_TYPE_MODULE:
2439 return KMP_HW_MODULE;
2440 case INTEL_LEVEL_TYPE_DIE:
2443 return KMP_HW_UNKNOWN;
2450__kmp_x2apicid_get_levels(
int leaf,
2451 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST],
2452 kmp_uint64 known_levels) {
2453 unsigned level, levels_index;
2454 unsigned level_type, mask_width, nitems;
2464 level = levels_index = 0;
2466 __kmp_x86_cpuid(leaf, level, &buf);
2467 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
2468 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
2469 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
2470 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0)
2473 if (known_levels & (1ull << level_type)) {
2475 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
2476 levels[levels_index].level_type = level_type;
2477 levels[levels_index].mask_width = mask_width;
2478 levels[levels_index].nitems = nitems;
2482 if (levels_index > 0) {
2483 levels[levels_index - 1].mask_width = mask_width;
2484 levels[levels_index - 1].nitems = nitems;
2488 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
2491 if (levels_index == 0 || levels[0].level_type == INTEL_LEVEL_TYPE_INVALID)
2495 for (
unsigned i = 0; i < levels_index; ++i) {
2496 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
2497 levels[i].mask = ~((-1) << levels[i].mask_width);
2498 levels[i].cache_mask = (-1) << levels[i].mask_width;
2499 for (
unsigned j = 0; j < i; ++j)
2500 levels[i].mask ^= levels[j].mask;
2502 KMP_DEBUG_ASSERT(i > 0);
2503 levels[i].mask = (-1) << levels[i - 1].mask_width;
2504 levels[i].cache_mask = 0;
2507 return levels_index;
2510static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
2512 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
2513 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
2514 unsigned levels_index;
2516 kmp_uint64 known_levels;
2517 int topology_leaf, highest_leaf, apic_id;
2519 static int leaves[] = {0, 0};
2521 kmp_i18n_id_t leaf_message_id;
2523 KMP_BUILD_ASSERT(
sizeof(known_levels) * CHAR_BIT > KMP_HW_LAST);
2525 *msg_id = kmp_i18n_null;
2526 if (__kmp_affinity.flags.verbose) {
2527 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
2531 known_levels = 0ull;
2532 for (
int i = 0; i < INTEL_LEVEL_TYPE_LAST; ++i) {
2533 if (__kmp_intel_type_2_topology_type(i) != KMP_HW_UNKNOWN) {
2534 known_levels |= (1ull << i);
2539 __kmp_x86_cpuid(0, 0, &buf);
2540 highest_leaf = buf.eax;
2545 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
2548 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2549 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
2552 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
2557 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2561 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2563 for (
int i = 0; i < num_leaves; ++i) {
2564 int leaf = leaves[i];
2565 if (highest_leaf < leaf)
2567 __kmp_x86_cpuid(leaf, 0, &buf);
2570 topology_leaf = leaf;
2571 levels_index = __kmp_x2apicid_get_levels(leaf, levels, known_levels);
2572 if (levels_index == 0)
2576 if (topology_leaf == -1 || levels_index == 0) {
2577 *msg_id = leaf_message_id;
2580 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
2587 if (!KMP_AFFINITY_CAPABLE()) {
2590 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2591 for (
unsigned i = 0; i < levels_index; ++i) {
2592 if (levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
2593 __kmp_nThreadsPerCore = levels[i].nitems;
2594 }
else if (levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
2595 nCoresPerPkg = levels[i].nitems;
2598 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
2599 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2604 int depth = levels_index;
2605 for (
int i = depth - 1, j = 0; i >= 0; --i, ++j)
2606 types[j] = __kmp_intel_type_2_topology_type(levels[i].level_type);
2608 kmp_topology_t::allocate(__kmp_avail_proc, levels_index, types);
2611 kmp_cache_info_t cache_info;
2612 for (
size_t i = 0; i < cache_info.get_depth(); ++i) {
2613 const kmp_cache_info_t::info_t &info = cache_info[i];
2614 unsigned cache_mask = info.mask;
2615 unsigned cache_level = info.level;
2616 for (
unsigned j = 0; j < levels_index; ++j) {
2617 unsigned hw_cache_mask = levels[j].cache_mask;
2618 kmp_hw_t cache_type = kmp_cache_info_t::get_topology_type(cache_level);
2619 if (hw_cache_mask == cache_mask && j < levels_index - 1) {
2621 __kmp_intel_type_2_topology_type(levels[j + 1].level_type);
2622 __kmp_topology->set_equivalent_type(cache_type, type);
2632 kmp_affinity_raii_t previous_affinity;
2637 int hw_thread_index = 0;
2638 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2639 cpuid_level_info_t my_levels[INTEL_LEVEL_TYPE_LAST];
2640 unsigned my_levels_index;
2643 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2646 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2648 __kmp_affinity_dispatch->bind_thread(proc);
2651 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2653 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
2655 __kmp_x2apicid_get_levels(topology_leaf, my_levels, known_levels);
2656 if (my_levels_index == 0 || my_levels_index != levels_index) {
2657 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2661 hw_thread.os_id = proc;
2663 for (
unsigned j = 0, idx = depth - 1; j < my_levels_index; ++j, --idx) {
2664 hw_thread.ids[idx] = apic_id & my_levels[j].mask;
2666 hw_thread.ids[idx] >>= my_levels[j - 1].mask_width;
2670 if (__kmp_is_hybrid_cpu() && highest_leaf >= 0x1a) {
2671 kmp_hw_core_type_t type;
2672 unsigned native_model_id;
2674 __kmp_get_hybrid_info(&type, &efficiency, &native_model_id);
2675 hw_thread.attrs.set_core_type(type);
2676 hw_thread.attrs.set_core_eff(efficiency);
2680 KMP_ASSERT(hw_thread_index > 0);
2681 __kmp_topology->sort_ids();
2682 if (!__kmp_topology->check_ids()) {
2683 kmp_topology_t::deallocate(__kmp_topology);
2684 __kmp_topology =
nullptr;
2685 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
2693#define threadIdIndex 1
2694#define coreIdIndex 2
2696#define nodeIdIndex 4
2698typedef unsigned *ProcCpuInfo;
2699static unsigned maxIndex = pkgIdIndex;
2701static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
2704 const unsigned *aa = *(
unsigned *
const *)a;
2705 const unsigned *bb = *(
unsigned *
const *)b;
2706 for (i = maxIndex;; i--) {
2717#if KMP_USE_HIER_SCHED
2719static void __kmp_dispatch_set_hierarchy_values() {
2725 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
2726 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2727 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
2728#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2730 if (__kmp_mic_type >= mic3)
2731 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
2734 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
2735 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
2736 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
2737 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
2740 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
2741 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
2742 __kmp_nThreadsPerCore;
2743#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2745 if (__kmp_mic_type >= mic3)
2746 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2747 2 * __kmp_nThreadsPerCore;
2750 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2751 __kmp_nThreadsPerCore;
2752 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
2753 nCoresPerPkg * __kmp_nThreadsPerCore;
2754 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
2755 nCoresPerPkg * __kmp_nThreadsPerCore;
2756 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
2757 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2762int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
2763 int index = type + 1;
2764 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
2765 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
2766 if (type == kmp_hier_layer_e::LAYER_THREAD)
2768 else if (type == kmp_hier_layer_e::LAYER_LOOP)
2770 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
2771 if (tid >= num_hw_threads)
2772 tid = tid % num_hw_threads;
2773 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
2777int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
2780 KMP_DEBUG_ASSERT(i1 <= i2);
2781 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
2782 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
2783 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
2785 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
2789static inline const char *__kmp_cpuinfo_get_filename() {
2790 const char *filename;
2791 if (__kmp_cpuinfo_file !=
nullptr)
2792 filename = __kmp_cpuinfo_file;
2794 filename =
"/proc/cpuinfo";
2798static inline const char *__kmp_cpuinfo_get_envvar() {
2799 const char *envvar =
nullptr;
2800 if (__kmp_cpuinfo_file !=
nullptr)
2801 envvar =
"KMP_CPUINFO_FILE";
2807static bool __kmp_affinity_create_cpuinfo_map(
int *line,
2808 kmp_i18n_id_t *
const msg_id) {
2809 const char *filename = __kmp_cpuinfo_get_filename();
2810 const char *envvar = __kmp_cpuinfo_get_envvar();
2811 *msg_id = kmp_i18n_null;
2813 if (__kmp_affinity.flags.verbose) {
2814 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
2822 unsigned num_records = 0;
2824 buf[
sizeof(buf) - 1] = 1;
2825 if (!fgets(buf,
sizeof(buf), f)) {
2830 char s1[] =
"processor";
2831 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2838 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
2840 if (level > (
unsigned)__kmp_xproc) {
2841 level = __kmp_xproc;
2843 if (nodeIdIndex + level >= maxIndex) {
2844 maxIndex = nodeIdIndex + level;
2852 if (num_records == 0) {
2853 *msg_id = kmp_i18n_str_NoProcRecords;
2856 if (num_records > (
unsigned)__kmp_xproc) {
2857 *msg_id = kmp_i18n_str_TooManyProcRecords;
2866 if (fseek(f, 0, SEEK_SET) != 0) {
2867 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
2873 unsigned **threadInfo =
2874 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
2876 for (i = 0; i <= num_records; i++) {
2878 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2881#define CLEANUP_THREAD_INFO \
2882 for (i = 0; i <= num_records; i++) { \
2883 __kmp_free(threadInfo[i]); \
2885 __kmp_free(threadInfo);
2890#define INIT_PROC_INFO(p) \
2891 for (__index = 0; __index <= maxIndex; __index++) { \
2892 (p)[__index] = UINT_MAX; \
2895 for (i = 0; i <= num_records; i++) {
2896 INIT_PROC_INFO(threadInfo[i]);
2899 unsigned num_avail = 0;
2906 buf[
sizeof(buf) - 1] = 1;
2907 bool long_line =
false;
2908 if (!fgets(buf,
sizeof(buf), f)) {
2913 for (i = 0; i <= maxIndex; i++) {
2914 if (threadInfo[num_avail][i] != UINT_MAX) {
2922 }
else if (!buf[
sizeof(buf) - 1]) {
2929 CLEANUP_THREAD_INFO; \
2930 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
2936#if KMP_ARCH_LOONGARCH64
2943 if (*buf ==
'\n' && *line == 2)
2947 char s1[] =
"processor";
2948 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2950 char *p = strchr(buf +
sizeof(s1) - 1,
':');
2952 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2954 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
2964 threadInfo[num_avail][osIdIndex] = val;
2965#if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
2969 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
2970 threadInfo[num_avail][osIdIndex]);
2971 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
2973 KMP_SNPRINTF(path,
sizeof(path),
2974 "/sys/devices/system/cpu/cpu%u/topology/core_id",
2975 threadInfo[num_avail][osIdIndex]);
2976 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
2980 char s2[] =
"physical id";
2981 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
2983 char *p = strchr(buf +
sizeof(s2) - 1,
':');
2985 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2987 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
2989 threadInfo[num_avail][pkgIdIndex] = val;
2992 char s3[] =
"core id";
2993 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
2995 char *p = strchr(buf +
sizeof(s3) - 1,
':');
2997 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2999 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
3001 threadInfo[num_avail][coreIdIndex] = val;
3005 char s4[] =
"thread id";
3006 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
3008 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3010 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3012 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
3014 threadInfo[num_avail][threadIdIndex] = val;
3018 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3020 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3022 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3025 if (level > (
unsigned)__kmp_xproc) {
3026 level = __kmp_xproc;
3028 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
3030 threadInfo[num_avail][nodeIdIndex + level] = val;
3037 if ((*buf != 0) && (*buf !=
'\n')) {
3042 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
3050 if ((
int)num_avail == __kmp_xproc) {
3051 CLEANUP_THREAD_INFO;
3052 *msg_id = kmp_i18n_str_TooManyEntries;
3058 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
3059 CLEANUP_THREAD_INFO;
3060 *msg_id = kmp_i18n_str_MissingProcField;
3063 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
3064 CLEANUP_THREAD_INFO;
3065 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
3070 if (KMP_AFFINITY_CAPABLE() &&
3071 !KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
3072 __kmp_affin_fullMask)) {
3073 INIT_PROC_INFO(threadInfo[num_avail]);
3080 KMP_ASSERT(num_avail <= num_records);
3081 INIT_PROC_INFO(threadInfo[num_avail]);
3086 CLEANUP_THREAD_INFO;
3087 *msg_id = kmp_i18n_str_MissingValCpuinfo;
3091 CLEANUP_THREAD_INFO;
3092 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
3097#if KMP_MIC && REDUCE_TEAM_SIZE
3098 unsigned teamSize = 0;
3106 KMP_ASSERT(num_avail > 0);
3107 KMP_ASSERT(num_avail <= num_records);
3110 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
3111 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
3123 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3125 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3127 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3129 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3131 bool assign_thread_ids =
false;
3132 unsigned threadIdCt;
3139 if (assign_thread_ids) {
3140 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
3141 threadInfo[0][threadIdIndex] = threadIdCt++;
3142 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
3143 threadIdCt = threadInfo[0][threadIdIndex] + 1;
3146 for (index = 0; index <= maxIndex; index++) {
3150 lastId[index] = threadInfo[0][index];
3155 for (i = 1; i < num_avail; i++) {
3158 for (index = maxIndex; index >= threadIdIndex; index--) {
3159 if (assign_thread_ids && (index == threadIdIndex)) {
3161 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3162 threadInfo[i][threadIdIndex] = threadIdCt++;
3166 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3167 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3170 if (threadInfo[i][index] != lastId[index]) {
3175 for (index2 = threadIdIndex; index2 < index; index2++) {
3177 if (counts[index2] > maxCt[index2]) {
3178 maxCt[index2] = counts[index2];
3181 lastId[index2] = threadInfo[i][index2];
3185 lastId[index] = threadInfo[i][index];
3187 if (assign_thread_ids && (index > threadIdIndex)) {
3189#if KMP_MIC && REDUCE_TEAM_SIZE
3192 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3199 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3200 threadInfo[i][threadIdIndex] = threadIdCt++;
3206 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3207 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3213 if (index < threadIdIndex) {
3217 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
3222 CLEANUP_THREAD_INFO;
3223 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3229 assign_thread_ids =
true;
3230 goto restart_radix_check;
3234#if KMP_MIC && REDUCE_TEAM_SIZE
3237 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3240 for (index = threadIdIndex; index <= maxIndex; index++) {
3241 if (counts[index] > maxCt[index]) {
3242 maxCt[index] = counts[index];
3246 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
3247 nCoresPerPkg = maxCt[coreIdIndex];
3248 nPackages = totals[pkgIdIndex];
3254 __kmp_ncores = totals[coreIdIndex];
3255 if (!KMP_AFFINITY_CAPABLE()) {
3256 KMP_ASSERT(__kmp_affinity.type == affinity_none);
3260#if KMP_MIC && REDUCE_TEAM_SIZE
3262 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
3263 __kmp_dflt_team_nth = teamSize;
3264 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
3265 "__kmp_dflt_team_nth = %d\n",
3266 __kmp_dflt_team_nth));
3270 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
3277 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
3278 for (index = threadIdIndex; index < maxIndex; index++) {
3279 KMP_ASSERT(totals[index] >= totals[index + 1]);
3280 inMap[index] = (totals[index] > totals[index + 1]);
3282 inMap[maxIndex] = (totals[maxIndex] > 1);
3283 inMap[pkgIdIndex] =
true;
3284 inMap[coreIdIndex] =
true;
3285 inMap[threadIdIndex] =
true;
3289 kmp_hw_t types[KMP_HW_LAST];
3292 int threadLevel = -1;
3293 for (index = threadIdIndex; index <= maxIndex; index++) {
3298 if (inMap[pkgIdIndex]) {
3300 types[idx++] = KMP_HW_SOCKET;
3302 if (inMap[coreIdIndex]) {
3304 types[idx++] = KMP_HW_CORE;
3306 if (inMap[threadIdIndex]) {
3308 types[idx++] = KMP_HW_THREAD;
3310 KMP_ASSERT(depth > 0);
3313 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
3315 for (i = 0; i < num_avail; ++i) {
3316 unsigned os = threadInfo[i][osIdIndex];
3318 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3320 hw_thread.os_id = os;
3323 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
3324 if (!inMap[src_index]) {
3327 if (src_index == pkgIdIndex) {
3328 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
3329 }
else if (src_index == coreIdIndex) {
3330 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
3331 }
else if (src_index == threadIdIndex) {
3332 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
3342 CLEANUP_THREAD_INFO;
3343 __kmp_topology->sort_ids();
3344 if (!__kmp_topology->check_ids()) {
3345 kmp_topology_t::deallocate(__kmp_topology);
3346 __kmp_topology =
nullptr;
3347 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3356static void __kmp_create_os_id_masks(
unsigned *numUnique,
3357 kmp_affinity_t &affinity) {
3361 int numAddrs = __kmp_topology->get_num_hw_threads();
3362 int depth = __kmp_topology->get_depth();
3363 const char *env_var = affinity.env_var;
3364 KMP_ASSERT(numAddrs);
3368 for (i = numAddrs - 1;; --i) {
3369 int osId = __kmp_topology->at(i).os_id;
3370 if (osId > maxOsId) {
3376 affinity.num_os_id_masks = maxOsId + 1;
3377 KMP_CPU_ALLOC_ARRAY(affinity.os_id_masks, affinity.num_os_id_masks);
3378 KMP_ASSERT(affinity.gran_levels >= 0);
3379 if (affinity.flags.verbose && (affinity.gran_levels > 0)) {
3380 KMP_INFORM(ThreadsMigrate, env_var, affinity.gran_levels);
3382 if (affinity.gran_levels >= (
int)depth) {
3383 KMP_AFF_WARNING(affinity, AffThreadsMayMigrate);
3393 kmp_affin_mask_t *sum;
3394 KMP_CPU_ALLOC_ON_STACK(sum);
3396 KMP_CPU_SET(__kmp_topology->at(0).os_id, sum);
3397 for (i = 1; i < numAddrs; i++) {
3401 if (__kmp_topology->is_close(leader, i, affinity.gran_levels)) {
3402 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3408 for (; j < i; j++) {
3409 int osId = __kmp_topology->at(j).os_id;
3410 KMP_DEBUG_ASSERT(osId <= maxOsId);
3411 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3412 KMP_CPU_COPY(mask, sum);
3413 __kmp_topology->at(j).leader = (j == leader);
3420 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3425 for (; j < i; j++) {
3426 int osId = __kmp_topology->at(j).os_id;
3427 KMP_DEBUG_ASSERT(osId <= maxOsId);
3428 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3429 KMP_CPU_COPY(mask, sum);
3430 __kmp_topology->at(j).leader = (j == leader);
3433 KMP_CPU_FREE_FROM_STACK(sum);
3435 *numUnique = unique;
3441static kmp_affin_mask_t *newMasks;
3442static int numNewMasks;
3443static int nextNewMask;
3445#define ADD_MASK(_mask) \
3447 if (nextNewMask >= numNewMasks) { \
3450 kmp_affin_mask_t *temp; \
3451 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
3452 for (i = 0; i < numNewMasks / 2; i++) { \
3453 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
3454 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
3455 KMP_CPU_COPY(dest, src); \
3457 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
3460 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
3464#define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
3466 if (((_osId) > _maxOsId) || \
3467 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
3468 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, _osId); \
3470 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
3476static void __kmp_affinity_process_proclist(kmp_affinity_t &affinity) {
3478 kmp_affin_mask_t **out_masks = &affinity.masks;
3479 unsigned *out_numMasks = &affinity.num_masks;
3480 const char *proclist = affinity.proclist;
3481 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3482 int maxOsId = affinity.num_os_id_masks - 1;
3483 const char *scan = proclist;
3484 const char *next = proclist;
3489 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3491 kmp_affin_mask_t *sumMask;
3492 KMP_CPU_ALLOC(sumMask);
3496 int start, end, stride;
3500 if (*next ==
'\0') {
3512 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
3514 num = __kmp_str_to_int(scan, *next);
3515 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3518 if ((num > maxOsId) ||
3519 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3520 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3521 KMP_CPU_ZERO(sumMask);
3523 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3543 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3546 num = __kmp_str_to_int(scan, *next);
3547 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3550 if ((num > maxOsId) ||
3551 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3552 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3554 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3571 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3573 start = __kmp_str_to_int(scan, *next);
3574 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
3579 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3593 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3595 end = __kmp_str_to_int(scan, *next);
3596 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
3613 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3615 stride = __kmp_str_to_int(scan, *next);
3616 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
3621 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
3623 KMP_ASSERT2(start <= end,
"bad explicit proc list");
3625 KMP_ASSERT2(start >= end,
"bad explicit proc list");
3627 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
3632 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3634 }
while (start <= end);
3637 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3639 }
while (start >= end);
3650 *out_numMasks = nextNewMask;
3651 if (nextNewMask == 0) {
3653 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3656 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3657 for (i = 0; i < nextNewMask; i++) {
3658 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3659 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3660 KMP_CPU_COPY(dest, src);
3662 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3663 KMP_CPU_FREE(sumMask);
3686static void __kmp_process_subplace_list(
const char **scan,
3687 kmp_affinity_t &affinity,
int maxOsId,
3688 kmp_affin_mask_t *tempMask,
3691 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3694 int start, count, stride, i;
3698 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3701 start = __kmp_str_to_int(*scan, *next);
3702 KMP_ASSERT(start >= 0);
3707 if (**scan ==
'}' || **scan ==
',') {
3708 if ((start > maxOsId) ||
3709 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3710 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3712 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3715 if (**scan ==
'}') {
3721 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3726 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3729 count = __kmp_str_to_int(*scan, *next);
3730 KMP_ASSERT(count >= 0);
3735 if (**scan ==
'}' || **scan ==
',') {
3736 for (i = 0; i < count; i++) {
3737 if ((start > maxOsId) ||
3738 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3739 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3742 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3747 if (**scan ==
'}') {
3753 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3760 if (**scan ==
'+') {
3764 if (**scan ==
'-') {
3772 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3775 stride = __kmp_str_to_int(*scan, *next);
3776 KMP_ASSERT(stride >= 0);
3782 if (**scan ==
'}' || **scan ==
',') {
3783 for (i = 0; i < count; i++) {
3784 if ((start > maxOsId) ||
3785 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3786 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3789 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3794 if (**scan ==
'}') {
3801 KMP_ASSERT2(0,
"bad explicit places list");
3805static void __kmp_process_place(
const char **scan, kmp_affinity_t &affinity,
3806 int maxOsId, kmp_affin_mask_t *tempMask,
3809 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3813 if (**scan ==
'{') {
3815 __kmp_process_subplace_list(scan, affinity, maxOsId, tempMask, setSize);
3816 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3818 }
else if (**scan ==
'!') {
3820 __kmp_process_place(scan, affinity, maxOsId, tempMask, setSize);
3821 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
3822 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
3825 int num = __kmp_str_to_int(*scan, *next);
3826 KMP_ASSERT(num >= 0);
3827 if ((num > maxOsId) ||
3828 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3829 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3831 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3836 KMP_ASSERT2(0,
"bad explicit places list");
3841void __kmp_affinity_process_placelist(kmp_affinity_t &affinity) {
3842 int i, j, count, stride, sign;
3843 kmp_affin_mask_t **out_masks = &affinity.masks;
3844 unsigned *out_numMasks = &affinity.num_masks;
3845 const char *placelist = affinity.proclist;
3846 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3847 int maxOsId = affinity.num_os_id_masks - 1;
3848 const char *scan = placelist;
3849 const char *next = placelist;
3852 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3858 kmp_affin_mask_t *tempMask;
3859 kmp_affin_mask_t *previousMask;
3860 KMP_CPU_ALLOC(tempMask);
3861 KMP_CPU_ZERO(tempMask);
3862 KMP_CPU_ALLOC(previousMask);
3863 KMP_CPU_ZERO(previousMask);
3867 __kmp_process_place(&scan, affinity, maxOsId, tempMask, &setSize);
3871 if (*scan ==
'\0' || *scan ==
',') {
3875 KMP_CPU_ZERO(tempMask);
3877 if (*scan ==
'\0') {
3884 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3889 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3892 count = __kmp_str_to_int(scan, *next);
3893 KMP_ASSERT(count >= 0);
3898 if (*scan ==
'\0' || *scan ==
',') {
3901 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3920 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3923 stride = __kmp_str_to_int(scan, *next);
3924 KMP_DEBUG_ASSERT(stride >= 0);
3930 for (i = 0; i < count; i++) {
3935 KMP_CPU_COPY(previousMask, tempMask);
3936 ADD_MASK(previousMask);
3937 KMP_CPU_ZERO(tempMask);
3939 KMP_CPU_SET_ITERATE(j, previousMask) {
3940 if (!KMP_CPU_ISSET(j, previousMask)) {
3943 if ((j + stride > maxOsId) || (j + stride < 0) ||
3944 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
3945 (!KMP_CPU_ISSET(j + stride,
3946 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
3947 if (i < count - 1) {
3948 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, j + stride);
3952 KMP_CPU_SET(j + stride, tempMask);
3956 KMP_CPU_ZERO(tempMask);
3961 if (*scan ==
'\0') {
3969 KMP_ASSERT2(0,
"bad explicit places list");
3972 *out_numMasks = nextNewMask;
3973 if (nextNewMask == 0) {
3975 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3978 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3979 KMP_CPU_FREE(tempMask);
3980 KMP_CPU_FREE(previousMask);
3981 for (i = 0; i < nextNewMask; i++) {
3982 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3983 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3984 KMP_CPU_COPY(dest, src);
3986 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3994static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
3997 for (
int i = 0; i < nprocs; i++) {
3998 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3999 for (
int j = bottom_level; j > 0; j--) {
4000 if (hw_thread.ids[j] > 0) {
4001 if (core_level < (j - 1)) {
4011static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
4013 return __kmp_topology->get_count(core_level);
4016static int __kmp_affinity_find_core(
int proc,
int bottom_level,
4019 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
4020 for (
int i = 0; i <= proc; ++i) {
4021 if (i + 1 <= proc) {
4022 for (
int j = 0; j <= core_level; ++j) {
4023 if (__kmp_topology->at(i + 1).sub_ids[j] !=
4024 __kmp_topology->at(i).sub_ids[j]) {
4036static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
4038 if (core_level >= bottom_level)
4040 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
4041 return __kmp_topology->calculate_ratio(thread_level, core_level);
4044static int *procarr = NULL;
4045static int __kmp_aff_depth = 0;
4046static int *__kmp_osid_to_hwthread_map = NULL;
4048static void __kmp_affinity_get_mask_topology_info(
const kmp_affin_mask_t *mask,
4049 kmp_affinity_ids_t &ids,
4050 kmp_affinity_attrs_t &attrs) {
4051 if (!KMP_AFFINITY_CAPABLE())
4055 for (
int i = 0; i < KMP_HW_LAST; ++i)
4056 ids[i] = kmp_hw_thread_t::UNKNOWN_ID;
4057 attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
4062 int depth = __kmp_topology->get_depth();
4063 KMP_CPU_SET_ITERATE(cpu, mask) {
4064 int osid_idx = __kmp_osid_to_hwthread_map[cpu];
4065 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(osid_idx);
4066 for (
int level = 0; level < depth; ++level) {
4067 kmp_hw_t type = __kmp_topology->get_type(level);
4068 int id = hw_thread.sub_ids[level];
4069 if (ids[type] == kmp_hw_thread_t::UNKNOWN_ID || ids[type] ==
id) {
4074 ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4075 for (; level < depth; ++level) {
4076 kmp_hw_t type = __kmp_topology->get_type(level);
4077 ids[type] = kmp_hw_thread_t::MULTIPLE_ID;
4082 attrs.core_type = hw_thread.attrs.get_core_type();
4083 attrs.core_eff = hw_thread.attrs.get_core_eff();
4087 if (attrs.core_type != hw_thread.attrs.get_core_type())
4088 attrs.core_type = KMP_HW_CORE_TYPE_UNKNOWN;
4089 if (attrs.core_eff != hw_thread.attrs.get_core_eff())
4090 attrs.core_eff = kmp_hw_attr_t::UNKNOWN_CORE_EFF;
4095static void __kmp_affinity_get_thread_topology_info(kmp_info_t *th) {
4096 if (!KMP_AFFINITY_CAPABLE())
4098 const kmp_affin_mask_t *mask = th->th.th_affin_mask;
4099 kmp_affinity_ids_t &ids = th->th.th_topology_ids;
4100 kmp_affinity_attrs_t &attrs = th->th.th_topology_attrs;
4101 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4107static void __kmp_affinity_get_topology_info(kmp_affinity_t &affinity) {
4108 if (!KMP_AFFINITY_CAPABLE())
4110 if (affinity.type != affinity_none) {
4111 KMP_ASSERT(affinity.num_os_id_masks);
4112 KMP_ASSERT(affinity.os_id_masks);
4114 KMP_ASSERT(affinity.num_masks);
4115 KMP_ASSERT(affinity.masks);
4116 KMP_ASSERT(__kmp_affin_fullMask);
4118 int max_cpu = __kmp_affin_fullMask->get_max_cpu();
4119 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4122 if (!affinity.ids) {
4123 affinity.ids = (kmp_affinity_ids_t *)__kmp_allocate(
4124 sizeof(kmp_affinity_ids_t) * affinity.num_masks);
4126 if (!affinity.attrs) {
4127 affinity.attrs = (kmp_affinity_attrs_t *)__kmp_allocate(
4128 sizeof(kmp_affinity_attrs_t) * affinity.num_masks);
4130 if (!__kmp_osid_to_hwthread_map) {
4132 __kmp_osid_to_hwthread_map =
4133 (
int *)__kmp_allocate(
sizeof(
int) * (max_cpu + 1));
4137 for (
int hw_thread = 0; hw_thread < num_hw_threads; ++hw_thread)
4138 __kmp_osid_to_hwthread_map[__kmp_topology->at(hw_thread).os_id] = hw_thread;
4140 for (
unsigned i = 0; i < affinity.num_masks; ++i) {
4141 kmp_affinity_ids_t &ids = affinity.ids[i];
4142 kmp_affinity_attrs_t &attrs = affinity.attrs[i];
4143 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.masks, i);
4144 __kmp_affinity_get_mask_topology_info(mask, ids, attrs);
4150static void __kmp_create_affinity_none_places(kmp_affinity_t &affinity) {
4151 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4152 KMP_ASSERT(affinity.type == affinity_none);
4153 affinity.num_masks = 1;
4154 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4155 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, 0);
4156 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4157 __kmp_affinity_get_topology_info(affinity);
4160static void __kmp_aux_affinity_initialize_masks(kmp_affinity_t &affinity) {
4165 int verbose = affinity.flags.verbose;
4166 const char *env_var = affinity.env_var;
4169 if (__kmp_affin_fullMask && __kmp_affin_origMask)
4172 if (__kmp_affin_fullMask == NULL) {
4173 KMP_CPU_ALLOC(__kmp_affin_fullMask);
4175 if (__kmp_affin_origMask == NULL) {
4176 KMP_CPU_ALLOC(__kmp_affin_origMask);
4178 if (KMP_AFFINITY_CAPABLE()) {
4179 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4181 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4182 if (affinity.flags.respect) {
4185 __kmp_avail_proc = 0;
4186 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4187 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4192 if (__kmp_avail_proc > __kmp_xproc) {
4193 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4194 affinity.type = affinity_none;
4195 KMP_AFFINITY_DISABLE();
4200 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4201 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4202 __kmp_affin_fullMask);
4203 KMP_INFORM(InitOSProcSetRespect, env_var, buf);
4207 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4208 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4209 __kmp_affin_fullMask);
4210 KMP_INFORM(InitOSProcSetNotRespect, env_var, buf);
4213 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4215 if (__kmp_num_proc_groups <= 1) {
4217 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4221 __kmp_affin_fullMask->set_process_affinity(
true);
4227static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
4228 bool success =
false;
4229 const char *env_var = affinity.env_var;
4230 kmp_i18n_id_t msg_id = kmp_i18n_null;
4231 int verbose = affinity.flags.verbose;
4235 if ((__kmp_cpuinfo_file != NULL) &&
4236 (__kmp_affinity_top_method == affinity_top_method_all)) {
4237 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4240 if (__kmp_affinity_top_method == affinity_top_method_all) {
4246 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4247 if (!__kmp_hwloc_error) {
4248 success = __kmp_affinity_create_hwloc_map(&msg_id);
4249 if (!success && verbose) {
4250 KMP_INFORM(AffIgnoringHwloc, env_var);
4252 }
else if (verbose) {
4253 KMP_INFORM(AffIgnoringHwloc, env_var);
4258#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4260 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4261 if (!success && verbose && msg_id != kmp_i18n_null) {
4262 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4266 success = __kmp_affinity_create_apicid_map(&msg_id);
4267 if (!success && verbose && msg_id != kmp_i18n_null) {
4268 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4276 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4277 if (!success && verbose && msg_id != kmp_i18n_null) {
4278 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4283#if KMP_GROUP_AFFINITY
4284 if (!success && (__kmp_num_proc_groups > 1)) {
4285 success = __kmp_affinity_create_proc_group_map(&msg_id);
4286 if (!success && verbose && msg_id != kmp_i18n_null) {
4287 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4293 success = __kmp_affinity_create_flat_map(&msg_id);
4294 if (!success && verbose && msg_id != kmp_i18n_null) {
4295 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4297 KMP_ASSERT(success);
4305 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4306 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4307 success = __kmp_affinity_create_hwloc_map(&msg_id);
4309 KMP_ASSERT(msg_id != kmp_i18n_null);
4310 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4315#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4316 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
4317 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
4318 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4320 KMP_ASSERT(msg_id != kmp_i18n_null);
4321 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4323 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4324 success = __kmp_affinity_create_apicid_map(&msg_id);
4326 KMP_ASSERT(msg_id != kmp_i18n_null);
4327 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4332 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4334 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4336 KMP_ASSERT(msg_id != kmp_i18n_null);
4337 const char *filename = __kmp_cpuinfo_get_filename();
4339 KMP_FATAL(FileLineMsgExiting, filename, line,
4340 __kmp_i18n_catgets(msg_id));
4342 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4347#if KMP_GROUP_AFFINITY
4348 else if (__kmp_affinity_top_method == affinity_top_method_group) {
4349 success = __kmp_affinity_create_proc_group_map(&msg_id);
4350 KMP_ASSERT(success);
4352 KMP_ASSERT(msg_id != kmp_i18n_null);
4353 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4358 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4359 success = __kmp_affinity_create_flat_map(&msg_id);
4361 KMP_ASSERT(success);
4365 if (!__kmp_topology) {
4366 if (KMP_AFFINITY_CAPABLE()) {
4367 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4369 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
4371 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
4372 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
4373 __kmp_nThreadsPerCore, __kmp_ncores);
4375 __kmp_topology->print(env_var);
4382 __kmp_topology->canonicalize();
4384 __kmp_topology->print(env_var);
4385 bool filtered = __kmp_topology->filter_hw_subset();
4389 if (__kmp_num_proc_groups <= 1)
4391 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4393 if (filtered && verbose)
4394 __kmp_topology->print(
"KMP_HW_SUBSET");
4398static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
4399 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4400 bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
4401 const char *env_var = affinity.env_var;
4403 if (affinity.flags.initialized) {
4404 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4408 if (is_regular_affinity && (!__kmp_affin_fullMask || !__kmp_affin_origMask))
4409 __kmp_aux_affinity_initialize_masks(affinity);
4411 if (is_regular_affinity && !__kmp_topology) {
4412 bool success = __kmp_aux_affinity_initialize_topology(affinity);
4415 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
4416 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4418 affinity.type = affinity_none;
4419 KMP_AFFINITY_DISABLE();
4426 if (affinity.type == affinity_none) {
4427 __kmp_create_affinity_none_places(affinity);
4428#if KMP_USE_HIER_SCHED
4429 __kmp_dispatch_set_hierarchy_values();
4431 affinity.flags.initialized = TRUE;
4435 __kmp_topology->set_granularity(affinity);
4436 int depth = __kmp_topology->get_depth();
4440 __kmp_create_os_id_masks(&numUnique, affinity);
4441 if (affinity.gran_levels == 0) {
4442 KMP_DEBUG_ASSERT((
int)numUnique == __kmp_avail_proc);
4445 switch (affinity.type) {
4447 case affinity_explicit:
4448 KMP_DEBUG_ASSERT(affinity.proclist != NULL);
4449 if (is_hidden_helper_affinity ||
4450 __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
4451 __kmp_affinity_process_proclist(affinity);
4453 __kmp_affinity_process_placelist(affinity);
4455 if (affinity.num_masks == 0) {
4456 KMP_AFF_WARNING(affinity, AffNoValidProcID);
4457 affinity.type = affinity_none;
4458 __kmp_create_affinity_none_places(affinity);
4459 affinity.flags.initialized = TRUE;
4468 case affinity_logical:
4469 affinity.compact = 0;
4470 if (affinity.offset) {
4472 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4476 case affinity_physical:
4477 if (__kmp_nThreadsPerCore > 1) {
4478 affinity.compact = 1;
4479 if (affinity.compact >= depth) {
4480 affinity.compact = 0;
4483 affinity.compact = 0;
4485 if (affinity.offset) {
4487 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4491 case affinity_scatter:
4492 if (affinity.compact >= depth) {
4493 affinity.compact = 0;
4495 affinity.compact = depth - 1 - affinity.compact;
4499 case affinity_compact:
4500 if (affinity.compact >= depth) {
4501 affinity.compact = depth - 1;
4505 case affinity_balanced:
4506 if (depth <= 1 || is_hidden_helper_affinity) {
4507 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4508 affinity.type = affinity_none;
4509 __kmp_create_affinity_none_places(affinity);
4510 affinity.flags.initialized = TRUE;
4512 }
else if (!__kmp_topology->is_uniform()) {
4514 __kmp_aff_depth = depth;
4517 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
4518 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
4520 int maxprocpercore = __kmp_affinity_max_proc_per_core(
4521 __kmp_avail_proc, depth - 1, core_level);
4523 int nproc = ncores * maxprocpercore;
4524 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
4525 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4526 affinity.type = affinity_none;
4527 __kmp_create_affinity_none_places(affinity);
4528 affinity.flags.initialized = TRUE;
4532 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
4533 for (
int i = 0; i < nproc; i++) {
4539 for (
int i = 0; i < __kmp_avail_proc; i++) {
4540 int proc = __kmp_topology->at(i).os_id;
4541 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
4543 if (core == lastcore) {
4550 procarr[core * maxprocpercore + inlastcore] = proc;
4553 if (affinity.compact >= depth) {
4554 affinity.compact = depth - 1;
4559 if (affinity.flags.dups) {
4560 affinity.num_masks = __kmp_avail_proc;
4562 affinity.num_masks = numUnique;
4565 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
4566 (__kmp_affinity_num_places > 0) &&
4567 ((
unsigned)__kmp_affinity_num_places < affinity.num_masks) &&
4568 !is_hidden_helper_affinity) {
4569 affinity.num_masks = __kmp_affinity_num_places;
4572 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4576 __kmp_topology->sort_compact(affinity);
4580 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4581 for (i = 0, j = 0; i < num_hw_threads; i++) {
4582 if ((!affinity.flags.dups) && (!__kmp_topology->at(i).leader)) {
4585 int osId = __kmp_topology->at(i).os_id;
4587 kmp_affin_mask_t *src = KMP_CPU_INDEX(affinity.os_id_masks, osId);
4588 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, j);
4589 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
4590 KMP_CPU_COPY(dest, src);
4591 if (++j >= affinity.num_masks) {
4595 KMP_DEBUG_ASSERT(j == affinity.num_masks);
4598 __kmp_topology->sort_ids();
4602 KMP_ASSERT2(0,
"Unexpected affinity setting");
4604 __kmp_affinity_get_topology_info(affinity);
4605 affinity.flags.initialized = TRUE;
4608void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
4617 int disabled = (affinity.type == affinity_disabled);
4618 if (!KMP_AFFINITY_CAPABLE())
4619 KMP_ASSERT(disabled);
4621 affinity.type = affinity_none;
4622 __kmp_aux_affinity_initialize(affinity);
4624 affinity.type = affinity_disabled;
4627void __kmp_affinity_uninitialize(
void) {
4628 for (kmp_affinity_t *affinity : __kmp_affinities) {
4629 if (affinity->masks != NULL)
4630 KMP_CPU_FREE_ARRAY(affinity->masks, affinity->num_masks);
4631 if (affinity->os_id_masks != NULL)
4632 KMP_CPU_FREE_ARRAY(affinity->os_id_masks, affinity->num_os_id_masks);
4633 if (affinity->proclist != NULL)
4634 __kmp_free(affinity->proclist);
4635 if (affinity->ids != NULL)
4636 __kmp_free(affinity->ids);
4637 if (affinity->attrs != NULL)
4638 __kmp_free(affinity->attrs);
4639 *affinity = KMP_AFFINITY_INIT(affinity->env_var);
4641 if (__kmp_affin_origMask != NULL) {
4642 if (KMP_AFFINITY_CAPABLE()) {
4643 __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
4645 KMP_CPU_FREE(__kmp_affin_origMask);
4646 __kmp_affin_origMask = NULL;
4648 __kmp_affinity_num_places = 0;
4649 if (procarr != NULL) {
4650 __kmp_free(procarr);
4653 if (__kmp_osid_to_hwthread_map) {
4654 __kmp_free(__kmp_osid_to_hwthread_map);
4655 __kmp_osid_to_hwthread_map = NULL;
4658 if (__kmp_hwloc_topology != NULL) {
4659 hwloc_topology_destroy(__kmp_hwloc_topology);
4660 __kmp_hwloc_topology = NULL;
4663 if (__kmp_hw_subset) {
4664 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
4665 __kmp_hw_subset =
nullptr;
4667 if (__kmp_topology) {
4668 kmp_topology_t::deallocate(__kmp_topology);
4669 __kmp_topology =
nullptr;
4671 KMPAffinity::destroy_api();
4674static void __kmp_select_mask_by_gtid(
int gtid,
const kmp_affinity_t *affinity,
4675 int *place, kmp_affin_mask_t **mask) {
4677 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
4678 if (is_hidden_helper)
4681 mask_idx = gtid - 2;
4683 mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
4684 KMP_DEBUG_ASSERT(affinity->num_masks > 0);
4685 *place = (mask_idx + affinity->offset) % affinity->num_masks;
4686 *mask = KMP_CPU_INDEX(affinity->masks, *place);
4691void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
4693 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4696 for (
int id = 0;
id < KMP_HW_LAST; ++id)
4697 th->th.th_topology_ids[
id] = kmp_hw_thread_t::UNKNOWN_ID;
4698 th->th.th_topology_attrs = KMP_AFFINITY_ATTRS_UNKNOWN;
4700 if (!KMP_AFFINITY_CAPABLE()) {
4704 if (th->th.th_affin_mask == NULL) {
4705 KMP_CPU_ALLOC(th->th.th_affin_mask);
4707 KMP_CPU_ZERO(th->th.th_affin_mask);
4715 kmp_affin_mask_t *mask;
4717 const kmp_affinity_t *affinity;
4718 const char *env_var;
4719 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
4721 if (is_hidden_helper)
4722 affinity = &__kmp_hh_affinity;
4724 affinity = &__kmp_affinity;
4725 env_var = affinity->env_var;
4727 if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
4728 if ((affinity->type == affinity_none) ||
4729 (affinity->type == affinity_balanced) ||
4730 KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
4731#if KMP_GROUP_AFFINITY
4732 if (__kmp_num_proc_groups > 1) {
4736 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4738 mask = __kmp_affin_fullMask;
4740 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
4743 if (!isa_root || __kmp_nested_proc_bind.bind_types[0] == proc_bind_false) {
4744#if KMP_GROUP_AFFINITY
4745 if (__kmp_num_proc_groups > 1) {
4749 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4751 mask = __kmp_affin_fullMask;
4753 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
4757 th->th.th_current_place = i;
4758 if (isa_root && !is_hidden_helper) {
4759 th->th.th_new_place = i;
4760 th->th.th_first_place = 0;
4761 th->th.th_last_place = affinity->num_masks - 1;
4762 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
4765 th->th.th_first_place = 0;
4766 th->th.th_last_place = affinity->num_masks - 1;
4770 th->th.th_topology_ids = __kmp_affinity.ids[i];
4771 th->th.th_topology_attrs = __kmp_affinity.attrs[i];
4774 if (i == KMP_PLACE_ALL) {
4775 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4778 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4782 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4785 if (affinity->flags.verbose &&
4786 (affinity->type == affinity_none ||
4787 (i != KMP_PLACE_ALL && affinity->type != affinity_balanced)) &&
4788 !KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
4789 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4790 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4791 th->th.th_affin_mask);
4792 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
4800 if (affinity->type == affinity_none) {
4801 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4804 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4807void __kmp_affinity_set_place(
int gtid) {
4809 if (!KMP_AFFINITY_CAPABLE() || KMP_HIDDEN_HELPER_THREAD(gtid)) {
4813 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4815 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current "
4817 gtid, th->th.th_new_place, th->th.th_current_place));
4820 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4821 KMP_ASSERT(th->th.th_new_place >= 0);
4822 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity.num_masks);
4823 if (th->th.th_first_place <= th->th.th_last_place) {
4824 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
4825 (th->th.th_new_place <= th->th.th_last_place));
4827 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
4828 (th->th.th_new_place >= th->th.th_last_place));
4833 kmp_affin_mask_t *mask =
4834 KMP_CPU_INDEX(__kmp_affinity.masks, th->th.th_new_place);
4835 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4836 th->th.th_current_place = th->th.th_new_place;
4838 th->th.th_topology_ids = __kmp_affinity.ids[th->th.th_new_place];
4839 th->th.th_topology_attrs = __kmp_affinity.attrs[th->th.th_new_place];
4841 if (__kmp_affinity.flags.verbose) {
4842 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4843 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4844 th->th.th_affin_mask);
4845 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
4846 __kmp_gettid(), gtid, buf);
4848 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4851int __kmp_aux_set_affinity(
void **mask) {
4856 if (!KMP_AFFINITY_CAPABLE()) {
4860 gtid = __kmp_entry_gtid();
4863 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4864 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4865 (kmp_affin_mask_t *)(*mask));
4867 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4871 if (__kmp_env_consistency_check) {
4872 if ((mask == NULL) || (*mask == NULL)) {
4873 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4878 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
4879 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4880 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4882 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4887 if (num_procs == 0) {
4888 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4891#if KMP_GROUP_AFFINITY
4892 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4893 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4899 th = __kmp_threads[gtid];
4900 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4901 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4903 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4906 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4907 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4908 th->th.th_first_place = 0;
4909 th->th.th_last_place = __kmp_affinity.num_masks - 1;
4912 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
4917int __kmp_aux_get_affinity(
void **mask) {
4920#if KMP_OS_WINDOWS || KMP_DEBUG
4923 if (!KMP_AFFINITY_CAPABLE()) {
4927 gtid = __kmp_entry_gtid();
4928#if KMP_OS_WINDOWS || KMP_DEBUG
4929 th = __kmp_threads[gtid];
4933 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4937 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4938 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4939 th->th.th_affin_mask);
4941 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
4945 if (__kmp_env_consistency_check) {
4946 if ((mask == NULL) || (*mask == NULL)) {
4947 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4953 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4956 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4957 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4958 (kmp_affin_mask_t *)(*mask));
4960 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
4968 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4974int __kmp_aux_get_affinity_max_proc() {
4975 if (!KMP_AFFINITY_CAPABLE()) {
4978#if KMP_GROUP_AFFINITY
4979 if (__kmp_num_proc_groups > 1) {
4980 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
4986int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
4987 if (!KMP_AFFINITY_CAPABLE()) {
4993 int gtid = __kmp_entry_gtid();
4994 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4995 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4996 (kmp_affin_mask_t *)(*mask));
4997 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
4998 "affinity mask for thread %d = %s\n",
5002 if (__kmp_env_consistency_check) {
5003 if ((mask == NULL) || (*mask == NULL)) {
5004 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
5008 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5011 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5015 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
5019int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
5020 if (!KMP_AFFINITY_CAPABLE()) {
5026 int gtid = __kmp_entry_gtid();
5027 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5028 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5029 (kmp_affin_mask_t *)(*mask));
5030 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
5031 "affinity mask for thread %d = %s\n",
5035 if (__kmp_env_consistency_check) {
5036 if ((mask == NULL) || (*mask == NULL)) {
5037 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
5041 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5044 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5048 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
5052int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
5053 if (!KMP_AFFINITY_CAPABLE()) {
5059 int gtid = __kmp_entry_gtid();
5060 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5061 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5062 (kmp_affin_mask_t *)(*mask));
5063 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
5064 "affinity mask for thread %d = %s\n",
5068 if (__kmp_env_consistency_check) {
5069 if ((mask == NULL) || (*mask == NULL)) {
5070 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
5074 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5077 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5081 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
5085void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
5086 KMP_DEBUG_ASSERT(th);
5087 bool fine_gran =
true;
5088 int tid = th->th.th_info.ds.ds_tid;
5089 const char *env_var =
"KMP_AFFINITY";
5092 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
5095 switch (__kmp_affinity.gran) {
5099 if (__kmp_nThreadsPerCore > 1) {
5104 if (nCoresPerPkg > 1) {
5112 if (__kmp_topology->is_uniform()) {
5116 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
5118 int ncores = __kmp_ncores;
5119 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
5120 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
5124 int chunk = nthreads / ncores;
5126 int big_cores = nthreads % ncores;
5128 int big_nth = (chunk + 1) * big_cores;
5129 if (tid < big_nth) {
5130 coreID = tid / (chunk + 1);
5131 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5133 coreID = (tid - big_cores) / chunk;
5134 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5136 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5137 "Illegal set affinity operation when not capable");
5139 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5144 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
5145 KMP_CPU_SET(osID, mask);
5147 for (
int i = 0; i < __kmp_nth_per_core; i++) {
5149 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
5150 KMP_CPU_SET(osID, mask);
5153 if (__kmp_affinity.flags.verbose) {
5154 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5155 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5156 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5159 __kmp_affinity_get_thread_topology_info(th);
5160 __kmp_set_system_affinity(mask, TRUE);
5163 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5167 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
5168 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
5169 __kmp_aff_depth - 1, core_level);
5170 int nth_per_core = __kmp_affinity_max_proc_per_core(
5171 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5175 if (nthreads == __kmp_avail_proc) {
5177 int osID = __kmp_topology->at(tid).os_id;
5178 KMP_CPU_SET(osID, mask);
5181 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
5182 for (
int i = 0; i < __kmp_avail_proc; i++) {
5183 int osID = __kmp_topology->at(i).os_id;
5184 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
5186 KMP_CPU_SET(osID, mask);
5190 }
else if (nthreads <= ncores) {
5193 for (
int i = 0; i < ncores; i++) {
5196 for (
int j = 0; j < nth_per_core; j++) {
5197 if (procarr[i * nth_per_core + j] != -1) {
5204 for (
int j = 0; j < nth_per_core; j++) {
5205 int osID = procarr[i * nth_per_core + j];
5207 KMP_CPU_SET(osID, mask);
5223 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
5225 int *ncores_with_x_procs =
5226 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5228 int *ncores_with_x_to_max_procs =
5229 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5231 for (
int i = 0; i <= nth_per_core; i++) {
5232 ncores_with_x_procs[i] = 0;
5233 ncores_with_x_to_max_procs[i] = 0;
5236 for (
int i = 0; i < ncores; i++) {
5238 for (
int j = 0; j < nth_per_core; j++) {
5239 if (procarr[i * nth_per_core + j] != -1) {
5243 nproc_at_core[i] = cnt;
5244 ncores_with_x_procs[cnt]++;
5247 for (
int i = 0; i <= nth_per_core; i++) {
5248 for (
int j = i; j <= nth_per_core; j++) {
5249 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5254 int nproc = nth_per_core * ncores;
5256 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5257 for (
int i = 0; i < nproc; i++) {
5264 for (
int j = 1; j <= nth_per_core; j++) {
5265 int cnt = ncores_with_x_to_max_procs[j];
5266 for (
int i = 0; i < ncores; i++) {
5268 if (nproc_at_core[i] == 0) {
5271 for (
int k = 0; k < nth_per_core; k++) {
5272 if (procarr[i * nth_per_core + k] != -1) {
5273 if (newarr[i * nth_per_core + k] == 0) {
5274 newarr[i * nth_per_core + k] = 1;
5280 newarr[i * nth_per_core + k]++;
5288 if (cnt == 0 || nth == 0) {
5299 for (
int i = 0; i < nproc; i++) {
5303 int osID = procarr[i];
5304 KMP_CPU_SET(osID, mask);
5306 int coreID = i / nth_per_core;
5307 for (
int ii = 0; ii < nth_per_core; ii++) {
5308 int osID = procarr[coreID * nth_per_core + ii];
5310 KMP_CPU_SET(osID, mask);
5320 if (__kmp_affinity.flags.verbose) {
5321 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5322 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5323 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5326 __kmp_affinity_get_thread_topology_info(th);
5327 __kmp_set_system_affinity(mask, TRUE);
5331#if KMP_OS_LINUX || KMP_OS_FREEBSD
5345 kmp_set_thread_affinity_mask_initial()
5350 int gtid = __kmp_get_gtid();
5353 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5354 "non-omp thread, returning\n"));
5357 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
5358 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5359 "affinity not initialized, returning\n"));
5362 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5363 "set full mask for thread %d\n",
5365 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
5366 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
int try_open(const char *filename, const char *mode)