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
1276class kmp_affinity_raii_t {
1277 kmp_affin_mask_t *mask;
1281 kmp_affinity_raii_t() : restored(false) {
1282 KMP_CPU_ALLOC(mask);
1283 KMP_ASSERT(mask != NULL);
1284 __kmp_get_system_affinity(mask, TRUE);
1287 __kmp_set_system_affinity(mask, TRUE);
1291 ~kmp_affinity_raii_t() {
1293 __kmp_set_system_affinity(mask, TRUE);
1299bool KMPAffinity::picked_api =
false;
1301void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
1302void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
1303void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
1304void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
1305void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
1306void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
1308void KMPAffinity::pick_api() {
1309 KMPAffinity *affinity_dispatch;
1315 if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
1316 __kmp_affinity.type != affinity_disabled) {
1317 affinity_dispatch =
new KMPHwlocAffinity();
1321 affinity_dispatch =
new KMPNativeAffinity();
1323 __kmp_affinity_dispatch = affinity_dispatch;
1327void KMPAffinity::destroy_api() {
1328 if (__kmp_affinity_dispatch != NULL) {
1329 delete __kmp_affinity_dispatch;
1330 __kmp_affinity_dispatch = NULL;
1335#define KMP_ADVANCE_SCAN(scan) \
1336 while (*scan != '\0') { \
1344char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
1345 kmp_affin_mask_t *mask) {
1346 int start = 0, finish = 0, previous = 0;
1349 KMP_ASSERT(buf_len >= 40);
1352 char *end = buf + buf_len - 1;
1355 if (mask->begin() == mask->end()) {
1356 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
1357 KMP_ADVANCE_SCAN(scan);
1358 KMP_ASSERT(scan <= end);
1363 start = mask->begin();
1367 for (finish = mask->next(start), previous = start;
1368 finish == previous + 1 && finish != mask->end();
1369 finish = mask->next(finish)) {
1376 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
1377 KMP_ADVANCE_SCAN(scan);
1379 first_range =
false;
1382 if (previous - start > 1) {
1383 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
1386 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
1387 KMP_ADVANCE_SCAN(scan);
1388 if (previous - start > 0) {
1389 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
1392 KMP_ADVANCE_SCAN(scan);
1395 if (start == mask->end())
1403 KMP_ASSERT(scan <= end);
1406#undef KMP_ADVANCE_SCAN
1412kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
1413 kmp_affin_mask_t *mask) {
1414 int start = 0, finish = 0, previous = 0;
1419 __kmp_str_buf_clear(buf);
1422 if (mask->begin() == mask->end()) {
1423 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
1428 start = mask->begin();
1432 for (finish = mask->next(start), previous = start;
1433 finish == previous + 1 && finish != mask->end();
1434 finish = mask->next(finish)) {
1441 __kmp_str_buf_print(buf,
"%s",
",");
1443 first_range =
false;
1446 if (previous - start > 1) {
1447 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
1450 __kmp_str_buf_print(buf,
"%u", start);
1451 if (previous - start > 0) {
1452 __kmp_str_buf_print(buf,
",%u", previous);
1457 if (start == mask->end())
1465kmp_affin_mask_t *__kmp_affinity_get_offline_cpus() {
1466 kmp_affin_mask_t *offline;
1467 KMP_CPU_ALLOC(offline);
1468 KMP_CPU_ZERO(offline);
1470 int n, begin_cpu, end_cpu;
1472 auto skip_ws = [](FILE *f) {
1476 }
while (isspace(c));
1482 int status = offline_file.
try_open(
"/sys/devices/system/cpu/offline",
"r");
1485 while (!feof(offline_file)) {
1486 skip_ws(offline_file);
1487 n = fscanf(offline_file,
"%d", &begin_cpu);
1490 skip_ws(offline_file);
1491 int c = fgetc(offline_file);
1492 if (c == EOF || c ==
',') {
1494 end_cpu = begin_cpu;
1495 }
else if (c ==
'-') {
1497 skip_ws(offline_file);
1498 n = fscanf(offline_file,
"%d", &end_cpu);
1501 skip_ws(offline_file);
1502 c = fgetc(offline_file);
1508 if (begin_cpu < 0 || begin_cpu >= __kmp_xproc || end_cpu < 0 ||
1509 end_cpu >= __kmp_xproc || begin_cpu > end_cpu) {
1513 for (
int cpu = begin_cpu; cpu <= end_cpu; ++cpu) {
1514 KMP_CPU_SET(cpu, offline);
1522int __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
1526#if KMP_GROUP_AFFINITY
1528 if (__kmp_num_proc_groups > 1) {
1530 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
1531 for (group = 0; group < __kmp_num_proc_groups; group++) {
1533 int num = __kmp_GetActiveProcessorCount(group);
1534 for (i = 0; i < num; i++) {
1535 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1545 kmp_affin_mask_t *offline_cpus = __kmp_affinity_get_offline_cpus();
1546 for (proc = 0; proc < __kmp_xproc; proc++) {
1548 if (KMP_CPU_ISSET(proc, offline_cpus))
1550 KMP_CPU_SET(proc, mask);
1553 KMP_CPU_FREE(offline_cpus);
1562kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1564kmp_affin_mask_t *__kmp_affin_origMask = NULL;
1567static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1568#if HWLOC_API_VERSION >= 0x00020000
1569 return hwloc_obj_type_is_cache(obj->type);
1571 return obj->type == HWLOC_OBJ_CACHE;
1576static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1578 if (__kmp_hwloc_is_cache_type(obj)) {
1579 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1580 return KMP_HW_UNKNOWN;
1581 switch (obj->attr->cache.depth) {
1585#if KMP_MIC_SUPPORTED
1586 if (__kmp_mic_type == mic3) {
1594 return KMP_HW_UNKNOWN;
1597 switch (obj->type) {
1598 case HWLOC_OBJ_PACKAGE:
1599 return KMP_HW_SOCKET;
1600 case HWLOC_OBJ_NUMANODE:
1602 case HWLOC_OBJ_CORE:
1605 return KMP_HW_THREAD;
1606 case HWLOC_OBJ_GROUP:
1607 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1609 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1611 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1612 return KMP_HW_MODULE;
1613 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1614 return KMP_HW_PROC_GROUP;
1615 return KMP_HW_UNKNOWN;
1616#if HWLOC_API_VERSION >= 0x00020100
1621 return KMP_HW_UNKNOWN;
1628static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1629 hwloc_obj_type_t type) {
1632 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1633 obj->logical_index, type, 0);
1634 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1635 obj->type, first) == obj;
1636 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1645static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1646 hwloc_obj_t lower) {
1648 hwloc_obj_type_t ltype = lower->type;
1649 int lindex = lower->logical_index - 1;
1652 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1653 while (obj && lindex >= 0 &&
1654 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1655 if (obj->userdata) {
1656 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1661 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1664 lower->userdata = RCAST(
void *, sub_id + 1);
1668static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1670 int hw_thread_index, sub_id;
1672 hwloc_obj_t pu, obj, root, prev;
1673 kmp_hw_t types[KMP_HW_LAST];
1674 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1676 hwloc_topology_t tp = __kmp_hwloc_topology;
1677 *msg_id = kmp_i18n_null;
1678 if (__kmp_affinity.flags.verbose) {
1679 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1682 if (!KMP_AFFINITY_CAPABLE()) {
1685 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1687 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1689 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1692 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1694 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1696 __kmp_nThreadsPerCore = 1;
1697 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1698 if (nCoresPerPkg == 0)
1700 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1705 int nr_cpu_kinds = hwloc_cpukinds_get_nr(tp, 0);
1707 typedef struct kmp_hwloc_cpukinds_info_t {
1709 kmp_hw_core_type_t core_type;
1710 hwloc_bitmap_t mask;
1711 } kmp_hwloc_cpukinds_info_t;
1712 kmp_hwloc_cpukinds_info_t *cpukinds =
nullptr;
1714 if (nr_cpu_kinds > 0) {
1716 struct hwloc_info_s *infos;
1717 cpukinds = (kmp_hwloc_cpukinds_info_t *)__kmp_allocate(
1718 sizeof(kmp_hwloc_cpukinds_info_t) * nr_cpu_kinds);
1719 for (
unsigned idx = 0; idx < (unsigned)nr_cpu_kinds; ++idx) {
1720 cpukinds[idx].efficiency = -1;
1721 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_UNKNOWN;
1722 cpukinds[idx].mask = hwloc_bitmap_alloc();
1723 if (hwloc_cpukinds_get_info(tp, idx, cpukinds[idx].mask,
1724 &cpukinds[idx].efficiency, &nr_infos, &infos,
1726 for (
unsigned i = 0; i < nr_infos; ++i) {
1727 if (__kmp_str_match(
"CoreType", 8, infos[i].name)) {
1728#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1729 if (__kmp_str_match(
"IntelAtom", 9, infos[i].value)) {
1730 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_ATOM;
1732 }
else if (__kmp_str_match(
"IntelCore", 9, infos[i].value)) {
1733 cpukinds[idx].core_type = KMP_HW_CORE_TYPE_CORE;
1743 root = hwloc_get_root_obj(tp);
1747 pu = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1750 types[depth] = KMP_HW_THREAD;
1751 hwloc_types[depth] = obj->type;
1753 while (obj != root && obj != NULL) {
1755#if HWLOC_API_VERSION >= 0x00020000
1756 if (obj->memory_arity) {
1758 for (memory = obj->memory_first_child; memory;
1759 memory = hwloc_get_next_child(tp, obj, memory)) {
1760 if (memory->type == HWLOC_OBJ_NUMANODE)
1763 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1764 types[depth] = KMP_HW_NUMA;
1765 hwloc_types[depth] = memory->type;
1770 type = __kmp_hwloc_type_2_topology_type(obj);
1771 if (type != KMP_HW_UNKNOWN) {
1772 types[depth] = type;
1773 hwloc_types[depth] = obj->type;
1777 KMP_ASSERT(depth > 0);
1780 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1781 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1782 kmp_hw_t temp = types[i];
1783 types[i] = types[j];
1785 hwloc_types[i] = hwloc_types[j];
1786 hwloc_types[j] = hwloc_temp;
1790 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1792 hw_thread_index = 0;
1794 while ((pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu))) {
1795 int index = depth - 1;
1796 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1797 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1800 hw_thread.ids[index] = pu->logical_index;
1801 hw_thread.os_id = pu->os_index;
1804 int cpukind_index = -1;
1805 for (
int i = 0; i < nr_cpu_kinds; ++i) {
1806 if (hwloc_bitmap_isset(cpukinds[i].mask, hw_thread.os_id)) {
1811 if (cpukind_index >= 0) {
1812 hw_thread.attrs.set_core_type(cpukinds[cpukind_index].core_type);
1813 hw_thread.attrs.set_core_eff(cpukinds[cpukind_index].efficiency);
1820 while (obj != root && obj != NULL) {
1822#if HWLOC_API_VERSION >= 0x00020000
1826 if (obj->memory_arity) {
1828 for (memory = obj->memory_first_child; memory;
1829 memory = hwloc_get_next_child(tp, obj, memory)) {
1830 if (memory->type == HWLOC_OBJ_NUMANODE)
1833 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1834 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1836 hw_thread.ids[index] = memory->logical_index;
1837 hw_thread.ids[index + 1] = sub_id;
1845 type = __kmp_hwloc_type_2_topology_type(obj);
1846 if (type != KMP_HW_UNKNOWN) {
1847 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1849 hw_thread.ids[index] = obj->logical_index;
1850 hw_thread.ids[index + 1] = sub_id;
1862 for (
int idx = 0; idx < nr_cpu_kinds; ++idx)
1863 hwloc_bitmap_free(cpukinds[idx].mask);
1864 __kmp_free(cpukinds);
1866 __kmp_topology->sort_ids();
1874static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
1875 *msg_id = kmp_i18n_null;
1877 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1879 if (__kmp_affinity.flags.verbose) {
1880 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
1886 if (!KMP_AFFINITY_CAPABLE()) {
1887 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1888 __kmp_ncores = nPackages = __kmp_xproc;
1889 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1897 __kmp_ncores = nPackages = __kmp_avail_proc;
1898 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1901 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1904 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1906 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1909 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
1911 hw_thread.os_id = i;
1912 hw_thread.ids[0] = i;
1913 hw_thread.ids[1] = 0;
1914 hw_thread.ids[2] = 0;
1917 if (__kmp_affinity.flags.verbose) {
1918 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
1923#if KMP_GROUP_AFFINITY
1928static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
1929 *msg_id = kmp_i18n_null;
1931 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
1932 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
1934 if (__kmp_affinity.flags.verbose) {
1935 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
1939 if (!KMP_AFFINITY_CAPABLE()) {
1940 KMP_ASSERT(__kmp_affinity.type == affinity_none);
1941 nPackages = __kmp_num_proc_groups;
1942 __kmp_nThreadsPerCore = 1;
1943 __kmp_ncores = __kmp_xproc;
1944 nCoresPerPkg = nPackages / __kmp_ncores;
1949 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1952 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1954 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1957 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct++);
1959 hw_thread.os_id = i;
1960 hw_thread.ids[0] = i / BITS_PER_GROUP;
1961 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
1967#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1969template <kmp_u
int32 LSB, kmp_u
int32 MSB>
1970static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
1971 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
1972 const kmp_uint32 SHIFT_RIGHT = LSB;
1973 kmp_uint32 retval = v;
1974 retval <<= SHIFT_LEFT;
1975 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
1979static int __kmp_cpuid_mask_width(
int count) {
1982 while ((1 << r) < count)
1987class apicThreadInfo {
1991 unsigned maxCoresPerPkg;
1992 unsigned maxThreadsPerPkg;
1998static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
2000 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
2001 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
2002 if (aa->pkgId < bb->pkgId)
2004 if (aa->pkgId > bb->pkgId)
2006 if (aa->coreId < bb->coreId)
2008 if (aa->coreId > bb->coreId)
2010 if (aa->threadId < bb->threadId)
2012 if (aa->threadId > bb->threadId)
2017class kmp_cache_info_t {
2020 unsigned level, mask;
2022 kmp_cache_info_t() : depth(0) { get_leaf4_levels(); }
2023 size_t get_depth()
const {
return depth; }
2024 info_t &operator[](
size_t index) {
return table[index]; }
2025 const info_t &operator[](
size_t index)
const {
return table[index]; }
2027 static kmp_hw_t get_topology_type(
unsigned level) {
2028 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
2037 return KMP_HW_UNKNOWN;
2041 static const int MAX_CACHE_LEVEL = 3;
2044 info_t table[MAX_CACHE_LEVEL];
2046 void get_leaf4_levels() {
2048 while (depth < MAX_CACHE_LEVEL) {
2049 unsigned cache_type, max_threads_sharing;
2050 unsigned cache_level, cache_mask_width;
2052 __kmp_x86_cpuid(4, level, &buf2);
2053 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
2057 if (cache_type == 2) {
2061 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
2062 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
2063 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
2064 table[depth].level = cache_level;
2065 table[depth].mask = ((-1) << cache_mask_width);
2076static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
2078 *msg_id = kmp_i18n_null;
2080 if (__kmp_affinity.flags.verbose) {
2081 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
2085 __kmp_x86_cpuid(0, 0, &buf);
2087 *msg_id = kmp_i18n_str_NoLeaf4Support;
2096 if (!KMP_AFFINITY_CAPABLE()) {
2099 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2105 __kmp_x86_cpuid(1, 0, &buf);
2106 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2107 if (maxThreadsPerPkg == 0) {
2108 maxThreadsPerPkg = 1;
2122 __kmp_x86_cpuid(0, 0, &buf);
2124 __kmp_x86_cpuid(4, 0, &buf);
2125 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2143 __kmp_ncores = __kmp_xproc;
2144 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2145 __kmp_nThreadsPerCore = 1;
2154 kmp_affinity_raii_t previous_affinity;
2182 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
2183 __kmp_avail_proc *
sizeof(apicThreadInfo));
2184 unsigned nApics = 0;
2185 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
2187 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
2190 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
2192 __kmp_affinity_dispatch->bind_thread(i);
2193 threadInfo[nApics].osId = i;
2196 __kmp_x86_cpuid(1, 0, &buf);
2197 if (((buf.edx >> 9) & 1) == 0) {
2198 __kmp_free(threadInfo);
2199 *msg_id = kmp_i18n_str_ApicNotPresent;
2202 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
2203 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
2204 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
2205 threadInfo[nApics].maxThreadsPerPkg = 1;
2214 __kmp_x86_cpuid(0, 0, &buf);
2216 __kmp_x86_cpuid(4, 0, &buf);
2217 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
2219 threadInfo[nApics].maxCoresPerPkg = 1;
2223 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
2224 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
2226 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
2227 int widthT = widthCT - widthC;
2232 __kmp_free(threadInfo);
2233 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2237 int maskC = (1 << widthC) - 1;
2238 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
2240 int maskT = (1 << widthT) - 1;
2241 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
2248 previous_affinity.restore();
2251 qsort(threadInfo, nApics,
sizeof(*threadInfo),
2252 __kmp_affinity_cmp_apicThreadInfo_phys_id);
2269 __kmp_nThreadsPerCore = 1;
2270 unsigned nCores = 1;
2273 unsigned lastPkgId = threadInfo[0].pkgId;
2274 unsigned coreCt = 1;
2275 unsigned lastCoreId = threadInfo[0].coreId;
2276 unsigned threadCt = 1;
2277 unsigned lastThreadId = threadInfo[0].threadId;
2280 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
2281 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
2283 for (i = 1; i < nApics; i++) {
2284 if (threadInfo[i].pkgId != lastPkgId) {
2287 lastPkgId = threadInfo[i].pkgId;
2288 if ((
int)coreCt > nCoresPerPkg)
2289 nCoresPerPkg = coreCt;
2291 lastCoreId = threadInfo[i].coreId;
2292 if ((
int)threadCt > __kmp_nThreadsPerCore)
2293 __kmp_nThreadsPerCore = threadCt;
2295 lastThreadId = threadInfo[i].threadId;
2299 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
2300 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
2304 if (threadInfo[i].coreId != lastCoreId) {
2307 lastCoreId = threadInfo[i].coreId;
2308 if ((
int)threadCt > __kmp_nThreadsPerCore)
2309 __kmp_nThreadsPerCore = threadCt;
2311 lastThreadId = threadInfo[i].threadId;
2312 }
else if (threadInfo[i].threadId != lastThreadId) {
2314 lastThreadId = threadInfo[i].threadId;
2316 __kmp_free(threadInfo);
2317 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2323 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
2324 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
2325 __kmp_free(threadInfo);
2326 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
2334 if ((
int)coreCt > nCoresPerPkg)
2335 nCoresPerPkg = coreCt;
2336 if ((
int)threadCt > __kmp_nThreadsPerCore)
2337 __kmp_nThreadsPerCore = threadCt;
2338 __kmp_ncores = nCores;
2339 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
2347 int threadLevel = 2;
2349 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
2352 types[idx++] = KMP_HW_SOCKET;
2354 types[idx++] = KMP_HW_CORE;
2355 if (threadLevel >= 0)
2356 types[idx++] = KMP_HW_THREAD;
2358 KMP_ASSERT(depth > 0);
2359 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
2361 for (i = 0; i < nApics; ++i) {
2363 unsigned os = threadInfo[i].osId;
2364 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2367 if (pkgLevel >= 0) {
2368 hw_thread.ids[idx++] = threadInfo[i].pkgId;
2370 if (coreLevel >= 0) {
2371 hw_thread.ids[idx++] = threadInfo[i].coreId;
2373 if (threadLevel >= 0) {
2374 hw_thread.ids[idx++] = threadInfo[i].threadId;
2376 hw_thread.os_id = os;
2379 __kmp_free(threadInfo);
2380 __kmp_topology->sort_ids();
2381 if (!__kmp_topology->check_ids()) {
2382 kmp_topology_t::deallocate(__kmp_topology);
2383 __kmp_topology =
nullptr;
2384 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
2392static void __kmp_get_hybrid_info(kmp_hw_core_type_t *type,
int *efficiency,
2393 unsigned *native_model_id) {
2395 __kmp_x86_cpuid(0x1a, 0, &buf);
2396 *type = (kmp_hw_core_type_t)__kmp_extract_bits<24, 31>(buf.eax);
2398 case KMP_HW_CORE_TYPE_ATOM:
2401 case KMP_HW_CORE_TYPE_CORE:
2407 *native_model_id = __kmp_extract_bits<0, 23>(buf.eax);
2429 INTEL_LEVEL_TYPE_INVALID = 0,
2430 INTEL_LEVEL_TYPE_SMT = 1,
2431 INTEL_LEVEL_TYPE_CORE = 2,
2432 INTEL_LEVEL_TYPE_TILE = 3,
2433 INTEL_LEVEL_TYPE_MODULE = 4,
2434 INTEL_LEVEL_TYPE_DIE = 5,
2435 INTEL_LEVEL_TYPE_LAST = 6,
2438struct cpuid_level_info_t {
2439 unsigned level_type, mask, mask_width, nitems, cache_mask;
2442static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
2443 switch (intel_type) {
2444 case INTEL_LEVEL_TYPE_INVALID:
2445 return KMP_HW_SOCKET;
2446 case INTEL_LEVEL_TYPE_SMT:
2447 return KMP_HW_THREAD;
2448 case INTEL_LEVEL_TYPE_CORE:
2450 case INTEL_LEVEL_TYPE_TILE:
2452 case INTEL_LEVEL_TYPE_MODULE:
2453 return KMP_HW_MODULE;
2454 case INTEL_LEVEL_TYPE_DIE:
2457 return KMP_HW_UNKNOWN;
2464__kmp_x2apicid_get_levels(
int leaf,
2465 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST],
2466 kmp_uint64 known_levels) {
2467 unsigned level, levels_index;
2468 unsigned level_type, mask_width, nitems;
2478 level = levels_index = 0;
2480 __kmp_x86_cpuid(leaf, level, &buf);
2481 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
2482 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
2483 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
2484 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0)
2487 if (known_levels & (1ull << level_type)) {
2489 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
2490 levels[levels_index].level_type = level_type;
2491 levels[levels_index].mask_width = mask_width;
2492 levels[levels_index].nitems = nitems;
2496 if (levels_index > 0) {
2497 levels[levels_index - 1].mask_width = mask_width;
2498 levels[levels_index - 1].nitems = nitems;
2502 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
2505 for (
unsigned i = 0; i < levels_index; ++i) {
2506 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
2507 levels[i].mask = ~((-1) << levels[i].mask_width);
2508 levels[i].cache_mask = (-1) << levels[i].mask_width;
2509 for (
unsigned j = 0; j < i; ++j)
2510 levels[i].mask ^= levels[j].mask;
2512 KMP_DEBUG_ASSERT(levels_index > 0);
2513 levels[i].mask = (-1) << levels[i - 1].mask_width;
2514 levels[i].cache_mask = 0;
2517 return levels_index;
2520static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
2522 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
2523 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
2524 unsigned levels_index;
2526 kmp_uint64 known_levels;
2527 int topology_leaf, highest_leaf, apic_id;
2529 static int leaves[] = {0, 0};
2531 kmp_i18n_id_t leaf_message_id;
2533 KMP_BUILD_ASSERT(
sizeof(known_levels) * CHAR_BIT > KMP_HW_LAST);
2535 *msg_id = kmp_i18n_null;
2536 if (__kmp_affinity.flags.verbose) {
2537 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
2541 known_levels = 0ull;
2542 for (
int i = 0; i < INTEL_LEVEL_TYPE_LAST; ++i) {
2543 if (__kmp_intel_type_2_topology_type(i) != KMP_HW_UNKNOWN) {
2544 known_levels |= (1ull << i);
2549 __kmp_x86_cpuid(0, 0, &buf);
2550 highest_leaf = buf.eax;
2555 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
2558 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2559 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
2562 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
2567 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
2571 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2573 for (
int i = 0; i < num_leaves; ++i) {
2574 int leaf = leaves[i];
2575 if (highest_leaf < leaf)
2577 __kmp_x86_cpuid(leaf, 0, &buf);
2580 topology_leaf = leaf;
2581 levels_index = __kmp_x2apicid_get_levels(leaf, levels, known_levels);
2582 if (levels_index == 0)
2586 if (topology_leaf == -1 || levels_index == 0) {
2587 *msg_id = leaf_message_id;
2590 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
2597 if (!KMP_AFFINITY_CAPABLE()) {
2600 KMP_ASSERT(__kmp_affinity.type == affinity_none);
2601 for (
unsigned i = 0; i < levels_index; ++i) {
2602 if (levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
2603 __kmp_nThreadsPerCore = levels[i].nitems;
2604 }
else if (levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
2605 nCoresPerPkg = levels[i].nitems;
2608 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
2609 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
2614 int depth = levels_index;
2615 for (
int i = depth - 1, j = 0; i >= 0; --i, ++j)
2616 types[j] = __kmp_intel_type_2_topology_type(levels[i].level_type);
2618 kmp_topology_t::allocate(__kmp_avail_proc, levels_index, types);
2621 kmp_cache_info_t cache_info;
2622 for (
size_t i = 0; i < cache_info.get_depth(); ++i) {
2623 const kmp_cache_info_t::info_t &info = cache_info[i];
2624 unsigned cache_mask = info.mask;
2625 unsigned cache_level = info.level;
2626 for (
unsigned j = 0; j < levels_index; ++j) {
2627 unsigned hw_cache_mask = levels[j].cache_mask;
2628 kmp_hw_t cache_type = kmp_cache_info_t::get_topology_type(cache_level);
2629 if (hw_cache_mask == cache_mask && j < levels_index - 1) {
2631 __kmp_intel_type_2_topology_type(levels[j + 1].level_type);
2632 __kmp_topology->set_equivalent_type(cache_type, type);
2642 kmp_affinity_raii_t previous_affinity;
2647 int hw_thread_index = 0;
2648 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2649 cpuid_level_info_t my_levels[INTEL_LEVEL_TYPE_LAST];
2650 unsigned my_levels_index;
2653 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2656 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2658 __kmp_affinity_dispatch->bind_thread(proc);
2661 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2663 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
2665 __kmp_x2apicid_get_levels(topology_leaf, my_levels, known_levels);
2666 if (my_levels_index == 0 || my_levels_index != levels_index) {
2667 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2671 hw_thread.os_id = proc;
2673 for (
unsigned j = 0, idx = depth - 1; j < my_levels_index; ++j, --idx) {
2674 hw_thread.ids[idx] = apic_id & my_levels[j].mask;
2676 hw_thread.ids[idx] >>= my_levels[j - 1].mask_width;
2680 if (__kmp_is_hybrid_cpu() && highest_leaf >= 0x1a) {
2681 kmp_hw_core_type_t type;
2682 unsigned native_model_id;
2684 __kmp_get_hybrid_info(&type, &efficiency, &native_model_id);
2685 hw_thread.attrs.set_core_type(type);
2686 hw_thread.attrs.set_core_eff(efficiency);
2690 KMP_ASSERT(hw_thread_index > 0);
2691 __kmp_topology->sort_ids();
2692 if (!__kmp_topology->check_ids()) {
2693 kmp_topology_t::deallocate(__kmp_topology);
2694 __kmp_topology =
nullptr;
2695 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
2703#define threadIdIndex 1
2704#define coreIdIndex 2
2706#define nodeIdIndex 4
2708typedef unsigned *ProcCpuInfo;
2709static unsigned maxIndex = pkgIdIndex;
2711static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
2714 const unsigned *aa = *(
unsigned *
const *)a;
2715 const unsigned *bb = *(
unsigned *
const *)b;
2716 for (i = maxIndex;; i--) {
2727#if KMP_USE_HIER_SCHED
2729static void __kmp_dispatch_set_hierarchy_values() {
2735 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
2736 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2737 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
2738#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2740 if (__kmp_mic_type >= mic3)
2741 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
2744 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
2745 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
2746 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
2747 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
2750 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
2751 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
2752 __kmp_nThreadsPerCore;
2753#if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2755 if (__kmp_mic_type >= mic3)
2756 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2757 2 * __kmp_nThreadsPerCore;
2760 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2761 __kmp_nThreadsPerCore;
2762 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
2763 nCoresPerPkg * __kmp_nThreadsPerCore;
2764 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
2765 nCoresPerPkg * __kmp_nThreadsPerCore;
2766 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
2767 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2772int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
2773 int index = type + 1;
2774 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
2775 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
2776 if (type == kmp_hier_layer_e::LAYER_THREAD)
2778 else if (type == kmp_hier_layer_e::LAYER_LOOP)
2780 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
2781 if (tid >= num_hw_threads)
2782 tid = tid % num_hw_threads;
2783 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
2787int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
2790 KMP_DEBUG_ASSERT(i1 <= i2);
2791 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
2792 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
2793 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
2795 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
2799static inline const char *__kmp_cpuinfo_get_filename() {
2800 const char *filename;
2801 if (__kmp_cpuinfo_file !=
nullptr)
2802 filename = __kmp_cpuinfo_file;
2804 filename =
"/proc/cpuinfo";
2808static inline const char *__kmp_cpuinfo_get_envvar() {
2809 const char *envvar =
nullptr;
2810 if (__kmp_cpuinfo_file !=
nullptr)
2811 envvar =
"KMP_CPUINFO_FILE";
2817static bool __kmp_affinity_create_cpuinfo_map(
int *line,
2818 kmp_i18n_id_t *
const msg_id) {
2819 const char *filename = __kmp_cpuinfo_get_filename();
2820 const char *envvar = __kmp_cpuinfo_get_envvar();
2821 *msg_id = kmp_i18n_null;
2823 if (__kmp_affinity.flags.verbose) {
2824 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
2832 unsigned num_records = 0;
2834 buf[
sizeof(buf) - 1] = 1;
2835 if (!fgets(buf,
sizeof(buf), f)) {
2840 char s1[] =
"processor";
2841 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2848 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
2850 if (level > (
unsigned)__kmp_xproc) {
2851 level = __kmp_xproc;
2853 if (nodeIdIndex + level >= maxIndex) {
2854 maxIndex = nodeIdIndex + level;
2862 if (num_records == 0) {
2863 *msg_id = kmp_i18n_str_NoProcRecords;
2866 if (num_records > (
unsigned)__kmp_xproc) {
2867 *msg_id = kmp_i18n_str_TooManyProcRecords;
2876 if (fseek(f, 0, SEEK_SET) != 0) {
2877 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
2883 unsigned **threadInfo =
2884 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
2886 for (i = 0; i <= num_records; i++) {
2888 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2891#define CLEANUP_THREAD_INFO \
2892 for (i = 0; i <= num_records; i++) { \
2893 __kmp_free(threadInfo[i]); \
2895 __kmp_free(threadInfo);
2900#define INIT_PROC_INFO(p) \
2901 for (__index = 0; __index <= maxIndex; __index++) { \
2902 (p)[__index] = UINT_MAX; \
2905 for (i = 0; i <= num_records; i++) {
2906 INIT_PROC_INFO(threadInfo[i]);
2909 unsigned num_avail = 0;
2916 buf[
sizeof(buf) - 1] = 1;
2917 bool long_line =
false;
2918 if (!fgets(buf,
sizeof(buf), f)) {
2923 for (i = 0; i <= maxIndex; i++) {
2924 if (threadInfo[num_avail][i] != UINT_MAX) {
2932 }
else if (!buf[
sizeof(buf) - 1]) {
2939 CLEANUP_THREAD_INFO; \
2940 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
2946 char s1[] =
"processor";
2947 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2949 char *p = strchr(buf +
sizeof(s1) - 1,
':');
2951 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2953 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
2963 threadInfo[num_avail][osIdIndex] = val;
2964#if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
2968 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
2969 threadInfo[num_avail][osIdIndex]);
2970 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
2972 KMP_SNPRINTF(path,
sizeof(path),
2973 "/sys/devices/system/cpu/cpu%u/topology/core_id",
2974 threadInfo[num_avail][osIdIndex]);
2975 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
2979 char s2[] =
"physical id";
2980 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
2982 char *p = strchr(buf +
sizeof(s2) - 1,
':');
2984 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2986 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
2988 threadInfo[num_avail][pkgIdIndex] = val;
2991 char s3[] =
"core id";
2992 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
2994 char *p = strchr(buf +
sizeof(s3) - 1,
':');
2996 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2998 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
3000 threadInfo[num_avail][coreIdIndex] = val;
3004 char s4[] =
"thread id";
3005 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
3007 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3009 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3011 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
3013 threadInfo[num_avail][threadIdIndex] = val;
3017 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
3019 char *p = strchr(buf +
sizeof(s4) - 1,
':');
3021 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
3024 if (level > (
unsigned)__kmp_xproc) {
3025 level = __kmp_xproc;
3027 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
3029 threadInfo[num_avail][nodeIdIndex + level] = val;
3036 if ((*buf != 0) && (*buf !=
'\n')) {
3041 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
3049 if ((
int)num_avail == __kmp_xproc) {
3050 CLEANUP_THREAD_INFO;
3051 *msg_id = kmp_i18n_str_TooManyEntries;
3057 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
3058 CLEANUP_THREAD_INFO;
3059 *msg_id = kmp_i18n_str_MissingProcField;
3062 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
3063 CLEANUP_THREAD_INFO;
3064 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
3069 if (KMP_AFFINITY_CAPABLE() &&
3070 !KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
3071 __kmp_affin_fullMask)) {
3072 INIT_PROC_INFO(threadInfo[num_avail]);
3079 KMP_ASSERT(num_avail <= num_records);
3080 INIT_PROC_INFO(threadInfo[num_avail]);
3085 CLEANUP_THREAD_INFO;
3086 *msg_id = kmp_i18n_str_MissingValCpuinfo;
3090 CLEANUP_THREAD_INFO;
3091 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
3096#if KMP_MIC && REDUCE_TEAM_SIZE
3097 unsigned teamSize = 0;
3105 KMP_ASSERT(num_avail > 0);
3106 KMP_ASSERT(num_avail <= num_records);
3109 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
3110 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
3122 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3124 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3126 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3128 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
3130 bool assign_thread_ids =
false;
3131 unsigned threadIdCt;
3138 if (assign_thread_ids) {
3139 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
3140 threadInfo[0][threadIdIndex] = threadIdCt++;
3141 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
3142 threadIdCt = threadInfo[0][threadIdIndex] + 1;
3145 for (index = 0; index <= maxIndex; index++) {
3149 lastId[index] = threadInfo[0][index];
3154 for (i = 1; i < num_avail; i++) {
3157 for (index = maxIndex; index >= threadIdIndex; index--) {
3158 if (assign_thread_ids && (index == threadIdIndex)) {
3160 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3161 threadInfo[i][threadIdIndex] = threadIdCt++;
3165 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3166 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3169 if (threadInfo[i][index] != lastId[index]) {
3174 for (index2 = threadIdIndex; index2 < index; index2++) {
3176 if (counts[index2] > maxCt[index2]) {
3177 maxCt[index2] = counts[index2];
3180 lastId[index2] = threadInfo[i][index2];
3184 lastId[index] = threadInfo[i][index];
3186 if (assign_thread_ids && (index > threadIdIndex)) {
3188#if KMP_MIC && REDUCE_TEAM_SIZE
3191 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3198 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
3199 threadInfo[i][threadIdIndex] = threadIdCt++;
3205 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
3206 threadIdCt = threadInfo[i][threadIdIndex] + 1;
3212 if (index < threadIdIndex) {
3216 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
3221 CLEANUP_THREAD_INFO;
3222 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3228 assign_thread_ids =
true;
3229 goto restart_radix_check;
3233#if KMP_MIC && REDUCE_TEAM_SIZE
3236 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
3239 for (index = threadIdIndex; index <= maxIndex; index++) {
3240 if (counts[index] > maxCt[index]) {
3241 maxCt[index] = counts[index];
3245 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
3246 nCoresPerPkg = maxCt[coreIdIndex];
3247 nPackages = totals[pkgIdIndex];
3253 __kmp_ncores = totals[coreIdIndex];
3254 if (!KMP_AFFINITY_CAPABLE()) {
3255 KMP_ASSERT(__kmp_affinity.type == affinity_none);
3259#if KMP_MIC && REDUCE_TEAM_SIZE
3261 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
3262 __kmp_dflt_team_nth = teamSize;
3263 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
3264 "__kmp_dflt_team_nth = %d\n",
3265 __kmp_dflt_team_nth));
3269 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
3276 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
3277 for (index = threadIdIndex; index < maxIndex; index++) {
3278 KMP_ASSERT(totals[index] >= totals[index + 1]);
3279 inMap[index] = (totals[index] > totals[index + 1]);
3281 inMap[maxIndex] = (totals[maxIndex] > 1);
3282 inMap[pkgIdIndex] =
true;
3283 inMap[coreIdIndex] =
true;
3284 inMap[threadIdIndex] =
true;
3288 kmp_hw_t types[KMP_HW_LAST];
3291 int threadLevel = -1;
3292 for (index = threadIdIndex; index <= maxIndex; index++) {
3297 if (inMap[pkgIdIndex]) {
3299 types[idx++] = KMP_HW_SOCKET;
3301 if (inMap[coreIdIndex]) {
3303 types[idx++] = KMP_HW_CORE;
3305 if (inMap[threadIdIndex]) {
3307 types[idx++] = KMP_HW_THREAD;
3309 KMP_ASSERT(depth > 0);
3312 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
3314 for (i = 0; i < num_avail; ++i) {
3315 unsigned os = threadInfo[i][osIdIndex];
3317 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3319 hw_thread.os_id = os;
3322 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
3323 if (!inMap[src_index]) {
3326 if (src_index == pkgIdIndex) {
3327 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
3328 }
else if (src_index == coreIdIndex) {
3329 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
3330 }
else if (src_index == threadIdIndex) {
3331 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
3341 CLEANUP_THREAD_INFO;
3342 __kmp_topology->sort_ids();
3343 if (!__kmp_topology->check_ids()) {
3344 kmp_topology_t::deallocate(__kmp_topology);
3345 __kmp_topology =
nullptr;
3346 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
3355static void __kmp_create_os_id_masks(
unsigned *numUnique,
3356 kmp_affinity_t &affinity) {
3360 int numAddrs = __kmp_topology->get_num_hw_threads();
3361 int depth = __kmp_topology->get_depth();
3362 const char *env_var = affinity.env_var;
3363 KMP_ASSERT(numAddrs);
3367 for (i = numAddrs - 1;; --i) {
3368 int osId = __kmp_topology->at(i).os_id;
3369 if (osId > maxOsId) {
3375 affinity.num_os_id_masks = maxOsId + 1;
3376 KMP_CPU_ALLOC_ARRAY(affinity.os_id_masks, affinity.num_os_id_masks);
3377 KMP_ASSERT(affinity.gran_levels >= 0);
3378 if (affinity.flags.verbose && (affinity.gran_levels > 0)) {
3379 KMP_INFORM(ThreadsMigrate, env_var, affinity.gran_levels);
3381 if (affinity.gran_levels >= (
int)depth) {
3382 KMP_AFF_WARNING(affinity, AffThreadsMayMigrate);
3392 kmp_affin_mask_t *sum;
3393 KMP_CPU_ALLOC_ON_STACK(sum);
3395 KMP_CPU_SET(__kmp_topology->at(0).os_id, sum);
3396 for (i = 1; i < numAddrs; i++) {
3400 if (__kmp_topology->is_close(leader, i, affinity.gran_levels)) {
3401 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3407 for (; j < i; j++) {
3408 int osId = __kmp_topology->at(j).os_id;
3409 KMP_DEBUG_ASSERT(osId <= maxOsId);
3410 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3411 KMP_CPU_COPY(mask, sum);
3412 __kmp_topology->at(j).leader = (j == leader);
3419 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
3424 for (; j < i; j++) {
3425 int osId = __kmp_topology->at(j).os_id;
3426 KMP_DEBUG_ASSERT(osId <= maxOsId);
3427 kmp_affin_mask_t *mask = KMP_CPU_INDEX(affinity.os_id_masks, osId);
3428 KMP_CPU_COPY(mask, sum);
3429 __kmp_topology->at(j).leader = (j == leader);
3432 KMP_CPU_FREE_FROM_STACK(sum);
3434 *numUnique = unique;
3440static kmp_affin_mask_t *newMasks;
3441static int numNewMasks;
3442static int nextNewMask;
3444#define ADD_MASK(_mask) \
3446 if (nextNewMask >= numNewMasks) { \
3449 kmp_affin_mask_t *temp; \
3450 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
3451 for (i = 0; i < numNewMasks / 2; i++) { \
3452 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
3453 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
3454 KMP_CPU_COPY(dest, src); \
3456 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
3459 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
3463#define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
3465 if (((_osId) > _maxOsId) || \
3466 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
3467 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, _osId); \
3469 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
3475static void __kmp_affinity_process_proclist(kmp_affinity_t &affinity) {
3477 kmp_affin_mask_t **out_masks = &affinity.masks;
3478 unsigned *out_numMasks = &affinity.num_masks;
3479 const char *proclist = affinity.proclist;
3480 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3481 int maxOsId = affinity.num_os_id_masks - 1;
3482 const char *scan = proclist;
3483 const char *next = proclist;
3488 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3490 kmp_affin_mask_t *sumMask;
3491 KMP_CPU_ALLOC(sumMask);
3495 int start, end, stride;
3499 if (*next ==
'\0') {
3511 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
3513 num = __kmp_str_to_int(scan, *next);
3514 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3517 if ((num > maxOsId) ||
3518 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3519 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3520 KMP_CPU_ZERO(sumMask);
3522 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3542 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3545 num = __kmp_str_to_int(scan, *next);
3546 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
3549 if ((num > maxOsId) ||
3550 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3551 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3553 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
3570 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3572 start = __kmp_str_to_int(scan, *next);
3573 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
3578 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3592 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3594 end = __kmp_str_to_int(scan, *next);
3595 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
3612 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
3614 stride = __kmp_str_to_int(scan, *next);
3615 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
3620 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
3622 KMP_ASSERT2(start <= end,
"bad explicit proc list");
3624 KMP_ASSERT2(start >= end,
"bad explicit proc list");
3626 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
3631 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3633 }
while (start <= end);
3636 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3638 }
while (start >= end);
3649 *out_numMasks = nextNewMask;
3650 if (nextNewMask == 0) {
3652 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3655 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3656 for (i = 0; i < nextNewMask; i++) {
3657 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3658 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3659 KMP_CPU_COPY(dest, src);
3661 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3662 KMP_CPU_FREE(sumMask);
3685static void __kmp_process_subplace_list(
const char **scan,
3686 kmp_affinity_t &affinity,
int maxOsId,
3687 kmp_affin_mask_t *tempMask,
3690 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3693 int start, count, stride, i;
3697 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3700 start = __kmp_str_to_int(*scan, *next);
3701 KMP_ASSERT(start >= 0);
3706 if (**scan ==
'}' || **scan ==
',') {
3707 if ((start > maxOsId) ||
3708 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3709 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3711 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3714 if (**scan ==
'}') {
3720 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3725 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3728 count = __kmp_str_to_int(*scan, *next);
3729 KMP_ASSERT(count >= 0);
3734 if (**scan ==
'}' || **scan ==
',') {
3735 for (i = 0; i < count; i++) {
3736 if ((start > maxOsId) ||
3737 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3738 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3741 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3746 if (**scan ==
'}') {
3752 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3759 if (**scan ==
'+') {
3763 if (**scan ==
'-') {
3771 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3774 stride = __kmp_str_to_int(*scan, *next);
3775 KMP_ASSERT(stride >= 0);
3781 if (**scan ==
'}' || **scan ==
',') {
3782 for (i = 0; i < count; i++) {
3783 if ((start > maxOsId) ||
3784 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3785 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, start);
3788 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3793 if (**scan ==
'}') {
3800 KMP_ASSERT2(0,
"bad explicit places list");
3804static void __kmp_process_place(
const char **scan, kmp_affinity_t &affinity,
3805 int maxOsId, kmp_affin_mask_t *tempMask,
3808 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3812 if (**scan ==
'{') {
3814 __kmp_process_subplace_list(scan, affinity, maxOsId, tempMask, setSize);
3815 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3817 }
else if (**scan ==
'!') {
3819 __kmp_process_place(scan, affinity, maxOsId, tempMask, setSize);
3820 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
3821 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
3824 int num = __kmp_str_to_int(*scan, *next);
3825 KMP_ASSERT(num >= 0);
3826 if ((num > maxOsId) ||
3827 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3828 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, num);
3830 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3835 KMP_ASSERT2(0,
"bad explicit places list");
3840void __kmp_affinity_process_placelist(kmp_affinity_t &affinity) {
3841 int i, j, count, stride, sign;
3842 kmp_affin_mask_t **out_masks = &affinity.masks;
3843 unsigned *out_numMasks = &affinity.num_masks;
3844 const char *placelist = affinity.proclist;
3845 kmp_affin_mask_t *osId2Mask = affinity.os_id_masks;
3846 int maxOsId = affinity.num_os_id_masks - 1;
3847 const char *scan = placelist;
3848 const char *next = placelist;
3851 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3857 kmp_affin_mask_t *tempMask;
3858 kmp_affin_mask_t *previousMask;
3859 KMP_CPU_ALLOC(tempMask);
3860 KMP_CPU_ZERO(tempMask);
3861 KMP_CPU_ALLOC(previousMask);
3862 KMP_CPU_ZERO(previousMask);
3866 __kmp_process_place(&scan, affinity, maxOsId, tempMask, &setSize);
3870 if (*scan ==
'\0' || *scan ==
',') {
3874 KMP_CPU_ZERO(tempMask);
3876 if (*scan ==
'\0') {
3883 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3888 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3891 count = __kmp_str_to_int(scan, *next);
3892 KMP_ASSERT(count >= 0);
3897 if (*scan ==
'\0' || *scan ==
',') {
3900 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3919 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3922 stride = __kmp_str_to_int(scan, *next);
3923 KMP_DEBUG_ASSERT(stride >= 0);
3929 for (i = 0; i < count; i++) {
3934 KMP_CPU_COPY(previousMask, tempMask);
3935 ADD_MASK(previousMask);
3936 KMP_CPU_ZERO(tempMask);
3938 KMP_CPU_SET_ITERATE(j, previousMask) {
3939 if (!KMP_CPU_ISSET(j, previousMask)) {
3942 if ((j + stride > maxOsId) || (j + stride < 0) ||
3943 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
3944 (!KMP_CPU_ISSET(j + stride,
3945 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
3946 if (i < count - 1) {
3947 KMP_AFF_WARNING(affinity, AffIgnoreInvalidProcID, j + stride);
3951 KMP_CPU_SET(j + stride, tempMask);
3955 KMP_CPU_ZERO(tempMask);
3960 if (*scan ==
'\0') {
3968 KMP_ASSERT2(0,
"bad explicit places list");
3971 *out_numMasks = nextNewMask;
3972 if (nextNewMask == 0) {
3974 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3977 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3978 KMP_CPU_FREE(tempMask);
3979 KMP_CPU_FREE(previousMask);
3980 for (i = 0; i < nextNewMask; i++) {
3981 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3982 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3983 KMP_CPU_COPY(dest, src);
3985 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3993static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
3996 for (
int i = 0; i < nprocs; i++) {
3997 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3998 for (
int j = bottom_level; j > 0; j--) {
3999 if (hw_thread.ids[j] > 0) {
4000 if (core_level < (j - 1)) {
4010static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
4012 return __kmp_topology->get_count(core_level);
4015static int __kmp_affinity_find_core(
int proc,
int bottom_level,
4018 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
4019 for (
int i = 0; i <= proc; ++i) {
4020 if (i + 1 <= proc) {
4021 for (
int j = 0; j <= core_level; ++j) {
4022 if (__kmp_topology->at(i + 1).sub_ids[j] !=
4023 __kmp_topology->at(i).sub_ids[j]) {
4035static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
4037 if (core_level >= bottom_level)
4039 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
4040 return __kmp_topology->calculate_ratio(thread_level, core_level);
4043static int *procarr = NULL;
4044static int __kmp_aff_depth = 0;
4048static void __kmp_create_affinity_none_places(kmp_affinity_t &affinity) {
4049 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4050 KMP_ASSERT(affinity.type == affinity_none);
4051 affinity.num_masks = 1;
4052 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4053 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, 0);
4054 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4057static void __kmp_aux_affinity_initialize_masks(kmp_affinity_t &affinity) {
4062 int verbose = affinity.flags.verbose;
4063 const char *env_var = affinity.env_var;
4066 if (__kmp_affin_fullMask && __kmp_affin_origMask)
4069 if (__kmp_affin_fullMask == NULL) {
4070 KMP_CPU_ALLOC(__kmp_affin_fullMask);
4072 if (__kmp_affin_origMask == NULL) {
4073 KMP_CPU_ALLOC(__kmp_affin_origMask);
4075 if (KMP_AFFINITY_CAPABLE()) {
4076 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4078 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4079 if (affinity.flags.respect) {
4082 __kmp_avail_proc = 0;
4083 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4084 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4089 if (__kmp_avail_proc > __kmp_xproc) {
4090 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4091 affinity.type = affinity_none;
4092 KMP_AFFINITY_DISABLE();
4097 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4098 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4099 __kmp_affin_fullMask);
4100 KMP_INFORM(InitOSProcSetRespect, env_var, buf);
4104 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4105 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4106 __kmp_affin_fullMask);
4107 KMP_INFORM(InitOSProcSetNotRespect, env_var, buf);
4110 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4112 if (__kmp_num_proc_groups <= 1) {
4114 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4118 __kmp_affin_fullMask->set_process_affinity(
true);
4124static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
4125 bool success =
false;
4126 const char *env_var = affinity.env_var;
4127 kmp_i18n_id_t msg_id = kmp_i18n_null;
4128 int verbose = affinity.flags.verbose;
4132 if ((__kmp_cpuinfo_file != NULL) &&
4133 (__kmp_affinity_top_method == affinity_top_method_all)) {
4134 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4137 if (__kmp_affinity_top_method == affinity_top_method_all) {
4143 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4144 if (!__kmp_hwloc_error) {
4145 success = __kmp_affinity_create_hwloc_map(&msg_id);
4146 if (!success && verbose) {
4147 KMP_INFORM(AffIgnoringHwloc, env_var);
4149 }
else if (verbose) {
4150 KMP_INFORM(AffIgnoringHwloc, env_var);
4155#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4157 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4158 if (!success && verbose && msg_id != kmp_i18n_null) {
4159 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4163 success = __kmp_affinity_create_apicid_map(&msg_id);
4164 if (!success && verbose && msg_id != kmp_i18n_null) {
4165 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4173 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4174 if (!success && verbose && msg_id != kmp_i18n_null) {
4175 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4180#if KMP_GROUP_AFFINITY
4181 if (!success && (__kmp_num_proc_groups > 1)) {
4182 success = __kmp_affinity_create_proc_group_map(&msg_id);
4183 if (!success && verbose && msg_id != kmp_i18n_null) {
4184 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4190 success = __kmp_affinity_create_flat_map(&msg_id);
4191 if (!success && verbose && msg_id != kmp_i18n_null) {
4192 KMP_INFORM(AffInfoStr, env_var, __kmp_i18n_catgets(msg_id));
4194 KMP_ASSERT(success);
4202 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4203 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4204 success = __kmp_affinity_create_hwloc_map(&msg_id);
4206 KMP_ASSERT(msg_id != kmp_i18n_null);
4207 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4212#if KMP_ARCH_X86 || KMP_ARCH_X86_64
4213 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
4214 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
4215 success = __kmp_affinity_create_x2apicid_map(&msg_id);
4217 KMP_ASSERT(msg_id != kmp_i18n_null);
4218 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4220 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4221 success = __kmp_affinity_create_apicid_map(&msg_id);
4223 KMP_ASSERT(msg_id != kmp_i18n_null);
4224 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4229 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4231 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
4233 KMP_ASSERT(msg_id != kmp_i18n_null);
4234 const char *filename = __kmp_cpuinfo_get_filename();
4236 KMP_FATAL(FileLineMsgExiting, filename, line,
4237 __kmp_i18n_catgets(msg_id));
4239 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4244#if KMP_GROUP_AFFINITY
4245 else if (__kmp_affinity_top_method == affinity_top_method_group) {
4246 success = __kmp_affinity_create_proc_group_map(&msg_id);
4247 KMP_ASSERT(success);
4249 KMP_ASSERT(msg_id != kmp_i18n_null);
4250 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4255 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4256 success = __kmp_affinity_create_flat_map(&msg_id);
4258 KMP_ASSERT(success);
4262 if (!__kmp_topology) {
4263 if (KMP_AFFINITY_CAPABLE()) {
4264 KMP_AFF_WARNING(affinity, ErrorInitializeAffinity);
4266 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
4268 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
4269 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
4270 __kmp_nThreadsPerCore, __kmp_ncores);
4272 __kmp_topology->print(env_var);
4279 __kmp_topology->canonicalize();
4281 __kmp_topology->print(env_var);
4282 bool filtered = __kmp_topology->filter_hw_subset();
4286 if (__kmp_num_proc_groups <= 1)
4288 __kmp_affin_origMask->copy(__kmp_affin_fullMask);
4290 if (filtered && verbose)
4291 __kmp_topology->print(
"KMP_HW_SUBSET");
4295static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
4296 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4297 bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
4298 const char *env_var = affinity.env_var;
4300 if (affinity.flags.initialized) {
4301 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4305 if (is_regular_affinity && (!__kmp_affin_fullMask || !__kmp_affin_origMask))
4306 __kmp_aux_affinity_initialize_masks(affinity);
4308 if (is_regular_affinity && !__kmp_topology) {
4309 bool success = __kmp_aux_affinity_initialize_topology(affinity);
4312 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
4313 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
4315 affinity.type = affinity_none;
4316 KMP_AFFINITY_DISABLE();
4323 if (affinity.type == affinity_none) {
4324 __kmp_create_affinity_none_places(affinity);
4325#if KMP_USE_HIER_SCHED
4326 __kmp_dispatch_set_hierarchy_values();
4328 affinity.flags.initialized = TRUE;
4332 __kmp_topology->set_granularity(affinity);
4333 int depth = __kmp_topology->get_depth();
4337 __kmp_create_os_id_masks(&numUnique, affinity);
4338 if (affinity.gran_levels == 0) {
4339 KMP_DEBUG_ASSERT((
int)numUnique == __kmp_avail_proc);
4342 switch (affinity.type) {
4344 case affinity_explicit:
4345 KMP_DEBUG_ASSERT(affinity.proclist != NULL);
4346 if (is_hidden_helper_affinity ||
4347 __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
4348 __kmp_affinity_process_proclist(affinity);
4350 __kmp_affinity_process_placelist(affinity);
4352 if (affinity.num_masks == 0) {
4353 KMP_AFF_WARNING(affinity, AffNoValidProcID);
4354 affinity.type = affinity_none;
4355 __kmp_create_affinity_none_places(affinity);
4356 affinity.flags.initialized = TRUE;
4365 case affinity_logical:
4366 affinity.compact = 0;
4367 if (affinity.offset) {
4369 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4373 case affinity_physical:
4374 if (__kmp_nThreadsPerCore > 1) {
4375 affinity.compact = 1;
4376 if (affinity.compact >= depth) {
4377 affinity.compact = 0;
4380 affinity.compact = 0;
4382 if (affinity.offset) {
4384 __kmp_nThreadsPerCore * affinity.offset % __kmp_avail_proc;
4388 case affinity_scatter:
4389 if (affinity.compact >= depth) {
4390 affinity.compact = 0;
4392 affinity.compact = depth - 1 - affinity.compact;
4396 case affinity_compact:
4397 if (affinity.compact >= depth) {
4398 affinity.compact = depth - 1;
4402 case affinity_balanced:
4403 if (depth <= 1 || is_hidden_helper_affinity) {
4404 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4405 affinity.type = affinity_none;
4406 __kmp_create_affinity_none_places(affinity);
4407 affinity.flags.initialized = TRUE;
4409 }
else if (!__kmp_topology->is_uniform()) {
4411 __kmp_aff_depth = depth;
4414 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
4415 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
4417 int maxprocpercore = __kmp_affinity_max_proc_per_core(
4418 __kmp_avail_proc, depth - 1, core_level);
4420 int nproc = ncores * maxprocpercore;
4421 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
4422 KMP_AFF_WARNING(affinity, AffBalancedNotAvail, env_var);
4423 affinity.type = affinity_none;
4424 affinity.flags.initialized = TRUE;
4428 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
4429 for (
int i = 0; i < nproc; i++) {
4435 for (
int i = 0; i < __kmp_avail_proc; i++) {
4436 int proc = __kmp_topology->at(i).os_id;
4437 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
4439 if (core == lastcore) {
4446 procarr[core * maxprocpercore + inlastcore] = proc;
4449 if (affinity.compact >= depth) {
4450 affinity.compact = depth - 1;
4455 if (affinity.flags.dups) {
4456 affinity.num_masks = __kmp_avail_proc;
4458 affinity.num_masks = numUnique;
4461 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
4462 (__kmp_affinity_num_places > 0) &&
4463 ((
unsigned)__kmp_affinity_num_places < affinity.num_masks) &&
4464 !is_hidden_helper_affinity) {
4465 affinity.num_masks = __kmp_affinity_num_places;
4468 KMP_CPU_ALLOC_ARRAY(affinity.masks, affinity.num_masks);
4472 __kmp_topology->sort_compact(affinity);
4476 int num_hw_threads = __kmp_topology->get_num_hw_threads();
4477 for (i = 0, j = 0; i < num_hw_threads; i++) {
4478 if ((!affinity.flags.dups) && (!__kmp_topology->at(i).leader)) {
4481 int osId = __kmp_topology->at(i).os_id;
4483 kmp_affin_mask_t *src = KMP_CPU_INDEX(affinity.os_id_masks, osId);
4484 kmp_affin_mask_t *dest = KMP_CPU_INDEX(affinity.masks, j);
4485 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
4486 KMP_CPU_COPY(dest, src);
4487 if (++j >= affinity.num_masks) {
4491 KMP_DEBUG_ASSERT(j == affinity.num_masks);
4494 __kmp_topology->sort_ids();
4498 KMP_ASSERT2(0,
"Unexpected affinity setting");
4500 affinity.flags.initialized = TRUE;
4503void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
4512 int disabled = (affinity.type == affinity_disabled);
4513 if (!KMP_AFFINITY_CAPABLE())
4514 KMP_ASSERT(disabled);
4516 affinity.type = affinity_none;
4517 __kmp_aux_affinity_initialize(affinity);
4519 affinity.type = affinity_disabled;
4522void __kmp_affinity_uninitialize(
void) {
4523 for (kmp_affinity_t *affinity : __kmp_affinities) {
4524 if (affinity->masks != NULL)
4525 KMP_CPU_FREE_ARRAY(affinity->masks, affinity->num_masks);
4526 if (affinity->os_id_masks != NULL)
4527 KMP_CPU_FREE_ARRAY(affinity->os_id_masks, affinity->num_os_id_masks);
4528 if (affinity->proclist != NULL)
4529 __kmp_free(affinity->proclist);
4530 *affinity = KMP_AFFINITY_INIT(affinity->env_var);
4532 if (__kmp_affin_origMask != NULL) {
4533 if (KMP_AFFINITY_CAPABLE()) {
4534 __kmp_set_system_affinity(__kmp_affin_origMask, FALSE);
4536 KMP_CPU_FREE(__kmp_affin_origMask);
4537 __kmp_affin_origMask = NULL;
4539 __kmp_affinity_num_places = 0;
4540 if (procarr != NULL) {
4541 __kmp_free(procarr);
4545 if (__kmp_hwloc_topology != NULL) {
4546 hwloc_topology_destroy(__kmp_hwloc_topology);
4547 __kmp_hwloc_topology = NULL;
4550 if (__kmp_hw_subset) {
4551 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
4552 __kmp_hw_subset =
nullptr;
4554 if (__kmp_topology) {
4555 kmp_topology_t::deallocate(__kmp_topology);
4556 __kmp_topology =
nullptr;
4558 KMPAffinity::destroy_api();
4561static void __kmp_select_mask_by_gtid(
int gtid,
const kmp_affinity_t *affinity,
4562 int *place, kmp_affin_mask_t **mask) {
4564 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
4565 if (is_hidden_helper)
4568 mask_idx = gtid - 2;
4570 mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
4571 KMP_DEBUG_ASSERT(affinity->num_masks > 0);
4572 *place = (mask_idx + affinity->offset) % affinity->num_masks;
4573 *mask = KMP_CPU_INDEX(affinity->masks, *place);
4576void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
4577 if (!KMP_AFFINITY_CAPABLE()) {
4581 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4582 if (th->th.th_affin_mask == NULL) {
4583 KMP_CPU_ALLOC(th->th.th_affin_mask);
4585 KMP_CPU_ZERO(th->th.th_affin_mask);
4593 kmp_affin_mask_t *mask;
4595 const kmp_affinity_t *affinity;
4596 const char *env_var;
4597 bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD(gtid);
4599 if (is_hidden_helper)
4600 affinity = &__kmp_hh_affinity;
4602 affinity = &__kmp_affinity;
4603 env_var = affinity->env_var;
4605 if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
4606 if ((affinity->type == affinity_none) ||
4607 (affinity->type == affinity_balanced) ||
4608 KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
4609#if KMP_GROUP_AFFINITY
4610 if (__kmp_num_proc_groups > 1) {
4614 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4616 mask = __kmp_affin_fullMask;
4618 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
4621 if (!isa_root || __kmp_nested_proc_bind.bind_types[0] == proc_bind_false) {
4622#if KMP_GROUP_AFFINITY
4623 if (__kmp_num_proc_groups > 1) {
4627 KMP_ASSERT(__kmp_affin_fullMask != NULL);
4629 mask = __kmp_affin_fullMask;
4631 __kmp_select_mask_by_gtid(gtid, affinity, &i, &mask);
4635 th->th.th_current_place = i;
4636 if (isa_root && !is_hidden_helper) {
4637 th->th.th_new_place = i;
4638 th->th.th_first_place = 0;
4639 th->th.th_last_place = affinity->num_masks - 1;
4640 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
4643 th->th.th_first_place = 0;
4644 th->th.th_last_place = affinity->num_masks - 1;
4647 if (i == KMP_PLACE_ALL) {
4648 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4651 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4655 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4658 if (affinity->flags.verbose &&
4659 (affinity->type == affinity_none ||
4660 (i != KMP_PLACE_ALL && affinity->type != affinity_balanced)) &&
4661 !KMP_HIDDEN_HELPER_MAIN_THREAD(gtid)) {
4662 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4663 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4664 th->th.th_affin_mask);
4665 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
4673 if (affinity->type == affinity_none) {
4674 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4677 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4680void __kmp_affinity_set_place(
int gtid) {
4682 if (!KMP_AFFINITY_CAPABLE() || KMP_HIDDEN_HELPER_THREAD(gtid)) {
4686 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4688 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current "
4690 gtid, th->th.th_new_place, th->th.th_current_place));
4693 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4694 KMP_ASSERT(th->th.th_new_place >= 0);
4695 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity.num_masks);
4696 if (th->th.th_first_place <= th->th.th_last_place) {
4697 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
4698 (th->th.th_new_place <= th->th.th_last_place));
4700 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
4701 (th->th.th_new_place >= th->th.th_last_place));
4706 kmp_affin_mask_t *mask =
4707 KMP_CPU_INDEX(__kmp_affinity.masks, th->th.th_new_place);
4708 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4709 th->th.th_current_place = th->th.th_new_place;
4711 if (__kmp_affinity.flags.verbose) {
4712 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4713 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4714 th->th.th_affin_mask);
4715 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
4716 __kmp_gettid(), gtid, buf);
4718 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4721int __kmp_aux_set_affinity(
void **mask) {
4726 if (!KMP_AFFINITY_CAPABLE()) {
4730 gtid = __kmp_entry_gtid();
4733 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4734 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4735 (kmp_affin_mask_t *)(*mask));
4737 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4741 if (__kmp_env_consistency_check) {
4742 if ((mask == NULL) || (*mask == NULL)) {
4743 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4748 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
4749 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4750 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4752 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4757 if (num_procs == 0) {
4758 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4761#if KMP_GROUP_AFFINITY
4762 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4763 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4769 th = __kmp_threads[gtid];
4770 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4771 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4773 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4776 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4777 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4778 th->th.th_first_place = 0;
4779 th->th.th_last_place = __kmp_affinity.num_masks - 1;
4782 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
4787int __kmp_aux_get_affinity(
void **mask) {
4790#if KMP_OS_WINDOWS || KMP_DEBUG
4793 if (!KMP_AFFINITY_CAPABLE()) {
4797 gtid = __kmp_entry_gtid();
4798#if KMP_OS_WINDOWS || KMP_DEBUG
4799 th = __kmp_threads[gtid];
4803 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4807 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4808 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4809 th->th.th_affin_mask);
4811 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
4815 if (__kmp_env_consistency_check) {
4816 if ((mask == NULL) || (*mask == NULL)) {
4817 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4823 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4826 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4827 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4828 (kmp_affin_mask_t *)(*mask));
4830 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
4838 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4844int __kmp_aux_get_affinity_max_proc() {
4845 if (!KMP_AFFINITY_CAPABLE()) {
4848#if KMP_GROUP_AFFINITY
4849 if (__kmp_num_proc_groups > 1) {
4850 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
4856int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
4857 if (!KMP_AFFINITY_CAPABLE()) {
4863 int gtid = __kmp_entry_gtid();
4864 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4865 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4866 (kmp_affin_mask_t *)(*mask));
4867 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
4868 "affinity mask for thread %d = %s\n",
4872 if (__kmp_env_consistency_check) {
4873 if ((mask == NULL) || (*mask == NULL)) {
4874 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4878 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4881 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4885 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
4889int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
4890 if (!KMP_AFFINITY_CAPABLE()) {
4896 int gtid = __kmp_entry_gtid();
4897 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4898 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4899 (kmp_affin_mask_t *)(*mask));
4900 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
4901 "affinity mask for thread %d = %s\n",
4905 if (__kmp_env_consistency_check) {
4906 if ((mask == NULL) || (*mask == NULL)) {
4907 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
4911 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4914 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4918 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
4922int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
4923 if (!KMP_AFFINITY_CAPABLE()) {
4929 int gtid = __kmp_entry_gtid();
4930 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4931 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4932 (kmp_affin_mask_t *)(*mask));
4933 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
4934 "affinity mask for thread %d = %s\n",
4938 if (__kmp_env_consistency_check) {
4939 if ((mask == NULL) || (*mask == NULL)) {
4940 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
4944 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4947 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4951 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
4955void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
4956 KMP_DEBUG_ASSERT(th);
4957 bool fine_gran =
true;
4958 int tid = th->th.th_info.ds.ds_tid;
4959 const char *env_var =
"KMP_AFFINITY";
4962 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
4965 switch (__kmp_affinity.gran) {
4969 if (__kmp_nThreadsPerCore > 1) {
4974 if (nCoresPerPkg > 1) {
4982 if (__kmp_topology->is_uniform()) {
4986 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
4988 int ncores = __kmp_ncores;
4989 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
4990 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
4994 int chunk = nthreads / ncores;
4996 int big_cores = nthreads % ncores;
4998 int big_nth = (chunk + 1) * big_cores;
4999 if (tid < big_nth) {
5000 coreID = tid / (chunk + 1);
5001 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5003 coreID = (tid - big_cores) / chunk;
5004 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5006 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5007 "Illegal set affinity operation when not capable");
5009 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5014 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
5015 KMP_CPU_SET(osID, mask);
5017 for (
int i = 0; i < __kmp_nth_per_core; i++) {
5019 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
5020 KMP_CPU_SET(osID, mask);
5023 if (__kmp_affinity.flags.verbose) {
5024 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5025 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5026 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5029 __kmp_set_system_affinity(mask, TRUE);
5032 kmp_affin_mask_t *mask = th->th.th_affin_mask;
5036 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
5037 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
5038 __kmp_aff_depth - 1, core_level);
5039 int nth_per_core = __kmp_affinity_max_proc_per_core(
5040 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5044 if (nthreads == __kmp_avail_proc) {
5046 int osID = __kmp_topology->at(tid).os_id;
5047 KMP_CPU_SET(osID, mask);
5050 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
5051 for (
int i = 0; i < __kmp_avail_proc; i++) {
5052 int osID = __kmp_topology->at(i).os_id;
5053 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
5055 KMP_CPU_SET(osID, mask);
5059 }
else if (nthreads <= ncores) {
5062 for (
int i = 0; i < ncores; i++) {
5065 for (
int j = 0; j < nth_per_core; j++) {
5066 if (procarr[i * nth_per_core + j] != -1) {
5073 for (
int j = 0; j < nth_per_core; j++) {
5074 int osID = procarr[i * nth_per_core + j];
5076 KMP_CPU_SET(osID, mask);
5092 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
5094 int *ncores_with_x_procs =
5095 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5097 int *ncores_with_x_to_max_procs =
5098 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
5100 for (
int i = 0; i <= nth_per_core; i++) {
5101 ncores_with_x_procs[i] = 0;
5102 ncores_with_x_to_max_procs[i] = 0;
5105 for (
int i = 0; i < ncores; i++) {
5107 for (
int j = 0; j < nth_per_core; j++) {
5108 if (procarr[i * nth_per_core + j] != -1) {
5112 nproc_at_core[i] = cnt;
5113 ncores_with_x_procs[cnt]++;
5116 for (
int i = 0; i <= nth_per_core; i++) {
5117 for (
int j = i; j <= nth_per_core; j++) {
5118 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5123 int nproc = nth_per_core * ncores;
5125 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
5126 for (
int i = 0; i < nproc; i++) {
5133 for (
int j = 1; j <= nth_per_core; j++) {
5134 int cnt = ncores_with_x_to_max_procs[j];
5135 for (
int i = 0; i < ncores; i++) {
5137 if (nproc_at_core[i] == 0) {
5140 for (
int k = 0; k < nth_per_core; k++) {
5141 if (procarr[i * nth_per_core + k] != -1) {
5142 if (newarr[i * nth_per_core + k] == 0) {
5143 newarr[i * nth_per_core + k] = 1;
5149 newarr[i * nth_per_core + k]++;
5157 if (cnt == 0 || nth == 0) {
5168 for (
int i = 0; i < nproc; i++) {
5172 int osID = procarr[i];
5173 KMP_CPU_SET(osID, mask);
5175 int coreID = i / nth_per_core;
5176 for (
int ii = 0; ii < nth_per_core; ii++) {
5177 int osID = procarr[coreID * nth_per_core + ii];
5179 KMP_CPU_SET(osID, mask);
5189 if (__kmp_affinity.flags.verbose) {
5190 char buf[KMP_AFFIN_MASK_PRINT_LEN];
5191 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5192 KMP_INFORM(BoundToOSProcSet, env_var, (kmp_int32)getpid(), __kmp_gettid(),
5195 __kmp_set_system_affinity(mask, TRUE);
5199#if KMP_OS_LINUX || KMP_OS_FREEBSD
5213 kmp_set_thread_affinity_mask_initial()
5218 int gtid = __kmp_get_gtid();
5221 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5222 "non-omp thread, returning\n"));
5225 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
5226 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5227 "affinity not initialized, returning\n"));
5230 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
5231 "set full mask for thread %d\n",
5233 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
5234 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
int try_open(const char *filename, const char *mode)