14 #include "kmp_affinity.h"
18 #include "kmp_wrapper_getpid.h"
19 #if KMP_USE_HIER_SCHED
20 #include "kmp_dispatch_hier.h"
24 #define HWLOC_GROUP_KIND_INTEL_MODULE 102
25 #define HWLOC_GROUP_KIND_INTEL_TILE 103
26 #define HWLOC_GROUP_KIND_INTEL_DIE 104
27 #define HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP 220
31 kmp_topology_t *__kmp_topology =
nullptr;
33 kmp_hw_subset_t *__kmp_hw_subset =
nullptr;
36 static hierarchy_info machine_hierarchy;
38 void __kmp_cleanup_hierarchy() { machine_hierarchy.fini(); }
40 void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar) {
44 if (TCR_1(machine_hierarchy.uninitialized))
45 machine_hierarchy.init(nproc);
48 if (nproc > machine_hierarchy.base_num_threads)
49 machine_hierarchy.resize(nproc);
51 depth = machine_hierarchy.depth;
52 KMP_DEBUG_ASSERT(depth > 0);
54 thr_bar->depth = depth;
55 __kmp_type_convert(machine_hierarchy.numPerLevel[0] - 1,
56 &(thr_bar->base_leaf_kids));
57 thr_bar->skip_per_level = machine_hierarchy.skipPerLevel;
60 static int nCoresPerPkg, nPackages;
61 static int __kmp_nThreadsPerCore;
62 #ifndef KMP_DFLT_NTH_CORES
63 static int __kmp_ncores;
66 const char *__kmp_hw_get_catalog_string(kmp_hw_t type,
bool plural) {
69 return ((plural) ? KMP_I18N_STR(Sockets) : KMP_I18N_STR(Socket));
71 return ((plural) ? KMP_I18N_STR(Dice) : KMP_I18N_STR(Die));
73 return ((plural) ? KMP_I18N_STR(Modules) : KMP_I18N_STR(Module));
75 return ((plural) ? KMP_I18N_STR(Tiles) : KMP_I18N_STR(Tile));
77 return ((plural) ? KMP_I18N_STR(NumaDomains) : KMP_I18N_STR(NumaDomain));
79 return ((plural) ? KMP_I18N_STR(L3Caches) : KMP_I18N_STR(L3Cache));
81 return ((plural) ? KMP_I18N_STR(L2Caches) : KMP_I18N_STR(L2Cache));
83 return ((plural) ? KMP_I18N_STR(L1Caches) : KMP_I18N_STR(L1Cache));
85 return ((plural) ? KMP_I18N_STR(LLCaches) : KMP_I18N_STR(LLCache));
87 return ((plural) ? KMP_I18N_STR(Cores) : KMP_I18N_STR(Core));
89 return ((plural) ? KMP_I18N_STR(Threads) : KMP_I18N_STR(Thread));
90 case KMP_HW_PROC_GROUP:
91 return ((plural) ? KMP_I18N_STR(ProcGroups) : KMP_I18N_STR(ProcGroup));
93 return KMP_I18N_STR(Unknown);
96 const char *__kmp_hw_get_keyword(kmp_hw_t type,
bool plural) {
99 return ((plural) ?
"sockets" :
"socket");
101 return ((plural) ?
"dice" :
"die");
103 return ((plural) ?
"modules" :
"module");
105 return ((plural) ?
"tiles" :
"tile");
107 return ((plural) ?
"numa_domains" :
"numa_domain");
109 return ((plural) ?
"l3_caches" :
"l3_cache");
111 return ((plural) ?
"l2_caches" :
"l2_cache");
113 return ((plural) ?
"l1_caches" :
"l1_cache");
115 return ((plural) ?
"ll_caches" :
"ll_cache");
117 return ((plural) ?
"cores" :
"core");
119 return ((plural) ?
"threads" :
"thread");
120 case KMP_HW_PROC_GROUP:
121 return ((plural) ?
"proc_groups" :
"proc_group");
123 return ((plural) ?
"unknowns" :
"unknown");
128 int kmp_hw_thread_t::compare_ids(
const void *a,
const void *b) {
129 const kmp_hw_thread_t *ahwthread = (
const kmp_hw_thread_t *)a;
130 const kmp_hw_thread_t *bhwthread = (
const kmp_hw_thread_t *)b;
131 int depth = __kmp_topology->get_depth();
132 for (
int level = 0; level < depth; ++level) {
133 if (ahwthread->ids[level] < bhwthread->ids[level])
135 else if (ahwthread->ids[level] > bhwthread->ids[level])
138 if (ahwthread->os_id < bhwthread->os_id)
140 else if (ahwthread->os_id > bhwthread->os_id)
145 #if KMP_AFFINITY_SUPPORTED
146 int kmp_hw_thread_t::compare_compact(
const void *a,
const void *b) {
148 const kmp_hw_thread_t *aa = (
const kmp_hw_thread_t *)a;
149 const kmp_hw_thread_t *bb = (
const kmp_hw_thread_t *)b;
150 int depth = __kmp_topology->get_depth();
151 KMP_DEBUG_ASSERT(__kmp_affinity_compact >= 0);
152 KMP_DEBUG_ASSERT(__kmp_affinity_compact <= depth);
153 for (i = 0; i < __kmp_affinity_compact; i++) {
154 int j = depth - i - 1;
155 if (aa->sub_ids[j] < bb->sub_ids[j])
157 if (aa->sub_ids[j] > bb->sub_ids[j])
160 for (; i < depth; i++) {
161 int j = i - __kmp_affinity_compact;
162 if (aa->sub_ids[j] < bb->sub_ids[j])
164 if (aa->sub_ids[j] > bb->sub_ids[j])
171 void kmp_hw_thread_t::print()
const {
172 int depth = __kmp_topology->get_depth();
173 printf(
"%4d ", os_id);
174 for (
int i = 0; i < depth; ++i) {
175 printf(
"%4d ", ids[i]);
185 void kmp_topology_t::_remove_radix1_layers() {
186 int preference[KMP_HW_LAST];
187 int top_index1, top_index2;
189 preference[KMP_HW_PROC_GROUP] = 110;
190 preference[KMP_HW_SOCKET] = 100;
191 preference[KMP_HW_CORE] = 95;
192 preference[KMP_HW_THREAD] = 90;
193 preference[KMP_HW_NUMA] = 85;
194 preference[KMP_HW_DIE] = 80;
195 preference[KMP_HW_TILE] = 75;
196 preference[KMP_HW_MODULE] = 73;
197 preference[KMP_HW_L3] = 70;
198 preference[KMP_HW_L2] = 65;
199 preference[KMP_HW_L1] = 60;
200 preference[KMP_HW_LLC] = 5;
203 while (top_index1 < depth - 1 && top_index2 < depth) {
204 kmp_hw_t type1 = types[top_index1];
205 kmp_hw_t type2 = types[top_index2];
206 KMP_ASSERT_VALID_HW_TYPE(type1);
207 KMP_ASSERT_VALID_HW_TYPE(type2);
210 if ((type1 == KMP_HW_THREAD || type1 == KMP_HW_CORE ||
211 type1 == KMP_HW_SOCKET) &&
212 (type2 == KMP_HW_THREAD || type2 == KMP_HW_CORE ||
213 type2 == KMP_HW_SOCKET)) {
214 top_index1 = top_index2++;
218 bool all_same =
true;
219 int id1 = hw_threads[0].ids[top_index1];
220 int id2 = hw_threads[0].ids[top_index2];
221 int pref1 = preference[type1];
222 int pref2 = preference[type2];
223 for (
int hwidx = 1; hwidx < num_hw_threads; ++hwidx) {
224 if (hw_threads[hwidx].ids[top_index1] == id1 &&
225 hw_threads[hwidx].ids[top_index2] != id2) {
229 if (hw_threads[hwidx].ids[top_index2] != id2)
231 id1 = hw_threads[hwidx].ids[top_index1];
232 id2 = hw_threads[hwidx].ids[top_index2];
236 kmp_hw_t remove_type, keep_type;
237 int remove_layer, remove_layer_ids;
240 remove_layer = remove_layer_ids = top_index2;
244 remove_layer = remove_layer_ids = top_index1;
250 remove_layer_ids = top_index2;
253 set_equivalent_type(remove_type, keep_type);
254 for (
int idx = 0; idx < num_hw_threads; ++idx) {
255 kmp_hw_thread_t &hw_thread = hw_threads[idx];
256 for (
int d = remove_layer_ids; d < depth - 1; ++d)
257 hw_thread.ids[d] = hw_thread.ids[d + 1];
259 for (
int idx = remove_layer; idx < depth - 1; ++idx)
260 types[idx] = types[idx + 1];
263 top_index1 = top_index2++;
266 KMP_ASSERT(depth > 0);
269 void kmp_topology_t::_set_last_level_cache() {
270 if (get_equivalent_type(KMP_HW_L3) != KMP_HW_UNKNOWN)
271 set_equivalent_type(KMP_HW_LLC, KMP_HW_L3);
272 else if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
273 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
274 #if KMP_MIC_SUPPORTED
275 else if (__kmp_mic_type == mic3) {
276 if (get_equivalent_type(KMP_HW_L2) != KMP_HW_UNKNOWN)
277 set_equivalent_type(KMP_HW_LLC, KMP_HW_L2);
278 else if (get_equivalent_type(KMP_HW_TILE) != KMP_HW_UNKNOWN)
279 set_equivalent_type(KMP_HW_LLC, KMP_HW_TILE);
282 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
285 else if (get_equivalent_type(KMP_HW_L1) != KMP_HW_UNKNOWN)
286 set_equivalent_type(KMP_HW_LLC, KMP_HW_L1);
288 if (get_equivalent_type(KMP_HW_LLC) == KMP_HW_UNKNOWN) {
289 if (get_equivalent_type(KMP_HW_SOCKET) != KMP_HW_UNKNOWN)
290 set_equivalent_type(KMP_HW_LLC, KMP_HW_SOCKET);
291 else if (get_equivalent_type(KMP_HW_CORE) != KMP_HW_UNKNOWN)
292 set_equivalent_type(KMP_HW_LLC, KMP_HW_CORE);
294 KMP_ASSERT(get_equivalent_type(KMP_HW_LLC) != KMP_HW_UNKNOWN);
298 void kmp_topology_t::_gather_enumeration_information() {
299 int previous_id[KMP_HW_LAST];
300 int max[KMP_HW_LAST];
302 for (
int i = 0; i < depth; ++i) {
303 previous_id[i] = kmp_hw_thread_t::UNKNOWN_ID;
308 for (
int i = 0; i < num_hw_threads; ++i) {
309 kmp_hw_thread_t &hw_thread = hw_threads[i];
310 for (
int layer = 0; layer < depth; ++layer) {
311 int id = hw_thread.ids[layer];
312 if (
id != previous_id[layer]) {
314 for (
int l = layer; l < depth; ++l)
318 for (
int l = layer + 1; l < depth; ++l) {
319 if (max[l] > ratio[l])
326 for (
int layer = 0; layer < depth; ++layer) {
327 previous_id[layer] = hw_thread.ids[layer];
330 for (
int layer = 0; layer < depth; ++layer) {
331 if (max[layer] > ratio[layer])
332 ratio[layer] = max[layer];
337 void kmp_topology_t::_discover_uniformity() {
339 for (
int level = 0; level < depth; ++level)
341 flags.uniform = (num == count[depth - 1]);
345 void kmp_topology_t::_set_sub_ids() {
346 int previous_id[KMP_HW_LAST];
347 int sub_id[KMP_HW_LAST];
349 for (
int i = 0; i < depth; ++i) {
353 for (
int i = 0; i < num_hw_threads; ++i) {
354 kmp_hw_thread_t &hw_thread = hw_threads[i];
356 for (
int j = 0; j < depth; ++j) {
357 if (hw_thread.ids[j] != previous_id[j]) {
359 for (
int k = j + 1; k < depth; ++k) {
366 for (
int j = 0; j < depth; ++j) {
367 previous_id[j] = hw_thread.ids[j];
370 for (
int j = 0; j < depth; ++j) {
371 hw_thread.sub_ids[j] = sub_id[j];
376 void kmp_topology_t::_set_globals() {
378 int core_level, thread_level, package_level;
379 package_level = get_level(KMP_HW_SOCKET);
380 #if KMP_GROUP_AFFINITY
381 if (package_level == -1)
382 package_level = get_level(KMP_HW_PROC_GROUP);
384 core_level = get_level(KMP_HW_CORE);
385 thread_level = get_level(KMP_HW_THREAD);
387 KMP_ASSERT(core_level != -1);
388 KMP_ASSERT(thread_level != -1);
390 __kmp_nThreadsPerCore = calculate_ratio(thread_level, core_level);
391 if (package_level != -1) {
392 nCoresPerPkg = calculate_ratio(core_level, package_level);
393 nPackages = get_count(package_level);
396 nCoresPerPkg = get_count(core_level);
399 #ifndef KMP_DFLT_NTH_CORES
400 __kmp_ncores = get_count(core_level);
404 kmp_topology_t *kmp_topology_t::allocate(
int nproc,
int ndepth,
405 const kmp_hw_t *types) {
406 kmp_topology_t *retval;
408 size_t size =
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc +
409 sizeof(int) * ndepth * 3;
410 char *bytes = (
char *)__kmp_allocate(size);
411 retval = (kmp_topology_t *)bytes;
413 retval->hw_threads = (kmp_hw_thread_t *)(bytes +
sizeof(kmp_topology_t));
415 retval->hw_threads =
nullptr;
417 retval->num_hw_threads = nproc;
418 retval->depth = ndepth;
420 (
int *)(bytes +
sizeof(kmp_topology_t) +
sizeof(kmp_hw_thread_t) * nproc);
421 retval->types = (kmp_hw_t *)arr;
422 retval->ratio = arr + ndepth;
423 retval->count = arr + 2 * ndepth;
424 KMP_FOREACH_HW_TYPE(type) { retval->equivalent[type] = KMP_HW_UNKNOWN; }
425 for (
int i = 0; i < ndepth; ++i) {
426 retval->types[i] = types[i];
427 retval->equivalent[types[i]] = types[i];
432 void kmp_topology_t::deallocate(kmp_topology_t *topology) {
434 __kmp_free(topology);
437 bool kmp_topology_t::check_ids()
const {
439 if (num_hw_threads == 0)
441 for (
int i = 1; i < num_hw_threads; ++i) {
442 kmp_hw_thread_t ¤t_thread = hw_threads[i];
443 kmp_hw_thread_t &previous_thread = hw_threads[i - 1];
445 for (
int j = 0; j < depth; ++j) {
446 if (previous_thread.ids[j] != current_thread.ids[j]) {
458 void kmp_topology_t::dump()
const {
459 printf(
"***********************\n");
460 printf(
"*** __kmp_topology: ***\n");
461 printf(
"***********************\n");
462 printf(
"* depth: %d\n", depth);
465 for (
int i = 0; i < depth; ++i)
466 printf(
"%15s ", __kmp_hw_get_keyword(types[i]));
470 for (
int i = 0; i < depth; ++i) {
471 printf(
"%15d ", ratio[i]);
476 for (
int i = 0; i < depth; ++i) {
477 printf(
"%15d ", count[i]);
481 printf(
"* equivalent map:\n");
482 KMP_FOREACH_HW_TYPE(i) {
483 const char *key = __kmp_hw_get_keyword(i);
484 const char *value = __kmp_hw_get_keyword(equivalent[i]);
485 printf(
"%-15s -> %-15s\n", key, value);
488 printf(
"* uniform: %s\n", (is_uniform() ?
"Yes" :
"No"));
490 printf(
"* num_hw_threads: %d\n", num_hw_threads);
491 printf(
"* hw_threads:\n");
492 for (
int i = 0; i < num_hw_threads; ++i) {
493 hw_threads[i].print();
495 printf(
"***********************\n");
498 void kmp_topology_t::print(
const char *env_var)
const {
500 int print_types_depth;
501 __kmp_str_buf_init(&buf);
502 kmp_hw_t print_types[KMP_HW_LAST + 2];
505 KMP_INFORM(AvailableOSProc, env_var, num_hw_threads);
509 KMP_INFORM(Uniform, env_var);
511 KMP_INFORM(NonUniform, env_var);
515 KMP_FOREACH_HW_TYPE(type) {
516 kmp_hw_t eq_type = equivalent[type];
517 if (eq_type != KMP_HW_UNKNOWN && eq_type != type) {
518 KMP_INFORM(AffEqualTopologyTypes, env_var,
519 __kmp_hw_get_catalog_string(type),
520 __kmp_hw_get_catalog_string(eq_type));
525 KMP_ASSERT(depth > 0 && depth <= (
int)KMP_HW_LAST);
528 print_types_depth = 0;
529 for (
int level = 0; level < depth; ++level)
530 print_types[print_types_depth++] = types[level];
531 if (equivalent[KMP_HW_CORE] != KMP_HW_CORE) {
533 if (print_types[print_types_depth - 1] == KMP_HW_THREAD) {
536 print_types[print_types_depth - 1] = KMP_HW_CORE;
537 print_types[print_types_depth++] = KMP_HW_THREAD;
539 print_types[print_types_depth++] = KMP_HW_CORE;
543 if (equivalent[KMP_HW_THREAD] != KMP_HW_THREAD)
544 print_types[print_types_depth++] = KMP_HW_THREAD;
546 __kmp_str_buf_clear(&buf);
547 kmp_hw_t numerator_type;
548 kmp_hw_t denominator_type = KMP_HW_UNKNOWN;
549 int core_level = get_level(KMP_HW_CORE);
550 int ncores = get_count(core_level);
552 for (
int plevel = 0, level = 0; plevel < print_types_depth; ++plevel) {
555 numerator_type = print_types[plevel];
556 KMP_ASSERT_VALID_HW_TYPE(numerator_type);
557 if (equivalent[numerator_type] != numerator_type)
560 c = get_ratio(level++);
563 __kmp_str_buf_print(&buf,
"%d %s", c,
564 __kmp_hw_get_catalog_string(numerator_type, plural));
566 __kmp_str_buf_print(&buf,
" x %d %s/%s", c,
567 __kmp_hw_get_catalog_string(numerator_type, plural),
568 __kmp_hw_get_catalog_string(denominator_type));
570 denominator_type = numerator_type;
572 KMP_INFORM(TopologyGeneric, env_var, buf.str, ncores);
574 if (num_hw_threads <= 0) {
575 __kmp_str_buf_free(&buf);
580 KMP_INFORM(OSProcToPhysicalThreadMap, env_var);
581 for (
int i = 0; i < num_hw_threads; i++) {
582 __kmp_str_buf_clear(&buf);
583 for (
int level = 0; level < depth; ++level) {
584 kmp_hw_t type = types[level];
585 __kmp_str_buf_print(&buf,
"%s ", __kmp_hw_get_catalog_string(type));
586 __kmp_str_buf_print(&buf,
"%d ", hw_threads[i].ids[level]);
588 KMP_INFORM(OSProcMapToPack, env_var, hw_threads[i].os_id, buf.str);
591 __kmp_str_buf_free(&buf);
594 void kmp_topology_t::canonicalize() {
595 _remove_radix1_layers();
596 _gather_enumeration_information();
597 _discover_uniformity();
600 _set_last_level_cache();
602 #if KMP_MIC_SUPPORTED
604 if (__kmp_mic_type == mic3) {
605 if (get_level(KMP_HW_L2) != -1)
606 set_equivalent_type(KMP_HW_TILE, KMP_HW_L2);
607 else if (get_level(KMP_HW_TILE) != -1)
608 set_equivalent_type(KMP_HW_L2, KMP_HW_TILE);
613 KMP_ASSERT(depth > 0);
614 for (
int level = 0; level < depth; ++level) {
616 KMP_ASSERT(count[level] > 0 && ratio[level] > 0);
617 KMP_ASSERT_VALID_HW_TYPE(types[level]);
619 KMP_ASSERT(equivalent[types[level]] == types[level]);
622 #if KMP_AFFINITY_SUPPORTED
624 if (__kmp_affinity_gran_levels < 0) {
625 kmp_hw_t gran_type = get_equivalent_type(__kmp_affinity_gran);
627 if (gran_type == KMP_HW_UNKNOWN) {
629 kmp_hw_t gran_types[3] = {KMP_HW_CORE, KMP_HW_THREAD, KMP_HW_SOCKET};
630 for (
auto g : gran_types) {
631 if (__kmp_topology->get_equivalent_type(g) != KMP_HW_UNKNOWN) {
636 KMP_ASSERT(gran_type != KMP_HW_UNKNOWN);
638 KMP_WARNING(AffGranularityBad,
"KMP_AFFINITY",
639 __kmp_hw_get_catalog_string(__kmp_affinity_gran),
640 __kmp_hw_get_catalog_string(gran_type));
641 __kmp_affinity_gran = gran_type;
643 __kmp_affinity_gran_levels = 0;
644 for (
int i = depth - 1; i >= 0 && get_type(i) != gran_type; --i)
645 __kmp_affinity_gran_levels++;
651 void kmp_topology_t::canonicalize(
int npackages,
int ncores_per_pkg,
652 int nthreads_per_core,
int ncores) {
655 KMP_FOREACH_HW_TYPE(i) { equivalent[i] = KMP_HW_UNKNOWN; }
656 for (
int level = 0; level < depth; ++level) {
660 count[0] = npackages;
662 count[2] = __kmp_xproc;
663 ratio[0] = npackages;
664 ratio[1] = ncores_per_pkg;
665 ratio[2] = nthreads_per_core;
666 equivalent[KMP_HW_SOCKET] = KMP_HW_SOCKET;
667 equivalent[KMP_HW_CORE] = KMP_HW_CORE;
668 equivalent[KMP_HW_THREAD] = KMP_HW_THREAD;
669 types[0] = KMP_HW_SOCKET;
670 types[1] = KMP_HW_CORE;
671 types[2] = KMP_HW_THREAD;
673 _discover_uniformity();
679 bool kmp_topology_t::filter_hw_subset() {
681 if (!__kmp_hw_subset)
685 int hw_subset_depth = __kmp_hw_subset->get_depth();
686 kmp_hw_t specified[KMP_HW_LAST];
687 KMP_ASSERT(hw_subset_depth > 0);
688 KMP_FOREACH_HW_TYPE(i) { specified[i] = KMP_HW_UNKNOWN; }
689 for (
int i = 0; i < hw_subset_depth; ++i) {
691 int num = __kmp_hw_subset->at(i).num;
692 int offset = __kmp_hw_subset->at(i).offset;
693 kmp_hw_t type = __kmp_hw_subset->at(i).type;
694 kmp_hw_t equivalent_type = equivalent[type];
695 int level = get_level(type);
698 if (equivalent_type != KMP_HW_UNKNOWN) {
699 __kmp_hw_subset->at(i).type = equivalent_type;
701 KMP_WARNING(AffHWSubsetNotExistGeneric,
702 __kmp_hw_get_catalog_string(type));
708 if (specified[equivalent_type] != KMP_HW_UNKNOWN) {
709 KMP_WARNING(AffHWSubsetEqvLayers, __kmp_hw_get_catalog_string(type),
710 __kmp_hw_get_catalog_string(specified[equivalent_type]));
713 specified[equivalent_type] = type;
716 if (i + 1 < hw_subset_depth) {
717 kmp_hw_t next_type = get_equivalent_type(__kmp_hw_subset->at(i + 1).type);
718 if (next_type == KMP_HW_UNKNOWN) {
720 AffHWSubsetNotExistGeneric,
721 __kmp_hw_get_catalog_string(__kmp_hw_subset->at(i + 1).type));
724 int next_topology_level = get_level(next_type);
725 if (level > next_topology_level) {
726 KMP_WARNING(AffHWSubsetOutOfOrder, __kmp_hw_get_catalog_string(type),
727 __kmp_hw_get_catalog_string(next_type));
733 max_count = get_ratio(level);
734 if (max_count < 0 || num + offset > max_count) {
735 bool plural = (num > 1);
736 KMP_WARNING(AffHWSubsetManyGeneric,
737 __kmp_hw_get_catalog_string(type, plural));
744 for (
int i = 0; i < num_hw_threads; ++i) {
745 kmp_hw_thread_t &hw_thread = hw_threads[i];
747 bool should_be_filtered =
false;
748 for (
int level = 0, hw_subset_index = 0;
749 level < depth && hw_subset_index < hw_subset_depth; ++level) {
750 kmp_hw_t topology_type = types[level];
751 auto hw_subset_item = __kmp_hw_subset->at(hw_subset_index);
752 kmp_hw_t hw_subset_type = hw_subset_item.type;
753 if (topology_type != hw_subset_type)
755 int num = hw_subset_item.num;
756 int offset = hw_subset_item.offset;
758 if (hw_thread.sub_ids[level] < offset ||
759 hw_thread.sub_ids[level] >= offset + num) {
760 should_be_filtered =
true;
764 if (!should_be_filtered) {
766 hw_threads[new_index] = hw_thread;
769 #if KMP_AFFINITY_SUPPORTED
770 KMP_CPU_CLR(hw_thread.os_id, __kmp_affin_fullMask);
775 KMP_DEBUG_ASSERT(new_index <= num_hw_threads);
776 num_hw_threads = new_index;
779 _gather_enumeration_information();
780 _discover_uniformity();
782 _set_last_level_cache();
786 bool kmp_topology_t::is_close(
int hwt1,
int hwt2,
int hw_level)
const {
787 if (hw_level >= depth)
790 const kmp_hw_thread_t &t1 = hw_threads[hwt1];
791 const kmp_hw_thread_t &t2 = hw_threads[hwt2];
792 for (
int i = 0; i < (depth - hw_level); ++i) {
793 if (t1.ids[i] != t2.ids[i])
801 #if KMP_AFFINITY_SUPPORTED
802 class kmp_affinity_raii_t {
803 kmp_affin_mask_t *mask;
807 kmp_affinity_raii_t() : restored(false) {
809 KMP_ASSERT(mask != NULL);
810 __kmp_get_system_affinity(mask, TRUE);
813 __kmp_set_system_affinity(mask, TRUE);
817 ~kmp_affinity_raii_t() {
819 __kmp_set_system_affinity(mask, TRUE);
825 bool KMPAffinity::picked_api =
false;
827 void *KMPAffinity::Mask::operator
new(
size_t n) {
return __kmp_allocate(n); }
828 void *KMPAffinity::Mask::operator
new[](
size_t n) {
return __kmp_allocate(n); }
829 void KMPAffinity::Mask::operator
delete(
void *p) { __kmp_free(p); }
830 void KMPAffinity::Mask::operator
delete[](
void *p) { __kmp_free(p); }
831 void *KMPAffinity::operator
new(
size_t n) {
return __kmp_allocate(n); }
832 void KMPAffinity::operator
delete(
void *p) { __kmp_free(p); }
834 void KMPAffinity::pick_api() {
835 KMPAffinity *affinity_dispatch;
841 if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
842 __kmp_affinity_type != affinity_disabled) {
843 affinity_dispatch =
new KMPHwlocAffinity();
847 affinity_dispatch =
new KMPNativeAffinity();
849 __kmp_affinity_dispatch = affinity_dispatch;
853 void KMPAffinity::destroy_api() {
854 if (__kmp_affinity_dispatch != NULL) {
855 delete __kmp_affinity_dispatch;
856 __kmp_affinity_dispatch = NULL;
861 #define KMP_ADVANCE_SCAN(scan) \
862 while (*scan != '\0') { \
870 char *__kmp_affinity_print_mask(
char *buf,
int buf_len,
871 kmp_affin_mask_t *mask) {
872 int start = 0, finish = 0, previous = 0;
875 KMP_ASSERT(buf_len >= 40);
878 char *end = buf + buf_len - 1;
881 if (mask->begin() == mask->end()) {
882 KMP_SNPRINTF(scan, end - scan + 1,
"{<empty>}");
883 KMP_ADVANCE_SCAN(scan);
884 KMP_ASSERT(scan <= end);
889 start = mask->begin();
893 for (finish = mask->next(start), previous = start;
894 finish == previous + 1 && finish != mask->end();
895 finish = mask->next(finish)) {
902 KMP_SNPRINTF(scan, end - scan + 1,
"%s",
",");
903 KMP_ADVANCE_SCAN(scan);
908 if (previous - start > 1) {
909 KMP_SNPRINTF(scan, end - scan + 1,
"%u-%u", start, previous);
912 KMP_SNPRINTF(scan, end - scan + 1,
"%u", start);
913 KMP_ADVANCE_SCAN(scan);
914 if (previous - start > 0) {
915 KMP_SNPRINTF(scan, end - scan + 1,
",%u", previous);
918 KMP_ADVANCE_SCAN(scan);
921 if (start == mask->end())
929 KMP_ASSERT(scan <= end);
932 #undef KMP_ADVANCE_SCAN
938 kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
939 kmp_affin_mask_t *mask) {
940 int start = 0, finish = 0, previous = 0;
945 __kmp_str_buf_clear(buf);
948 if (mask->begin() == mask->end()) {
949 __kmp_str_buf_print(buf,
"%s",
"{<empty>}");
954 start = mask->begin();
958 for (finish = mask->next(start), previous = start;
959 finish == previous + 1 && finish != mask->end();
960 finish = mask->next(finish)) {
967 __kmp_str_buf_print(buf,
"%s",
",");
972 if (previous - start > 1) {
973 __kmp_str_buf_print(buf,
"%u-%u", start, previous);
976 __kmp_str_buf_print(buf,
"%u", start);
977 if (previous - start > 0) {
978 __kmp_str_buf_print(buf,
",%u", previous);
983 if (start == mask->end())
989 void __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
992 #if KMP_GROUP_AFFINITY
994 if (__kmp_num_proc_groups > 1) {
996 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
997 for (group = 0; group < __kmp_num_proc_groups; group++) {
999 int num = __kmp_GetActiveProcessorCount(group);
1000 for (i = 0; i < num; i++) {
1001 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
1010 for (proc = 0; proc < __kmp_xproc; proc++) {
1011 KMP_CPU_SET(proc, mask);
1019 kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
1022 static inline bool __kmp_hwloc_is_cache_type(hwloc_obj_t obj) {
1023 #if HWLOC_API_VERSION >= 0x00020000
1024 return hwloc_obj_type_is_cache(obj->type);
1026 return obj->type == HWLOC_OBJ_CACHE;
1031 static inline kmp_hw_t __kmp_hwloc_type_2_topology_type(hwloc_obj_t obj) {
1033 if (__kmp_hwloc_is_cache_type(obj)) {
1034 if (obj->attr->cache.type == HWLOC_OBJ_CACHE_INSTRUCTION)
1035 return KMP_HW_UNKNOWN;
1036 switch (obj->attr->cache.depth) {
1040 #if KMP_MIC_SUPPORTED
1041 if (__kmp_mic_type == mic3) {
1049 return KMP_HW_UNKNOWN;
1052 switch (obj->type) {
1053 case HWLOC_OBJ_PACKAGE:
1054 return KMP_HW_SOCKET;
1055 case HWLOC_OBJ_NUMANODE:
1057 case HWLOC_OBJ_CORE:
1060 return KMP_HW_THREAD;
1061 case HWLOC_OBJ_GROUP:
1062 if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_DIE)
1064 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_TILE)
1066 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_INTEL_MODULE)
1067 return KMP_HW_MODULE;
1068 else if (obj->attr->group.kind == HWLOC_GROUP_KIND_WINDOWS_PROCESSOR_GROUP)
1069 return KMP_HW_PROC_GROUP;
1070 return KMP_HW_UNKNOWN;
1071 #if HWLOC_API_VERSION >= 0x00020100
1076 return KMP_HW_UNKNOWN;
1083 static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
1084 hwloc_obj_type_t type) {
1087 for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
1088 obj->logical_index, type, 0);
1089 first != NULL && hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology,
1090 obj->type, first) == obj;
1091 first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
1100 static int __kmp_hwloc_get_sub_id(hwloc_topology_t t, hwloc_obj_t higher,
1101 hwloc_obj_t lower) {
1103 hwloc_obj_type_t ltype = lower->type;
1104 int lindex = lower->logical_index - 1;
1107 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1108 while (obj && lindex >= 0 &&
1109 hwloc_bitmap_isincluded(obj->cpuset, higher->cpuset)) {
1110 if (obj->userdata) {
1111 sub_id = (int)(RCAST(kmp_intptr_t, obj->userdata));
1116 obj = hwloc_get_obj_by_type(t, ltype, lindex);
1119 lower->userdata = RCAST(
void *, sub_id + 1);
1123 static bool __kmp_affinity_create_hwloc_map(kmp_i18n_id_t *
const msg_id) {
1125 int hw_thread_index, sub_id;
1127 hwloc_obj_t pu, obj, root, prev;
1128 kmp_hw_t types[KMP_HW_LAST];
1129 hwloc_obj_type_t hwloc_types[KMP_HW_LAST];
1131 hwloc_topology_t tp = __kmp_hwloc_topology;
1132 *msg_id = kmp_i18n_null;
1133 if (__kmp_affinity_verbose) {
1134 KMP_INFORM(AffUsingHwloc,
"KMP_AFFINITY");
1137 if (!KMP_AFFINITY_CAPABLE()) {
1140 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1142 hwloc_obj_t o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0);
1144 nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_CORE);
1147 o = hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0);
1149 __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(o, HWLOC_OBJ_PU);
1151 __kmp_nThreadsPerCore = 1;
1152 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1153 if (nCoresPerPkg == 0)
1155 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1159 root = hwloc_get_root_obj(tp);
1163 pu = hwloc_get_pu_obj_by_os_index(tp, __kmp_affin_fullMask->begin());
1166 types[depth] = KMP_HW_THREAD;
1167 hwloc_types[depth] = obj->type;
1169 while (obj != root && obj != NULL) {
1171 #if HWLOC_API_VERSION >= 0x00020000
1172 if (obj->memory_arity) {
1174 for (memory = obj->memory_first_child; memory;
1175 memory = hwloc_get_next_child(tp, obj, memory)) {
1176 if (memory->type == HWLOC_OBJ_NUMANODE)
1179 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1180 types[depth] = KMP_HW_NUMA;
1181 hwloc_types[depth] = memory->type;
1186 type = __kmp_hwloc_type_2_topology_type(obj);
1187 if (type != KMP_HW_UNKNOWN) {
1188 types[depth] = type;
1189 hwloc_types[depth] = obj->type;
1193 KMP_ASSERT(depth > 0);
1196 for (
int i = 0, j = depth - 1; i < j; ++i, --j) {
1197 hwloc_obj_type_t hwloc_temp = hwloc_types[i];
1198 kmp_hw_t temp = types[i];
1199 types[i] = types[j];
1201 hwloc_types[i] = hwloc_types[j];
1202 hwloc_types[j] = hwloc_temp;
1206 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1208 hw_thread_index = 0;
1210 while (pu = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, pu)) {
1211 int index = depth - 1;
1212 bool included = KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask);
1213 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
1216 hw_thread.ids[index] = pu->logical_index;
1217 hw_thread.os_id = pu->os_index;
1222 while (obj != root && obj != NULL) {
1224 #if HWLOC_API_VERSION >= 0x00020000
1228 if (obj->memory_arity) {
1230 for (memory = obj->memory_first_child; memory;
1231 memory = hwloc_get_next_child(tp, obj, memory)) {
1232 if (memory->type == HWLOC_OBJ_NUMANODE)
1235 if (memory && memory->type == HWLOC_OBJ_NUMANODE) {
1236 sub_id = __kmp_hwloc_get_sub_id(tp, memory, prev);
1238 hw_thread.ids[index] = memory->logical_index;
1239 hw_thread.ids[index + 1] = sub_id;
1247 type = __kmp_hwloc_type_2_topology_type(obj);
1248 if (type != KMP_HW_UNKNOWN) {
1249 sub_id = __kmp_hwloc_get_sub_id(tp, obj, prev);
1251 hw_thread.ids[index] = obj->logical_index;
1252 hw_thread.ids[index + 1] = sub_id;
1261 __kmp_topology->sort_ids();
1269 static bool __kmp_affinity_create_flat_map(kmp_i18n_id_t *
const msg_id) {
1270 *msg_id = kmp_i18n_null;
1272 kmp_hw_t types[] = {KMP_HW_SOCKET, KMP_HW_CORE, KMP_HW_THREAD};
1274 if (__kmp_affinity_verbose) {
1275 KMP_INFORM(UsingFlatOS,
"KMP_AFFINITY");
1281 if (!KMP_AFFINITY_CAPABLE()) {
1282 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1283 __kmp_ncores = nPackages = __kmp_xproc;
1284 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1292 __kmp_ncores = nPackages = __kmp_avail_proc;
1293 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1296 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1299 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1301 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1304 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct);
1306 hw_thread.os_id = i;
1307 hw_thread.ids[0] = i;
1308 hw_thread.ids[1] = 0;
1309 hw_thread.ids[2] = 0;
1312 if (__kmp_affinity_verbose) {
1313 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
1318 #if KMP_GROUP_AFFINITY
1323 static bool __kmp_affinity_create_proc_group_map(kmp_i18n_id_t *
const msg_id) {
1324 *msg_id = kmp_i18n_null;
1326 kmp_hw_t types[] = {KMP_HW_PROC_GROUP, KMP_HW_CORE, KMP_HW_THREAD};
1327 const static size_t BITS_PER_GROUP = CHAR_BIT *
sizeof(DWORD_PTR);
1329 if (__kmp_affinity_verbose) {
1330 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
1334 if (!KMP_AFFINITY_CAPABLE()) {
1335 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1336 nPackages = __kmp_num_proc_groups;
1337 __kmp_nThreadsPerCore = 1;
1338 __kmp_ncores = __kmp_xproc;
1339 nCoresPerPkg = nPackages / __kmp_ncores;
1344 __kmp_topology = kmp_topology_t::allocate(__kmp_avail_proc, depth, types);
1347 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1349 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1352 kmp_hw_thread_t &hw_thread = __kmp_topology->at(avail_ct++);
1354 hw_thread.os_id = i;
1355 hw_thread.ids[0] = i / BITS_PER_GROUP;
1356 hw_thread.ids[1] = hw_thread.ids[2] = i % BITS_PER_GROUP;
1362 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1364 template <kmp_u
int32 LSB, kmp_u
int32 MSB>
1365 static inline unsigned __kmp_extract_bits(kmp_uint32 v) {
1366 const kmp_uint32 SHIFT_LEFT =
sizeof(kmp_uint32) * 8 - 1 - MSB;
1367 const kmp_uint32 SHIFT_RIGHT = LSB;
1368 kmp_uint32 retval = v;
1369 retval <<= SHIFT_LEFT;
1370 retval >>= (SHIFT_LEFT + SHIFT_RIGHT);
1374 static int __kmp_cpuid_mask_width(
int count) {
1377 while ((1 << r) < count)
1382 class apicThreadInfo {
1386 unsigned maxCoresPerPkg;
1387 unsigned maxThreadsPerPkg;
1393 static int __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
1395 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
1396 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
1397 if (aa->pkgId < bb->pkgId)
1399 if (aa->pkgId > bb->pkgId)
1401 if (aa->coreId < bb->coreId)
1403 if (aa->coreId > bb->coreId)
1405 if (aa->threadId < bb->threadId)
1407 if (aa->threadId > bb->threadId)
1412 class kmp_cache_info_t {
1415 unsigned level, mask;
1417 kmp_cache_info_t() : depth(0) { get_leaf4_levels(); }
1418 size_t get_depth()
const {
return depth; }
1419 info_t &operator[](
size_t index) {
return table[index]; }
1420 const info_t &operator[](
size_t index)
const {
return table[index]; }
1422 static kmp_hw_t get_topology_type(
unsigned level) {
1423 KMP_DEBUG_ASSERT(level >= 1 && level <= MAX_CACHE_LEVEL);
1432 return KMP_HW_UNKNOWN;
1436 static const int MAX_CACHE_LEVEL = 3;
1439 info_t table[MAX_CACHE_LEVEL];
1441 void get_leaf4_levels() {
1443 while (depth < MAX_CACHE_LEVEL) {
1444 unsigned cache_type, max_threads_sharing;
1445 unsigned cache_level, cache_mask_width;
1447 __kmp_x86_cpuid(4, level, &buf2);
1448 cache_type = __kmp_extract_bits<0, 4>(buf2.eax);
1452 if (cache_type == 2) {
1456 max_threads_sharing = __kmp_extract_bits<14, 25>(buf2.eax) + 1;
1457 cache_mask_width = __kmp_cpuid_mask_width(max_threads_sharing);
1458 cache_level = __kmp_extract_bits<5, 7>(buf2.eax);
1459 table[depth].level = cache_level;
1460 table[depth].mask = ((-1) << cache_mask_width);
1471 static bool __kmp_affinity_create_apicid_map(kmp_i18n_id_t *
const msg_id) {
1473 *msg_id = kmp_i18n_null;
1475 if (__kmp_affinity_verbose) {
1476 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
1480 __kmp_x86_cpuid(0, 0, &buf);
1482 *msg_id = kmp_i18n_str_NoLeaf4Support;
1491 if (!KMP_AFFINITY_CAPABLE()) {
1494 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1500 __kmp_x86_cpuid(1, 0, &buf);
1501 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
1502 if (maxThreadsPerPkg == 0) {
1503 maxThreadsPerPkg = 1;
1517 __kmp_x86_cpuid(0, 0, &buf);
1519 __kmp_x86_cpuid(4, 0, &buf);
1520 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
1538 __kmp_ncores = __kmp_xproc;
1539 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1540 __kmp_nThreadsPerCore = 1;
1549 kmp_affinity_raii_t previous_affinity;
1577 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
1578 __kmp_avail_proc *
sizeof(apicThreadInfo));
1579 unsigned nApics = 0;
1580 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1582 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1585 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
1587 __kmp_affinity_dispatch->bind_thread(i);
1588 threadInfo[nApics].osId = i;
1591 __kmp_x86_cpuid(1, 0, &buf);
1592 if (((buf.edx >> 9) & 1) == 0) {
1593 __kmp_free(threadInfo);
1594 *msg_id = kmp_i18n_str_ApicNotPresent;
1597 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
1598 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
1599 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
1600 threadInfo[nApics].maxThreadsPerPkg = 1;
1609 __kmp_x86_cpuid(0, 0, &buf);
1611 __kmp_x86_cpuid(4, 0, &buf);
1612 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
1614 threadInfo[nApics].maxCoresPerPkg = 1;
1618 int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
1619 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
1621 int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
1622 int widthT = widthCT - widthC;
1627 __kmp_free(threadInfo);
1628 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1632 int maskC = (1 << widthC) - 1;
1633 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
1635 int maskT = (1 << widthT) - 1;
1636 threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
1643 previous_affinity.restore();
1646 qsort(threadInfo, nApics,
sizeof(*threadInfo),
1647 __kmp_affinity_cmp_apicThreadInfo_phys_id);
1664 __kmp_nThreadsPerCore = 1;
1665 unsigned nCores = 1;
1668 unsigned lastPkgId = threadInfo[0].pkgId;
1669 unsigned coreCt = 1;
1670 unsigned lastCoreId = threadInfo[0].coreId;
1671 unsigned threadCt = 1;
1672 unsigned lastThreadId = threadInfo[0].threadId;
1675 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
1676 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
1678 for (i = 1; i < nApics; i++) {
1679 if (threadInfo[i].pkgId != lastPkgId) {
1682 lastPkgId = threadInfo[i].pkgId;
1683 if ((
int)coreCt > nCoresPerPkg)
1684 nCoresPerPkg = coreCt;
1686 lastCoreId = threadInfo[i].coreId;
1687 if ((
int)threadCt > __kmp_nThreadsPerCore)
1688 __kmp_nThreadsPerCore = threadCt;
1690 lastThreadId = threadInfo[i].threadId;
1694 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
1695 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
1699 if (threadInfo[i].coreId != lastCoreId) {
1702 lastCoreId = threadInfo[i].coreId;
1703 if ((
int)threadCt > __kmp_nThreadsPerCore)
1704 __kmp_nThreadsPerCore = threadCt;
1706 lastThreadId = threadInfo[i].threadId;
1707 }
else if (threadInfo[i].threadId != lastThreadId) {
1709 lastThreadId = threadInfo[i].threadId;
1711 __kmp_free(threadInfo);
1712 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
1718 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
1719 (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
1720 __kmp_free(threadInfo);
1721 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1729 if ((
int)coreCt > nCoresPerPkg)
1730 nCoresPerPkg = coreCt;
1731 if ((
int)threadCt > __kmp_nThreadsPerCore)
1732 __kmp_nThreadsPerCore = threadCt;
1733 __kmp_ncores = nCores;
1734 KMP_DEBUG_ASSERT(nApics == (
unsigned)__kmp_avail_proc);
1742 int threadLevel = 2;
1744 int depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
1747 types[idx++] = KMP_HW_SOCKET;
1749 types[idx++] = KMP_HW_CORE;
1750 if (threadLevel >= 0)
1751 types[idx++] = KMP_HW_THREAD;
1753 KMP_ASSERT(depth > 0);
1754 __kmp_topology = kmp_topology_t::allocate(nApics, depth, types);
1756 for (i = 0; i < nApics; ++i) {
1758 unsigned os = threadInfo[i].osId;
1759 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
1762 if (pkgLevel >= 0) {
1763 hw_thread.ids[idx++] = threadInfo[i].pkgId;
1765 if (coreLevel >= 0) {
1766 hw_thread.ids[idx++] = threadInfo[i].coreId;
1768 if (threadLevel >= 0) {
1769 hw_thread.ids[idx++] = threadInfo[i].threadId;
1771 hw_thread.os_id = os;
1774 __kmp_free(threadInfo);
1775 __kmp_topology->sort_ids();
1776 if (!__kmp_topology->check_ids()) {
1777 kmp_topology_t::deallocate(__kmp_topology);
1778 __kmp_topology =
nullptr;
1779 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
1804 INTEL_LEVEL_TYPE_INVALID = 0,
1805 INTEL_LEVEL_TYPE_SMT = 1,
1806 INTEL_LEVEL_TYPE_CORE = 2,
1807 INTEL_LEVEL_TYPE_TILE = 3,
1808 INTEL_LEVEL_TYPE_MODULE = 4,
1809 INTEL_LEVEL_TYPE_DIE = 5,
1810 INTEL_LEVEL_TYPE_LAST = 6,
1813 struct cpuid_level_info_t {
1814 unsigned level_type, mask, mask_width, nitems, cache_mask;
1817 static kmp_hw_t __kmp_intel_type_2_topology_type(
int intel_type) {
1818 switch (intel_type) {
1819 case INTEL_LEVEL_TYPE_INVALID:
1820 return KMP_HW_SOCKET;
1821 case INTEL_LEVEL_TYPE_SMT:
1822 return KMP_HW_THREAD;
1823 case INTEL_LEVEL_TYPE_CORE:
1825 case INTEL_LEVEL_TYPE_TILE:
1827 case INTEL_LEVEL_TYPE_MODULE:
1828 return KMP_HW_MODULE;
1829 case INTEL_LEVEL_TYPE_DIE:
1832 return KMP_HW_UNKNOWN;
1839 __kmp_x2apicid_get_levels(
int leaf,
1840 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST],
1841 kmp_uint64 known_levels) {
1842 unsigned level, levels_index;
1843 unsigned level_type, mask_width, nitems;
1853 level = levels_index = 0;
1855 __kmp_x86_cpuid(leaf, level, &buf);
1856 level_type = __kmp_extract_bits<8, 15>(buf.ecx);
1857 mask_width = __kmp_extract_bits<0, 4>(buf.eax);
1858 nitems = __kmp_extract_bits<0, 15>(buf.ebx);
1859 if (level_type != INTEL_LEVEL_TYPE_INVALID && nitems == 0)
1862 if (known_levels & (1ull << level_type)) {
1864 KMP_ASSERT(levels_index < INTEL_LEVEL_TYPE_LAST);
1865 levels[levels_index].level_type = level_type;
1866 levels[levels_index].mask_width = mask_width;
1867 levels[levels_index].nitems = nitems;
1871 if (levels_index > 0) {
1872 levels[levels_index - 1].mask_width = mask_width;
1873 levels[levels_index - 1].nitems = nitems;
1877 }
while (level_type != INTEL_LEVEL_TYPE_INVALID);
1880 for (
unsigned i = 0; i < levels_index; ++i) {
1881 if (levels[i].level_type != INTEL_LEVEL_TYPE_INVALID) {
1882 levels[i].mask = ~((-1) << levels[i].mask_width);
1883 levels[i].cache_mask = (-1) << levels[i].mask_width;
1884 for (
unsigned j = 0; j < i; ++j)
1885 levels[i].mask ^= levels[j].mask;
1887 KMP_DEBUG_ASSERT(levels_index > 0);
1888 levels[i].mask = (-1) << levels[i - 1].mask_width;
1889 levels[i].cache_mask = 0;
1892 return levels_index;
1895 static bool __kmp_affinity_create_x2apicid_map(kmp_i18n_id_t *
const msg_id) {
1897 cpuid_level_info_t levels[INTEL_LEVEL_TYPE_LAST];
1898 kmp_hw_t types[INTEL_LEVEL_TYPE_LAST];
1899 unsigned levels_index;
1901 kmp_uint64 known_levels;
1902 int topology_leaf, highest_leaf, apic_id;
1904 static int leaves[] = {0, 0};
1906 kmp_i18n_id_t leaf_message_id;
1908 KMP_BUILD_ASSERT(
sizeof(known_levels) * CHAR_BIT > KMP_HW_LAST);
1910 *msg_id = kmp_i18n_null;
1911 if (__kmp_affinity_verbose) {
1912 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
1916 known_levels = 0ull;
1917 for (
int i = 0; i < INTEL_LEVEL_TYPE_LAST; ++i) {
1918 if (__kmp_intel_type_2_topology_type(i) != KMP_HW_UNKNOWN) {
1919 known_levels |= (1ull << i);
1924 __kmp_x86_cpuid(0, 0, &buf);
1925 highest_leaf = buf.eax;
1930 if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
1933 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
1934 }
else if (__kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
1937 leaf_message_id = kmp_i18n_str_NoLeaf31Support;
1942 leaf_message_id = kmp_i18n_str_NoLeaf11Support;
1946 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
1948 for (
int i = 0; i < num_leaves; ++i) {
1949 int leaf = leaves[i];
1950 if (highest_leaf < leaf)
1952 __kmp_x86_cpuid(leaf, 0, &buf);
1955 topology_leaf = leaf;
1956 levels_index = __kmp_x2apicid_get_levels(leaf, levels, known_levels);
1957 if (levels_index == 0)
1961 if (topology_leaf == -1 || levels_index == 0) {
1962 *msg_id = leaf_message_id;
1965 KMP_ASSERT(levels_index <= INTEL_LEVEL_TYPE_LAST);
1972 if (!KMP_AFFINITY_CAPABLE()) {
1975 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1976 for (
unsigned i = 0; i < levels_index; ++i) {
1977 if (levels[i].level_type == INTEL_LEVEL_TYPE_SMT) {
1978 __kmp_nThreadsPerCore = levels[i].nitems;
1979 }
else if (levels[i].level_type == INTEL_LEVEL_TYPE_CORE) {
1980 nCoresPerPkg = levels[i].nitems;
1983 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1984 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1989 int depth = levels_index;
1990 for (
int i = depth - 1, j = 0; i >= 0; --i, ++j)
1991 types[j] = __kmp_intel_type_2_topology_type(levels[i].level_type);
1993 kmp_topology_t::allocate(__kmp_avail_proc, levels_index, types);
1996 kmp_cache_info_t cache_info;
1997 for (
size_t i = 0; i < cache_info.get_depth(); ++i) {
1998 const kmp_cache_info_t::info_t &info = cache_info[i];
1999 unsigned cache_mask = info.mask;
2000 unsigned cache_level = info.level;
2001 for (
unsigned j = 0; j < levels_index; ++j) {
2002 unsigned hw_cache_mask = levels[j].cache_mask;
2003 kmp_hw_t cache_type = kmp_cache_info_t::get_topology_type(cache_level);
2004 if (hw_cache_mask == cache_mask && j < levels_index - 1) {
2006 __kmp_intel_type_2_topology_type(levels[j + 1].level_type);
2007 __kmp_topology->set_equivalent_type(cache_type, type);
2017 kmp_affinity_raii_t previous_affinity;
2022 int hw_thread_index = 0;
2023 KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
2024 cpuid_level_info_t my_levels[INTEL_LEVEL_TYPE_LAST];
2025 unsigned my_levels_index;
2028 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
2031 KMP_DEBUG_ASSERT(hw_thread_index < __kmp_avail_proc);
2033 __kmp_affinity_dispatch->bind_thread(proc);
2036 __kmp_x86_cpuid(topology_leaf, 0, &buf);
2038 kmp_hw_thread_t &hw_thread = __kmp_topology->at(hw_thread_index);
2040 __kmp_x2apicid_get_levels(topology_leaf, my_levels, known_levels);
2041 if (my_levels_index == 0 || my_levels_index != levels_index) {
2042 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
2046 hw_thread.os_id = proc;
2048 for (
unsigned j = 0, idx = depth - 1; j < my_levels_index; ++j, --idx) {
2049 hw_thread.ids[idx] = apic_id & my_levels[j].mask;
2051 hw_thread.ids[idx] >>= my_levels[j - 1].mask_width;
2056 KMP_ASSERT(hw_thread_index > 0);
2057 __kmp_topology->sort_ids();
2058 if (!__kmp_topology->check_ids()) {
2059 kmp_topology_t::deallocate(__kmp_topology);
2060 __kmp_topology =
nullptr;
2061 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
2069 #define threadIdIndex 1
2070 #define coreIdIndex 2
2071 #define pkgIdIndex 3
2072 #define nodeIdIndex 4
2074 typedef unsigned *ProcCpuInfo;
2075 static unsigned maxIndex = pkgIdIndex;
2077 static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
2080 const unsigned *aa = *(
unsigned *
const *)a;
2081 const unsigned *bb = *(
unsigned *
const *)b;
2082 for (i = maxIndex;; i--) {
2093 #if KMP_USE_HIER_SCHED
2095 static void __kmp_dispatch_set_hierarchy_values() {
2101 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
2102 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2103 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
2104 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2106 if (__kmp_mic_type >= mic3)
2107 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
2110 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
2111 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
2112 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
2113 __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
2116 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
2117 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
2118 __kmp_nThreadsPerCore;
2119 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_WINDOWS) && \
2121 if (__kmp_mic_type >= mic3)
2122 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2123 2 * __kmp_nThreadsPerCore;
2126 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
2127 __kmp_nThreadsPerCore;
2128 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
2129 nCoresPerPkg * __kmp_nThreadsPerCore;
2130 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
2131 nCoresPerPkg * __kmp_nThreadsPerCore;
2132 __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
2133 nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
2138 int __kmp_dispatch_get_index(
int tid, kmp_hier_layer_e type) {
2139 int index = type + 1;
2140 int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
2141 KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
2142 if (type == kmp_hier_layer_e::LAYER_THREAD)
2144 else if (type == kmp_hier_layer_e::LAYER_LOOP)
2146 KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
2147 if (tid >= num_hw_threads)
2148 tid = tid % num_hw_threads;
2149 return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
2153 int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
2156 KMP_DEBUG_ASSERT(i1 <= i2);
2157 KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
2158 KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
2159 KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
2161 return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
2165 static inline const char *__kmp_cpuinfo_get_filename() {
2166 const char *filename;
2167 if (__kmp_cpuinfo_file !=
nullptr)
2168 filename = __kmp_cpuinfo_file;
2170 filename =
"/proc/cpuinfo";
2174 static inline const char *__kmp_cpuinfo_get_envvar() {
2175 const char *envvar =
nullptr;
2176 if (__kmp_cpuinfo_file !=
nullptr)
2177 envvar =
"KMP_CPUINFO_FILE";
2183 static bool __kmp_affinity_create_cpuinfo_map(
int *line,
2184 kmp_i18n_id_t *
const msg_id) {
2185 const char *filename = __kmp_cpuinfo_get_filename();
2186 const char *envvar = __kmp_cpuinfo_get_envvar();
2187 *msg_id = kmp_i18n_null;
2189 if (__kmp_affinity_verbose) {
2190 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
2198 unsigned num_records = 0;
2200 buf[
sizeof(buf) - 1] = 1;
2201 if (!fgets(buf,
sizeof(buf), f)) {
2206 char s1[] =
"processor";
2207 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2214 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
2216 if (level > (
unsigned)__kmp_xproc) {
2217 level = __kmp_xproc;
2219 if (nodeIdIndex + level >= maxIndex) {
2220 maxIndex = nodeIdIndex + level;
2228 if (num_records == 0) {
2229 *msg_id = kmp_i18n_str_NoProcRecords;
2232 if (num_records > (
unsigned)__kmp_xproc) {
2233 *msg_id = kmp_i18n_str_TooManyProcRecords;
2242 if (fseek(f, 0, SEEK_SET) != 0) {
2243 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
2249 unsigned **threadInfo =
2250 (
unsigned **)__kmp_allocate((num_records + 1) *
sizeof(
unsigned *));
2252 for (i = 0; i <= num_records; i++) {
2254 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2257 #define CLEANUP_THREAD_INFO \
2258 for (i = 0; i <= num_records; i++) { \
2259 __kmp_free(threadInfo[i]); \
2261 __kmp_free(threadInfo);
2266 #define INIT_PROC_INFO(p) \
2267 for (__index = 0; __index <= maxIndex; __index++) { \
2268 (p)[__index] = UINT_MAX; \
2271 for (i = 0; i <= num_records; i++) {
2272 INIT_PROC_INFO(threadInfo[i]);
2275 unsigned num_avail = 0;
2282 buf[
sizeof(buf) - 1] = 1;
2283 bool long_line =
false;
2284 if (!fgets(buf,
sizeof(buf), f)) {
2289 for (i = 0; i <= maxIndex; i++) {
2290 if (threadInfo[num_avail][i] != UINT_MAX) {
2298 }
else if (!buf[
sizeof(buf) - 1]) {
2303 #define CHECK_LINE \
2305 CLEANUP_THREAD_INFO; \
2306 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
2312 char s1[] =
"processor";
2313 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
2315 char *p = strchr(buf +
sizeof(s1) - 1,
':');
2317 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2319 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
2320 #if KMP_ARCH_AARCH64
2329 threadInfo[num_avail][osIdIndex] = val;
2330 #if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
2334 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
2335 threadInfo[num_avail][osIdIndex]);
2336 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
2338 KMP_SNPRINTF(path,
sizeof(path),
2339 "/sys/devices/system/cpu/cpu%u/topology/core_id",
2340 threadInfo[num_avail][osIdIndex]);
2341 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
2345 char s2[] =
"physical id";
2346 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
2348 char *p = strchr(buf +
sizeof(s2) - 1,
':');
2350 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2352 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
2354 threadInfo[num_avail][pkgIdIndex] = val;
2357 char s3[] =
"core id";
2358 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
2360 char *p = strchr(buf +
sizeof(s3) - 1,
':');
2362 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2364 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
2366 threadInfo[num_avail][coreIdIndex] = val;
2370 char s4[] =
"thread id";
2371 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
2373 char *p = strchr(buf +
sizeof(s4) - 1,
':');
2375 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2377 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
2379 threadInfo[num_avail][threadIdIndex] = val;
2383 if (KMP_SSCANF(buf,
"node_%u id", &level) == 1) {
2385 char *p = strchr(buf +
sizeof(s4) - 1,
':');
2387 if ((p == NULL) || (KMP_SSCANF(p + 1,
"%u\n", &val) != 1))
2390 if (level > (
unsigned)__kmp_xproc) {
2391 level = __kmp_xproc;
2393 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
2395 threadInfo[num_avail][nodeIdIndex + level] = val;
2402 if ((*buf != 0) && (*buf !=
'\n')) {
2407 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'))
2415 if ((
int)num_avail == __kmp_xproc) {
2416 CLEANUP_THREAD_INFO;
2417 *msg_id = kmp_i18n_str_TooManyEntries;
2423 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
2424 CLEANUP_THREAD_INFO;
2425 *msg_id = kmp_i18n_str_MissingProcField;
2428 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
2429 CLEANUP_THREAD_INFO;
2430 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
2435 if (!KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
2436 __kmp_affin_fullMask)) {
2437 INIT_PROC_INFO(threadInfo[num_avail]);
2444 KMP_ASSERT(num_avail <= num_records);
2445 INIT_PROC_INFO(threadInfo[num_avail]);
2450 CLEANUP_THREAD_INFO;
2451 *msg_id = kmp_i18n_str_MissingValCpuinfo;
2455 CLEANUP_THREAD_INFO;
2456 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
2461 #if KMP_MIC && REDUCE_TEAM_SIZE
2462 unsigned teamSize = 0;
2470 KMP_ASSERT(num_avail > 0);
2471 KMP_ASSERT(num_avail <= num_records);
2474 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
2475 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
2487 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2489 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2491 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2493 (
unsigned *)__kmp_allocate((maxIndex + 1) *
sizeof(unsigned));
2495 bool assign_thread_ids =
false;
2496 unsigned threadIdCt;
2499 restart_radix_check:
2503 if (assign_thread_ids) {
2504 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
2505 threadInfo[0][threadIdIndex] = threadIdCt++;
2506 }
else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
2507 threadIdCt = threadInfo[0][threadIdIndex] + 1;
2510 for (index = 0; index <= maxIndex; index++) {
2514 lastId[index] = threadInfo[0][index];
2519 for (i = 1; i < num_avail; i++) {
2522 for (index = maxIndex; index >= threadIdIndex; index--) {
2523 if (assign_thread_ids && (index == threadIdIndex)) {
2525 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2526 threadInfo[i][threadIdIndex] = threadIdCt++;
2530 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2531 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2534 if (threadInfo[i][index] != lastId[index]) {
2539 for (index2 = threadIdIndex; index2 < index; index2++) {
2541 if (counts[index2] > maxCt[index2]) {
2542 maxCt[index2] = counts[index2];
2545 lastId[index2] = threadInfo[i][index2];
2549 lastId[index] = threadInfo[i][index];
2551 if (assign_thread_ids && (index > threadIdIndex)) {
2553 #if KMP_MIC && REDUCE_TEAM_SIZE
2556 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
2563 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2564 threadInfo[i][threadIdIndex] = threadIdCt++;
2570 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2571 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2577 if (index < threadIdIndex) {
2581 if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
2586 CLEANUP_THREAD_INFO;
2587 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2593 assign_thread_ids =
true;
2594 goto restart_radix_check;
2598 #if KMP_MIC && REDUCE_TEAM_SIZE
2601 teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
2604 for (index = threadIdIndex; index <= maxIndex; index++) {
2605 if (counts[index] > maxCt[index]) {
2606 maxCt[index] = counts[index];
2610 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
2611 nCoresPerPkg = maxCt[coreIdIndex];
2612 nPackages = totals[pkgIdIndex];
2618 __kmp_ncores = totals[coreIdIndex];
2619 if (!KMP_AFFINITY_CAPABLE()) {
2620 KMP_ASSERT(__kmp_affinity_type == affinity_none);
2624 #if KMP_MIC && REDUCE_TEAM_SIZE
2626 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
2627 __kmp_dflt_team_nth = teamSize;
2628 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting "
2629 "__kmp_dflt_team_nth = %d\n",
2630 __kmp_dflt_team_nth));
2634 KMP_DEBUG_ASSERT(num_avail == (
unsigned)__kmp_avail_proc);
2641 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
2642 for (index = threadIdIndex; index < maxIndex; index++) {
2643 KMP_ASSERT(totals[index] >= totals[index + 1]);
2644 inMap[index] = (totals[index] > totals[index + 1]);
2646 inMap[maxIndex] = (totals[maxIndex] > 1);
2647 inMap[pkgIdIndex] =
true;
2648 inMap[coreIdIndex] =
true;
2649 inMap[threadIdIndex] =
true;
2653 kmp_hw_t types[KMP_HW_LAST];
2656 int threadLevel = -1;
2657 for (index = threadIdIndex; index <= maxIndex; index++) {
2662 if (inMap[pkgIdIndex]) {
2664 types[idx++] = KMP_HW_SOCKET;
2666 if (inMap[coreIdIndex]) {
2668 types[idx++] = KMP_HW_CORE;
2670 if (inMap[threadIdIndex]) {
2672 types[idx++] = KMP_HW_THREAD;
2674 KMP_ASSERT(depth > 0);
2677 __kmp_topology = kmp_topology_t::allocate(num_avail, depth, types);
2679 for (i = 0; i < num_avail; ++i) {
2680 unsigned os = threadInfo[i][osIdIndex];
2683 kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
2685 hw_thread.os_id = os;
2688 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
2689 if (!inMap[src_index]) {
2692 if (src_index == pkgIdIndex) {
2693 hw_thread.ids[pkgLevel] = threadInfo[i][src_index];
2694 }
else if (src_index == coreIdIndex) {
2695 hw_thread.ids[coreLevel] = threadInfo[i][src_index];
2696 }
else if (src_index == threadIdIndex) {
2697 hw_thread.ids[threadLevel] = threadInfo[i][src_index];
2708 CLEANUP_THREAD_INFO;
2709 __kmp_topology->sort_ids();
2710 if (!__kmp_topology->check_ids()) {
2711 kmp_topology_t::deallocate(__kmp_topology);
2712 __kmp_topology =
nullptr;
2713 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2722 static kmp_affin_mask_t *__kmp_create_masks(
unsigned *maxIndex,
2723 unsigned *numUnique) {
2727 int numAddrs = __kmp_topology->get_num_hw_threads();
2728 int depth = __kmp_topology->get_depth();
2729 KMP_ASSERT(numAddrs);
2733 for (i = numAddrs - 1;; --i) {
2734 int osId = __kmp_topology->at(i).os_id;
2735 if (osId > maxOsId) {
2741 kmp_affin_mask_t *osId2Mask;
2742 KMP_CPU_ALLOC_ARRAY(osId2Mask, (maxOsId + 1));
2743 KMP_ASSERT(__kmp_affinity_gran_levels >= 0);
2744 if (__kmp_affinity_verbose && (__kmp_affinity_gran_levels > 0)) {
2745 KMP_INFORM(ThreadsMigrate,
"KMP_AFFINITY", __kmp_affinity_gran_levels);
2747 if (__kmp_affinity_gran_levels >= (
int)depth) {
2748 if (__kmp_affinity_verbose ||
2749 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
2750 KMP_WARNING(AffThreadsMayMigrate);
2761 kmp_affin_mask_t *sum;
2762 KMP_CPU_ALLOC_ON_STACK(sum);
2764 KMP_CPU_SET(__kmp_topology->at(0).os_id, sum);
2765 for (i = 1; i < numAddrs; i++) {
2769 if (__kmp_topology->is_close(leader, i, __kmp_affinity_gran_levels)) {
2770 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
2776 for (; j < i; j++) {
2777 int osId = __kmp_topology->at(j).os_id;
2778 KMP_DEBUG_ASSERT(osId <= maxOsId);
2779 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2780 KMP_CPU_COPY(mask, sum);
2781 __kmp_topology->at(j).leader = (j == leader);
2788 KMP_CPU_SET(__kmp_topology->at(i).os_id, sum);
2793 for (; j < i; j++) {
2794 int osId = __kmp_topology->at(j).os_id;
2795 KMP_DEBUG_ASSERT(osId <= maxOsId);
2796 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2797 KMP_CPU_COPY(mask, sum);
2798 __kmp_topology->at(j).leader = (j == leader);
2801 KMP_CPU_FREE_FROM_STACK(sum);
2803 *maxIndex = maxOsId;
2804 *numUnique = unique;
2811 static kmp_affin_mask_t *newMasks;
2812 static int numNewMasks;
2813 static int nextNewMask;
2815 #define ADD_MASK(_mask) \
2817 if (nextNewMask >= numNewMasks) { \
2820 kmp_affin_mask_t *temp; \
2821 KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks); \
2822 for (i = 0; i < numNewMasks / 2; i++) { \
2823 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i); \
2824 kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i); \
2825 KMP_CPU_COPY(dest, src); \
2827 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2); \
2830 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
2834 #define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId) \
2836 if (((_osId) > _maxOsId) || \
2837 (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) { \
2838 if (__kmp_affinity_verbose || \
2839 (__kmp_affinity_warnings && \
2840 (__kmp_affinity_type != affinity_none))) { \
2841 KMP_WARNING(AffIgnoreInvalidProcID, _osId); \
2844 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
2850 static void __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
2851 unsigned int *out_numMasks,
2852 const char *proclist,
2853 kmp_affin_mask_t *osId2Mask,
2856 const char *scan = proclist;
2857 const char *next = proclist;
2862 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
2864 kmp_affin_mask_t *sumMask;
2865 KMP_CPU_ALLOC(sumMask);
2869 int start, end, stride;
2873 if (*next ==
'\0') {
2885 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad proclist");
2887 num = __kmp_str_to_int(scan, *next);
2888 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2891 if ((num > maxOsId) ||
2892 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2893 if (__kmp_affinity_verbose ||
2894 (__kmp_affinity_warnings &&
2895 (__kmp_affinity_type != affinity_none))) {
2896 KMP_WARNING(AffIgnoreInvalidProcID, num);
2898 KMP_CPU_ZERO(sumMask);
2900 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2920 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2923 num = __kmp_str_to_int(scan, *next);
2924 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2927 if ((num > maxOsId) ||
2928 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2929 if (__kmp_affinity_verbose ||
2930 (__kmp_affinity_warnings &&
2931 (__kmp_affinity_type != affinity_none))) {
2932 KMP_WARNING(AffIgnoreInvalidProcID, num);
2935 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2952 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2954 start = __kmp_str_to_int(scan, *next);
2955 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
2960 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2974 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2976 end = __kmp_str_to_int(scan, *next);
2977 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
2994 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2996 stride = __kmp_str_to_int(scan, *next);
2997 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
3002 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
3004 KMP_ASSERT2(start <= end,
"bad explicit proc list");
3006 KMP_ASSERT2(start >= end,
"bad explicit proc list");
3008 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
3013 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3015 }
while (start <= end);
3018 ADD_MASK_OSID(start, osId2Mask, maxOsId);
3020 }
while (start >= end);
3031 *out_numMasks = nextNewMask;
3032 if (nextNewMask == 0) {
3034 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3037 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3038 for (i = 0; i < nextNewMask; i++) {
3039 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3040 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3041 KMP_CPU_COPY(dest, src);
3043 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3044 KMP_CPU_FREE(sumMask);
3067 static void __kmp_process_subplace_list(
const char **scan,
3068 kmp_affin_mask_t *osId2Mask,
3069 int maxOsId, kmp_affin_mask_t *tempMask,
3074 int start, count, stride, i;
3078 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3081 start = __kmp_str_to_int(*scan, *next);
3082 KMP_ASSERT(start >= 0);
3087 if (**scan ==
'}' || **scan ==
',') {
3088 if ((start > maxOsId) ||
3089 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3090 if (__kmp_affinity_verbose ||
3091 (__kmp_affinity_warnings &&
3092 (__kmp_affinity_type != affinity_none))) {
3093 KMP_WARNING(AffIgnoreInvalidProcID, start);
3096 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3099 if (**scan ==
'}') {
3105 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3110 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3113 count = __kmp_str_to_int(*scan, *next);
3114 KMP_ASSERT(count >= 0);
3119 if (**scan ==
'}' || **scan ==
',') {
3120 for (i = 0; i < count; i++) {
3121 if ((start > maxOsId) ||
3122 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3123 if (__kmp_affinity_verbose ||
3124 (__kmp_affinity_warnings &&
3125 (__kmp_affinity_type != affinity_none))) {
3126 KMP_WARNING(AffIgnoreInvalidProcID, start);
3130 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3135 if (**scan ==
'}') {
3141 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
3148 if (**scan ==
'+') {
3152 if (**scan ==
'-') {
3160 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
"bad explicit places list");
3163 stride = __kmp_str_to_int(*scan, *next);
3164 KMP_ASSERT(stride >= 0);
3170 if (**scan ==
'}' || **scan ==
',') {
3171 for (i = 0; i < count; i++) {
3172 if ((start > maxOsId) ||
3173 (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3174 if (__kmp_affinity_verbose ||
3175 (__kmp_affinity_warnings &&
3176 (__kmp_affinity_type != affinity_none))) {
3177 KMP_WARNING(AffIgnoreInvalidProcID, start);
3181 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3186 if (**scan ==
'}') {
3193 KMP_ASSERT2(0,
"bad explicit places list");
3197 static void __kmp_process_place(
const char **scan, kmp_affin_mask_t *osId2Mask,
3198 int maxOsId, kmp_affin_mask_t *tempMask,
3204 if (**scan ==
'{') {
3206 __kmp_process_subplace_list(scan, osId2Mask, maxOsId, tempMask, setSize);
3207 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3209 }
else if (**scan ==
'!') {
3211 __kmp_process_place(scan, osId2Mask, maxOsId, tempMask, setSize);
3212 KMP_CPU_COMPLEMENT(maxOsId, tempMask);
3213 }
else if ((**scan >=
'0') && (**scan <=
'9')) {
3216 int num = __kmp_str_to_int(*scan, *next);
3217 KMP_ASSERT(num >= 0);
3218 if ((num > maxOsId) ||
3219 (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3220 if (__kmp_affinity_verbose ||
3221 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
3222 KMP_WARNING(AffIgnoreInvalidProcID, num);
3225 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3230 KMP_ASSERT2(0,
"bad explicit places list");
3235 void __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
3236 unsigned int *out_numMasks,
3237 const char *placelist,
3238 kmp_affin_mask_t *osId2Mask,
3240 int i, j, count, stride, sign;
3241 const char *scan = placelist;
3242 const char *next = placelist;
3245 KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3251 kmp_affin_mask_t *tempMask;
3252 kmp_affin_mask_t *previousMask;
3253 KMP_CPU_ALLOC(tempMask);
3254 KMP_CPU_ZERO(tempMask);
3255 KMP_CPU_ALLOC(previousMask);
3256 KMP_CPU_ZERO(previousMask);
3260 __kmp_process_place(&scan, osId2Mask, maxOsId, tempMask, &setSize);
3264 if (*scan ==
'\0' || *scan ==
',') {
3268 KMP_CPU_ZERO(tempMask);
3270 if (*scan ==
'\0') {
3277 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3282 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3285 count = __kmp_str_to_int(scan, *next);
3286 KMP_ASSERT(count >= 0);
3291 if (*scan ==
'\0' || *scan ==
',') {
3294 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3313 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
"bad explicit places list");
3316 stride = __kmp_str_to_int(scan, *next);
3317 KMP_DEBUG_ASSERT(stride >= 0);
3323 for (i = 0; i < count; i++) {
3328 KMP_CPU_COPY(previousMask, tempMask);
3329 ADD_MASK(previousMask);
3330 KMP_CPU_ZERO(tempMask);
3332 KMP_CPU_SET_ITERATE(j, previousMask) {
3333 if (!KMP_CPU_ISSET(j, previousMask)) {
3336 if ((j + stride > maxOsId) || (j + stride < 0) ||
3337 (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
3338 (!KMP_CPU_ISSET(j + stride,
3339 KMP_CPU_INDEX(osId2Mask, j + stride)))) {
3340 if ((__kmp_affinity_verbose ||
3341 (__kmp_affinity_warnings &&
3342 (__kmp_affinity_type != affinity_none))) &&
3344 KMP_WARNING(AffIgnoreInvalidProcID, j + stride);
3348 KMP_CPU_SET(j + stride, tempMask);
3352 KMP_CPU_ZERO(tempMask);
3357 if (*scan ==
'\0') {
3365 KMP_ASSERT2(0,
"bad explicit places list");
3368 *out_numMasks = nextNewMask;
3369 if (nextNewMask == 0) {
3371 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3374 KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3375 KMP_CPU_FREE(tempMask);
3376 KMP_CPU_FREE(previousMask);
3377 for (i = 0; i < nextNewMask; i++) {
3378 kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3379 kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3380 KMP_CPU_COPY(dest, src);
3382 KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3386 #undef ADD_MASK_OSID
3390 static int __kmp_affinity_find_core_level(
int nprocs,
int bottom_level) {
3393 for (
int i = 0; i < nprocs; i++) {
3394 const kmp_hw_thread_t &hw_thread = __kmp_topology->at(i);
3395 for (
int j = bottom_level; j > 0; j--) {
3396 if (hw_thread.ids[j] > 0) {
3397 if (core_level < (j - 1)) {
3407 static int __kmp_affinity_compute_ncores(
int nprocs,
int bottom_level,
3409 return __kmp_topology->get_count(core_level);
3412 static int __kmp_affinity_find_core(
int proc,
int bottom_level,
3415 KMP_DEBUG_ASSERT(proc >= 0 && proc < __kmp_topology->get_num_hw_threads());
3416 for (
int i = 0; i <= proc; ++i) {
3417 if (i + 1 <= proc) {
3418 for (
int j = 0; j <= core_level; ++j) {
3419 if (__kmp_topology->at(i + 1).sub_ids[j] !=
3420 __kmp_topology->at(i).sub_ids[j]) {
3432 static int __kmp_affinity_max_proc_per_core(
int nprocs,
int bottom_level,
3434 if (core_level >= bottom_level)
3436 int thread_level = __kmp_topology->get_level(KMP_HW_THREAD);
3437 return __kmp_topology->calculate_ratio(thread_level, core_level);
3440 static int *procarr = NULL;
3441 static int __kmp_aff_depth = 0;
3445 static void __kmp_create_affinity_none_places() {
3446 KMP_ASSERT(__kmp_affin_fullMask != NULL);
3447 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3448 __kmp_affinity_num_masks = 1;
3449 KMP_CPU_ALLOC_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
3450 kmp_affin_mask_t *dest = KMP_CPU_INDEX(__kmp_affinity_masks, 0);
3451 KMP_CPU_COPY(dest, __kmp_affin_fullMask);
3454 static void __kmp_aux_affinity_initialize(
void) {
3455 if (__kmp_affinity_masks != NULL) {
3456 KMP_ASSERT(__kmp_affin_fullMask != NULL);
3464 if (__kmp_affin_fullMask == NULL) {
3465 KMP_CPU_ALLOC(__kmp_affin_fullMask);
3467 if (KMP_AFFINITY_CAPABLE()) {
3468 __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
3469 if (__kmp_affinity_respect_mask) {
3472 __kmp_avail_proc = 0;
3473 KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
3474 if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
3479 if (__kmp_avail_proc > __kmp_xproc) {
3480 if (__kmp_affinity_verbose ||
3481 (__kmp_affinity_warnings &&
3482 (__kmp_affinity_type != affinity_none))) {
3483 KMP_WARNING(ErrorInitializeAffinity);
3485 __kmp_affinity_type = affinity_none;
3486 KMP_AFFINITY_DISABLE();
3490 if (__kmp_affinity_verbose) {
3491 char buf[KMP_AFFIN_MASK_PRINT_LEN];
3492 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
3493 __kmp_affin_fullMask);
3494 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
3497 if (__kmp_affinity_verbose) {
3498 char buf[KMP_AFFIN_MASK_PRINT_LEN];
3499 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
3500 __kmp_affin_fullMask);
3501 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
3503 __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
3504 __kmp_avail_proc = __kmp_xproc;
3508 __kmp_affin_fullMask->set_process_affinity(
true);
3513 kmp_i18n_id_t msg_id = kmp_i18n_null;
3517 if ((__kmp_cpuinfo_file != NULL) &&
3518 (__kmp_affinity_top_method == affinity_top_method_all)) {
3519 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3522 bool success =
false;
3523 if (__kmp_affinity_top_method == affinity_top_method_all) {
3529 __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
3530 if (!__kmp_hwloc_error) {
3531 success = __kmp_affinity_create_hwloc_map(&msg_id);
3532 if (!success && __kmp_affinity_verbose) {
3533 KMP_INFORM(AffIgnoringHwloc,
"KMP_AFFINITY");
3535 }
else if (__kmp_affinity_verbose) {
3536 KMP_INFORM(AffIgnoringHwloc,
"KMP_AFFINITY");
3541 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3543 success = __kmp_affinity_create_x2apicid_map(&msg_id);
3544 if (!success && __kmp_affinity_verbose && msg_id != kmp_i18n_null) {
3545 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id));
3549 success = __kmp_affinity_create_apicid_map(&msg_id);
3550 if (!success && __kmp_affinity_verbose && msg_id != kmp_i18n_null) {
3551 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id));
3559 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
3560 if (!success && __kmp_affinity_verbose && msg_id != kmp_i18n_null) {
3561 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id));
3566 #if KMP_GROUP_AFFINITY
3567 if (!success && (__kmp_num_proc_groups > 1)) {
3568 success = __kmp_affinity_create_proc_group_map(&msg_id);
3569 if (!success && __kmp_affinity_verbose && msg_id != kmp_i18n_null) {
3570 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id));
3576 success = __kmp_affinity_create_flat_map(&msg_id);
3577 if (!success && __kmp_affinity_verbose && msg_id != kmp_i18n_null) {
3578 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id));
3580 KMP_ASSERT(success);
3588 else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
3589 KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
3590 success = __kmp_affinity_create_hwloc_map(&msg_id);
3592 KMP_ASSERT(msg_id != kmp_i18n_null);
3593 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3598 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3599 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid ||
3600 __kmp_affinity_top_method == affinity_top_method_x2apicid_1f) {
3601 success = __kmp_affinity_create_x2apicid_map(&msg_id);
3603 KMP_ASSERT(msg_id != kmp_i18n_null);
3604 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3606 }
else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
3607 success = __kmp_affinity_create_apicid_map(&msg_id);
3609 KMP_ASSERT(msg_id != kmp_i18n_null);
3610 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3615 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
3617 success = __kmp_affinity_create_cpuinfo_map(&line, &msg_id);
3619 KMP_ASSERT(msg_id != kmp_i18n_null);
3620 const char *filename = __kmp_cpuinfo_get_filename();
3622 KMP_FATAL(FileLineMsgExiting, filename, line,
3623 __kmp_i18n_catgets(msg_id));
3625 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
3630 #if KMP_GROUP_AFFINITY
3631 else if (__kmp_affinity_top_method == affinity_top_method_group) {
3632 success = __kmp_affinity_create_proc_group_map(&msg_id);
3633 KMP_ASSERT(success);
3635 KMP_ASSERT(msg_id != kmp_i18n_null);
3636 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3641 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
3642 success = __kmp_affinity_create_flat_map(&msg_id);
3644 KMP_ASSERT(success);
3648 if (!__kmp_topology) {
3649 if (KMP_AFFINITY_CAPABLE() &&
3650 (__kmp_affinity_verbose ||
3651 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none)))) {
3652 KMP_WARNING(ErrorInitializeAffinity);
3654 if (nPackages > 0 && nCoresPerPkg > 0 && __kmp_nThreadsPerCore > 0 &&
3656 __kmp_topology = kmp_topology_t::allocate(0, 0, NULL);
3657 __kmp_topology->canonicalize(nPackages, nCoresPerPkg,
3658 __kmp_nThreadsPerCore, __kmp_ncores);
3659 if (__kmp_affinity_verbose) {
3660 __kmp_topology->print(
"KMP_AFFINITY");
3663 __kmp_affinity_type = affinity_none;
3664 __kmp_create_affinity_none_places();
3665 #if KMP_USE_HIER_SCHED
3666 __kmp_dispatch_set_hierarchy_values();
3668 KMP_AFFINITY_DISABLE();
3674 __kmp_topology->canonicalize();
3675 if (__kmp_affinity_verbose)
3676 __kmp_topology->print(
"KMP_AFFINITY");
3677 bool filtered = __kmp_topology->filter_hw_subset();
3678 if (filtered && __kmp_affinity_verbose)
3679 __kmp_topology->print(
"KMP_HW_SUBSET");
3680 machine_hierarchy.init(__kmp_topology->get_num_hw_threads());
3681 KMP_ASSERT(__kmp_avail_proc == __kmp_topology->get_num_hw_threads());
3685 if (__kmp_affinity_type == affinity_none) {
3686 __kmp_create_affinity_none_places();
3687 #if KMP_USE_HIER_SCHED
3688 __kmp_dispatch_set_hierarchy_values();
3692 int depth = __kmp_topology->get_depth();
3697 kmp_affin_mask_t *osId2Mask = __kmp_create_masks(&maxIndex, &numUnique);
3698 if (__kmp_affinity_gran_levels == 0) {
3699 KMP_DEBUG_ASSERT((
int)numUnique == __kmp_avail_proc);
3702 switch (__kmp_affinity_type) {
3704 case affinity_explicit:
3705 KMP_DEBUG_ASSERT(__kmp_affinity_proclist != NULL);
3706 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) {
3707 __kmp_affinity_process_proclist(
3708 &__kmp_affinity_masks, &__kmp_affinity_num_masks,
3709 __kmp_affinity_proclist, osId2Mask, maxIndex);
3711 __kmp_affinity_process_placelist(
3712 &__kmp_affinity_masks, &__kmp_affinity_num_masks,
3713 __kmp_affinity_proclist, osId2Mask, maxIndex);
3715 if (__kmp_affinity_num_masks == 0) {
3716 if (__kmp_affinity_verbose ||
3717 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
3718 KMP_WARNING(AffNoValidProcID);
3720 __kmp_affinity_type = affinity_none;
3721 __kmp_create_affinity_none_places();
3730 case affinity_logical:
3731 __kmp_affinity_compact = 0;
3732 if (__kmp_affinity_offset) {
3733 __kmp_affinity_offset =
3734 __kmp_nThreadsPerCore * __kmp_affinity_offset % __kmp_avail_proc;
3738 case affinity_physical:
3739 if (__kmp_nThreadsPerCore > 1) {
3740 __kmp_affinity_compact = 1;
3741 if (__kmp_affinity_compact >= depth) {
3742 __kmp_affinity_compact = 0;
3745 __kmp_affinity_compact = 0;
3747 if (__kmp_affinity_offset) {
3748 __kmp_affinity_offset =
3749 __kmp_nThreadsPerCore * __kmp_affinity_offset % __kmp_avail_proc;
3753 case affinity_scatter:
3754 if (__kmp_affinity_compact >= depth) {
3755 __kmp_affinity_compact = 0;
3757 __kmp_affinity_compact = depth - 1 - __kmp_affinity_compact;
3761 case affinity_compact:
3762 if (__kmp_affinity_compact >= depth) {
3763 __kmp_affinity_compact = depth - 1;
3767 case affinity_balanced:
3769 if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
3770 KMP_WARNING(AffBalancedNotAvail,
"KMP_AFFINITY");
3772 __kmp_affinity_type = affinity_none;
3773 __kmp_create_affinity_none_places();
3775 }
else if (!__kmp_topology->is_uniform()) {
3777 __kmp_aff_depth = depth;
3780 __kmp_affinity_find_core_level(__kmp_avail_proc, depth - 1);
3781 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc, depth - 1,
3783 int maxprocpercore = __kmp_affinity_max_proc_per_core(
3784 __kmp_avail_proc, depth - 1, core_level);
3786 int nproc = ncores * maxprocpercore;
3787 if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
3788 if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
3789 KMP_WARNING(AffBalancedNotAvail,
"KMP_AFFINITY");
3791 __kmp_affinity_type = affinity_none;
3795 procarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
3796 for (
int i = 0; i < nproc; i++) {
3802 for (
int i = 0; i < __kmp_avail_proc; i++) {
3803 int proc = __kmp_topology->at(i).os_id;
3804 int core = __kmp_affinity_find_core(i, depth - 1, core_level);
3806 if (core == lastcore) {
3813 procarr[core * maxprocpercore + inlastcore] = proc;
3816 if (__kmp_affinity_compact >= depth) {
3817 __kmp_affinity_compact = depth - 1;
3822 if (__kmp_affinity_dups) {
3823 __kmp_affinity_num_masks = __kmp_avail_proc;
3825 __kmp_affinity_num_masks = numUnique;
3828 if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
3829 (__kmp_affinity_num_places > 0) &&
3830 ((
unsigned)__kmp_affinity_num_places < __kmp_affinity_num_masks)) {
3831 __kmp_affinity_num_masks = __kmp_affinity_num_places;
3834 KMP_CPU_ALLOC_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
3838 __kmp_topology->sort_compact();
3842 int num_hw_threads = __kmp_topology->get_num_hw_threads();
3843 for (i = 0, j = 0; i < num_hw_threads; i++) {
3844 if ((!__kmp_affinity_dups) && (!__kmp_topology->at(i).leader)) {
3847 int osId = __kmp_topology->at(i).os_id;
3849 kmp_affin_mask_t *src = KMP_CPU_INDEX(osId2Mask, osId);
3850 kmp_affin_mask_t *dest = KMP_CPU_INDEX(__kmp_affinity_masks, j);
3851 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
3852 KMP_CPU_COPY(dest, src);
3853 if (++j >= __kmp_affinity_num_masks) {
3857 KMP_DEBUG_ASSERT(j == __kmp_affinity_num_masks);
3860 __kmp_topology->sort_ids();
3864 KMP_ASSERT2(0,
"Unexpected affinity setting");
3867 KMP_CPU_FREE_ARRAY(osId2Mask, maxIndex + 1);
3870 void __kmp_affinity_initialize(
void) {
3879 int disabled = (__kmp_affinity_type == affinity_disabled);
3880 if (!KMP_AFFINITY_CAPABLE()) {
3881 KMP_ASSERT(disabled);
3884 __kmp_affinity_type = affinity_none;
3886 __kmp_aux_affinity_initialize();
3888 __kmp_affinity_type = affinity_disabled;
3892 void __kmp_affinity_uninitialize(
void) {
3893 if (__kmp_affinity_masks != NULL) {
3894 KMP_CPU_FREE_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
3895 __kmp_affinity_masks = NULL;
3897 if (__kmp_affin_fullMask != NULL) {
3898 KMP_CPU_FREE(__kmp_affin_fullMask);
3899 __kmp_affin_fullMask = NULL;
3901 __kmp_affinity_num_masks = 0;
3902 __kmp_affinity_type = affinity_default;
3903 __kmp_affinity_num_places = 0;
3904 if (__kmp_affinity_proclist != NULL) {
3905 __kmp_free(__kmp_affinity_proclist);
3906 __kmp_affinity_proclist = NULL;
3908 if (procarr != NULL) {
3909 __kmp_free(procarr);
3913 if (__kmp_hwloc_topology != NULL) {
3914 hwloc_topology_destroy(__kmp_hwloc_topology);
3915 __kmp_hwloc_topology = NULL;
3918 if (__kmp_hw_subset) {
3919 kmp_hw_subset_t::deallocate(__kmp_hw_subset);
3920 __kmp_hw_subset =
nullptr;
3922 if (__kmp_topology) {
3923 kmp_topology_t::deallocate(__kmp_topology);
3924 __kmp_topology =
nullptr;
3926 KMPAffinity::destroy_api();
3929 void __kmp_affinity_set_init_mask(
int gtid,
int isa_root) {
3930 if (!KMP_AFFINITY_CAPABLE()) {
3934 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
3935 if (th->th.th_affin_mask == NULL) {
3936 KMP_CPU_ALLOC(th->th.th_affin_mask);
3938 KMP_CPU_ZERO(th->th.th_affin_mask);
3945 kmp_affin_mask_t *mask;
3948 if (KMP_AFFINITY_NON_PROC_BIND) {
3949 if ((__kmp_affinity_type == affinity_none) ||
3950 (__kmp_affinity_type == affinity_balanced) ||
3951 KMP_HIDDEN_HELPER_THREAD(gtid)) {
3952 #if KMP_GROUP_AFFINITY
3953 if (__kmp_num_proc_groups > 1) {
3957 KMP_ASSERT(__kmp_affin_fullMask != NULL);
3959 mask = __kmp_affin_fullMask;
3961 int mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
3962 KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0);
3963 i = (mask_idx + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3964 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3967 if ((!isa_root) || KMP_HIDDEN_HELPER_THREAD(gtid) ||
3968 (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
3969 #if KMP_GROUP_AFFINITY
3970 if (__kmp_num_proc_groups > 1) {
3974 KMP_ASSERT(__kmp_affin_fullMask != NULL);
3976 mask = __kmp_affin_fullMask;
3980 int mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid);
3981 KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0);
3982 i = (mask_idx + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3983 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3987 th->th.th_current_place = i;
3988 if (isa_root || KMP_HIDDEN_HELPER_THREAD(gtid)) {
3989 th->th.th_new_place = i;
3990 th->th.th_first_place = 0;
3991 th->th.th_last_place = __kmp_affinity_num_masks - 1;
3992 }
else if (KMP_AFFINITY_NON_PROC_BIND) {
3995 th->th.th_first_place = 0;
3996 th->th.th_last_place = __kmp_affinity_num_masks - 1;
3999 if (i == KMP_PLACE_ALL) {
4000 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4003 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4007 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4009 if (__kmp_affinity_verbose && !KMP_HIDDEN_HELPER_THREAD(gtid)
4011 && (__kmp_affinity_type == affinity_none ||
4012 (i != KMP_PLACE_ALL && __kmp_affinity_type != affinity_balanced))) {
4013 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4014 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4015 th->th.th_affin_mask);
4016 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", (kmp_int32)getpid(),
4017 __kmp_gettid(), gtid, buf);
4022 if (__kmp_affinity_verbose && KMP_HIDDEN_HELPER_THREAD(gtid)) {
4023 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4024 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4025 th->th.th_affin_mask);
4026 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY (hidden helper thread)",
4027 (kmp_int32)getpid(), __kmp_gettid(), gtid, buf);
4035 if (__kmp_affinity_type == affinity_none) {
4036 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4039 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4042 void __kmp_affinity_set_place(
int gtid) {
4043 if (!KMP_AFFINITY_CAPABLE()) {
4047 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4049 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current "
4051 gtid, th->th.th_new_place, th->th.th_current_place));
4054 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4055 KMP_ASSERT(th->th.th_new_place >= 0);
4056 KMP_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity_num_masks);
4057 if (th->th.th_first_place <= th->th.th_last_place) {
4058 KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
4059 (th->th.th_new_place <= th->th.th_last_place));
4061 KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
4062 (th->th.th_new_place >= th->th.th_last_place));
4067 kmp_affin_mask_t *mask =
4068 KMP_CPU_INDEX(__kmp_affinity_masks, th->th.th_new_place);
4069 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4070 th->th.th_current_place = th->th.th_new_place;
4072 if (__kmp_affinity_verbose) {
4073 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4074 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4075 th->th.th_affin_mask);
4076 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", (kmp_int32)getpid(),
4077 __kmp_gettid(), gtid, buf);
4079 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4082 int __kmp_aux_set_affinity(
void **mask) {
4087 if (!KMP_AFFINITY_CAPABLE()) {
4091 gtid = __kmp_entry_gtid();
4094 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4095 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4096 (kmp_affin_mask_t *)(*mask));
4098 "kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4102 if (__kmp_env_consistency_check) {
4103 if ((mask == NULL) || (*mask == NULL)) {
4104 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4109 KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
4110 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4111 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4113 if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4118 if (num_procs == 0) {
4119 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4122 #if KMP_GROUP_AFFINITY
4123 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4124 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4130 th = __kmp_threads[gtid];
4131 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4132 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4134 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4137 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4138 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4139 th->th.th_first_place = 0;
4140 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4143 th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
4148 int __kmp_aux_get_affinity(
void **mask) {
4151 #if KMP_OS_WINDOWS || KMP_DEBUG
4154 if (!KMP_AFFINITY_CAPABLE()) {
4158 gtid = __kmp_entry_gtid();
4159 #if KMP_OS_WINDOWS || KMP_DEBUG
4160 th = __kmp_threads[gtid];
4164 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4168 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4169 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4170 th->th.th_affin_mask);
4172 "kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid,
4176 if (__kmp_env_consistency_check) {
4177 if ((mask == NULL) || (*mask == NULL)) {
4178 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4184 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4187 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4188 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4189 (kmp_affin_mask_t *)(*mask));
4191 "kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid,
4199 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4205 int __kmp_aux_get_affinity_max_proc() {
4206 if (!KMP_AFFINITY_CAPABLE()) {
4209 #if KMP_GROUP_AFFINITY
4210 if (__kmp_num_proc_groups > 1) {
4211 return (
int)(__kmp_num_proc_groups *
sizeof(DWORD_PTR) * CHAR_BIT);
4217 int __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask) {
4218 if (!KMP_AFFINITY_CAPABLE()) {
4224 int gtid = __kmp_entry_gtid();
4225 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4226 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4227 (kmp_affin_mask_t *)(*mask));
4228 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in "
4229 "affinity mask for thread %d = %s\n",
4233 if (__kmp_env_consistency_check) {
4234 if ((mask == NULL) || (*mask == NULL)) {
4235 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4239 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4242 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4246 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
4250 int __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask) {
4251 if (!KMP_AFFINITY_CAPABLE()) {
4257 int gtid = __kmp_entry_gtid();
4258 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4259 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4260 (kmp_affin_mask_t *)(*mask));
4261 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in "
4262 "affinity mask for thread %d = %s\n",
4266 if (__kmp_env_consistency_check) {
4267 if ((mask == NULL) || (*mask == NULL)) {
4268 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
4272 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4275 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4279 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
4283 int __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask) {
4284 if (!KMP_AFFINITY_CAPABLE()) {
4290 int gtid = __kmp_entry_gtid();
4291 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4292 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4293 (kmp_affin_mask_t *)(*mask));
4294 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in "
4295 "affinity mask for thread %d = %s\n",
4299 if (__kmp_env_consistency_check) {
4300 if ((mask == NULL) || (*mask == NULL)) {
4301 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity_mask_proc");
4305 if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
4308 if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4312 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
4316 void __kmp_balanced_affinity(kmp_info_t *th,
int nthreads) {
4317 KMP_DEBUG_ASSERT(th);
4318 bool fine_gran =
true;
4319 int tid = th->th.th_info.ds.ds_tid;
4322 if (KMP_HIDDEN_HELPER_THREAD(__kmp_gtid_from_thread(th)))
4325 switch (__kmp_affinity_gran) {
4329 if (__kmp_nThreadsPerCore > 1) {
4334 if (nCoresPerPkg > 1) {
4342 if (__kmp_topology->is_uniform()) {
4346 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
4348 int ncores = __kmp_ncores;
4349 if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
4350 __kmp_nth_per_core = __kmp_avail_proc / nPackages;
4354 int chunk = nthreads / ncores;
4356 int big_cores = nthreads % ncores;
4358 int big_nth = (chunk + 1) * big_cores;
4359 if (tid < big_nth) {
4360 coreID = tid / (chunk + 1);
4361 threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
4363 coreID = (tid - big_cores) / chunk;
4364 threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
4366 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
4367 "Illegal set affinity operation when not capable");
4369 kmp_affin_mask_t *mask = th->th.th_affin_mask;
4374 __kmp_topology->at(coreID * __kmp_nth_per_core + threadID).os_id;
4375 KMP_CPU_SET(osID, mask);
4377 for (
int i = 0; i < __kmp_nth_per_core; i++) {
4379 osID = __kmp_topology->at(coreID * __kmp_nth_per_core + i).os_id;
4380 KMP_CPU_SET(osID, mask);
4383 if (__kmp_affinity_verbose) {
4384 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4385 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4386 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", (kmp_int32)getpid(),
4387 __kmp_gettid(), tid, buf);
4389 __kmp_set_system_affinity(mask, TRUE);
4392 kmp_affin_mask_t *mask = th->th.th_affin_mask;
4396 __kmp_affinity_find_core_level(__kmp_avail_proc, __kmp_aff_depth - 1);
4397 int ncores = __kmp_affinity_compute_ncores(__kmp_avail_proc,
4398 __kmp_aff_depth - 1, core_level);
4399 int nth_per_core = __kmp_affinity_max_proc_per_core(
4400 __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
4404 if (nthreads == __kmp_avail_proc) {
4406 int osID = __kmp_topology->at(tid).os_id;
4407 KMP_CPU_SET(osID, mask);
4410 __kmp_affinity_find_core(tid, __kmp_aff_depth - 1, core_level);
4411 for (
int i = 0; i < __kmp_avail_proc; i++) {
4412 int osID = __kmp_topology->at(i).os_id;
4413 if (__kmp_affinity_find_core(i, __kmp_aff_depth - 1, core_level) ==
4415 KMP_CPU_SET(osID, mask);
4419 }
else if (nthreads <= ncores) {
4422 for (
int i = 0; i < ncores; i++) {
4425 for (
int j = 0; j < nth_per_core; j++) {
4426 if (procarr[i * nth_per_core + j] != -1) {
4433 for (
int j = 0; j < nth_per_core; j++) {
4434 int osID = procarr[i * nth_per_core + j];
4436 KMP_CPU_SET(osID, mask);
4452 int *nproc_at_core = (
int *)KMP_ALLOCA(
sizeof(
int) * ncores);
4454 int *ncores_with_x_procs =
4455 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
4457 int *ncores_with_x_to_max_procs =
4458 (
int *)KMP_ALLOCA(
sizeof(
int) * (nth_per_core + 1));
4460 for (
int i = 0; i <= nth_per_core; i++) {
4461 ncores_with_x_procs[i] = 0;
4462 ncores_with_x_to_max_procs[i] = 0;
4465 for (
int i = 0; i < ncores; i++) {
4467 for (
int j = 0; j < nth_per_core; j++) {
4468 if (procarr[i * nth_per_core + j] != -1) {
4472 nproc_at_core[i] = cnt;
4473 ncores_with_x_procs[cnt]++;
4476 for (
int i = 0; i <= nth_per_core; i++) {
4477 for (
int j = i; j <= nth_per_core; j++) {
4478 ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
4483 int nproc = nth_per_core * ncores;
4485 int *newarr = (
int *)__kmp_allocate(
sizeof(
int) * nproc);
4486 for (
int i = 0; i < nproc; i++) {
4493 for (
int j = 1; j <= nth_per_core; j++) {
4494 int cnt = ncores_with_x_to_max_procs[j];
4495 for (
int i = 0; i < ncores; i++) {
4497 if (nproc_at_core[i] == 0) {
4500 for (
int k = 0; k < nth_per_core; k++) {
4501 if (procarr[i * nth_per_core + k] != -1) {
4502 if (newarr[i * nth_per_core + k] == 0) {
4503 newarr[i * nth_per_core + k] = 1;
4509 newarr[i * nth_per_core + k]++;
4517 if (cnt == 0 || nth == 0) {
4528 for (
int i = 0; i < nproc; i++) {
4532 int osID = procarr[i];
4533 KMP_CPU_SET(osID, mask);
4535 int coreID = i / nth_per_core;
4536 for (
int ii = 0; ii < nth_per_core; ii++) {
4537 int osID = procarr[coreID * nth_per_core + ii];
4539 KMP_CPU_SET(osID, mask);
4549 if (__kmp_affinity_verbose) {
4550 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4551 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4552 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", (kmp_int32)getpid(),
4553 __kmp_gettid(), tid, buf);
4555 __kmp_set_system_affinity(mask, TRUE);
4559 #if KMP_OS_LINUX || KMP_OS_FREEBSD
4573 kmp_set_thread_affinity_mask_initial()
4578 int gtid = __kmp_get_gtid();
4581 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
4582 "non-omp thread, returning\n"));
4585 if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
4586 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
4587 "affinity not initialized, returning\n"));
4590 KA_TRACE(30, (
"kmp_set_thread_affinity_mask_initial: "
4591 "set full mask for thread %d\n",
4593 KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
4594 return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);