1 /*
2  * kmp_affinity.cpp -- affinity management
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "kmp.h"
14 #include "kmp_affinity.h"
15 #include "kmp_i18n.h"
16 #include "kmp_io.h"
17 #include "kmp_str.h"
18 #include "kmp_wrapper_getpid.h"
19 #if KMP_USE_HIER_SCHED
20 #include "kmp_dispatch_hier.h"
21 #endif
22 
23 // Store the real or imagined machine hierarchy here
24 static hierarchy_info machine_hierarchy;
25 
26 void __kmp_cleanup_hierarchy() { machine_hierarchy.fini(); }
27 
28 void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar) {
29   kmp_uint32 depth;
30   // The test below is true if affinity is available, but set to "none". Need to
31   // init on first use of hierarchical barrier.
32   if (TCR_1(machine_hierarchy.uninitialized))
33     machine_hierarchy.init(NULL, nproc);
34 
35   // Adjust the hierarchy in case num threads exceeds original
36   if (nproc > machine_hierarchy.base_num_threads)
37     machine_hierarchy.resize(nproc);
38 
39   depth = machine_hierarchy.depth;
40   KMP_DEBUG_ASSERT(depth > 0);
41 
42   thr_bar->depth = depth;
43   thr_bar->base_leaf_kids = (kmp_uint8)machine_hierarchy.numPerLevel[0] - 1;
44   thr_bar->skip_per_level = machine_hierarchy.skipPerLevel;
45 }
46 
47 #if KMP_AFFINITY_SUPPORTED
48 
49 bool KMPAffinity::picked_api = false;
50 
51 void *KMPAffinity::Mask::operator new(size_t n) { return __kmp_allocate(n); }
52 void *KMPAffinity::Mask::operator new[](size_t n) { return __kmp_allocate(n); }
53 void KMPAffinity::Mask::operator delete(void *p) { __kmp_free(p); }
54 void KMPAffinity::Mask::operator delete[](void *p) { __kmp_free(p); }
55 void *KMPAffinity::operator new(size_t n) { return __kmp_allocate(n); }
56 void KMPAffinity::operator delete(void *p) { __kmp_free(p); }
57 
58 void KMPAffinity::pick_api() {
59   KMPAffinity *affinity_dispatch;
60   if (picked_api)
61     return;
62 #if KMP_USE_HWLOC
63   // Only use Hwloc if affinity isn't explicitly disabled and
64   // user requests Hwloc topology method
65   if (__kmp_affinity_top_method == affinity_top_method_hwloc &&
66       __kmp_affinity_type != affinity_disabled) {
67     affinity_dispatch = new KMPHwlocAffinity();
68   } else
69 #endif
70   {
71     affinity_dispatch = new KMPNativeAffinity();
72   }
73   __kmp_affinity_dispatch = affinity_dispatch;
74   picked_api = true;
75 }
76 
77 void KMPAffinity::destroy_api() {
78   if (__kmp_affinity_dispatch != NULL) {
79     delete __kmp_affinity_dispatch;
80     __kmp_affinity_dispatch = NULL;
81     picked_api = false;
82   }
83 }
84 
85 #define KMP_ADVANCE_SCAN(scan)                                                 \
86   while (*scan != '\0') {                                                      \
87     scan++;                                                                    \
88   }
89 
90 // Print the affinity mask to the character array in a pretty format.
91 // The format is a comma separated list of non-negative integers or integer
92 // ranges: e.g., 1,2,3-5,7,9-15
93 // The format can also be the string "{<empty>}" if no bits are set in mask
94 char *__kmp_affinity_print_mask(char *buf, int buf_len,
95                                 kmp_affin_mask_t *mask) {
96   int start = 0, finish = 0, previous = 0;
97   bool first_range;
98   KMP_ASSERT(buf);
99   KMP_ASSERT(buf_len >= 40);
100   KMP_ASSERT(mask);
101   char *scan = buf;
102   char *end = buf + buf_len - 1;
103 
104   // Check for empty set.
105   if (mask->begin() == mask->end()) {
106     KMP_SNPRINTF(scan, end - scan + 1, "{<empty>}");
107     KMP_ADVANCE_SCAN(scan);
108     KMP_ASSERT(scan <= end);
109     return buf;
110   }
111 
112   first_range = true;
113   start = mask->begin();
114   while (1) {
115     // Find next range
116     // [start, previous] is inclusive range of contiguous bits in mask
117     for (finish = mask->next(start), previous = start;
118          finish == previous + 1 && finish != mask->end();
119          finish = mask->next(finish)) {
120       previous = finish;
121     }
122 
123     // The first range does not need a comma printed before it, but the rest
124     // of the ranges do need a comma beforehand
125     if (!first_range) {
126       KMP_SNPRINTF(scan, end - scan + 1, "%s", ",");
127       KMP_ADVANCE_SCAN(scan);
128     } else {
129       first_range = false;
130     }
131     // Range with three or more contiguous bits in the affinity mask
132     if (previous - start > 1) {
133       KMP_SNPRINTF(scan, end - scan + 1, "%d-%d", static_cast<int>(start),
134                    static_cast<int>(previous));
135     } else {
136       // Range with one or two contiguous bits in the affinity mask
137       KMP_SNPRINTF(scan, end - scan + 1, "%d", static_cast<int>(start));
138       KMP_ADVANCE_SCAN(scan);
139       if (previous - start > 0) {
140         KMP_SNPRINTF(scan, end - scan + 1, ",%d", static_cast<int>(previous));
141       }
142     }
143     KMP_ADVANCE_SCAN(scan);
144     // Start over with new start point
145     start = finish;
146     if (start == mask->end())
147       break;
148     // Check for overflow
149     if (end - scan < 2)
150       break;
151   }
152 
153   // Check for overflow
154   KMP_ASSERT(scan <= end);
155   return buf;
156 }
157 #undef KMP_ADVANCE_SCAN
158 
159 // Print the affinity mask to the string buffer object in a pretty format
160 // The format is a comma separated list of non-negative integers or integer
161 // ranges: e.g., 1,2,3-5,7,9-15
162 // The format can also be the string "{<empty>}" if no bits are set in mask
163 kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf,
164                                            kmp_affin_mask_t *mask) {
165   int start = 0, finish = 0, previous = 0;
166   bool first_range;
167   KMP_ASSERT(buf);
168   KMP_ASSERT(mask);
169 
170   __kmp_str_buf_clear(buf);
171 
172   // Check for empty set.
173   if (mask->begin() == mask->end()) {
174     __kmp_str_buf_print(buf, "%s", "{<empty>}");
175     return buf;
176   }
177 
178   first_range = true;
179   start = mask->begin();
180   while (1) {
181     // Find next range
182     // [start, previous] is inclusive range of contiguous bits in mask
183     for (finish = mask->next(start), previous = start;
184          finish == previous + 1 && finish != mask->end();
185          finish = mask->next(finish)) {
186       previous = finish;
187     }
188 
189     // The first range does not need a comma printed before it, but the rest
190     // of the ranges do need a comma beforehand
191     if (!first_range) {
192       __kmp_str_buf_print(buf, "%s", ",");
193     } else {
194       first_range = false;
195     }
196     // Range with three or more contiguous bits in the affinity mask
197     if (previous - start > 1) {
198       __kmp_str_buf_print(buf, "%d-%d", static_cast<int>(start),
199                           static_cast<int>(previous));
200     } else {
201       // Range with one or two contiguous bits in the affinity mask
202       __kmp_str_buf_print(buf, "%d", static_cast<int>(start));
203       if (previous - start > 0) {
204         __kmp_str_buf_print(buf, ",%d", static_cast<int>(previous));
205       }
206     }
207     // Start over with new start point
208     start = finish;
209     if (start == mask->end())
210       break;
211   }
212   return buf;
213 }
214 
215 void __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) {
216   KMP_CPU_ZERO(mask);
217 
218 #if KMP_GROUP_AFFINITY
219 
220   if (__kmp_num_proc_groups > 1) {
221     int group;
222     KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
223     for (group = 0; group < __kmp_num_proc_groups; group++) {
224       int i;
225       int num = __kmp_GetActiveProcessorCount(group);
226       for (i = 0; i < num; i++) {
227         KMP_CPU_SET(i + group * (CHAR_BIT * sizeof(DWORD_PTR)), mask);
228       }
229     }
230   } else
231 
232 #endif /* KMP_GROUP_AFFINITY */
233 
234   {
235     int proc;
236     for (proc = 0; proc < __kmp_xproc; proc++) {
237       KMP_CPU_SET(proc, mask);
238     }
239   }
240 }
241 
242 // When sorting by labels, __kmp_affinity_assign_child_nums() must first be
243 // called to renumber the labels from [0..n] and place them into the child_num
244 // vector of the address object.  This is done in case the labels used for
245 // the children at one node of the hierarchy differ from those used for
246 // another node at the same level.  Example:  suppose the machine has 2 nodes
247 // with 2 packages each.  The first node contains packages 601 and 602, and
248 // second node contains packages 603 and 604.  If we try to sort the table
249 // for "scatter" affinity, the table will still be sorted 601, 602, 603, 604
250 // because we are paying attention to the labels themselves, not the ordinal
251 // child numbers.  By using the child numbers in the sort, the result is
252 // {0,0}=601, {0,1}=603, {1,0}=602, {1,1}=604.
253 static void __kmp_affinity_assign_child_nums(AddrUnsPair *address2os,
254                                              int numAddrs) {
255   KMP_DEBUG_ASSERT(numAddrs > 0);
256   int depth = address2os->first.depth;
257   unsigned *counts = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
258   unsigned *lastLabel = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
259   int labCt;
260   for (labCt = 0; labCt < depth; labCt++) {
261     address2os[0].first.childNums[labCt] = counts[labCt] = 0;
262     lastLabel[labCt] = address2os[0].first.labels[labCt];
263   }
264   int i;
265   for (i = 1; i < numAddrs; i++) {
266     for (labCt = 0; labCt < depth; labCt++) {
267       if (address2os[i].first.labels[labCt] != lastLabel[labCt]) {
268         int labCt2;
269         for (labCt2 = labCt + 1; labCt2 < depth; labCt2++) {
270           counts[labCt2] = 0;
271           lastLabel[labCt2] = address2os[i].first.labels[labCt2];
272         }
273         counts[labCt]++;
274         lastLabel[labCt] = address2os[i].first.labels[labCt];
275         break;
276       }
277     }
278     for (labCt = 0; labCt < depth; labCt++) {
279       address2os[i].first.childNums[labCt] = counts[labCt];
280     }
281     for (; labCt < (int)Address::maxDepth; labCt++) {
282       address2os[i].first.childNums[labCt] = 0;
283     }
284   }
285   __kmp_free(lastLabel);
286   __kmp_free(counts);
287 }
288 
289 // All of the __kmp_affinity_create_*_map() routines should set
290 // __kmp_affinity_masks to a vector of affinity mask objects of length
291 // __kmp_affinity_num_masks, if __kmp_affinity_type != affinity_none, and return
292 // the number of levels in the machine topology tree (zero if
293 // __kmp_affinity_type == affinity_none).
294 //
295 // All of the __kmp_affinity_create_*_map() routines should set
296 // *__kmp_affin_fullMask to the affinity mask for the initialization thread.
297 // They need to save and restore the mask, and it could be needed later, so
298 // saving it is just an optimization to avoid calling kmp_get_system_affinity()
299 // again.
300 kmp_affin_mask_t *__kmp_affin_fullMask = NULL;
301 
302 static int nCoresPerPkg, nPackages;
303 static int __kmp_nThreadsPerCore;
304 #ifndef KMP_DFLT_NTH_CORES
305 static int __kmp_ncores;
306 #endif
307 static int *__kmp_pu_os_idx = NULL;
308 
309 // __kmp_affinity_uniform_topology() doesn't work when called from
310 // places which support arbitrarily many levels in the machine topology
311 // map, i.e. the non-default cases in __kmp_affinity_create_cpuinfo_map()
312 // __kmp_affinity_create_x2apicid_map().
313 inline static bool __kmp_affinity_uniform_topology() {
314   return __kmp_avail_proc == (__kmp_nThreadsPerCore * nCoresPerPkg * nPackages);
315 }
316 
317 // Print out the detailed machine topology map, i.e. the physical locations
318 // of each OS proc.
319 static void __kmp_affinity_print_topology(AddrUnsPair *address2os, int len,
320                                           int depth, int pkgLevel,
321                                           int coreLevel, int threadLevel) {
322   int proc;
323 
324   KMP_INFORM(OSProcToPhysicalThreadMap, "KMP_AFFINITY");
325   for (proc = 0; proc < len; proc++) {
326     int level;
327     kmp_str_buf_t buf;
328     __kmp_str_buf_init(&buf);
329     for (level = 0; level < depth; level++) {
330       if (level == threadLevel) {
331         __kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Thread));
332       } else if (level == coreLevel) {
333         __kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Core));
334       } else if (level == pkgLevel) {
335         __kmp_str_buf_print(&buf, "%s ", KMP_I18N_STR(Package));
336       } else if (level > pkgLevel) {
337         __kmp_str_buf_print(&buf, "%s_%d ", KMP_I18N_STR(Node),
338                             level - pkgLevel - 1);
339       } else {
340         __kmp_str_buf_print(&buf, "L%d ", level);
341       }
342       __kmp_str_buf_print(&buf, "%d ", address2os[proc].first.labels[level]);
343     }
344     KMP_INFORM(OSProcMapToPack, "KMP_AFFINITY", address2os[proc].second,
345                buf.str);
346     __kmp_str_buf_free(&buf);
347   }
348 }
349 
350 #if KMP_USE_HWLOC
351 
352 static void __kmp_affinity_print_hwloc_tp(AddrUnsPair *addrP, int len,
353                                           int depth, int *levels) {
354   int proc;
355   kmp_str_buf_t buf;
356   __kmp_str_buf_init(&buf);
357   KMP_INFORM(OSProcToPhysicalThreadMap, "KMP_AFFINITY");
358   for (proc = 0; proc < len; proc++) {
359     __kmp_str_buf_print(&buf, "%s %d ", KMP_I18N_STR(Package),
360                         addrP[proc].first.labels[0]);
361     if (depth > 1) {
362       int level = 1; // iterate over levels
363       int label = 1; // iterate over labels
364       if (__kmp_numa_detected)
365         // node level follows package
366         if (levels[level++] > 0)
367           __kmp_str_buf_print(&buf, "%s %d ", KMP_I18N_STR(Node),
368                               addrP[proc].first.labels[label++]);
369       if (__kmp_tile_depth > 0)
370         // tile level follows node if any, or package
371         if (levels[level++] > 0)
372           __kmp_str_buf_print(&buf, "%s %d ", KMP_I18N_STR(Tile),
373                               addrP[proc].first.labels[label++]);
374       if (levels[level++] > 0)
375         // core level follows
376         __kmp_str_buf_print(&buf, "%s %d ", KMP_I18N_STR(Core),
377                             addrP[proc].first.labels[label++]);
378       if (levels[level++] > 0)
379         // thread level is the latest
380         __kmp_str_buf_print(&buf, "%s %d ", KMP_I18N_STR(Thread),
381                             addrP[proc].first.labels[label++]);
382       KMP_DEBUG_ASSERT(label == depth);
383     }
384     KMP_INFORM(OSProcMapToPack, "KMP_AFFINITY", addrP[proc].second, buf.str);
385     __kmp_str_buf_clear(&buf);
386   }
387   __kmp_str_buf_free(&buf);
388 }
389 
390 static int nNodePerPkg, nTilePerPkg, nTilePerNode, nCorePerNode, nCorePerTile;
391 
392 // This function removes the topology levels that are radix 1 and don't offer
393 // further information about the topology.  The most common example is when you
394 // have one thread context per core, we don't want the extra thread context
395 // level if it offers no unique labels.  So they are removed.
396 // return value: the new depth of address2os
397 static int __kmp_affinity_remove_radix_one_levels(AddrUnsPair *addrP, int nTh,
398                                                   int depth, int *levels) {
399   int level;
400   int i;
401   int radix1_detected;
402   int new_depth = depth;
403   for (level = depth - 1; level > 0; --level) {
404     // Detect if this level is radix 1
405     radix1_detected = 1;
406     for (i = 1; i < nTh; ++i) {
407       if (addrP[0].first.labels[level] != addrP[i].first.labels[level]) {
408         // There are differing label values for this level so it stays
409         radix1_detected = 0;
410         break;
411       }
412     }
413     if (!radix1_detected)
414       continue;
415     // Radix 1 was detected
416     --new_depth;
417     levels[level] = -1; // mark level as not present in address2os array
418     if (level == new_depth) {
419       // "turn off" deepest level, just decrement the depth that removes
420       // the level from address2os array
421       for (i = 0; i < nTh; ++i) {
422         addrP[i].first.depth--;
423       }
424     } else {
425       // For other levels, we move labels over and also reduce the depth
426       int j;
427       for (j = level; j < new_depth; ++j) {
428         for (i = 0; i < nTh; ++i) {
429           addrP[i].first.labels[j] = addrP[i].first.labels[j + 1];
430           addrP[i].first.depth--;
431         }
432         levels[j + 1] -= 1;
433       }
434     }
435   }
436   return new_depth;
437 }
438 
439 // Returns the number of objects of type 'type' below 'obj' within the topology
440 // tree structure. e.g., if obj is a HWLOC_OBJ_PACKAGE object, and type is
441 // HWLOC_OBJ_PU, then this will return the number of PU's under the SOCKET
442 // object.
443 static int __kmp_hwloc_get_nobjs_under_obj(hwloc_obj_t obj,
444                                            hwloc_obj_type_t type) {
445   int retval = 0;
446   hwloc_obj_t first;
447   for (first = hwloc_get_obj_below_by_type(__kmp_hwloc_topology, obj->type,
448                                            obj->logical_index, type, 0);
449        first != NULL &&
450        hwloc_get_ancestor_obj_by_type(__kmp_hwloc_topology, obj->type, first) ==
451            obj;
452        first = hwloc_get_next_obj_by_type(__kmp_hwloc_topology, first->type,
453                                           first)) {
454     ++retval;
455   }
456   return retval;
457 }
458 
459 static int __kmp_hwloc_count_children_by_depth(hwloc_topology_t t,
460                                                hwloc_obj_t o,
461                                                kmp_hwloc_depth_t depth,
462                                                hwloc_obj_t *f) {
463   if (o->depth == depth) {
464     if (*f == NULL)
465       *f = o; // output first descendant found
466     return 1;
467   }
468   int sum = 0;
469   for (unsigned i = 0; i < o->arity; i++)
470     sum += __kmp_hwloc_count_children_by_depth(t, o->children[i], depth, f);
471   return sum; // will be 0 if no one found (as PU arity is 0)
472 }
473 
474 static int __kmp_hwloc_count_children_by_type(hwloc_topology_t t, hwloc_obj_t o,
475                                               hwloc_obj_type_t type,
476                                               hwloc_obj_t *f) {
477   if (!hwloc_compare_types(o->type, type)) {
478     if (*f == NULL)
479       *f = o; // output first descendant found
480     return 1;
481   }
482   int sum = 0;
483   for (unsigned i = 0; i < o->arity; i++)
484     sum += __kmp_hwloc_count_children_by_type(t, o->children[i], type, f);
485   return sum; // will be 0 if no one found (as PU arity is 0)
486 }
487 
488 static int __kmp_hwloc_process_obj_core_pu(AddrUnsPair *addrPair,
489                                            int &nActiveThreads,
490                                            int &num_active_cores,
491                                            hwloc_obj_t obj, int depth,
492                                            int *labels) {
493   hwloc_obj_t core = NULL;
494   hwloc_topology_t &tp = __kmp_hwloc_topology;
495   int NC = __kmp_hwloc_count_children_by_type(tp, obj, HWLOC_OBJ_CORE, &core);
496   for (int core_id = 0; core_id < NC; ++core_id, core = core->next_cousin) {
497     hwloc_obj_t pu = NULL;
498     KMP_DEBUG_ASSERT(core != NULL);
499     int num_active_threads = 0;
500     int NT = __kmp_hwloc_count_children_by_type(tp, core, HWLOC_OBJ_PU, &pu);
501     // int NT = core->arity; pu = core->first_child; // faster?
502     for (int pu_id = 0; pu_id < NT; ++pu_id, pu = pu->next_cousin) {
503       KMP_DEBUG_ASSERT(pu != NULL);
504       if (!KMP_CPU_ISSET(pu->os_index, __kmp_affin_fullMask))
505         continue; // skip inactive (inaccessible) unit
506       Address addr(depth + 2);
507       KA_TRACE(20, ("Hwloc inserting %d (%d) %d (%d) %d (%d) into address2os\n",
508                     obj->os_index, obj->logical_index, core->os_index,
509                     core->logical_index, pu->os_index, pu->logical_index));
510       for (int i = 0; i < depth; ++i)
511         addr.labels[i] = labels[i]; // package, etc.
512       addr.labels[depth] = core_id; // core
513       addr.labels[depth + 1] = pu_id; // pu
514       addrPair[nActiveThreads] = AddrUnsPair(addr, pu->os_index);
515       __kmp_pu_os_idx[nActiveThreads] = pu->os_index;
516       nActiveThreads++;
517       ++num_active_threads; // count active threads per core
518     }
519     if (num_active_threads) { // were there any active threads on the core?
520       ++__kmp_ncores; // count total active cores
521       ++num_active_cores; // count active cores per socket
522       if (num_active_threads > __kmp_nThreadsPerCore)
523         __kmp_nThreadsPerCore = num_active_threads; // calc maximum
524     }
525   }
526   return 0;
527 }
528 
529 // Check if NUMA node detected below the package,
530 // and if tile object is detected and return its depth
531 static int __kmp_hwloc_check_numa() {
532   hwloc_topology_t &tp = __kmp_hwloc_topology;
533   hwloc_obj_t hT, hC, hL, hN, hS; // hwloc objects (pointers to)
534   int depth, l2cache_depth, package_depth;
535 
536   // Get some PU
537   hT = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PU, 0);
538   if (hT == NULL) // something has gone wrong
539     return 1;
540 
541   // check NUMA node below PACKAGE
542   hN = hwloc_get_ancestor_obj_by_type(tp, HWLOC_OBJ_NUMANODE, hT);
543   hS = hwloc_get_ancestor_obj_by_type(tp, HWLOC_OBJ_PACKAGE, hT);
544   KMP_DEBUG_ASSERT(hS != NULL);
545   if (hN != NULL && hN->depth > hS->depth) {
546     __kmp_numa_detected = TRUE; // socket includes node(s)
547     if (__kmp_affinity_gran == affinity_gran_node) {
548       __kmp_affinity_gran = affinity_gran_numa;
549     }
550   }
551 
552   package_depth = hwloc_get_type_depth(tp, HWLOC_OBJ_PACKAGE);
553   l2cache_depth = hwloc_get_cache_type_depth(tp, 2, HWLOC_OBJ_CACHE_UNIFIED);
554   // check tile, get object by depth because of multiple caches possible
555   depth = (l2cache_depth < package_depth) ? package_depth : l2cache_depth;
556   hL = hwloc_get_ancestor_obj_by_depth(tp, depth, hT);
557   hC = NULL; // not used, but reset it here just in case
558   if (hL != NULL &&
559       __kmp_hwloc_count_children_by_type(tp, hL, HWLOC_OBJ_CORE, &hC) > 1)
560     __kmp_tile_depth = depth; // tile consists of multiple cores
561   return 0;
562 }
563 
564 static int __kmp_affinity_create_hwloc_map(AddrUnsPair **address2os,
565                                            kmp_i18n_id_t *const msg_id) {
566   hwloc_topology_t &tp = __kmp_hwloc_topology; // shortcut of a long name
567   *address2os = NULL;
568   *msg_id = kmp_i18n_null;
569 
570   // Save the affinity mask for the current thread.
571   kmp_affin_mask_t *oldMask;
572   KMP_CPU_ALLOC(oldMask);
573   __kmp_get_system_affinity(oldMask, TRUE);
574   __kmp_hwloc_check_numa();
575 
576   if (!KMP_AFFINITY_CAPABLE()) {
577     // Hack to try and infer the machine topology using only the data
578     // available from cpuid on the current thread, and __kmp_xproc.
579     KMP_ASSERT(__kmp_affinity_type == affinity_none);
580 
581     nCoresPerPkg = __kmp_hwloc_get_nobjs_under_obj(
582         hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0), HWLOC_OBJ_CORE);
583     __kmp_nThreadsPerCore = __kmp_hwloc_get_nobjs_under_obj(
584         hwloc_get_obj_by_type(tp, HWLOC_OBJ_CORE, 0), HWLOC_OBJ_PU);
585     __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
586     nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
587     if (__kmp_affinity_verbose) {
588       KMP_INFORM(AffNotCapableUseLocCpuidL11, "KMP_AFFINITY");
589       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
590       if (__kmp_affinity_uniform_topology()) {
591         KMP_INFORM(Uniform, "KMP_AFFINITY");
592       } else {
593         KMP_INFORM(NonUniform, "KMP_AFFINITY");
594       }
595       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
596                  __kmp_nThreadsPerCore, __kmp_ncores);
597     }
598     KMP_CPU_FREE(oldMask);
599     return 0;
600   }
601 
602   int depth = 3;
603   int levels[5] = {0, 1, 2, 3, 4}; // package, [node,] [tile,] core, thread
604   int labels[3] = {0}; // package [,node] [,tile] - head of lables array
605   if (__kmp_numa_detected)
606     ++depth;
607   if (__kmp_tile_depth)
608     ++depth;
609 
610   // Allocate the data structure to be returned.
611   AddrUnsPair *retval =
612       (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * __kmp_avail_proc);
613   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
614   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
615 
616   // When affinity is off, this routine will still be called to set
617   // __kmp_ncores, as well as __kmp_nThreadsPerCore,
618   // nCoresPerPkg, & nPackages.  Make sure all these vars are set
619   // correctly, and return if affinity is not enabled.
620 
621   hwloc_obj_t socket, node, tile;
622   int nActiveThreads = 0;
623   int socket_id = 0;
624   // re-calculate globals to count only accessible resources
625   __kmp_ncores = nPackages = nCoresPerPkg = __kmp_nThreadsPerCore = 0;
626   nNodePerPkg = nTilePerPkg = nTilePerNode = nCorePerNode = nCorePerTile = 0;
627   for (socket = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PACKAGE, 0); socket != NULL;
628        socket = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PACKAGE, socket),
629       socket_id++) {
630     labels[0] = socket_id;
631     if (__kmp_numa_detected) {
632       int NN;
633       int n_active_nodes = 0;
634       node = NULL;
635       NN = __kmp_hwloc_count_children_by_type(tp, socket, HWLOC_OBJ_NUMANODE,
636                                               &node);
637       for (int node_id = 0; node_id < NN; ++node_id, node = node->next_cousin) {
638         labels[1] = node_id;
639         if (__kmp_tile_depth) {
640           // NUMA + tiles
641           int NT;
642           int n_active_tiles = 0;
643           tile = NULL;
644           NT = __kmp_hwloc_count_children_by_depth(tp, node, __kmp_tile_depth,
645                                                    &tile);
646           for (int tl_id = 0; tl_id < NT; ++tl_id, tile = tile->next_cousin) {
647             labels[2] = tl_id;
648             int n_active_cores = 0;
649             __kmp_hwloc_process_obj_core_pu(retval, nActiveThreads,
650                                             n_active_cores, tile, 3, labels);
651             if (n_active_cores) { // were there any active cores on the socket?
652               ++n_active_tiles; // count active tiles per node
653               if (n_active_cores > nCorePerTile)
654                 nCorePerTile = n_active_cores; // calc maximum
655             }
656           }
657           if (n_active_tiles) { // were there any active tiles on the socket?
658             ++n_active_nodes; // count active nodes per package
659             if (n_active_tiles > nTilePerNode)
660               nTilePerNode = n_active_tiles; // calc maximum
661           }
662         } else {
663           // NUMA, no tiles
664           int n_active_cores = 0;
665           __kmp_hwloc_process_obj_core_pu(retval, nActiveThreads,
666                                           n_active_cores, node, 2, labels);
667           if (n_active_cores) { // were there any active cores on the socket?
668             ++n_active_nodes; // count active nodes per package
669             if (n_active_cores > nCorePerNode)
670               nCorePerNode = n_active_cores; // calc maximum
671           }
672         }
673       }
674       if (n_active_nodes) { // were there any active nodes on the socket?
675         ++nPackages; // count total active packages
676         if (n_active_nodes > nNodePerPkg)
677           nNodePerPkg = n_active_nodes; // calc maximum
678       }
679     } else {
680       if (__kmp_tile_depth) {
681         // no NUMA, tiles
682         int NT;
683         int n_active_tiles = 0;
684         tile = NULL;
685         NT = __kmp_hwloc_count_children_by_depth(tp, socket, __kmp_tile_depth,
686                                                  &tile);
687         for (int tl_id = 0; tl_id < NT; ++tl_id, tile = tile->next_cousin) {
688           labels[1] = tl_id;
689           int n_active_cores = 0;
690           __kmp_hwloc_process_obj_core_pu(retval, nActiveThreads,
691                                           n_active_cores, tile, 2, labels);
692           if (n_active_cores) { // were there any active cores on the socket?
693             ++n_active_tiles; // count active tiles per package
694             if (n_active_cores > nCorePerTile)
695               nCorePerTile = n_active_cores; // calc maximum
696           }
697         }
698         if (n_active_tiles) { // were there any active tiles on the socket?
699           ++nPackages; // count total active packages
700           if (n_active_tiles > nTilePerPkg)
701             nTilePerPkg = n_active_tiles; // calc maximum
702         }
703       } else {
704         // no NUMA, no tiles
705         int n_active_cores = 0;
706         __kmp_hwloc_process_obj_core_pu(retval, nActiveThreads, n_active_cores,
707                                         socket, 1, labels);
708         if (n_active_cores) { // were there any active cores on the socket?
709           ++nPackages; // count total active packages
710           if (n_active_cores > nCoresPerPkg)
711             nCoresPerPkg = n_active_cores; // calc maximum
712         }
713       }
714     }
715   }
716 
717   // If there's only one thread context to bind to, return now.
718   KMP_DEBUG_ASSERT(nActiveThreads == __kmp_avail_proc);
719   KMP_ASSERT(nActiveThreads > 0);
720   if (nActiveThreads == 1) {
721     __kmp_ncores = nPackages = 1;
722     __kmp_nThreadsPerCore = nCoresPerPkg = 1;
723     if (__kmp_affinity_verbose) {
724       char buf[KMP_AFFIN_MASK_PRINT_LEN];
725       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
726 
727       KMP_INFORM(AffUsingHwloc, "KMP_AFFINITY");
728       if (__kmp_affinity_respect_mask) {
729         KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
730       } else {
731         KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
732       }
733       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
734       KMP_INFORM(Uniform, "KMP_AFFINITY");
735       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
736                  __kmp_nThreadsPerCore, __kmp_ncores);
737     }
738 
739     if (__kmp_affinity_type == affinity_none) {
740       __kmp_free(retval);
741       KMP_CPU_FREE(oldMask);
742       return 0;
743     }
744 
745     // Form an Address object which only includes the package level.
746     Address addr(1);
747     addr.labels[0] = retval[0].first.labels[0];
748     retval[0].first = addr;
749 
750     if (__kmp_affinity_gran_levels < 0) {
751       __kmp_affinity_gran_levels = 0;
752     }
753 
754     if (__kmp_affinity_verbose) {
755       __kmp_affinity_print_topology(retval, 1, 1, 0, -1, -1);
756     }
757 
758     *address2os = retval;
759     KMP_CPU_FREE(oldMask);
760     return 1;
761   }
762 
763   // Sort the table by physical Id.
764   qsort(retval, nActiveThreads, sizeof(*retval),
765         __kmp_affinity_cmp_Address_labels);
766 
767   // Check to see if the machine topology is uniform
768   int nPUs = nPackages * __kmp_nThreadsPerCore;
769   if (__kmp_numa_detected) {
770     if (__kmp_tile_depth) { // NUMA + tiles
771       nPUs *= (nNodePerPkg * nTilePerNode * nCorePerTile);
772     } else { // NUMA, no tiles
773       nPUs *= (nNodePerPkg * nCorePerNode);
774     }
775   } else {
776     if (__kmp_tile_depth) { // no NUMA, tiles
777       nPUs *= (nTilePerPkg * nCorePerTile);
778     } else { // no NUMA, no tiles
779       nPUs *= nCoresPerPkg;
780     }
781   }
782   unsigned uniform = (nPUs == nActiveThreads);
783 
784   // Print the machine topology summary.
785   if (__kmp_affinity_verbose) {
786     char mask[KMP_AFFIN_MASK_PRINT_LEN];
787     __kmp_affinity_print_mask(mask, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
788     if (__kmp_affinity_respect_mask) {
789       KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", mask);
790     } else {
791       KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", mask);
792     }
793     KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
794     if (uniform) {
795       KMP_INFORM(Uniform, "KMP_AFFINITY");
796     } else {
797       KMP_INFORM(NonUniform, "KMP_AFFINITY");
798     }
799     if (__kmp_numa_detected) {
800       if (__kmp_tile_depth) { // NUMA + tiles
801         KMP_INFORM(TopologyExtraNoTi, "KMP_AFFINITY", nPackages, nNodePerPkg,
802                    nTilePerNode, nCorePerTile, __kmp_nThreadsPerCore,
803                    __kmp_ncores);
804       } else { // NUMA, no tiles
805         KMP_INFORM(TopologyExtraNode, "KMP_AFFINITY", nPackages, nNodePerPkg,
806                    nCorePerNode, __kmp_nThreadsPerCore, __kmp_ncores);
807         nPUs *= (nNodePerPkg * nCorePerNode);
808       }
809     } else {
810       if (__kmp_tile_depth) { // no NUMA, tiles
811         KMP_INFORM(TopologyExtraTile, "KMP_AFFINITY", nPackages, nTilePerPkg,
812                    nCorePerTile, __kmp_nThreadsPerCore, __kmp_ncores);
813       } else { // no NUMA, no tiles
814         kmp_str_buf_t buf;
815         __kmp_str_buf_init(&buf);
816         __kmp_str_buf_print(&buf, "%d", nPackages);
817         KMP_INFORM(TopologyExtra, "KMP_AFFINITY", buf.str, nCoresPerPkg,
818                    __kmp_nThreadsPerCore, __kmp_ncores);
819         __kmp_str_buf_free(&buf);
820       }
821     }
822   }
823 
824   if (__kmp_affinity_type == affinity_none) {
825     __kmp_free(retval);
826     KMP_CPU_FREE(oldMask);
827     return 0;
828   }
829 
830   int depth_full = depth; // number of levels before compressing
831   // Find any levels with radiix 1, and remove them from the map
832   // (except for the package level).
833   depth = __kmp_affinity_remove_radix_one_levels(retval, nActiveThreads, depth,
834                                                  levels);
835   KMP_DEBUG_ASSERT(__kmp_affinity_gran != affinity_gran_default);
836   if (__kmp_affinity_gran_levels < 0) {
837     // Set the granularity level based on what levels are modeled
838     // in the machine topology map.
839     __kmp_affinity_gran_levels = 0; // lowest level (e.g. fine)
840     if (__kmp_affinity_gran > affinity_gran_thread) {
841       for (int i = 1; i <= depth_full; ++i) {
842         if (__kmp_affinity_gran <= i) // only count deeper levels
843           break;
844         if (levels[depth_full - i] > 0)
845           __kmp_affinity_gran_levels++;
846       }
847     }
848     if (__kmp_affinity_gran > affinity_gran_package)
849       __kmp_affinity_gran_levels++; // e.g. granularity = group
850   }
851 
852   if (__kmp_affinity_verbose)
853     __kmp_affinity_print_hwloc_tp(retval, nActiveThreads, depth, levels);
854 
855   KMP_CPU_FREE(oldMask);
856   *address2os = retval;
857   return depth;
858 }
859 #endif // KMP_USE_HWLOC
860 
861 // If we don't know how to retrieve the machine's processor topology, or
862 // encounter an error in doing so, this routine is called to form a "flat"
863 // mapping of os thread id's <-> processor id's.
864 static int __kmp_affinity_create_flat_map(AddrUnsPair **address2os,
865                                           kmp_i18n_id_t *const msg_id) {
866   *address2os = NULL;
867   *msg_id = kmp_i18n_null;
868 
869   // Even if __kmp_affinity_type == affinity_none, this routine might still
870   // called to set __kmp_ncores, as well as
871   // __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
872   if (!KMP_AFFINITY_CAPABLE()) {
873     KMP_ASSERT(__kmp_affinity_type == affinity_none);
874     __kmp_ncores = nPackages = __kmp_xproc;
875     __kmp_nThreadsPerCore = nCoresPerPkg = 1;
876     if (__kmp_affinity_verbose) {
877       KMP_INFORM(AffFlatTopology, "KMP_AFFINITY");
878       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
879       KMP_INFORM(Uniform, "KMP_AFFINITY");
880       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
881                  __kmp_nThreadsPerCore, __kmp_ncores);
882     }
883     return 0;
884   }
885 
886   // When affinity is off, this routine will still be called to set
887   // __kmp_ncores, as well as __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
888   // Make sure all these vars are set correctly, and return now if affinity is
889   // not enabled.
890   __kmp_ncores = nPackages = __kmp_avail_proc;
891   __kmp_nThreadsPerCore = nCoresPerPkg = 1;
892   if (__kmp_affinity_verbose) {
893     char buf[KMP_AFFIN_MASK_PRINT_LEN];
894     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
895                               __kmp_affin_fullMask);
896 
897     KMP_INFORM(AffCapableUseFlat, "KMP_AFFINITY");
898     if (__kmp_affinity_respect_mask) {
899       KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
900     } else {
901       KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
902     }
903     KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
904     KMP_INFORM(Uniform, "KMP_AFFINITY");
905     KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
906                __kmp_nThreadsPerCore, __kmp_ncores);
907   }
908   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
909   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
910   if (__kmp_affinity_type == affinity_none) {
911     int avail_ct = 0;
912     int i;
913     KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
914       if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask))
915         continue;
916       __kmp_pu_os_idx[avail_ct++] = i; // suppose indices are flat
917     }
918     return 0;
919   }
920 
921   // Contruct the data structure to be returned.
922   *address2os =
923       (AddrUnsPair *)__kmp_allocate(sizeof(**address2os) * __kmp_avail_proc);
924   int avail_ct = 0;
925   int i;
926   KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
927     // Skip this proc if it is not included in the machine model.
928     if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
929       continue;
930     }
931     __kmp_pu_os_idx[avail_ct] = i; // suppose indices are flat
932     Address addr(1);
933     addr.labels[0] = i;
934     (*address2os)[avail_ct++] = AddrUnsPair(addr, i);
935   }
936   if (__kmp_affinity_verbose) {
937     KMP_INFORM(OSProcToPackage, "KMP_AFFINITY");
938   }
939 
940   if (__kmp_affinity_gran_levels < 0) {
941     // Only the package level is modeled in the machine topology map,
942     // so the #levels of granularity is either 0 or 1.
943     if (__kmp_affinity_gran > affinity_gran_package) {
944       __kmp_affinity_gran_levels = 1;
945     } else {
946       __kmp_affinity_gran_levels = 0;
947     }
948   }
949   return 1;
950 }
951 
952 #if KMP_GROUP_AFFINITY
953 
954 // If multiple Windows* OS processor groups exist, we can create a 2-level
955 // topology map with the groups at level 0 and the individual procs at level 1.
956 // This facilitates letting the threads float among all procs in a group,
957 // if granularity=group (the default when there are multiple groups).
958 static int __kmp_affinity_create_proc_group_map(AddrUnsPair **address2os,
959                                                 kmp_i18n_id_t *const msg_id) {
960   *address2os = NULL;
961   *msg_id = kmp_i18n_null;
962 
963   // If we aren't affinity capable, then return now.
964   // The flat mapping will be used.
965   if (!KMP_AFFINITY_CAPABLE()) {
966     // FIXME set *msg_id
967     return -1;
968   }
969 
970   // Contruct the data structure to be returned.
971   *address2os =
972       (AddrUnsPair *)__kmp_allocate(sizeof(**address2os) * __kmp_avail_proc);
973   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
974   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
975   int avail_ct = 0;
976   int i;
977   KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
978     // Skip this proc if it is not included in the machine model.
979     if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
980       continue;
981     }
982     __kmp_pu_os_idx[avail_ct] = i; // suppose indices are flat
983     Address addr(2);
984     addr.labels[0] = i / (CHAR_BIT * sizeof(DWORD_PTR));
985     addr.labels[1] = i % (CHAR_BIT * sizeof(DWORD_PTR));
986     (*address2os)[avail_ct++] = AddrUnsPair(addr, i);
987 
988     if (__kmp_affinity_verbose) {
989       KMP_INFORM(AffOSProcToGroup, "KMP_AFFINITY", i, addr.labels[0],
990                  addr.labels[1]);
991     }
992   }
993 
994   if (__kmp_affinity_gran_levels < 0) {
995     if (__kmp_affinity_gran == affinity_gran_group) {
996       __kmp_affinity_gran_levels = 1;
997     } else if ((__kmp_affinity_gran == affinity_gran_fine) ||
998                (__kmp_affinity_gran == affinity_gran_thread)) {
999       __kmp_affinity_gran_levels = 0;
1000     } else {
1001       const char *gran_str = NULL;
1002       if (__kmp_affinity_gran == affinity_gran_core) {
1003         gran_str = "core";
1004       } else if (__kmp_affinity_gran == affinity_gran_package) {
1005         gran_str = "package";
1006       } else if (__kmp_affinity_gran == affinity_gran_node) {
1007         gran_str = "node";
1008       } else {
1009         KMP_ASSERT(0);
1010       }
1011 
1012       // Warning: can't use affinity granularity \"gran\" with group topology
1013       // method, using "thread"
1014       __kmp_affinity_gran_levels = 0;
1015     }
1016   }
1017   return 2;
1018 }
1019 
1020 #endif /* KMP_GROUP_AFFINITY */
1021 
1022 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1023 
1024 static int __kmp_cpuid_mask_width(int count) {
1025   int r = 0;
1026 
1027   while ((1 << r) < count)
1028     ++r;
1029   return r;
1030 }
1031 
1032 class apicThreadInfo {
1033 public:
1034   unsigned osId; // param to __kmp_affinity_bind_thread
1035   unsigned apicId; // from cpuid after binding
1036   unsigned maxCoresPerPkg; //      ""
1037   unsigned maxThreadsPerPkg; //      ""
1038   unsigned pkgId; // inferred from above values
1039   unsigned coreId; //      ""
1040   unsigned threadId; //      ""
1041 };
1042 
1043 static int __kmp_affinity_cmp_apicThreadInfo_phys_id(const void *a,
1044                                                      const void *b) {
1045   const apicThreadInfo *aa = (const apicThreadInfo *)a;
1046   const apicThreadInfo *bb = (const apicThreadInfo *)b;
1047   if (aa->pkgId < bb->pkgId)
1048     return -1;
1049   if (aa->pkgId > bb->pkgId)
1050     return 1;
1051   if (aa->coreId < bb->coreId)
1052     return -1;
1053   if (aa->coreId > bb->coreId)
1054     return 1;
1055   if (aa->threadId < bb->threadId)
1056     return -1;
1057   if (aa->threadId > bb->threadId)
1058     return 1;
1059   return 0;
1060 }
1061 
1062 // On IA-32 architecture and Intel(R) 64 architecture, we attempt to use
1063 // an algorithm which cycles through the available os threads, setting
1064 // the current thread's affinity mask to that thread, and then retrieves
1065 // the Apic Id for each thread context using the cpuid instruction.
1066 static int __kmp_affinity_create_apicid_map(AddrUnsPair **address2os,
1067                                             kmp_i18n_id_t *const msg_id) {
1068   kmp_cpuid buf;
1069   *address2os = NULL;
1070   *msg_id = kmp_i18n_null;
1071 
1072   // Check if cpuid leaf 4 is supported.
1073   __kmp_x86_cpuid(0, 0, &buf);
1074   if (buf.eax < 4) {
1075     *msg_id = kmp_i18n_str_NoLeaf4Support;
1076     return -1;
1077   }
1078 
1079   // The algorithm used starts by setting the affinity to each available thread
1080   // and retrieving info from the cpuid instruction, so if we are not capable of
1081   // calling __kmp_get_system_affinity() and _kmp_get_system_affinity(), then we
1082   // need to do something else - use the defaults that we calculated from
1083   // issuing cpuid without binding to each proc.
1084   if (!KMP_AFFINITY_CAPABLE()) {
1085     // Hack to try and infer the machine topology using only the data
1086     // available from cpuid on the current thread, and __kmp_xproc.
1087     KMP_ASSERT(__kmp_affinity_type == affinity_none);
1088 
1089     // Get an upper bound on the number of threads per package using cpuid(1).
1090     // On some OS/chps combinations where HT is supported by the chip but is
1091     // disabled, this value will be 2 on a single core chip. Usually, it will be
1092     // 2 if HT is enabled and 1 if HT is disabled.
1093     __kmp_x86_cpuid(1, 0, &buf);
1094     int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
1095     if (maxThreadsPerPkg == 0) {
1096       maxThreadsPerPkg = 1;
1097     }
1098 
1099     // The num cores per pkg comes from cpuid(4). 1 must be added to the encoded
1100     // value.
1101     //
1102     // The author of cpu_count.cpp treated this only an upper bound on the
1103     // number of cores, but I haven't seen any cases where it was greater than
1104     // the actual number of cores, so we will treat it as exact in this block of
1105     // code.
1106     //
1107     // First, we need to check if cpuid(4) is supported on this chip. To see if
1108     // cpuid(n) is supported, issue cpuid(0) and check if eax has the value n or
1109     // greater.
1110     __kmp_x86_cpuid(0, 0, &buf);
1111     if (buf.eax >= 4) {
1112       __kmp_x86_cpuid(4, 0, &buf);
1113       nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
1114     } else {
1115       nCoresPerPkg = 1;
1116     }
1117 
1118     // There is no way to reliably tell if HT is enabled without issuing the
1119     // cpuid instruction from every thread, can correlating the cpuid info, so
1120     // if the machine is not affinity capable, we assume that HT is off. We have
1121     // seen quite a few machines where maxThreadsPerPkg is 2, yet the machine
1122     // does not support HT.
1123     //
1124     // - Older OSes are usually found on machines with older chips, which do not
1125     //   support HT.
1126     // - The performance penalty for mistakenly identifying a machine as HT when
1127     //   it isn't (which results in blocktime being incorrecly set to 0) is
1128     //   greater than the penalty when for mistakenly identifying a machine as
1129     //   being 1 thread/core when it is really HT enabled (which results in
1130     //   blocktime being incorrectly set to a positive value).
1131     __kmp_ncores = __kmp_xproc;
1132     nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1133     __kmp_nThreadsPerCore = 1;
1134     if (__kmp_affinity_verbose) {
1135       KMP_INFORM(AffNotCapableUseLocCpuid, "KMP_AFFINITY");
1136       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1137       if (__kmp_affinity_uniform_topology()) {
1138         KMP_INFORM(Uniform, "KMP_AFFINITY");
1139       } else {
1140         KMP_INFORM(NonUniform, "KMP_AFFINITY");
1141       }
1142       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
1143                  __kmp_nThreadsPerCore, __kmp_ncores);
1144     }
1145     return 0;
1146   }
1147 
1148   // From here on, we can assume that it is safe to call
1149   // __kmp_get_system_affinity() and __kmp_set_system_affinity(), even if
1150   // __kmp_affinity_type = affinity_none.
1151 
1152   // Save the affinity mask for the current thread.
1153   kmp_affin_mask_t *oldMask;
1154   KMP_CPU_ALLOC(oldMask);
1155   KMP_ASSERT(oldMask != NULL);
1156   __kmp_get_system_affinity(oldMask, TRUE);
1157 
1158   // Run through each of the available contexts, binding the current thread
1159   // to it, and obtaining the pertinent information using the cpuid instr.
1160   //
1161   // The relevant information is:
1162   // - Apic Id: Bits 24:31 of ebx after issuing cpuid(1) - each thread context
1163   //     has a uniqie Apic Id, which is of the form pkg# : core# : thread#.
1164   // - Max Threads Per Pkg: Bits 16:23 of ebx after issuing cpuid(1). The value
1165   //     of this field determines the width of the core# + thread# fields in the
1166   //     Apic Id. It is also an upper bound on the number of threads per
1167   //     package, but it has been verified that situations happen were it is not
1168   //     exact. In particular, on certain OS/chip combinations where Intel(R)
1169   //     Hyper-Threading Technology is supported by the chip but has been
1170   //     disabled, the value of this field will be 2 (for a single core chip).
1171   //     On other OS/chip combinations supporting Intel(R) Hyper-Threading
1172   //     Technology, the value of this field will be 1 when Intel(R)
1173   //     Hyper-Threading Technology is disabled and 2 when it is enabled.
1174   // - Max Cores Per Pkg:  Bits 26:31 of eax after issuing cpuid(4). The value
1175   //     of this field (+1) determines the width of the core# field in the Apic
1176   //     Id. The comments in "cpucount.cpp" say that this value is an upper
1177   //     bound, but the IA-32 architecture manual says that it is exactly the
1178   //     number of cores per package, and I haven't seen any case where it
1179   //     wasn't.
1180   //
1181   // From this information, deduce the package Id, core Id, and thread Id,
1182   // and set the corresponding fields in the apicThreadInfo struct.
1183   unsigned i;
1184   apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
1185       __kmp_avail_proc * sizeof(apicThreadInfo));
1186   unsigned nApics = 0;
1187   KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
1188     // Skip this proc if it is not included in the machine model.
1189     if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
1190       continue;
1191     }
1192     KMP_DEBUG_ASSERT((int)nApics < __kmp_avail_proc);
1193 
1194     __kmp_affinity_dispatch->bind_thread(i);
1195     threadInfo[nApics].osId = i;
1196 
1197     // The apic id and max threads per pkg come from cpuid(1).
1198     __kmp_x86_cpuid(1, 0, &buf);
1199     if (((buf.edx >> 9) & 1) == 0) {
1200       __kmp_set_system_affinity(oldMask, TRUE);
1201       __kmp_free(threadInfo);
1202       KMP_CPU_FREE(oldMask);
1203       *msg_id = kmp_i18n_str_ApicNotPresent;
1204       return -1;
1205     }
1206     threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
1207     threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
1208     if (threadInfo[nApics].maxThreadsPerPkg == 0) {
1209       threadInfo[nApics].maxThreadsPerPkg = 1;
1210     }
1211 
1212     // Max cores per pkg comes from cpuid(4). 1 must be added to the encoded
1213     // value.
1214     //
1215     // First, we need to check if cpuid(4) is supported on this chip. To see if
1216     // cpuid(n) is supported, issue cpuid(0) and check if eax has the value n
1217     // or greater.
1218     __kmp_x86_cpuid(0, 0, &buf);
1219     if (buf.eax >= 4) {
1220       __kmp_x86_cpuid(4, 0, &buf);
1221       threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
1222     } else {
1223       threadInfo[nApics].maxCoresPerPkg = 1;
1224     }
1225 
1226     // Infer the pkgId / coreId / threadId using only the info obtained locally.
1227     int widthCT = __kmp_cpuid_mask_width(threadInfo[nApics].maxThreadsPerPkg);
1228     threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
1229 
1230     int widthC = __kmp_cpuid_mask_width(threadInfo[nApics].maxCoresPerPkg);
1231     int widthT = widthCT - widthC;
1232     if (widthT < 0) {
1233       // I've never seen this one happen, but I suppose it could, if the cpuid
1234       // instruction on a chip was really screwed up. Make sure to restore the
1235       // affinity mask before the tail call.
1236       __kmp_set_system_affinity(oldMask, TRUE);
1237       __kmp_free(threadInfo);
1238       KMP_CPU_FREE(oldMask);
1239       *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1240       return -1;
1241     }
1242 
1243     int maskC = (1 << widthC) - 1;
1244     threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT) & maskC;
1245 
1246     int maskT = (1 << widthT) - 1;
1247     threadInfo[nApics].threadId = threadInfo[nApics].apicId & maskT;
1248 
1249     nApics++;
1250   }
1251 
1252   // We've collected all the info we need.
1253   // Restore the old affinity mask for this thread.
1254   __kmp_set_system_affinity(oldMask, TRUE);
1255 
1256   // If there's only one thread context to bind to, form an Address object
1257   // with depth 1 and return immediately (or, if affinity is off, set
1258   // address2os to NULL and return).
1259   //
1260   // If it is configured to omit the package level when there is only a single
1261   // package, the logic at the end of this routine won't work if there is only
1262   // a single thread - it would try to form an Address object with depth 0.
1263   KMP_ASSERT(nApics > 0);
1264   if (nApics == 1) {
1265     __kmp_ncores = nPackages = 1;
1266     __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1267     if (__kmp_affinity_verbose) {
1268       char buf[KMP_AFFIN_MASK_PRINT_LEN];
1269       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1270 
1271       KMP_INFORM(AffUseGlobCpuid, "KMP_AFFINITY");
1272       if (__kmp_affinity_respect_mask) {
1273         KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
1274       } else {
1275         KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
1276       }
1277       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1278       KMP_INFORM(Uniform, "KMP_AFFINITY");
1279       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
1280                  __kmp_nThreadsPerCore, __kmp_ncores);
1281     }
1282 
1283     if (__kmp_affinity_type == affinity_none) {
1284       __kmp_free(threadInfo);
1285       KMP_CPU_FREE(oldMask);
1286       return 0;
1287     }
1288 
1289     *address2os = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair));
1290     Address addr(1);
1291     addr.labels[0] = threadInfo[0].pkgId;
1292     (*address2os)[0] = AddrUnsPair(addr, threadInfo[0].osId);
1293 
1294     if (__kmp_affinity_gran_levels < 0) {
1295       __kmp_affinity_gran_levels = 0;
1296     }
1297 
1298     if (__kmp_affinity_verbose) {
1299       __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
1300     }
1301 
1302     __kmp_free(threadInfo);
1303     KMP_CPU_FREE(oldMask);
1304     return 1;
1305   }
1306 
1307   // Sort the threadInfo table by physical Id.
1308   qsort(threadInfo, nApics, sizeof(*threadInfo),
1309         __kmp_affinity_cmp_apicThreadInfo_phys_id);
1310 
1311   // The table is now sorted by pkgId / coreId / threadId, but we really don't
1312   // know the radix of any of the fields. pkgId's may be sparsely assigned among
1313   // the chips on a system. Although coreId's are usually assigned
1314   // [0 .. coresPerPkg-1] and threadId's are usually assigned
1315   // [0..threadsPerCore-1], we don't want to make any such assumptions.
1316   //
1317   // For that matter, we don't know what coresPerPkg and threadsPerCore (or the
1318   // total # packages) are at this point - we want to determine that now. We
1319   // only have an upper bound on the first two figures.
1320   //
1321   // We also perform a consistency check at this point: the values returned by
1322   // the cpuid instruction for any thread bound to a given package had better
1323   // return the same info for maxThreadsPerPkg and maxCoresPerPkg.
1324   nPackages = 1;
1325   nCoresPerPkg = 1;
1326   __kmp_nThreadsPerCore = 1;
1327   unsigned nCores = 1;
1328 
1329   unsigned pkgCt = 1; // to determine radii
1330   unsigned lastPkgId = threadInfo[0].pkgId;
1331   unsigned coreCt = 1;
1332   unsigned lastCoreId = threadInfo[0].coreId;
1333   unsigned threadCt = 1;
1334   unsigned lastThreadId = threadInfo[0].threadId;
1335 
1336   // intra-pkg consist checks
1337   unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
1338   unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
1339 
1340   for (i = 1; i < nApics; i++) {
1341     if (threadInfo[i].pkgId != lastPkgId) {
1342       nCores++;
1343       pkgCt++;
1344       lastPkgId = threadInfo[i].pkgId;
1345       if ((int)coreCt > nCoresPerPkg)
1346         nCoresPerPkg = coreCt;
1347       coreCt = 1;
1348       lastCoreId = threadInfo[i].coreId;
1349       if ((int)threadCt > __kmp_nThreadsPerCore)
1350         __kmp_nThreadsPerCore = threadCt;
1351       threadCt = 1;
1352       lastThreadId = threadInfo[i].threadId;
1353 
1354       // This is a different package, so go on to the next iteration without
1355       // doing any consistency checks. Reset the consistency check vars, though.
1356       prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
1357       prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
1358       continue;
1359     }
1360 
1361     if (threadInfo[i].coreId != lastCoreId) {
1362       nCores++;
1363       coreCt++;
1364       lastCoreId = threadInfo[i].coreId;
1365       if ((int)threadCt > __kmp_nThreadsPerCore)
1366         __kmp_nThreadsPerCore = threadCt;
1367       threadCt = 1;
1368       lastThreadId = threadInfo[i].threadId;
1369     } else if (threadInfo[i].threadId != lastThreadId) {
1370       threadCt++;
1371       lastThreadId = threadInfo[i].threadId;
1372     } else {
1373       __kmp_free(threadInfo);
1374       KMP_CPU_FREE(oldMask);
1375       *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
1376       return -1;
1377     }
1378 
1379     // Check to make certain that the maxCoresPerPkg and maxThreadsPerPkg
1380     // fields agree between all the threads bounds to a given package.
1381     if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg) ||
1382         (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
1383       __kmp_free(threadInfo);
1384       KMP_CPU_FREE(oldMask);
1385       *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1386       return -1;
1387     }
1388   }
1389   nPackages = pkgCt;
1390   if ((int)coreCt > nCoresPerPkg)
1391     nCoresPerPkg = coreCt;
1392   if ((int)threadCt > __kmp_nThreadsPerCore)
1393     __kmp_nThreadsPerCore = threadCt;
1394 
1395   // When affinity is off, this routine will still be called to set
1396   // __kmp_ncores, as well as __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
1397   // Make sure all these vars are set correctly, and return now if affinity is
1398   // not enabled.
1399   __kmp_ncores = nCores;
1400   if (__kmp_affinity_verbose) {
1401     char buf[KMP_AFFIN_MASK_PRINT_LEN];
1402     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1403 
1404     KMP_INFORM(AffUseGlobCpuid, "KMP_AFFINITY");
1405     if (__kmp_affinity_respect_mask) {
1406       KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
1407     } else {
1408       KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
1409     }
1410     KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1411     if (__kmp_affinity_uniform_topology()) {
1412       KMP_INFORM(Uniform, "KMP_AFFINITY");
1413     } else {
1414       KMP_INFORM(NonUniform, "KMP_AFFINITY");
1415     }
1416     KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
1417                __kmp_nThreadsPerCore, __kmp_ncores);
1418   }
1419   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
1420   KMP_DEBUG_ASSERT(nApics == (unsigned)__kmp_avail_proc);
1421   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
1422   for (i = 0; i < nApics; ++i) {
1423     __kmp_pu_os_idx[i] = threadInfo[i].osId;
1424   }
1425   if (__kmp_affinity_type == affinity_none) {
1426     __kmp_free(threadInfo);
1427     KMP_CPU_FREE(oldMask);
1428     return 0;
1429   }
1430 
1431   // Now that we've determined the number of packages, the number of cores per
1432   // package, and the number of threads per core, we can construct the data
1433   // structure that is to be returned.
1434   int pkgLevel = 0;
1435   int coreLevel = (nCoresPerPkg <= 1) ? -1 : 1;
1436   int threadLevel =
1437       (__kmp_nThreadsPerCore <= 1) ? -1 : ((coreLevel >= 0) ? 2 : 1);
1438   unsigned depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
1439 
1440   KMP_ASSERT(depth > 0);
1441   *address2os = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * nApics);
1442 
1443   for (i = 0; i < nApics; ++i) {
1444     Address addr(depth);
1445     unsigned os = threadInfo[i].osId;
1446     int d = 0;
1447 
1448     if (pkgLevel >= 0) {
1449       addr.labels[d++] = threadInfo[i].pkgId;
1450     }
1451     if (coreLevel >= 0) {
1452       addr.labels[d++] = threadInfo[i].coreId;
1453     }
1454     if (threadLevel >= 0) {
1455       addr.labels[d++] = threadInfo[i].threadId;
1456     }
1457     (*address2os)[i] = AddrUnsPair(addr, os);
1458   }
1459 
1460   if (__kmp_affinity_gran_levels < 0) {
1461     // Set the granularity level based on what levels are modeled in the machine
1462     // topology map.
1463     __kmp_affinity_gran_levels = 0;
1464     if ((threadLevel >= 0) && (__kmp_affinity_gran > affinity_gran_thread)) {
1465       __kmp_affinity_gran_levels++;
1466     }
1467     if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1468       __kmp_affinity_gran_levels++;
1469     }
1470     if ((pkgLevel >= 0) && (__kmp_affinity_gran > affinity_gran_package)) {
1471       __kmp_affinity_gran_levels++;
1472     }
1473   }
1474 
1475   if (__kmp_affinity_verbose) {
1476     __kmp_affinity_print_topology(*address2os, nApics, depth, pkgLevel,
1477                                   coreLevel, threadLevel);
1478   }
1479 
1480   __kmp_free(threadInfo);
1481   KMP_CPU_FREE(oldMask);
1482   return depth;
1483 }
1484 
1485 // Intel(R) microarchitecture code name Nehalem, Dunnington and later
1486 // architectures support a newer interface for specifying the x2APIC Ids,
1487 // based on cpuid leaf 11.
1488 static int __kmp_affinity_create_x2apicid_map(AddrUnsPair **address2os,
1489                                               kmp_i18n_id_t *const msg_id) {
1490   kmp_cpuid buf;
1491   *address2os = NULL;
1492   *msg_id = kmp_i18n_null;
1493 
1494   // Check to see if cpuid leaf 11 is supported.
1495   __kmp_x86_cpuid(0, 0, &buf);
1496   if (buf.eax < 11) {
1497     *msg_id = kmp_i18n_str_NoLeaf11Support;
1498     return -1;
1499   }
1500   __kmp_x86_cpuid(11, 0, &buf);
1501   if (buf.ebx == 0) {
1502     *msg_id = kmp_i18n_str_NoLeaf11Support;
1503     return -1;
1504   }
1505 
1506   // Find the number of levels in the machine topology. While we're at it, get
1507   // the default values for __kmp_nThreadsPerCore & nCoresPerPkg. We will try to
1508   // get more accurate values later by explicitly counting them, but get
1509   // reasonable defaults now, in case we return early.
1510   int level;
1511   int threadLevel = -1;
1512   int coreLevel = -1;
1513   int pkgLevel = -1;
1514   __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
1515 
1516   for (level = 0;; level++) {
1517     if (level > 31) {
1518       // FIXME: Hack for DPD200163180
1519       //
1520       // If level is big then something went wrong -> exiting
1521       //
1522       // There could actually be 32 valid levels in the machine topology, but so
1523       // far, the only machine we have seen which does not exit this loop before
1524       // iteration 32 has fubar x2APIC settings.
1525       //
1526       // For now, just reject this case based upon loop trip count.
1527       *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1528       return -1;
1529     }
1530     __kmp_x86_cpuid(11, level, &buf);
1531     if (buf.ebx == 0) {
1532       if (pkgLevel < 0) {
1533         // Will infer nPackages from __kmp_xproc
1534         pkgLevel = level;
1535         level++;
1536       }
1537       break;
1538     }
1539     int kind = (buf.ecx >> 8) & 0xff;
1540     if (kind == 1) {
1541       // SMT level
1542       threadLevel = level;
1543       coreLevel = -1;
1544       pkgLevel = -1;
1545       __kmp_nThreadsPerCore = buf.ebx & 0xffff;
1546       if (__kmp_nThreadsPerCore == 0) {
1547         *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1548         return -1;
1549       }
1550     } else if (kind == 2) {
1551       // core level
1552       coreLevel = level;
1553       pkgLevel = -1;
1554       nCoresPerPkg = buf.ebx & 0xffff;
1555       if (nCoresPerPkg == 0) {
1556         *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1557         return -1;
1558       }
1559     } else {
1560       if (level <= 0) {
1561         *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1562         return -1;
1563       }
1564       if (pkgLevel >= 0) {
1565         continue;
1566       }
1567       pkgLevel = level;
1568       nPackages = buf.ebx & 0xffff;
1569       if (nPackages == 0) {
1570         *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1571         return -1;
1572       }
1573     }
1574   }
1575   int depth = level;
1576 
1577   // In the above loop, "level" was counted from the finest level (usually
1578   // thread) to the coarsest.  The caller expects that we will place the labels
1579   // in (*address2os)[].first.labels[] in the inverse order, so we need to
1580   // invert the vars saying which level means what.
1581   if (threadLevel >= 0) {
1582     threadLevel = depth - threadLevel - 1;
1583   }
1584   if (coreLevel >= 0) {
1585     coreLevel = depth - coreLevel - 1;
1586   }
1587   KMP_DEBUG_ASSERT(pkgLevel >= 0);
1588   pkgLevel = depth - pkgLevel - 1;
1589 
1590   // The algorithm used starts by setting the affinity to each available thread
1591   // and retrieving info from the cpuid instruction, so if we are not capable of
1592   // calling __kmp_get_system_affinity() and _kmp_get_system_affinity(), then we
1593   // need to do something else - use the defaults that we calculated from
1594   // issuing cpuid without binding to each proc.
1595   if (!KMP_AFFINITY_CAPABLE()) {
1596     // Hack to try and infer the machine topology using only the data
1597     // available from cpuid on the current thread, and __kmp_xproc.
1598     KMP_ASSERT(__kmp_affinity_type == affinity_none);
1599 
1600     __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1601     nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1602     if (__kmp_affinity_verbose) {
1603       KMP_INFORM(AffNotCapableUseLocCpuidL11, "KMP_AFFINITY");
1604       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1605       if (__kmp_affinity_uniform_topology()) {
1606         KMP_INFORM(Uniform, "KMP_AFFINITY");
1607       } else {
1608         KMP_INFORM(NonUniform, "KMP_AFFINITY");
1609       }
1610       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
1611                  __kmp_nThreadsPerCore, __kmp_ncores);
1612     }
1613     return 0;
1614   }
1615 
1616   // From here on, we can assume that it is safe to call
1617   // __kmp_get_system_affinity() and __kmp_set_system_affinity(), even if
1618   // __kmp_affinity_type = affinity_none.
1619 
1620   // Save the affinity mask for the current thread.
1621   kmp_affin_mask_t *oldMask;
1622   KMP_CPU_ALLOC(oldMask);
1623   __kmp_get_system_affinity(oldMask, TRUE);
1624 
1625   // Allocate the data structure to be returned.
1626   AddrUnsPair *retval =
1627       (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * __kmp_avail_proc);
1628 
1629   // Run through each of the available contexts, binding the current thread
1630   // to it, and obtaining the pertinent information using the cpuid instr.
1631   unsigned int proc;
1632   int nApics = 0;
1633   KMP_CPU_SET_ITERATE(proc, __kmp_affin_fullMask) {
1634     // Skip this proc if it is not included in the machine model.
1635     if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
1636       continue;
1637     }
1638     KMP_DEBUG_ASSERT(nApics < __kmp_avail_proc);
1639 
1640     __kmp_affinity_dispatch->bind_thread(proc);
1641 
1642     // Extract labels for each level in the machine topology map from Apic ID.
1643     Address addr(depth);
1644     int prev_shift = 0;
1645 
1646     for (level = 0; level < depth; level++) {
1647       __kmp_x86_cpuid(11, level, &buf);
1648       unsigned apicId = buf.edx;
1649       if (buf.ebx == 0) {
1650         if (level != depth - 1) {
1651           KMP_CPU_FREE(oldMask);
1652           *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1653           return -1;
1654         }
1655         addr.labels[depth - level - 1] = apicId >> prev_shift;
1656         level++;
1657         break;
1658       }
1659       int shift = buf.eax & 0x1f;
1660       int mask = (1 << shift) - 1;
1661       addr.labels[depth - level - 1] = (apicId & mask) >> prev_shift;
1662       prev_shift = shift;
1663     }
1664     if (level != depth) {
1665       KMP_CPU_FREE(oldMask);
1666       *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1667       return -1;
1668     }
1669 
1670     retval[nApics] = AddrUnsPair(addr, proc);
1671     nApics++;
1672   }
1673 
1674   // We've collected all the info we need.
1675   // Restore the old affinity mask for this thread.
1676   __kmp_set_system_affinity(oldMask, TRUE);
1677 
1678   // If there's only one thread context to bind to, return now.
1679   KMP_ASSERT(nApics > 0);
1680   if (nApics == 1) {
1681     __kmp_ncores = nPackages = 1;
1682     __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1683     if (__kmp_affinity_verbose) {
1684       char buf[KMP_AFFIN_MASK_PRINT_LEN];
1685       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1686 
1687       KMP_INFORM(AffUseGlobCpuidL11, "KMP_AFFINITY");
1688       if (__kmp_affinity_respect_mask) {
1689         KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
1690       } else {
1691         KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
1692       }
1693       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1694       KMP_INFORM(Uniform, "KMP_AFFINITY");
1695       KMP_INFORM(Topology, "KMP_AFFINITY", nPackages, nCoresPerPkg,
1696                  __kmp_nThreadsPerCore, __kmp_ncores);
1697     }
1698 
1699     if (__kmp_affinity_type == affinity_none) {
1700       __kmp_free(retval);
1701       KMP_CPU_FREE(oldMask);
1702       return 0;
1703     }
1704 
1705     // Form an Address object which only includes the package level.
1706     Address addr(1);
1707     addr.labels[0] = retval[0].first.labels[pkgLevel];
1708     retval[0].first = addr;
1709 
1710     if (__kmp_affinity_gran_levels < 0) {
1711       __kmp_affinity_gran_levels = 0;
1712     }
1713 
1714     if (__kmp_affinity_verbose) {
1715       __kmp_affinity_print_topology(retval, 1, 1, 0, -1, -1);
1716     }
1717 
1718     *address2os = retval;
1719     KMP_CPU_FREE(oldMask);
1720     return 1;
1721   }
1722 
1723   // Sort the table by physical Id.
1724   qsort(retval, nApics, sizeof(*retval), __kmp_affinity_cmp_Address_labels);
1725 
1726   // Find the radix at each of the levels.
1727   unsigned *totals = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
1728   unsigned *counts = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
1729   unsigned *maxCt = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
1730   unsigned *last = (unsigned *)__kmp_allocate(depth * sizeof(unsigned));
1731   for (level = 0; level < depth; level++) {
1732     totals[level] = 1;
1733     maxCt[level] = 1;
1734     counts[level] = 1;
1735     last[level] = retval[0].first.labels[level];
1736   }
1737 
1738   // From here on, the iteration variable "level" runs from the finest level to
1739   // the coarsest, i.e. we iterate forward through
1740   // (*address2os)[].first.labels[] - in the previous loops, we iterated
1741   // backwards.
1742   for (proc = 1; (int)proc < nApics; proc++) {
1743     int level;
1744     for (level = 0; level < depth; level++) {
1745       if (retval[proc].first.labels[level] != last[level]) {
1746         int j;
1747         for (j = level + 1; j < depth; j++) {
1748           totals[j]++;
1749           counts[j] = 1;
1750           // The line below causes printing incorrect topology information in
1751           // case the max value for some level (maxCt[level]) is encountered
1752           // earlier than some less value while going through the array. For
1753           // example, let pkg0 has 4 cores and pkg1 has 2 cores. Then
1754           // maxCt[1] == 2
1755           // whereas it must be 4.
1756           // TODO!!! Check if it can be commented safely
1757           // maxCt[j] = 1;
1758           last[j] = retval[proc].first.labels[j];
1759         }
1760         totals[level]++;
1761         counts[level]++;
1762         if (counts[level] > maxCt[level]) {
1763           maxCt[level] = counts[level];
1764         }
1765         last[level] = retval[proc].first.labels[level];
1766         break;
1767       } else if (level == depth - 1) {
1768         __kmp_free(last);
1769         __kmp_free(maxCt);
1770         __kmp_free(counts);
1771         __kmp_free(totals);
1772         __kmp_free(retval);
1773         KMP_CPU_FREE(oldMask);
1774         *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
1775         return -1;
1776       }
1777     }
1778   }
1779 
1780   // When affinity is off, this routine will still be called to set
1781   // __kmp_ncores, as well as __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
1782   // Make sure all these vars are set correctly, and return if affinity is not
1783   // enabled.
1784   if (threadLevel >= 0) {
1785     __kmp_nThreadsPerCore = maxCt[threadLevel];
1786   } else {
1787     __kmp_nThreadsPerCore = 1;
1788   }
1789   nPackages = totals[pkgLevel];
1790 
1791   if (coreLevel >= 0) {
1792     __kmp_ncores = totals[coreLevel];
1793     nCoresPerPkg = maxCt[coreLevel];
1794   } else {
1795     __kmp_ncores = nPackages;
1796     nCoresPerPkg = 1;
1797   }
1798 
1799   // Check to see if the machine topology is uniform
1800   unsigned prod = maxCt[0];
1801   for (level = 1; level < depth; level++) {
1802     prod *= maxCt[level];
1803   }
1804   bool uniform = (prod == totals[level - 1]);
1805 
1806   // Print the machine topology summary.
1807   if (__kmp_affinity_verbose) {
1808     char mask[KMP_AFFIN_MASK_PRINT_LEN];
1809     __kmp_affinity_print_mask(mask, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1810 
1811     KMP_INFORM(AffUseGlobCpuidL11, "KMP_AFFINITY");
1812     if (__kmp_affinity_respect_mask) {
1813       KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", mask);
1814     } else {
1815       KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", mask);
1816     }
1817     KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
1818     if (uniform) {
1819       KMP_INFORM(Uniform, "KMP_AFFINITY");
1820     } else {
1821       KMP_INFORM(NonUniform, "KMP_AFFINITY");
1822     }
1823 
1824     kmp_str_buf_t buf;
1825     __kmp_str_buf_init(&buf);
1826 
1827     __kmp_str_buf_print(&buf, "%d", totals[0]);
1828     for (level = 1; level <= pkgLevel; level++) {
1829       __kmp_str_buf_print(&buf, " x %d", maxCt[level]);
1830     }
1831     KMP_INFORM(TopologyExtra, "KMP_AFFINITY", buf.str, nCoresPerPkg,
1832                __kmp_nThreadsPerCore, __kmp_ncores);
1833 
1834     __kmp_str_buf_free(&buf);
1835   }
1836   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
1837   KMP_DEBUG_ASSERT(nApics == __kmp_avail_proc);
1838   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
1839   for (proc = 0; (int)proc < nApics; ++proc) {
1840     __kmp_pu_os_idx[proc] = retval[proc].second;
1841   }
1842   if (__kmp_affinity_type == affinity_none) {
1843     __kmp_free(last);
1844     __kmp_free(maxCt);
1845     __kmp_free(counts);
1846     __kmp_free(totals);
1847     __kmp_free(retval);
1848     KMP_CPU_FREE(oldMask);
1849     return 0;
1850   }
1851 
1852   // Find any levels with radiix 1, and remove them from the map
1853   // (except for the package level).
1854   int new_depth = 0;
1855   for (level = 0; level < depth; level++) {
1856     if ((maxCt[level] == 1) && (level != pkgLevel)) {
1857       continue;
1858     }
1859     new_depth++;
1860   }
1861 
1862   // If we are removing any levels, allocate a new vector to return,
1863   // and copy the relevant information to it.
1864   if (new_depth != depth) {
1865     AddrUnsPair *new_retval =
1866         (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * nApics);
1867     for (proc = 0; (int)proc < nApics; proc++) {
1868       Address addr(new_depth);
1869       new_retval[proc] = AddrUnsPair(addr, retval[proc].second);
1870     }
1871     int new_level = 0;
1872     int newPkgLevel = -1;
1873     int newCoreLevel = -1;
1874     int newThreadLevel = -1;
1875     for (level = 0; level < depth; level++) {
1876       if ((maxCt[level] == 1) && (level != pkgLevel)) {
1877         // Remove this level. Never remove the package level
1878         continue;
1879       }
1880       if (level == pkgLevel) {
1881         newPkgLevel = new_level;
1882       }
1883       if (level == coreLevel) {
1884         newCoreLevel = new_level;
1885       }
1886       if (level == threadLevel) {
1887         newThreadLevel = new_level;
1888       }
1889       for (proc = 0; (int)proc < nApics; proc++) {
1890         new_retval[proc].first.labels[new_level] =
1891             retval[proc].first.labels[level];
1892       }
1893       new_level++;
1894     }
1895 
1896     __kmp_free(retval);
1897     retval = new_retval;
1898     depth = new_depth;
1899     pkgLevel = newPkgLevel;
1900     coreLevel = newCoreLevel;
1901     threadLevel = newThreadLevel;
1902   }
1903 
1904   if (__kmp_affinity_gran_levels < 0) {
1905     // Set the granularity level based on what levels are modeled
1906     // in the machine topology map.
1907     __kmp_affinity_gran_levels = 0;
1908     if ((threadLevel >= 0) && (__kmp_affinity_gran > affinity_gran_thread)) {
1909       __kmp_affinity_gran_levels++;
1910     }
1911     if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1912       __kmp_affinity_gran_levels++;
1913     }
1914     if (__kmp_affinity_gran > affinity_gran_package) {
1915       __kmp_affinity_gran_levels++;
1916     }
1917   }
1918 
1919   if (__kmp_affinity_verbose) {
1920     __kmp_affinity_print_topology(retval, nApics, depth, pkgLevel, coreLevel,
1921                                   threadLevel);
1922   }
1923 
1924   __kmp_free(last);
1925   __kmp_free(maxCt);
1926   __kmp_free(counts);
1927   __kmp_free(totals);
1928   KMP_CPU_FREE(oldMask);
1929   *address2os = retval;
1930   return depth;
1931 }
1932 
1933 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1934 
1935 #define osIdIndex 0
1936 #define threadIdIndex 1
1937 #define coreIdIndex 2
1938 #define pkgIdIndex 3
1939 #define nodeIdIndex 4
1940 
1941 typedef unsigned *ProcCpuInfo;
1942 static unsigned maxIndex = pkgIdIndex;
1943 
1944 static int __kmp_affinity_cmp_ProcCpuInfo_phys_id(const void *a,
1945                                                   const void *b) {
1946   unsigned i;
1947   const unsigned *aa = *(unsigned *const *)a;
1948   const unsigned *bb = *(unsigned *const *)b;
1949   for (i = maxIndex;; i--) {
1950     if (aa[i] < bb[i])
1951       return -1;
1952     if (aa[i] > bb[i])
1953       return 1;
1954     if (i == osIdIndex)
1955       break;
1956   }
1957   return 0;
1958 }
1959 
1960 #if KMP_USE_HIER_SCHED
1961 // Set the array sizes for the hierarchy layers
1962 static void __kmp_dispatch_set_hierarchy_values() {
1963   // Set the maximum number of L1's to number of cores
1964   // Set the maximum number of L2's to to either number of cores / 2 for
1965   // Intel(R) Xeon Phi(TM) coprocessor formally codenamed Knights Landing
1966   // Or the number of cores for Intel(R) Xeon(R) processors
1967   // Set the maximum number of NUMA nodes and L3's to number of packages
1968   __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1] =
1969       nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
1970   __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L1 + 1] = __kmp_ncores;
1971 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1972   if (__kmp_mic_type >= mic3)
1973     __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores / 2;
1974   else
1975 #endif // KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1976     __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L2 + 1] = __kmp_ncores;
1977   __kmp_hier_max_units[kmp_hier_layer_e::LAYER_L3 + 1] = nPackages;
1978   __kmp_hier_max_units[kmp_hier_layer_e::LAYER_NUMA + 1] = nPackages;
1979   __kmp_hier_max_units[kmp_hier_layer_e::LAYER_LOOP + 1] = 1;
1980   // Set the number of threads per unit
1981   // Number of hardware threads per L1/L2/L3/NUMA/LOOP
1982   __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_THREAD + 1] = 1;
1983   __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L1 + 1] =
1984       __kmp_nThreadsPerCore;
1985 #if KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1986   if (__kmp_mic_type >= mic3)
1987     __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
1988         2 * __kmp_nThreadsPerCore;
1989   else
1990 #endif // KMP_ARCH_X86_64 && (KMP_OS_LINUX || KMP_OS_WINDOWS)
1991     __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L2 + 1] =
1992         __kmp_nThreadsPerCore;
1993   __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_L3 + 1] =
1994       nCoresPerPkg * __kmp_nThreadsPerCore;
1995   __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_NUMA + 1] =
1996       nCoresPerPkg * __kmp_nThreadsPerCore;
1997   __kmp_hier_threads_per[kmp_hier_layer_e::LAYER_LOOP + 1] =
1998       nPackages * nCoresPerPkg * __kmp_nThreadsPerCore;
1999 }
2000 
2001 // Return the index into the hierarchy for this tid and layer type (L1, L2, etc)
2002 // i.e., this thread's L1 or this thread's L2, etc.
2003 int __kmp_dispatch_get_index(int tid, kmp_hier_layer_e type) {
2004   int index = type + 1;
2005   int num_hw_threads = __kmp_hier_max_units[kmp_hier_layer_e::LAYER_THREAD + 1];
2006   KMP_DEBUG_ASSERT(type != kmp_hier_layer_e::LAYER_LAST);
2007   if (type == kmp_hier_layer_e::LAYER_THREAD)
2008     return tid;
2009   else if (type == kmp_hier_layer_e::LAYER_LOOP)
2010     return 0;
2011   KMP_DEBUG_ASSERT(__kmp_hier_max_units[index] != 0);
2012   if (tid >= num_hw_threads)
2013     tid = tid % num_hw_threads;
2014   return (tid / __kmp_hier_threads_per[index]) % __kmp_hier_max_units[index];
2015 }
2016 
2017 // Return the number of t1's per t2
2018 int __kmp_dispatch_get_t1_per_t2(kmp_hier_layer_e t1, kmp_hier_layer_e t2) {
2019   int i1 = t1 + 1;
2020   int i2 = t2 + 1;
2021   KMP_DEBUG_ASSERT(i1 <= i2);
2022   KMP_DEBUG_ASSERT(t1 != kmp_hier_layer_e::LAYER_LAST);
2023   KMP_DEBUG_ASSERT(t2 != kmp_hier_layer_e::LAYER_LAST);
2024   KMP_DEBUG_ASSERT(__kmp_hier_threads_per[i1] != 0);
2025   // (nthreads/t2) / (nthreads/t1) = t1 / t2
2026   return __kmp_hier_threads_per[i2] / __kmp_hier_threads_per[i1];
2027 }
2028 #endif // KMP_USE_HIER_SCHED
2029 
2030 // Parse /proc/cpuinfo (or an alternate file in the same format) to obtain the
2031 // affinity map.
2032 static int __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os,
2033                                              int *line,
2034                                              kmp_i18n_id_t *const msg_id,
2035                                              FILE *f) {
2036   *address2os = NULL;
2037   *msg_id = kmp_i18n_null;
2038 
2039   // Scan of the file, and count the number of "processor" (osId) fields,
2040   // and find the highest value of <n> for a node_<n> field.
2041   char buf[256];
2042   unsigned num_records = 0;
2043   while (!feof(f)) {
2044     buf[sizeof(buf) - 1] = 1;
2045     if (!fgets(buf, sizeof(buf), f)) {
2046       // Read errors presumably because of EOF
2047       break;
2048     }
2049 
2050     char s1[] = "processor";
2051     if (strncmp(buf, s1, sizeof(s1) - 1) == 0) {
2052       num_records++;
2053       continue;
2054     }
2055 
2056     // FIXME - this will match "node_<n> <garbage>"
2057     unsigned level;
2058     if (KMP_SSCANF(buf, "node_%u id", &level) == 1) {
2059       if (nodeIdIndex + level >= maxIndex) {
2060         maxIndex = nodeIdIndex + level;
2061       }
2062       continue;
2063     }
2064   }
2065 
2066   // Check for empty file / no valid processor records, or too many. The number
2067   // of records can't exceed the number of valid bits in the affinity mask.
2068   if (num_records == 0) {
2069     *line = 0;
2070     *msg_id = kmp_i18n_str_NoProcRecords;
2071     return -1;
2072   }
2073   if (num_records > (unsigned)__kmp_xproc) {
2074     *line = 0;
2075     *msg_id = kmp_i18n_str_TooManyProcRecords;
2076     return -1;
2077   }
2078 
2079   // Set the file pointer back to the begginning, so that we can scan the file
2080   // again, this time performing a full parse of the data. Allocate a vector of
2081   // ProcCpuInfo object, where we will place the data. Adding an extra element
2082   // at the end allows us to remove a lot of extra checks for termination
2083   // conditions.
2084   if (fseek(f, 0, SEEK_SET) != 0) {
2085     *line = 0;
2086     *msg_id = kmp_i18n_str_CantRewindCpuinfo;
2087     return -1;
2088   }
2089 
2090   // Allocate the array of records to store the proc info in.  The dummy
2091   // element at the end makes the logic in filling them out easier to code.
2092   unsigned **threadInfo =
2093       (unsigned **)__kmp_allocate((num_records + 1) * sizeof(unsigned *));
2094   unsigned i;
2095   for (i = 0; i <= num_records; i++) {
2096     threadInfo[i] =
2097         (unsigned *)__kmp_allocate((maxIndex + 1) * sizeof(unsigned));
2098   }
2099 
2100 #define CLEANUP_THREAD_INFO                                                    \
2101   for (i = 0; i <= num_records; i++) {                                         \
2102     __kmp_free(threadInfo[i]);                                                 \
2103   }                                                                            \
2104   __kmp_free(threadInfo);
2105 
2106   // A value of UINT_MAX means that we didn't find the field
2107   unsigned __index;
2108 
2109 #define INIT_PROC_INFO(p)                                                      \
2110   for (__index = 0; __index <= maxIndex; __index++) {                          \
2111     (p)[__index] = UINT_MAX;                                                   \
2112   }
2113 
2114   for (i = 0; i <= num_records; i++) {
2115     INIT_PROC_INFO(threadInfo[i]);
2116   }
2117 
2118   unsigned num_avail = 0;
2119   *line = 0;
2120   while (!feof(f)) {
2121     // Create an inner scoping level, so that all the goto targets at the end of
2122     // the loop appear in an outer scoping level. This avoids warnings about
2123     // jumping past an initialization to a target in the same block.
2124     {
2125       buf[sizeof(buf) - 1] = 1;
2126       bool long_line = false;
2127       if (!fgets(buf, sizeof(buf), f)) {
2128         // Read errors presumably because of EOF
2129         // If there is valid data in threadInfo[num_avail], then fake
2130         // a blank line in ensure that the last address gets parsed.
2131         bool valid = false;
2132         for (i = 0; i <= maxIndex; i++) {
2133           if (threadInfo[num_avail][i] != UINT_MAX) {
2134             valid = true;
2135           }
2136         }
2137         if (!valid) {
2138           break;
2139         }
2140         buf[0] = 0;
2141       } else if (!buf[sizeof(buf) - 1]) {
2142         // The line is longer than the buffer.  Set a flag and don't
2143         // emit an error if we were going to ignore the line, anyway.
2144         long_line = true;
2145 
2146 #define CHECK_LINE                                                             \
2147   if (long_line) {                                                             \
2148     CLEANUP_THREAD_INFO;                                                       \
2149     *msg_id = kmp_i18n_str_LongLineCpuinfo;                                    \
2150     return -1;                                                                 \
2151   }
2152       }
2153       (*line)++;
2154 
2155       char s1[] = "processor";
2156       if (strncmp(buf, s1, sizeof(s1) - 1) == 0) {
2157         CHECK_LINE;
2158         char *p = strchr(buf + sizeof(s1) - 1, ':');
2159         unsigned val;
2160         if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1))
2161           goto no_val;
2162         if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
2163 #if KMP_ARCH_AARCH64
2164           // Handle the old AArch64 /proc/cpuinfo layout differently,
2165           // it contains all of the 'processor' entries listed in a
2166           // single 'Processor' section, therefore the normal looking
2167           // for duplicates in that section will always fail.
2168           num_avail++;
2169 #else
2170           goto dup_field;
2171 #endif
2172         threadInfo[num_avail][osIdIndex] = val;
2173 #if KMP_OS_LINUX && !(KMP_ARCH_X86 || KMP_ARCH_X86_64)
2174         char path[256];
2175         KMP_SNPRINTF(
2176             path, sizeof(path),
2177             "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
2178             threadInfo[num_avail][osIdIndex]);
2179         __kmp_read_from_file(path, "%u", &threadInfo[num_avail][pkgIdIndex]);
2180 
2181         KMP_SNPRINTF(path, sizeof(path),
2182                      "/sys/devices/system/cpu/cpu%u/topology/core_id",
2183                      threadInfo[num_avail][osIdIndex]);
2184         __kmp_read_from_file(path, "%u", &threadInfo[num_avail][coreIdIndex]);
2185         continue;
2186 #else
2187       }
2188       char s2[] = "physical id";
2189       if (strncmp(buf, s2, sizeof(s2) - 1) == 0) {
2190         CHECK_LINE;
2191         char *p = strchr(buf + sizeof(s2) - 1, ':');
2192         unsigned val;
2193         if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1))
2194           goto no_val;
2195         if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
2196           goto dup_field;
2197         threadInfo[num_avail][pkgIdIndex] = val;
2198         continue;
2199       }
2200       char s3[] = "core id";
2201       if (strncmp(buf, s3, sizeof(s3) - 1) == 0) {
2202         CHECK_LINE;
2203         char *p = strchr(buf + sizeof(s3) - 1, ':');
2204         unsigned val;
2205         if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1))
2206           goto no_val;
2207         if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
2208           goto dup_field;
2209         threadInfo[num_avail][coreIdIndex] = val;
2210         continue;
2211 #endif // KMP_OS_LINUX && USE_SYSFS_INFO
2212       }
2213       char s4[] = "thread id";
2214       if (strncmp(buf, s4, sizeof(s4) - 1) == 0) {
2215         CHECK_LINE;
2216         char *p = strchr(buf + sizeof(s4) - 1, ':');
2217         unsigned val;
2218         if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1))
2219           goto no_val;
2220         if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
2221           goto dup_field;
2222         threadInfo[num_avail][threadIdIndex] = val;
2223         continue;
2224       }
2225       unsigned level;
2226       if (KMP_SSCANF(buf, "node_%u id", &level) == 1) {
2227         CHECK_LINE;
2228         char *p = strchr(buf + sizeof(s4) - 1, ':');
2229         unsigned val;
2230         if ((p == NULL) || (KMP_SSCANF(p + 1, "%u\n", &val) != 1))
2231           goto no_val;
2232         KMP_ASSERT(nodeIdIndex + level <= maxIndex);
2233         if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
2234           goto dup_field;
2235         threadInfo[num_avail][nodeIdIndex + level] = val;
2236         continue;
2237       }
2238 
2239       // We didn't recognize the leading token on the line. There are lots of
2240       // leading tokens that we don't recognize - if the line isn't empty, go on
2241       // to the next line.
2242       if ((*buf != 0) && (*buf != '\n')) {
2243         // If the line is longer than the buffer, read characters
2244         // until we find a newline.
2245         if (long_line) {
2246           int ch;
2247           while (((ch = fgetc(f)) != EOF) && (ch != '\n'))
2248             ;
2249         }
2250         continue;
2251       }
2252 
2253       // A newline has signalled the end of the processor record.
2254       // Check that there aren't too many procs specified.
2255       if ((int)num_avail == __kmp_xproc) {
2256         CLEANUP_THREAD_INFO;
2257         *msg_id = kmp_i18n_str_TooManyEntries;
2258         return -1;
2259       }
2260 
2261       // Check for missing fields.  The osId field must be there, and we
2262       // currently require that the physical id field is specified, also.
2263       if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
2264         CLEANUP_THREAD_INFO;
2265         *msg_id = kmp_i18n_str_MissingProcField;
2266         return -1;
2267       }
2268       if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
2269         CLEANUP_THREAD_INFO;
2270         *msg_id = kmp_i18n_str_MissingPhysicalIDField;
2271         return -1;
2272       }
2273 
2274       // Skip this proc if it is not included in the machine model.
2275       if (!KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex],
2276                          __kmp_affin_fullMask)) {
2277         INIT_PROC_INFO(threadInfo[num_avail]);
2278         continue;
2279       }
2280 
2281       // We have a successful parse of this proc's info.
2282       // Increment the counter, and prepare for the next proc.
2283       num_avail++;
2284       KMP_ASSERT(num_avail <= num_records);
2285       INIT_PROC_INFO(threadInfo[num_avail]);
2286     }
2287     continue;
2288 
2289   no_val:
2290     CLEANUP_THREAD_INFO;
2291     *msg_id = kmp_i18n_str_MissingValCpuinfo;
2292     return -1;
2293 
2294   dup_field:
2295     CLEANUP_THREAD_INFO;
2296     *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
2297     return -1;
2298   }
2299   *line = 0;
2300 
2301 #if KMP_MIC && REDUCE_TEAM_SIZE
2302   unsigned teamSize = 0;
2303 #endif // KMP_MIC && REDUCE_TEAM_SIZE
2304 
2305   // check for num_records == __kmp_xproc ???
2306 
2307   // If there's only one thread context to bind to, form an Address object with
2308   // depth 1 and return immediately (or, if affinity is off, set address2os to
2309   // NULL and return).
2310   //
2311   // If it is configured to omit the package level when there is only a single
2312   // package, the logic at the end of this routine won't work if there is only a
2313   // single thread - it would try to form an Address object with depth 0.
2314   KMP_ASSERT(num_avail > 0);
2315   KMP_ASSERT(num_avail <= num_records);
2316   if (num_avail == 1) {
2317     __kmp_ncores = 1;
2318     __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2319     if (__kmp_affinity_verbose) {
2320       if (!KMP_AFFINITY_CAPABLE()) {
2321         KMP_INFORM(AffNotCapableUseCpuinfo, "KMP_AFFINITY");
2322         KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
2323         KMP_INFORM(Uniform, "KMP_AFFINITY");
2324       } else {
2325         char buf[KMP_AFFIN_MASK_PRINT_LEN];
2326         __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
2327                                   __kmp_affin_fullMask);
2328         KMP_INFORM(AffCapableUseCpuinfo, "KMP_AFFINITY");
2329         if (__kmp_affinity_respect_mask) {
2330           KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
2331         } else {
2332           KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
2333         }
2334         KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
2335         KMP_INFORM(Uniform, "KMP_AFFINITY");
2336       }
2337       int index;
2338       kmp_str_buf_t buf;
2339       __kmp_str_buf_init(&buf);
2340       __kmp_str_buf_print(&buf, "1");
2341       for (index = maxIndex - 1; index > pkgIdIndex; index--) {
2342         __kmp_str_buf_print(&buf, " x 1");
2343       }
2344       KMP_INFORM(TopologyExtra, "KMP_AFFINITY", buf.str, 1, 1, 1);
2345       __kmp_str_buf_free(&buf);
2346     }
2347 
2348     if (__kmp_affinity_type == affinity_none) {
2349       CLEANUP_THREAD_INFO;
2350       return 0;
2351     }
2352 
2353     *address2os = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair));
2354     Address addr(1);
2355     addr.labels[0] = threadInfo[0][pkgIdIndex];
2356     (*address2os)[0] = AddrUnsPair(addr, threadInfo[0][osIdIndex]);
2357 
2358     if (__kmp_affinity_gran_levels < 0) {
2359       __kmp_affinity_gran_levels = 0;
2360     }
2361 
2362     if (__kmp_affinity_verbose) {
2363       __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
2364     }
2365 
2366     CLEANUP_THREAD_INFO;
2367     return 1;
2368   }
2369 
2370   // Sort the threadInfo table by physical Id.
2371   qsort(threadInfo, num_avail, sizeof(*threadInfo),
2372         __kmp_affinity_cmp_ProcCpuInfo_phys_id);
2373 
2374   // The table is now sorted by pkgId / coreId / threadId, but we really don't
2375   // know the radix of any of the fields. pkgId's may be sparsely assigned among
2376   // the chips on a system. Although coreId's are usually assigned
2377   // [0 .. coresPerPkg-1] and threadId's are usually assigned
2378   // [0..threadsPerCore-1], we don't want to make any such assumptions.
2379   //
2380   // For that matter, we don't know what coresPerPkg and threadsPerCore (or the
2381   // total # packages) are at this point - we want to determine that now. We
2382   // only have an upper bound on the first two figures.
2383   unsigned *counts =
2384       (unsigned *)__kmp_allocate((maxIndex + 1) * sizeof(unsigned));
2385   unsigned *maxCt =
2386       (unsigned *)__kmp_allocate((maxIndex + 1) * sizeof(unsigned));
2387   unsigned *totals =
2388       (unsigned *)__kmp_allocate((maxIndex + 1) * sizeof(unsigned));
2389   unsigned *lastId =
2390       (unsigned *)__kmp_allocate((maxIndex + 1) * sizeof(unsigned));
2391 
2392   bool assign_thread_ids = false;
2393   unsigned threadIdCt;
2394   unsigned index;
2395 
2396 restart_radix_check:
2397   threadIdCt = 0;
2398 
2399   // Initialize the counter arrays with data from threadInfo[0].
2400   if (assign_thread_ids) {
2401     if (threadInfo[0][threadIdIndex] == UINT_MAX) {
2402       threadInfo[0][threadIdIndex] = threadIdCt++;
2403     } else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
2404       threadIdCt = threadInfo[0][threadIdIndex] + 1;
2405     }
2406   }
2407   for (index = 0; index <= maxIndex; index++) {
2408     counts[index] = 1;
2409     maxCt[index] = 1;
2410     totals[index] = 1;
2411     lastId[index] = threadInfo[0][index];
2412     ;
2413   }
2414 
2415   // Run through the rest of the OS procs.
2416   for (i = 1; i < num_avail; i++) {
2417     // Find the most significant index whose id differs from the id for the
2418     // previous OS proc.
2419     for (index = maxIndex; index >= threadIdIndex; index--) {
2420       if (assign_thread_ids && (index == threadIdIndex)) {
2421         // Auto-assign the thread id field if it wasn't specified.
2422         if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2423           threadInfo[i][threadIdIndex] = threadIdCt++;
2424         }
2425         // Apparently the thread id field was specified for some entries and not
2426         // others. Start the thread id counter off at the next higher thread id.
2427         else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2428           threadIdCt = threadInfo[i][threadIdIndex] + 1;
2429         }
2430       }
2431       if (threadInfo[i][index] != lastId[index]) {
2432         // Run through all indices which are less significant, and reset the
2433         // counts to 1. At all levels up to and including index, we need to
2434         // increment the totals and record the last id.
2435         unsigned index2;
2436         for (index2 = threadIdIndex; index2 < index; index2++) {
2437           totals[index2]++;
2438           if (counts[index2] > maxCt[index2]) {
2439             maxCt[index2] = counts[index2];
2440           }
2441           counts[index2] = 1;
2442           lastId[index2] = threadInfo[i][index2];
2443         }
2444         counts[index]++;
2445         totals[index]++;
2446         lastId[index] = threadInfo[i][index];
2447 
2448         if (assign_thread_ids && (index > threadIdIndex)) {
2449 
2450 #if KMP_MIC && REDUCE_TEAM_SIZE
2451           // The default team size is the total #threads in the machine
2452           // minus 1 thread for every core that has 3 or more threads.
2453           teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
2454 #endif // KMP_MIC && REDUCE_TEAM_SIZE
2455 
2456           // Restart the thread counter, as we are on a new core.
2457           threadIdCt = 0;
2458 
2459           // Auto-assign the thread id field if it wasn't specified.
2460           if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2461             threadInfo[i][threadIdIndex] = threadIdCt++;
2462           }
2463 
2464           // Aparrently the thread id field was specified for some entries and
2465           // not others. Start the thread id counter off at the next higher
2466           // thread id.
2467           else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2468             threadIdCt = threadInfo[i][threadIdIndex] + 1;
2469           }
2470         }
2471         break;
2472       }
2473     }
2474     if (index < threadIdIndex) {
2475       // If thread ids were specified, it is an error if they are not unique.
2476       // Also, check that we waven't already restarted the loop (to be safe -
2477       // shouldn't need to).
2478       if ((threadInfo[i][threadIdIndex] != UINT_MAX) || assign_thread_ids) {
2479         __kmp_free(lastId);
2480         __kmp_free(totals);
2481         __kmp_free(maxCt);
2482         __kmp_free(counts);
2483         CLEANUP_THREAD_INFO;
2484         *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2485         return -1;
2486       }
2487 
2488       // If the thread ids were not specified and we see entries entries that
2489       // are duplicates, start the loop over and assign the thread ids manually.
2490       assign_thread_ids = true;
2491       goto restart_radix_check;
2492     }
2493   }
2494 
2495 #if KMP_MIC && REDUCE_TEAM_SIZE
2496   // The default team size is the total #threads in the machine
2497   // minus 1 thread for every core that has 3 or more threads.
2498   teamSize += (threadIdCt <= 2) ? (threadIdCt) : (threadIdCt - 1);
2499 #endif // KMP_MIC && REDUCE_TEAM_SIZE
2500 
2501   for (index = threadIdIndex; index <= maxIndex; index++) {
2502     if (counts[index] > maxCt[index]) {
2503       maxCt[index] = counts[index];
2504     }
2505   }
2506 
2507   __kmp_nThreadsPerCore = maxCt[threadIdIndex];
2508   nCoresPerPkg = maxCt[coreIdIndex];
2509   nPackages = totals[pkgIdIndex];
2510 
2511   // Check to see if the machine topology is uniform
2512   unsigned prod = totals[maxIndex];
2513   for (index = threadIdIndex; index < maxIndex; index++) {
2514     prod *= maxCt[index];
2515   }
2516   bool uniform = (prod == totals[threadIdIndex]);
2517 
2518   // When affinity is off, this routine will still be called to set
2519   // __kmp_ncores, as well as __kmp_nThreadsPerCore, nCoresPerPkg, & nPackages.
2520   // Make sure all these vars are set correctly, and return now if affinity is
2521   // not enabled.
2522   __kmp_ncores = totals[coreIdIndex];
2523 
2524   if (__kmp_affinity_verbose) {
2525     if (!KMP_AFFINITY_CAPABLE()) {
2526       KMP_INFORM(AffNotCapableUseCpuinfo, "KMP_AFFINITY");
2527       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
2528       if (uniform) {
2529         KMP_INFORM(Uniform, "KMP_AFFINITY");
2530       } else {
2531         KMP_INFORM(NonUniform, "KMP_AFFINITY");
2532       }
2533     } else {
2534       char buf[KMP_AFFIN_MASK_PRINT_LEN];
2535       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
2536                                 __kmp_affin_fullMask);
2537       KMP_INFORM(AffCapableUseCpuinfo, "KMP_AFFINITY");
2538       if (__kmp_affinity_respect_mask) {
2539         KMP_INFORM(InitOSProcSetRespect, "KMP_AFFINITY", buf);
2540       } else {
2541         KMP_INFORM(InitOSProcSetNotRespect, "KMP_AFFINITY", buf);
2542       }
2543       KMP_INFORM(AvailableOSProc, "KMP_AFFINITY", __kmp_avail_proc);
2544       if (uniform) {
2545         KMP_INFORM(Uniform, "KMP_AFFINITY");
2546       } else {
2547         KMP_INFORM(NonUniform, "KMP_AFFINITY");
2548       }
2549     }
2550     kmp_str_buf_t buf;
2551     __kmp_str_buf_init(&buf);
2552 
2553     __kmp_str_buf_print(&buf, "%d", totals[maxIndex]);
2554     for (index = maxIndex - 1; index >= pkgIdIndex; index--) {
2555       __kmp_str_buf_print(&buf, " x %d", maxCt[index]);
2556     }
2557     KMP_INFORM(TopologyExtra, "KMP_AFFINITY", buf.str, maxCt[coreIdIndex],
2558                maxCt[threadIdIndex], __kmp_ncores);
2559 
2560     __kmp_str_buf_free(&buf);
2561   }
2562 
2563 #if KMP_MIC && REDUCE_TEAM_SIZE
2564   // Set the default team size.
2565   if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
2566     __kmp_dflt_team_nth = teamSize;
2567     KA_TRACE(20, ("__kmp_affinity_create_cpuinfo_map: setting "
2568                   "__kmp_dflt_team_nth = %d\n",
2569                   __kmp_dflt_team_nth));
2570   }
2571 #endif // KMP_MIC && REDUCE_TEAM_SIZE
2572 
2573   KMP_DEBUG_ASSERT(__kmp_pu_os_idx == NULL);
2574   KMP_DEBUG_ASSERT(num_avail == (unsigned)__kmp_avail_proc);
2575   __kmp_pu_os_idx = (int *)__kmp_allocate(sizeof(int) * __kmp_avail_proc);
2576   for (i = 0; i < num_avail; ++i) { // fill the os indices
2577     __kmp_pu_os_idx[i] = threadInfo[i][osIdIndex];
2578   }
2579 
2580   if (__kmp_affinity_type == affinity_none) {
2581     __kmp_free(lastId);
2582     __kmp_free(totals);
2583     __kmp_free(maxCt);
2584     __kmp_free(counts);
2585     CLEANUP_THREAD_INFO;
2586     return 0;
2587   }
2588 
2589   // Count the number of levels which have more nodes at that level than at the
2590   // parent's level (with there being an implicit root node of the top level).
2591   // This is equivalent to saying that there is at least one node at this level
2592   // which has a sibling. These levels are in the map, and the package level is
2593   // always in the map.
2594   bool *inMap = (bool *)__kmp_allocate((maxIndex + 1) * sizeof(bool));
2595   for (index = threadIdIndex; index < maxIndex; index++) {
2596     KMP_ASSERT(totals[index] >= totals[index + 1]);
2597     inMap[index] = (totals[index] > totals[index + 1]);
2598   }
2599   inMap[maxIndex] = (totals[maxIndex] > 1);
2600   inMap[pkgIdIndex] = true;
2601 
2602   int depth = 0;
2603   for (index = threadIdIndex; index <= maxIndex; index++) {
2604     if (inMap[index]) {
2605       depth++;
2606     }
2607   }
2608   KMP_ASSERT(depth > 0);
2609 
2610   // Construct the data structure that is to be returned.
2611   *address2os = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) * num_avail);
2612   int pkgLevel = -1;
2613   int coreLevel = -1;
2614   int threadLevel = -1;
2615 
2616   for (i = 0; i < num_avail; ++i) {
2617     Address addr(depth);
2618     unsigned os = threadInfo[i][osIdIndex];
2619     int src_index;
2620     int dst_index = 0;
2621 
2622     for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
2623       if (!inMap[src_index]) {
2624         continue;
2625       }
2626       addr.labels[dst_index] = threadInfo[i][src_index];
2627       if (src_index == pkgIdIndex) {
2628         pkgLevel = dst_index;
2629       } else if (src_index == coreIdIndex) {
2630         coreLevel = dst_index;
2631       } else if (src_index == threadIdIndex) {
2632         threadLevel = dst_index;
2633       }
2634       dst_index++;
2635     }
2636     (*address2os)[i] = AddrUnsPair(addr, os);
2637   }
2638 
2639   if (__kmp_affinity_gran_levels < 0) {
2640     // Set the granularity level based on what levels are modeled
2641     // in the machine topology map.
2642     unsigned src_index;
2643     __kmp_affinity_gran_levels = 0;
2644     for (src_index = threadIdIndex; src_index <= maxIndex; src_index++) {
2645       if (!inMap[src_index]) {
2646         continue;
2647       }
2648       switch (src_index) {
2649       case threadIdIndex:
2650         if (__kmp_affinity_gran > affinity_gran_thread) {
2651           __kmp_affinity_gran_levels++;
2652         }
2653 
2654         break;
2655       case coreIdIndex:
2656         if (__kmp_affinity_gran > affinity_gran_core) {
2657           __kmp_affinity_gran_levels++;
2658         }
2659         break;
2660 
2661       case pkgIdIndex:
2662         if (__kmp_affinity_gran > affinity_gran_package) {
2663           __kmp_affinity_gran_levels++;
2664         }
2665         break;
2666       }
2667     }
2668   }
2669 
2670   if (__kmp_affinity_verbose) {
2671     __kmp_affinity_print_topology(*address2os, num_avail, depth, pkgLevel,
2672                                   coreLevel, threadLevel);
2673   }
2674 
2675   __kmp_free(inMap);
2676   __kmp_free(lastId);
2677   __kmp_free(totals);
2678   __kmp_free(maxCt);
2679   __kmp_free(counts);
2680   CLEANUP_THREAD_INFO;
2681   return depth;
2682 }
2683 
2684 // Create and return a table of affinity masks, indexed by OS thread ID.
2685 // This routine handles OR'ing together all the affinity masks of threads
2686 // that are sufficiently close, if granularity > fine.
2687 static kmp_affin_mask_t *__kmp_create_masks(unsigned *maxIndex,
2688                                             unsigned *numUnique,
2689                                             AddrUnsPair *address2os,
2690                                             unsigned numAddrs) {
2691   // First form a table of affinity masks in order of OS thread id.
2692   unsigned depth;
2693   unsigned maxOsId;
2694   unsigned i;
2695 
2696   KMP_ASSERT(numAddrs > 0);
2697   depth = address2os[0].first.depth;
2698 
2699   maxOsId = 0;
2700   for (i = numAddrs - 1;; --i) {
2701     unsigned osId = address2os[i].second;
2702     if (osId > maxOsId) {
2703       maxOsId = osId;
2704     }
2705     if (i == 0)
2706       break;
2707   }
2708   kmp_affin_mask_t *osId2Mask;
2709   KMP_CPU_ALLOC_ARRAY(osId2Mask, (maxOsId + 1));
2710 
2711   // Sort the address2os table according to physical order. Doing so will put
2712   // all threads on the same core/package/node in consecutive locations.
2713   qsort(address2os, numAddrs, sizeof(*address2os),
2714         __kmp_affinity_cmp_Address_labels);
2715 
2716   KMP_ASSERT(__kmp_affinity_gran_levels >= 0);
2717   if (__kmp_affinity_verbose && (__kmp_affinity_gran_levels > 0)) {
2718     KMP_INFORM(ThreadsMigrate, "KMP_AFFINITY", __kmp_affinity_gran_levels);
2719   }
2720   if (__kmp_affinity_gran_levels >= (int)depth) {
2721     if (__kmp_affinity_verbose ||
2722         (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
2723       KMP_WARNING(AffThreadsMayMigrate);
2724     }
2725   }
2726 
2727   // Run through the table, forming the masks for all threads on each core.
2728   // Threads on the same core will have identical "Address" objects, not
2729   // considering the last level, which must be the thread id. All threads on a
2730   // core will appear consecutively.
2731   unsigned unique = 0;
2732   unsigned j = 0; // index of 1st thread on core
2733   unsigned leader = 0;
2734   Address *leaderAddr = &(address2os[0].first);
2735   kmp_affin_mask_t *sum;
2736   KMP_CPU_ALLOC_ON_STACK(sum);
2737   KMP_CPU_ZERO(sum);
2738   KMP_CPU_SET(address2os[0].second, sum);
2739   for (i = 1; i < numAddrs; i++) {
2740     // If this thread is sufficiently close to the leader (within the
2741     // granularity setting), then set the bit for this os thread in the
2742     // affinity mask for this group, and go on to the next thread.
2743     if (leaderAddr->isClose(address2os[i].first, __kmp_affinity_gran_levels)) {
2744       KMP_CPU_SET(address2os[i].second, sum);
2745       continue;
2746     }
2747 
2748     // For every thread in this group, copy the mask to the thread's entry in
2749     // the osId2Mask table.  Mark the first address as a leader.
2750     for (; j < i; j++) {
2751       unsigned osId = address2os[j].second;
2752       KMP_DEBUG_ASSERT(osId <= maxOsId);
2753       kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2754       KMP_CPU_COPY(mask, sum);
2755       address2os[j].first.leader = (j == leader);
2756     }
2757     unique++;
2758 
2759     // Start a new mask.
2760     leader = i;
2761     leaderAddr = &(address2os[i].first);
2762     KMP_CPU_ZERO(sum);
2763     KMP_CPU_SET(address2os[i].second, sum);
2764   }
2765 
2766   // For every thread in last group, copy the mask to the thread's
2767   // entry in the osId2Mask table.
2768   for (; j < i; j++) {
2769     unsigned osId = address2os[j].second;
2770     KMP_DEBUG_ASSERT(osId <= maxOsId);
2771     kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2772     KMP_CPU_COPY(mask, sum);
2773     address2os[j].first.leader = (j == leader);
2774   }
2775   unique++;
2776   KMP_CPU_FREE_FROM_STACK(sum);
2777 
2778   *maxIndex = maxOsId;
2779   *numUnique = unique;
2780   return osId2Mask;
2781 }
2782 
2783 // Stuff for the affinity proclist parsers.  It's easier to declare these vars
2784 // as file-static than to try and pass them through the calling sequence of
2785 // the recursive-descent OMP_PLACES parser.
2786 static kmp_affin_mask_t *newMasks;
2787 static int numNewMasks;
2788 static int nextNewMask;
2789 
2790 #define ADD_MASK(_mask)                                                        \
2791   {                                                                            \
2792     if (nextNewMask >= numNewMasks) {                                          \
2793       int i;                                                                   \
2794       numNewMasks *= 2;                                                        \
2795       kmp_affin_mask_t *temp;                                                  \
2796       KMP_CPU_INTERNAL_ALLOC_ARRAY(temp, numNewMasks);                         \
2797       for (i = 0; i < numNewMasks / 2; i++) {                                  \
2798         kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);                    \
2799         kmp_affin_mask_t *dest = KMP_CPU_INDEX(temp, i);                       \
2800         KMP_CPU_COPY(dest, src);                                               \
2801       }                                                                        \
2802       KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks / 2);                  \
2803       newMasks = temp;                                                         \
2804     }                                                                          \
2805     KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask));               \
2806     nextNewMask++;                                                             \
2807   }
2808 
2809 #define ADD_MASK_OSID(_osId, _osId2Mask, _maxOsId)                             \
2810   {                                                                            \
2811     if (((_osId) > _maxOsId) ||                                                \
2812         (!KMP_CPU_ISSET((_osId), KMP_CPU_INDEX((_osId2Mask), (_osId))))) {     \
2813       if (__kmp_affinity_verbose ||                                            \
2814           (__kmp_affinity_warnings &&                                          \
2815            (__kmp_affinity_type != affinity_none))) {                          \
2816         KMP_WARNING(AffIgnoreInvalidProcID, _osId);                            \
2817       }                                                                        \
2818     } else {                                                                   \
2819       ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId)));                            \
2820     }                                                                          \
2821   }
2822 
2823 // Re-parse the proclist (for the explicit affinity type), and form the list
2824 // of affinity newMasks indexed by gtid.
2825 static void __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
2826                                             unsigned int *out_numMasks,
2827                                             const char *proclist,
2828                                             kmp_affin_mask_t *osId2Mask,
2829                                             int maxOsId) {
2830   int i;
2831   const char *scan = proclist;
2832   const char *next = proclist;
2833 
2834   // We use malloc() for the temporary mask vector, so that we can use
2835   // realloc() to extend it.
2836   numNewMasks = 2;
2837   KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
2838   nextNewMask = 0;
2839   kmp_affin_mask_t *sumMask;
2840   KMP_CPU_ALLOC(sumMask);
2841   int setSize = 0;
2842 
2843   for (;;) {
2844     int start, end, stride;
2845 
2846     SKIP_WS(scan);
2847     next = scan;
2848     if (*next == '\0') {
2849       break;
2850     }
2851 
2852     if (*next == '{') {
2853       int num;
2854       setSize = 0;
2855       next++; // skip '{'
2856       SKIP_WS(next);
2857       scan = next;
2858 
2859       // Read the first integer in the set.
2860       KMP_ASSERT2((*next >= '0') && (*next <= '9'), "bad proclist");
2861       SKIP_DIGITS(next);
2862       num = __kmp_str_to_int(scan, *next);
2863       KMP_ASSERT2(num >= 0, "bad explicit proc list");
2864 
2865       // Copy the mask for that osId to the sum (union) mask.
2866       if ((num > maxOsId) ||
2867           (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2868         if (__kmp_affinity_verbose ||
2869             (__kmp_affinity_warnings &&
2870              (__kmp_affinity_type != affinity_none))) {
2871           KMP_WARNING(AffIgnoreInvalidProcID, num);
2872         }
2873         KMP_CPU_ZERO(sumMask);
2874       } else {
2875         KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2876         setSize = 1;
2877       }
2878 
2879       for (;;) {
2880         // Check for end of set.
2881         SKIP_WS(next);
2882         if (*next == '}') {
2883           next++; // skip '}'
2884           break;
2885         }
2886 
2887         // Skip optional comma.
2888         if (*next == ',') {
2889           next++;
2890         }
2891         SKIP_WS(next);
2892 
2893         // Read the next integer in the set.
2894         scan = next;
2895         KMP_ASSERT2((*next >= '0') && (*next <= '9'), "bad explicit proc list");
2896 
2897         SKIP_DIGITS(next);
2898         num = __kmp_str_to_int(scan, *next);
2899         KMP_ASSERT2(num >= 0, "bad explicit proc list");
2900 
2901         // Add the mask for that osId to the sum mask.
2902         if ((num > maxOsId) ||
2903             (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2904           if (__kmp_affinity_verbose ||
2905               (__kmp_affinity_warnings &&
2906                (__kmp_affinity_type != affinity_none))) {
2907             KMP_WARNING(AffIgnoreInvalidProcID, num);
2908           }
2909         } else {
2910           KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2911           setSize++;
2912         }
2913       }
2914       if (setSize > 0) {
2915         ADD_MASK(sumMask);
2916       }
2917 
2918       SKIP_WS(next);
2919       if (*next == ',') {
2920         next++;
2921       }
2922       scan = next;
2923       continue;
2924     }
2925 
2926     // Read the first integer.
2927     KMP_ASSERT2((*next >= '0') && (*next <= '9'), "bad explicit proc list");
2928     SKIP_DIGITS(next);
2929     start = __kmp_str_to_int(scan, *next);
2930     KMP_ASSERT2(start >= 0, "bad explicit proc list");
2931     SKIP_WS(next);
2932 
2933     // If this isn't a range, then add a mask to the list and go on.
2934     if (*next != '-') {
2935       ADD_MASK_OSID(start, osId2Mask, maxOsId);
2936 
2937       // Skip optional comma.
2938       if (*next == ',') {
2939         next++;
2940       }
2941       scan = next;
2942       continue;
2943     }
2944 
2945     // This is a range.  Skip over the '-' and read in the 2nd int.
2946     next++; // skip '-'
2947     SKIP_WS(next);
2948     scan = next;
2949     KMP_ASSERT2((*next >= '0') && (*next <= '9'), "bad explicit proc list");
2950     SKIP_DIGITS(next);
2951     end = __kmp_str_to_int(scan, *next);
2952     KMP_ASSERT2(end >= 0, "bad explicit proc list");
2953 
2954     // Check for a stride parameter
2955     stride = 1;
2956     SKIP_WS(next);
2957     if (*next == ':') {
2958       // A stride is specified.  Skip over the ':" and read the 3rd int.
2959       int sign = +1;
2960       next++; // skip ':'
2961       SKIP_WS(next);
2962       scan = next;
2963       if (*next == '-') {
2964         sign = -1;
2965         next++;
2966         SKIP_WS(next);
2967         scan = next;
2968       }
2969       KMP_ASSERT2((*next >= '0') && (*next <= '9'), "bad explicit proc list");
2970       SKIP_DIGITS(next);
2971       stride = __kmp_str_to_int(scan, *next);
2972       KMP_ASSERT2(stride >= 0, "bad explicit proc list");
2973       stride *= sign;
2974     }
2975 
2976     // Do some range checks.
2977     KMP_ASSERT2(stride != 0, "bad explicit proc list");
2978     if (stride > 0) {
2979       KMP_ASSERT2(start <= end, "bad explicit proc list");
2980     } else {
2981       KMP_ASSERT2(start >= end, "bad explicit proc list");
2982     }
2983     KMP_ASSERT2((end - start) / stride <= 65536, "bad explicit proc list");
2984 
2985     // Add the mask for each OS proc # to the list.
2986     if (stride > 0) {
2987       do {
2988         ADD_MASK_OSID(start, osId2Mask, maxOsId);
2989         start += stride;
2990       } while (start <= end);
2991     } else {
2992       do {
2993         ADD_MASK_OSID(start, osId2Mask, maxOsId);
2994         start += stride;
2995       } while (start >= end);
2996     }
2997 
2998     // Skip optional comma.
2999     SKIP_WS(next);
3000     if (*next == ',') {
3001       next++;
3002     }
3003     scan = next;
3004   }
3005 
3006   *out_numMasks = nextNewMask;
3007   if (nextNewMask == 0) {
3008     *out_masks = NULL;
3009     KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3010     return;
3011   }
3012   KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3013   for (i = 0; i < nextNewMask; i++) {
3014     kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3015     kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3016     KMP_CPU_COPY(dest, src);
3017   }
3018   KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3019   KMP_CPU_FREE(sumMask);
3020 }
3021 
3022 #if OMP_40_ENABLED
3023 
3024 /*-----------------------------------------------------------------------------
3025 Re-parse the OMP_PLACES proc id list, forming the newMasks for the different
3026 places.  Again, Here is the grammar:
3027 
3028 place_list := place
3029 place_list := place , place_list
3030 place := num
3031 place := place : num
3032 place := place : num : signed
3033 place := { subplacelist }
3034 place := ! place                  // (lowest priority)
3035 subplace_list := subplace
3036 subplace_list := subplace , subplace_list
3037 subplace := num
3038 subplace := num : num
3039 subplace := num : num : signed
3040 signed := num
3041 signed := + signed
3042 signed := - signed
3043 -----------------------------------------------------------------------------*/
3044 
3045 static void __kmp_process_subplace_list(const char **scan,
3046                                         kmp_affin_mask_t *osId2Mask,
3047                                         int maxOsId, kmp_affin_mask_t *tempMask,
3048                                         int *setSize) {
3049   const char *next;
3050 
3051   for (;;) {
3052     int start, count, stride, i;
3053 
3054     // Read in the starting proc id
3055     SKIP_WS(*scan);
3056     KMP_ASSERT2((**scan >= '0') && (**scan <= '9'), "bad explicit places list");
3057     next = *scan;
3058     SKIP_DIGITS(next);
3059     start = __kmp_str_to_int(*scan, *next);
3060     KMP_ASSERT(start >= 0);
3061     *scan = next;
3062 
3063     // valid follow sets are ',' ':' and '}'
3064     SKIP_WS(*scan);
3065     if (**scan == '}' || **scan == ',') {
3066       if ((start > maxOsId) ||
3067           (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3068         if (__kmp_affinity_verbose ||
3069             (__kmp_affinity_warnings &&
3070              (__kmp_affinity_type != affinity_none))) {
3071           KMP_WARNING(AffIgnoreInvalidProcID, start);
3072         }
3073       } else {
3074         KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3075         (*setSize)++;
3076       }
3077       if (**scan == '}') {
3078         break;
3079       }
3080       (*scan)++; // skip ','
3081       continue;
3082     }
3083     KMP_ASSERT2(**scan == ':', "bad explicit places list");
3084     (*scan)++; // skip ':'
3085 
3086     // Read count parameter
3087     SKIP_WS(*scan);
3088     KMP_ASSERT2((**scan >= '0') && (**scan <= '9'), "bad explicit places list");
3089     next = *scan;
3090     SKIP_DIGITS(next);
3091     count = __kmp_str_to_int(*scan, *next);
3092     KMP_ASSERT(count >= 0);
3093     *scan = next;
3094 
3095     // valid follow sets are ',' ':' and '}'
3096     SKIP_WS(*scan);
3097     if (**scan == '}' || **scan == ',') {
3098       for (i = 0; i < count; i++) {
3099         if ((start > maxOsId) ||
3100             (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3101           if (__kmp_affinity_verbose ||
3102               (__kmp_affinity_warnings &&
3103                (__kmp_affinity_type != affinity_none))) {
3104             KMP_WARNING(AffIgnoreInvalidProcID, start);
3105           }
3106           break; // don't proliferate warnings for large count
3107         } else {
3108           KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3109           start++;
3110           (*setSize)++;
3111         }
3112       }
3113       if (**scan == '}') {
3114         break;
3115       }
3116       (*scan)++; // skip ','
3117       continue;
3118     }
3119     KMP_ASSERT2(**scan == ':', "bad explicit places list");
3120     (*scan)++; // skip ':'
3121 
3122     // Read stride parameter
3123     int sign = +1;
3124     for (;;) {
3125       SKIP_WS(*scan);
3126       if (**scan == '+') {
3127         (*scan)++; // skip '+'
3128         continue;
3129       }
3130       if (**scan == '-') {
3131         sign *= -1;
3132         (*scan)++; // skip '-'
3133         continue;
3134       }
3135       break;
3136     }
3137     SKIP_WS(*scan);
3138     KMP_ASSERT2((**scan >= '0') && (**scan <= '9'), "bad explicit places list");
3139     next = *scan;
3140     SKIP_DIGITS(next);
3141     stride = __kmp_str_to_int(*scan, *next);
3142     KMP_ASSERT(stride >= 0);
3143     *scan = next;
3144     stride *= sign;
3145 
3146     // valid follow sets are ',' and '}'
3147     SKIP_WS(*scan);
3148     if (**scan == '}' || **scan == ',') {
3149       for (i = 0; i < count; i++) {
3150         if ((start > maxOsId) ||
3151             (!KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3152           if (__kmp_affinity_verbose ||
3153               (__kmp_affinity_warnings &&
3154                (__kmp_affinity_type != affinity_none))) {
3155             KMP_WARNING(AffIgnoreInvalidProcID, start);
3156           }
3157           break; // don't proliferate warnings for large count
3158         } else {
3159           KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3160           start += stride;
3161           (*setSize)++;
3162         }
3163       }
3164       if (**scan == '}') {
3165         break;
3166       }
3167       (*scan)++; // skip ','
3168       continue;
3169     }
3170 
3171     KMP_ASSERT2(0, "bad explicit places list");
3172   }
3173 }
3174 
3175 static void __kmp_process_place(const char **scan, kmp_affin_mask_t *osId2Mask,
3176                                 int maxOsId, kmp_affin_mask_t *tempMask,
3177                                 int *setSize) {
3178   const char *next;
3179 
3180   // valid follow sets are '{' '!' and num
3181   SKIP_WS(*scan);
3182   if (**scan == '{') {
3183     (*scan)++; // skip '{'
3184     __kmp_process_subplace_list(scan, osId2Mask, maxOsId, tempMask, setSize);
3185     KMP_ASSERT2(**scan == '}', "bad explicit places list");
3186     (*scan)++; // skip '}'
3187   } else if (**scan == '!') {
3188     (*scan)++; // skip '!'
3189     __kmp_process_place(scan, osId2Mask, maxOsId, tempMask, setSize);
3190     KMP_CPU_COMPLEMENT(maxOsId, tempMask);
3191   } else if ((**scan >= '0') && (**scan <= '9')) {
3192     next = *scan;
3193     SKIP_DIGITS(next);
3194     int num = __kmp_str_to_int(*scan, *next);
3195     KMP_ASSERT(num >= 0);
3196     if ((num > maxOsId) ||
3197         (!KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3198       if (__kmp_affinity_verbose ||
3199           (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
3200         KMP_WARNING(AffIgnoreInvalidProcID, num);
3201       }
3202     } else {
3203       KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3204       (*setSize)++;
3205     }
3206     *scan = next; // skip num
3207   } else {
3208     KMP_ASSERT2(0, "bad explicit places list");
3209   }
3210 }
3211 
3212 // static void
3213 void __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
3214                                       unsigned int *out_numMasks,
3215                                       const char *placelist,
3216                                       kmp_affin_mask_t *osId2Mask,
3217                                       int maxOsId) {
3218   int i, j, count, stride, sign;
3219   const char *scan = placelist;
3220   const char *next = placelist;
3221 
3222   numNewMasks = 2;
3223   KMP_CPU_INTERNAL_ALLOC_ARRAY(newMasks, numNewMasks);
3224   nextNewMask = 0;
3225 
3226   // tempMask is modified based on the previous or initial
3227   //   place to form the current place
3228   // previousMask contains the previous place
3229   kmp_affin_mask_t *tempMask;
3230   kmp_affin_mask_t *previousMask;
3231   KMP_CPU_ALLOC(tempMask);
3232   KMP_CPU_ZERO(tempMask);
3233   KMP_CPU_ALLOC(previousMask);
3234   KMP_CPU_ZERO(previousMask);
3235   int setSize = 0;
3236 
3237   for (;;) {
3238     __kmp_process_place(&scan, osId2Mask, maxOsId, tempMask, &setSize);
3239 
3240     // valid follow sets are ',' ':' and EOL
3241     SKIP_WS(scan);
3242     if (*scan == '\0' || *scan == ',') {
3243       if (setSize > 0) {
3244         ADD_MASK(tempMask);
3245       }
3246       KMP_CPU_ZERO(tempMask);
3247       setSize = 0;
3248       if (*scan == '\0') {
3249         break;
3250       }
3251       scan++; // skip ','
3252       continue;
3253     }
3254 
3255     KMP_ASSERT2(*scan == ':', "bad explicit places list");
3256     scan++; // skip ':'
3257 
3258     // Read count parameter
3259     SKIP_WS(scan);
3260     KMP_ASSERT2((*scan >= '0') && (*scan <= '9'), "bad explicit places list");
3261     next = scan;
3262     SKIP_DIGITS(next);
3263     count = __kmp_str_to_int(scan, *next);
3264     KMP_ASSERT(count >= 0);
3265     scan = next;
3266 
3267     // valid follow sets are ',' ':' and EOL
3268     SKIP_WS(scan);
3269     if (*scan == '\0' || *scan == ',') {
3270       stride = +1;
3271     } else {
3272       KMP_ASSERT2(*scan == ':', "bad explicit places list");
3273       scan++; // skip ':'
3274 
3275       // Read stride parameter
3276       sign = +1;
3277       for (;;) {
3278         SKIP_WS(scan);
3279         if (*scan == '+') {
3280           scan++; // skip '+'
3281           continue;
3282         }
3283         if (*scan == '-') {
3284           sign *= -1;
3285           scan++; // skip '-'
3286           continue;
3287         }
3288         break;
3289       }
3290       SKIP_WS(scan);
3291       KMP_ASSERT2((*scan >= '0') && (*scan <= '9'), "bad explicit places list");
3292       next = scan;
3293       SKIP_DIGITS(next);
3294       stride = __kmp_str_to_int(scan, *next);
3295       KMP_DEBUG_ASSERT(stride >= 0);
3296       scan = next;
3297       stride *= sign;
3298     }
3299 
3300     // Add places determined by initial_place : count : stride
3301     for (i = 0; i < count; i++) {
3302       if (setSize == 0) {
3303         break;
3304       }
3305       // Add the current place, then build the next place (tempMask) from that
3306       KMP_CPU_COPY(previousMask, tempMask);
3307       ADD_MASK(previousMask);
3308       KMP_CPU_ZERO(tempMask);
3309       setSize = 0;
3310       KMP_CPU_SET_ITERATE(j, previousMask) {
3311         if (!KMP_CPU_ISSET(j, previousMask)) {
3312           continue;
3313         }
3314         if ((j + stride > maxOsId) || (j + stride < 0) ||
3315             (!KMP_CPU_ISSET(j, __kmp_affin_fullMask)) ||
3316             (!KMP_CPU_ISSET(j + stride,
3317                             KMP_CPU_INDEX(osId2Mask, j + stride)))) {
3318           if ((__kmp_affinity_verbose ||
3319                (__kmp_affinity_warnings &&
3320                 (__kmp_affinity_type != affinity_none))) &&
3321               i < count - 1) {
3322             KMP_WARNING(AffIgnoreInvalidProcID, j + stride);
3323           }
3324           continue;
3325         }
3326         KMP_CPU_SET(j + stride, tempMask);
3327         setSize++;
3328       }
3329     }
3330     KMP_CPU_ZERO(tempMask);
3331     setSize = 0;
3332 
3333     // valid follow sets are ',' and EOL
3334     SKIP_WS(scan);
3335     if (*scan == '\0') {
3336       break;
3337     }
3338     if (*scan == ',') {
3339       scan++; // skip ','
3340       continue;
3341     }
3342 
3343     KMP_ASSERT2(0, "bad explicit places list");
3344   }
3345 
3346   *out_numMasks = nextNewMask;
3347   if (nextNewMask == 0) {
3348     *out_masks = NULL;
3349     KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3350     return;
3351   }
3352   KMP_CPU_ALLOC_ARRAY((*out_masks), nextNewMask);
3353   KMP_CPU_FREE(tempMask);
3354   KMP_CPU_FREE(previousMask);
3355   for (i = 0; i < nextNewMask; i++) {
3356     kmp_affin_mask_t *src = KMP_CPU_INDEX(newMasks, i);
3357     kmp_affin_mask_t *dest = KMP_CPU_INDEX((*out_masks), i);
3358     KMP_CPU_COPY(dest, src);
3359   }
3360   KMP_CPU_INTERNAL_FREE_ARRAY(newMasks, numNewMasks);
3361 }
3362 
3363 #endif /* OMP_40_ENABLED */
3364 
3365 #undef ADD_MASK
3366 #undef ADD_MASK_OSID
3367 
3368 #if KMP_USE_HWLOC
3369 static int __kmp_hwloc_skip_PUs_obj(hwloc_topology_t t, hwloc_obj_t o) {
3370   // skip PUs descendants of the object o
3371   int skipped = 0;
3372   hwloc_obj_t hT = NULL;
3373   int N = __kmp_hwloc_count_children_by_type(t, o, HWLOC_OBJ_PU, &hT);
3374   for (int i = 0; i < N; ++i) {
3375     KMP_DEBUG_ASSERT(hT);
3376     unsigned idx = hT->os_index;
3377     if (KMP_CPU_ISSET(idx, __kmp_affin_fullMask)) {
3378       KMP_CPU_CLR(idx, __kmp_affin_fullMask);
3379       KC_TRACE(200, ("KMP_HW_SUBSET: skipped proc %d\n", idx));
3380       ++skipped;
3381     }
3382     hT = hwloc_get_next_obj_by_type(t, HWLOC_OBJ_PU, hT);
3383   }
3384   return skipped; // count number of skipped units
3385 }
3386 
3387 static int __kmp_hwloc_obj_has_PUs(hwloc_topology_t t, hwloc_obj_t o) {
3388   // check if obj has PUs present in fullMask
3389   hwloc_obj_t hT = NULL;
3390   int N = __kmp_hwloc_count_children_by_type(t, o, HWLOC_OBJ_PU, &hT);
3391   for (int i = 0; i < N; ++i) {
3392     KMP_DEBUG_ASSERT(hT);
3393     unsigned idx = hT->os_index;
3394     if (KMP_CPU_ISSET(idx, __kmp_affin_fullMask))
3395       return 1; // found PU
3396     hT = hwloc_get_next_obj_by_type(t, HWLOC_OBJ_PU, hT);
3397   }
3398   return 0; // no PUs found
3399 }
3400 #endif // KMP_USE_HWLOC
3401 
3402 static void __kmp_apply_thread_places(AddrUnsPair **pAddr, int depth) {
3403   AddrUnsPair *newAddr;
3404   if (__kmp_hws_requested == 0)
3405     goto _exit; // no topology limiting actions requested, exit
3406 #if KMP_USE_HWLOC
3407   if (__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
3408     // Number of subobjects calculated dynamically, this works fine for
3409     // any non-uniform topology.
3410     // L2 cache objects are determined by depth, other objects - by type.
3411     hwloc_topology_t tp = __kmp_hwloc_topology;
3412     int nS = 0, nN = 0, nL = 0, nC = 0,
3413         nT = 0; // logical index including skipped
3414     int nCr = 0, nTr = 0; // number of requested units
3415     int nPkg = 0, nCo = 0, n_new = 0, n_old = 0, nCpP = 0, nTpC = 0; // counters
3416     hwloc_obj_t hT, hC, hL, hN, hS; // hwloc objects (pointers to)
3417     int L2depth, idx;
3418 
3419     // check support of extensions ----------------------------------
3420     int numa_support = 0, tile_support = 0;
3421     if (__kmp_pu_os_idx)
3422       hT = hwloc_get_pu_obj_by_os_index(tp,
3423                                         __kmp_pu_os_idx[__kmp_avail_proc - 1]);
3424     else
3425       hT = hwloc_get_obj_by_type(tp, HWLOC_OBJ_PU, __kmp_avail_proc - 1);
3426     if (hT == NULL) { // something's gone wrong
3427       KMP_WARNING(AffHWSubsetUnsupported);
3428       goto _exit;
3429     }
3430     // check NUMA node
3431     hN = hwloc_get_ancestor_obj_by_type(tp, HWLOC_OBJ_NUMANODE, hT);
3432     hS = hwloc_get_ancestor_obj_by_type(tp, HWLOC_OBJ_PACKAGE, hT);
3433     if (hN != NULL && hN->depth > hS->depth) {
3434       numa_support = 1; // 1 in case socket includes node(s)
3435     } else if (__kmp_hws_node.num > 0) {
3436       // don't support sockets inside NUMA node (no such HW found for testing)
3437       KMP_WARNING(AffHWSubsetUnsupported);
3438       goto _exit;
3439     }
3440     // check L2 cahce, get object by depth because of multiple caches
3441     L2depth = hwloc_get_cache_type_depth(tp, 2, HWLOC_OBJ_CACHE_UNIFIED);
3442     hL = hwloc_get_ancestor_obj_by_depth(tp, L2depth, hT);
3443     if (hL != NULL &&
3444         __kmp_hwloc_count_children_by_type(tp, hL, HWLOC_OBJ_CORE, &hC) > 1) {
3445       tile_support = 1; // no sense to count L2 if it includes single core
3446     } else if (__kmp_hws_tile.num > 0) {
3447       if (__kmp_hws_core.num == 0) {
3448         __kmp_hws_core = __kmp_hws_tile; // replace L2 with core
3449         __kmp_hws_tile.num = 0;
3450       } else {
3451         // L2 and core are both requested, but represent same object
3452         KMP_WARNING(AffHWSubsetInvalid);
3453         goto _exit;
3454       }
3455     }
3456     // end of check of extensions -----------------------------------
3457 
3458     // fill in unset items, validate settings -----------------------
3459     if (__kmp_hws_socket.num == 0)
3460       __kmp_hws_socket.num = nPackages; // use all available sockets
3461     if (__kmp_hws_socket.offset >= nPackages) {
3462       KMP_WARNING(AffHWSubsetManySockets);
3463       goto _exit;
3464     }
3465     if (numa_support) {
3466       hN = NULL;
3467       int NN = __kmp_hwloc_count_children_by_type(tp, hS, HWLOC_OBJ_NUMANODE,
3468                                                   &hN); // num nodes in socket
3469       if (__kmp_hws_node.num == 0)
3470         __kmp_hws_node.num = NN; // use all available nodes
3471       if (__kmp_hws_node.offset >= NN) {
3472         KMP_WARNING(AffHWSubsetManyNodes);
3473         goto _exit;
3474       }
3475       if (tile_support) {
3476         // get num tiles in node
3477         int NL = __kmp_hwloc_count_children_by_depth(tp, hN, L2depth, &hL);
3478         if (__kmp_hws_tile.num == 0) {
3479           __kmp_hws_tile.num = NL + 1;
3480         } // use all available tiles, some node may have more tiles, thus +1
3481         if (__kmp_hws_tile.offset >= NL) {
3482           KMP_WARNING(AffHWSubsetManyTiles);
3483           goto _exit;
3484         }
3485         int NC = __kmp_hwloc_count_children_by_type(tp, hL, HWLOC_OBJ_CORE,
3486                                                     &hC); // num cores in tile
3487         if (__kmp_hws_core.num == 0)
3488           __kmp_hws_core.num = NC; // use all available cores
3489         if (__kmp_hws_core.offset >= NC) {
3490           KMP_WARNING(AffHWSubsetManyCores);
3491           goto _exit;
3492         }
3493       } else { // tile_support
3494         int NC = __kmp_hwloc_count_children_by_type(tp, hN, HWLOC_OBJ_CORE,
3495                                                     &hC); // num cores in node
3496         if (__kmp_hws_core.num == 0)
3497           __kmp_hws_core.num = NC; // use all available cores
3498         if (__kmp_hws_core.offset >= NC) {
3499           KMP_WARNING(AffHWSubsetManyCores);
3500           goto _exit;
3501         }
3502       } // tile_support
3503     } else { // numa_support
3504       if (tile_support) {
3505         // get num tiles in socket
3506         int NL = __kmp_hwloc_count_children_by_depth(tp, hS, L2depth, &hL);
3507         if (__kmp_hws_tile.num == 0)
3508           __kmp_hws_tile.num = NL; // use all available tiles
3509         if (__kmp_hws_tile.offset >= NL) {
3510           KMP_WARNING(AffHWSubsetManyTiles);
3511           goto _exit;
3512         }
3513         int NC = __kmp_hwloc_count_children_by_type(tp, hL, HWLOC_OBJ_CORE,
3514                                                     &hC); // num cores in tile
3515         if (__kmp_hws_core.num == 0)
3516           __kmp_hws_core.num = NC; // use all available cores
3517         if (__kmp_hws_core.offset >= NC) {
3518           KMP_WARNING(AffHWSubsetManyCores);
3519           goto _exit;
3520         }
3521       } else { // tile_support
3522         int NC = __kmp_hwloc_count_children_by_type(tp, hS, HWLOC_OBJ_CORE,
3523                                                     &hC); // num cores in socket
3524         if (__kmp_hws_core.num == 0)
3525           __kmp_hws_core.num = NC; // use all available cores
3526         if (__kmp_hws_core.offset >= NC) {
3527           KMP_WARNING(AffHWSubsetManyCores);
3528           goto _exit;
3529         }
3530       } // tile_support
3531     }
3532     if (__kmp_hws_proc.num == 0)
3533       __kmp_hws_proc.num = __kmp_nThreadsPerCore; // use all available procs
3534     if (__kmp_hws_proc.offset >= __kmp_nThreadsPerCore) {
3535       KMP_WARNING(AffHWSubsetManyProcs);
3536       goto _exit;
3537     }
3538     // end of validation --------------------------------------------
3539 
3540     if (pAddr) // pAddr is NULL in case of affinity_none
3541       newAddr = (AddrUnsPair *)__kmp_allocate(sizeof(AddrUnsPair) *
3542                                               __kmp_avail_proc); // max size
3543     // main loop to form HW subset ----------------------------------
3544     hS = NULL;
3545     int NP = hwloc_get_nbobjs_by_type(tp, HWLOC_OBJ_PACKAGE);
3546     for (int s = 0; s < NP; ++s) {
3547       // Check Socket -----------------------------------------------
3548       hS = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PACKAGE, hS);
3549       if (!__kmp_hwloc_obj_has_PUs(tp, hS))
3550         continue; // skip socket if all PUs are out of fullMask
3551       ++nS; // only count objects those have PUs in affinity mask
3552       if (nS <= __kmp_hws_socket.offset ||
3553           nS > __kmp_hws_socket.num + __kmp_hws_socket.offset) {
3554         n_old += __kmp_hwloc_skip_PUs_obj(tp, hS); // skip socket
3555         continue; // move to next socket
3556       }
3557       nCr = 0; // count number of cores per socket
3558       // socket requested, go down the topology tree
3559       // check 4 cases: (+NUMA+Tile), (+NUMA-Tile), (-NUMA+Tile), (-NUMA-Tile)
3560       if (numa_support) {
3561         nN = 0;
3562         hN = NULL;
3563         // num nodes in current socket
3564         int NN =
3565             __kmp_hwloc_count_children_by_type(tp, hS, HWLOC_OBJ_NUMANODE, &hN);
3566         for (int n = 0; n < NN; ++n) {
3567           // Check NUMA Node ----------------------------------------
3568           if (!__kmp_hwloc_obj_has_PUs(tp, hN)) {
3569             hN = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_NUMANODE, hN);
3570             continue; // skip node if all PUs are out of fullMask
3571           }
3572           ++nN;
3573           if (nN <= __kmp_hws_node.offset ||
3574               nN > __kmp_hws_node.num + __kmp_hws_node.offset) {
3575             // skip node as not requested
3576             n_old += __kmp_hwloc_skip_PUs_obj(tp, hN); // skip node
3577             hN = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_NUMANODE, hN);
3578             continue; // move to next node
3579           }
3580           // node requested, go down the topology tree
3581           if (tile_support) {
3582             nL = 0;
3583             hL = NULL;
3584             int NL = __kmp_hwloc_count_children_by_depth(tp, hN, L2depth, &hL);
3585             for (int l = 0; l < NL; ++l) {
3586               // Check L2 (tile) ------------------------------------
3587               if (!__kmp_hwloc_obj_has_PUs(tp, hL)) {
3588                 hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3589                 continue; // skip tile if all PUs are out of fullMask
3590               }
3591               ++nL;
3592               if (nL <= __kmp_hws_tile.offset ||
3593                   nL > __kmp_hws_tile.num + __kmp_hws_tile.offset) {
3594                 // skip tile as not requested
3595                 n_old += __kmp_hwloc_skip_PUs_obj(tp, hL); // skip tile
3596                 hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3597                 continue; // move to next tile
3598               }
3599               // tile requested, go down the topology tree
3600               nC = 0;
3601               hC = NULL;
3602               // num cores in current tile
3603               int NC = __kmp_hwloc_count_children_by_type(tp, hL,
3604                                                           HWLOC_OBJ_CORE, &hC);
3605               for (int c = 0; c < NC; ++c) {
3606                 // Check Core ---------------------------------------
3607                 if (!__kmp_hwloc_obj_has_PUs(tp, hC)) {
3608                   hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3609                   continue; // skip core if all PUs are out of fullMask
3610                 }
3611                 ++nC;
3612                 if (nC <= __kmp_hws_core.offset ||
3613                     nC > __kmp_hws_core.num + __kmp_hws_core.offset) {
3614                   // skip node as not requested
3615                   n_old += __kmp_hwloc_skip_PUs_obj(tp, hC); // skip core
3616                   hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3617                   continue; // move to next node
3618                 }
3619                 // core requested, go down to PUs
3620                 nT = 0;
3621                 nTr = 0;
3622                 hT = NULL;
3623                 // num procs in current core
3624                 int NT = __kmp_hwloc_count_children_by_type(tp, hC,
3625                                                             HWLOC_OBJ_PU, &hT);
3626                 for (int t = 0; t < NT; ++t) {
3627                   // Check PU ---------------------------------------
3628                   idx = hT->os_index;
3629                   if (!KMP_CPU_ISSET(idx, __kmp_affin_fullMask)) {
3630                     hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3631                     continue; // skip PU if not in fullMask
3632                   }
3633                   ++nT;
3634                   if (nT <= __kmp_hws_proc.offset ||
3635                       nT > __kmp_hws_proc.num + __kmp_hws_proc.offset) {
3636                     // skip PU
3637                     KMP_CPU_CLR(idx, __kmp_affin_fullMask);
3638                     ++n_old;
3639                     KC_TRACE(200, ("KMP_HW_SUBSET: skipped proc %d\n", idx));
3640                     hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3641                     continue; // move to next node
3642                   }
3643                   ++nTr;
3644                   if (pAddr) // collect requested thread's data
3645                     newAddr[n_new] = (*pAddr)[n_old];
3646                   ++n_new;
3647                   ++n_old;
3648                   hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3649                 } // threads loop
3650                 if (nTr > 0) {
3651                   ++nCr; // num cores per socket
3652                   ++nCo; // total num cores
3653                   if (nTr > nTpC)
3654                     nTpC = nTr; // calc max threads per core
3655                 }
3656                 hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3657               } // cores loop
3658               hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3659             } // tiles loop
3660           } else { // tile_support
3661             // no tiles, check cores
3662             nC = 0;
3663             hC = NULL;
3664             // num cores in current node
3665             int NC =
3666                 __kmp_hwloc_count_children_by_type(tp, hN, HWLOC_OBJ_CORE, &hC);
3667             for (int c = 0; c < NC; ++c) {
3668               // Check Core ---------------------------------------
3669               if (!__kmp_hwloc_obj_has_PUs(tp, hC)) {
3670                 hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3671                 continue; // skip core if all PUs are out of fullMask
3672               }
3673               ++nC;
3674               if (nC <= __kmp_hws_core.offset ||
3675                   nC > __kmp_hws_core.num + __kmp_hws_core.offset) {
3676                 // skip node as not requested
3677                 n_old += __kmp_hwloc_skip_PUs_obj(tp, hC); // skip core
3678                 hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3679                 continue; // move to next node
3680               }
3681               // core requested, go down to PUs
3682               nT = 0;
3683               nTr = 0;
3684               hT = NULL;
3685               int NT =
3686                   __kmp_hwloc_count_children_by_type(tp, hC, HWLOC_OBJ_PU, &hT);
3687               for (int t = 0; t < NT; ++t) {
3688                 // Check PU ---------------------------------------
3689                 idx = hT->os_index;
3690                 if (!KMP_CPU_ISSET(idx, __kmp_affin_fullMask)) {
3691                   hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3692                   continue; // skip PU if not in fullMask
3693                 }
3694                 ++nT;
3695                 if (nT <= __kmp_hws_proc.offset ||
3696                     nT > __kmp_hws_proc.num + __kmp_hws_proc.offset) {
3697                   // skip PU
3698                   KMP_CPU_CLR(idx, __kmp_affin_fullMask);
3699                   ++n_old;
3700                   KC_TRACE(200, ("KMP_HW_SUBSET: skipped proc %d\n", idx));
3701                   hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3702                   continue; // move to next node
3703                 }
3704                 ++nTr;
3705                 if (pAddr) // collect requested thread's data
3706                   newAddr[n_new] = (*pAddr)[n_old];
3707                 ++n_new;
3708                 ++n_old;
3709                 hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3710               } // threads loop
3711               if (nTr > 0) {
3712                 ++nCr; // num cores per socket
3713                 ++nCo; // total num cores
3714                 if (nTr > nTpC)
3715                   nTpC = nTr; // calc max threads per core
3716               }
3717               hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3718             } // cores loop
3719           } // tiles support
3720           hN = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_NUMANODE, hN);
3721         } // nodes loop
3722       } else { // numa_support
3723         // no NUMA support
3724         if (tile_support) {
3725           nL = 0;
3726           hL = NULL;
3727           // num tiles in current socket
3728           int NL = __kmp_hwloc_count_children_by_depth(tp, hS, L2depth, &hL);
3729           for (int l = 0; l < NL; ++l) {
3730             // Check L2 (tile) ------------------------------------
3731             if (!__kmp_hwloc_obj_has_PUs(tp, hL)) {
3732               hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3733               continue; // skip tile if all PUs are out of fullMask
3734             }
3735             ++nL;
3736             if (nL <= __kmp_hws_tile.offset ||
3737                 nL > __kmp_hws_tile.num + __kmp_hws_tile.offset) {
3738               // skip tile as not requested
3739               n_old += __kmp_hwloc_skip_PUs_obj(tp, hL); // skip tile
3740               hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3741               continue; // move to next tile
3742             }
3743             // tile requested, go down the topology tree
3744             nC = 0;
3745             hC = NULL;
3746             // num cores per tile
3747             int NC =
3748                 __kmp_hwloc_count_children_by_type(tp, hL, HWLOC_OBJ_CORE, &hC);
3749             for (int c = 0; c < NC; ++c) {
3750               // Check Core ---------------------------------------
3751               if (!__kmp_hwloc_obj_has_PUs(tp, hC)) {
3752                 hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3753                 continue; // skip core if all PUs are out of fullMask
3754               }
3755               ++nC;
3756               if (nC <= __kmp_hws_core.offset ||
3757                   nC > __kmp_hws_core.num + __kmp_hws_core.offset) {
3758                 // skip node as not requested
3759                 n_old += __kmp_hwloc_skip_PUs_obj(tp, hC); // skip core
3760                 hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3761                 continue; // move to next node
3762               }
3763               // core requested, go down to PUs
3764               nT = 0;
3765               nTr = 0;
3766               hT = NULL;
3767               // num procs per core
3768               int NT =
3769                   __kmp_hwloc_count_children_by_type(tp, hC, HWLOC_OBJ_PU, &hT);
3770               for (int t = 0; t < NT; ++t) {
3771                 // Check PU ---------------------------------------
3772                 idx = hT->os_index;
3773                 if (!KMP_CPU_ISSET(idx, __kmp_affin_fullMask)) {
3774                   hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3775                   continue; // skip PU if not in fullMask
3776                 }
3777                 ++nT;
3778                 if (nT <= __kmp_hws_proc.offset ||
3779                     nT > __kmp_hws_proc.num + __kmp_hws_proc.offset) {
3780                   // skip PU
3781                   KMP_CPU_CLR(idx, __kmp_affin_fullMask);
3782                   ++n_old;
3783                   KC_TRACE(200, ("KMP_HW_SUBSET: skipped proc %d\n", idx));
3784                   hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3785                   continue; // move to next node
3786                 }
3787                 ++nTr;
3788                 if (pAddr) // collect requested thread's data
3789                   newAddr[n_new] = (*pAddr)[n_old];
3790                 ++n_new;
3791                 ++n_old;
3792                 hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3793               } // threads loop
3794               if (nTr > 0) {
3795                 ++nCr; // num cores per socket
3796                 ++nCo; // total num cores
3797                 if (nTr > nTpC)
3798                   nTpC = nTr; // calc max threads per core
3799               }
3800               hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3801             } // cores loop
3802             hL = hwloc_get_next_obj_by_depth(tp, L2depth, hL);
3803           } // tiles loop
3804         } else { // tile_support
3805           // no tiles, check cores
3806           nC = 0;
3807           hC = NULL;
3808           // num cores in socket
3809           int NC =
3810               __kmp_hwloc_count_children_by_type(tp, hS, HWLOC_OBJ_CORE, &hC);
3811           for (int c = 0; c < NC; ++c) {
3812             // Check Core -------------------------------------------
3813             if (!__kmp_hwloc_obj_has_PUs(tp, hC)) {
3814               hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3815               continue; // skip core if all PUs are out of fullMask
3816             }
3817             ++nC;
3818             if (nC <= __kmp_hws_core.offset ||
3819                 nC > __kmp_hws_core.num + __kmp_hws_core.offset) {
3820               // skip node as not requested
3821               n_old += __kmp_hwloc_skip_PUs_obj(tp, hC); // skip core
3822               hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3823               continue; // move to next node
3824             }
3825             // core requested, go down to PUs
3826             nT = 0;
3827             nTr = 0;
3828             hT = NULL;
3829             // num procs per core
3830             int NT =
3831                 __kmp_hwloc_count_children_by_type(tp, hC, HWLOC_OBJ_PU, &hT);
3832             for (int t = 0; t < NT; ++t) {
3833               // Check PU ---------------------------------------
3834               idx = hT->os_index;
3835               if (!KMP_CPU_ISSET(idx, __kmp_affin_fullMask)) {
3836                 hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3837                 continue; // skip PU if not in fullMask
3838               }
3839               ++nT;
3840               if (nT <= __kmp_hws_proc.offset ||
3841                   nT > __kmp_hws_proc.num + __kmp_hws_proc.offset) {
3842                 // skip PU
3843                 KMP_CPU_CLR(idx, __kmp_affin_fullMask);
3844                 ++n_old;
3845                 KC_TRACE(200, ("KMP_HW_SUBSET: skipped proc %d\n", idx));
3846                 hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3847                 continue; // move to next node
3848               }
3849               ++nTr;
3850               if (pAddr) // collect requested thread's data
3851                 newAddr[n_new] = (*pAddr)[n_old];
3852               ++n_new;
3853               ++n_old;
3854               hT = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_PU, hT);
3855             } // threads loop
3856             if (nTr > 0) {
3857               ++nCr; // num cores per socket
3858               ++nCo; // total num cores
3859               if (nTr > nTpC)
3860                 nTpC = nTr; // calc max threads per core
3861             }
3862             hC = hwloc_get_next_obj_by_type(tp, HWLOC_OBJ_CORE, hC);
3863           } // cores loop
3864         } // tiles support
3865       } // numa_support
3866       if (nCr > 0) { // found cores?
3867         ++nPkg; // num sockets
3868         if (nCr > nCpP)
3869           nCpP = nCr; // calc max cores per socket
3870       }
3871     } // sockets loop
3872 
3873     // check the subset is valid
3874     KMP_DEBUG_ASSERT(n_old == __kmp_avail_proc);
3875     KMP_DEBUG_ASSERT(nPkg > 0);
3876     KMP_DEBUG_ASSERT(nCpP > 0);
3877     KMP_DEBUG_ASSERT(nTpC > 0);
3878     KMP_DEBUG_ASSERT(nCo > 0);
3879     KMP_DEBUG_ASSERT(nPkg <= nPackages);
3880     KMP_DEBUG_ASSERT(nCpP <= nCoresPerPkg);
3881     KMP_DEBUG_ASSERT(nTpC <= __kmp_nThreadsPerCore);
3882     KMP_DEBUG_ASSERT(nCo <= __kmp_ncores);
3883 
3884     nPackages = nPkg; // correct num sockets
3885     nCoresPerPkg = nCpP; // correct num cores per socket
3886     __kmp_nThreadsPerCore = nTpC; // correct num threads per core
3887     __kmp_avail_proc = n_new; // correct num procs
3888     __kmp_ncores = nCo; // correct num cores
3889     // hwloc topology method end
3890   } else
3891 #endif // KMP_USE_HWLOC
3892   {
3893     int n_old = 0, n_new = 0, proc_num = 0;
3894     if (__kmp_hws_node.num > 0 || __kmp_hws_tile.num > 0) {
3895       KMP_WARNING(AffHWSubsetNoHWLOC);
3896       goto _exit;
3897     }
3898     if (__kmp_hws_socket.num == 0)
3899       __kmp_hws_socket.num = nPackages; // use all available sockets
3900     if (__kmp_hws_core.num == 0)
3901       __kmp_hws_core.num = nCoresPerPkg; // use all available cores
3902     if (__kmp_hws_proc.num == 0 || __kmp_hws_proc.num > __kmp_nThreadsPerCore)
3903       __kmp_hws_proc.num = __kmp_nThreadsPerCore; // use all HW contexts
3904     if (!__kmp_affinity_uniform_topology()) {
3905       KMP_WARNING(AffHWSubsetNonUniform);
3906       goto _exit; // don't support non-uniform topology
3907     }
3908     if (depth > 3) {
3909       KMP_WARNING(AffHWSubsetNonThreeLevel);
3910       goto _exit; // don't support not-3-level topology
3911     }
3912     if (__kmp_hws_socket.offset + __kmp_hws_socket.num > nPackages) {
3913       KMP_WARNING(AffHWSubsetManySockets);
3914       goto _exit;
3915     }
3916     if (__kmp_hws_core.offset + __kmp_hws_core.num > nCoresPerPkg) {
3917       KMP_WARNING(AffHWSubsetManyCores);
3918       goto _exit;
3919     }
3920     // Form the requested subset
3921     if (pAddr) // pAddr is NULL in case of affinity_none
3922       newAddr = (AddrUnsPair *)__kmp_allocate(
3923           sizeof(AddrUnsPair) * __kmp_hws_socket.num * __kmp_hws_core.num *
3924           __kmp_hws_proc.num);
3925     for (int i = 0; i < nPackages; ++i) {
3926       if (i < __kmp_hws_socket.offset ||
3927           i >= __kmp_hws_socket.offset + __kmp_hws_socket.num) {
3928         // skip not-requested socket
3929         n_old += nCoresPerPkg * __kmp_nThreadsPerCore;
3930         if (__kmp_pu_os_idx != NULL) {
3931           // walk through skipped socket
3932           for (int j = 0; j < nCoresPerPkg; ++j) {
3933             for (int k = 0; k < __kmp_nThreadsPerCore; ++k) {
3934               KMP_CPU_CLR(__kmp_pu_os_idx[proc_num], __kmp_affin_fullMask);
3935               ++proc_num;
3936             }
3937           }
3938         }
3939       } else {
3940         // walk through requested socket
3941         for (int j = 0; j < nCoresPerPkg; ++j) {
3942           if (j < __kmp_hws_core.offset ||
3943               j >= __kmp_hws_core.offset +
3944                        __kmp_hws_core.num) { // skip not-requested core
3945             n_old += __kmp_nThreadsPerCore;
3946             if (__kmp_pu_os_idx != NULL) {
3947               for (int k = 0; k < __kmp_nThreadsPerCore; ++k) {
3948                 KMP_CPU_CLR(__kmp_pu_os_idx[proc_num], __kmp_affin_fullMask);
3949                 ++proc_num;
3950               }
3951             }
3952           } else {
3953             // walk through requested core
3954             for (int k = 0; k < __kmp_nThreadsPerCore; ++k) {
3955               if (k < __kmp_hws_proc.num) {
3956                 if (pAddr) // collect requested thread's data
3957                   newAddr[n_new] = (*pAddr)[n_old];
3958                 n_new++;
3959               } else {
3960                 if (__kmp_pu_os_idx != NULL)
3961                   KMP_CPU_CLR(__kmp_pu_os_idx[proc_num], __kmp_affin_fullMask);
3962               }
3963               n_old++;
3964               ++proc_num;
3965             }
3966           }
3967         }
3968       }
3969     }
3970     KMP_DEBUG_ASSERT(n_old == nPackages * nCoresPerPkg * __kmp_nThreadsPerCore);
3971     KMP_DEBUG_ASSERT(n_new ==
3972                      __kmp_hws_socket.num * __kmp_hws_core.num *
3973                          __kmp_hws_proc.num);
3974     nPackages = __kmp_hws_socket.num; // correct nPackages
3975     nCoresPerPkg = __kmp_hws_core.num; // correct nCoresPerPkg
3976     __kmp_nThreadsPerCore = __kmp_hws_proc.num; // correct __kmp_nThreadsPerCore
3977     __kmp_avail_proc = n_new; // correct avail_proc
3978     __kmp_ncores = nPackages * __kmp_hws_core.num; // correct ncores
3979   } // non-hwloc topology method
3980   if (pAddr) {
3981     __kmp_free(*pAddr);
3982     *pAddr = newAddr; // replace old topology with new one
3983   }
3984   if (__kmp_affinity_verbose) {
3985     char m[KMP_AFFIN_MASK_PRINT_LEN];
3986     __kmp_affinity_print_mask(m, KMP_AFFIN_MASK_PRINT_LEN,
3987                               __kmp_affin_fullMask);
3988     if (__kmp_affinity_respect_mask) {
3989       KMP_INFORM(InitOSProcSetRespect, "KMP_HW_SUBSET", m);
3990     } else {
3991       KMP_INFORM(InitOSProcSetNotRespect, "KMP_HW_SUBSET", m);
3992     }
3993     KMP_INFORM(AvailableOSProc, "KMP_HW_SUBSET", __kmp_avail_proc);
3994     kmp_str_buf_t buf;
3995     __kmp_str_buf_init(&buf);
3996     __kmp_str_buf_print(&buf, "%d", nPackages);
3997     KMP_INFORM(TopologyExtra, "KMP_HW_SUBSET", buf.str, nCoresPerPkg,
3998                __kmp_nThreadsPerCore, __kmp_ncores);
3999     __kmp_str_buf_free(&buf);
4000   }
4001 _exit:
4002   if (__kmp_pu_os_idx != NULL) {
4003     __kmp_free(__kmp_pu_os_idx);
4004     __kmp_pu_os_idx = NULL;
4005   }
4006 }
4007 
4008 // This function figures out the deepest level at which there is at least one
4009 // cluster/core with more than one processing unit bound to it.
4010 static int __kmp_affinity_find_core_level(const AddrUnsPair *address2os,
4011                                           int nprocs, int bottom_level) {
4012   int core_level = 0;
4013 
4014   for (int i = 0; i < nprocs; i++) {
4015     for (int j = bottom_level; j > 0; j--) {
4016       if (address2os[i].first.labels[j] > 0) {
4017         if (core_level < (j - 1)) {
4018           core_level = j - 1;
4019         }
4020       }
4021     }
4022   }
4023   return core_level;
4024 }
4025 
4026 // This function counts number of clusters/cores at given level.
4027 static int __kmp_affinity_compute_ncores(const AddrUnsPair *address2os,
4028                                          int nprocs, int bottom_level,
4029                                          int core_level) {
4030   int ncores = 0;
4031   int i, j;
4032 
4033   j = bottom_level;
4034   for (i = 0; i < nprocs; i++) {
4035     for (j = bottom_level; j > core_level; j--) {
4036       if ((i + 1) < nprocs) {
4037         if (address2os[i + 1].first.labels[j] > 0) {
4038           break;
4039         }
4040       }
4041     }
4042     if (j == core_level) {
4043       ncores++;
4044     }
4045   }
4046   if (j > core_level) {
4047     // In case of ( nprocs < __kmp_avail_proc ) we may end too deep and miss one
4048     // core. May occur when called from __kmp_affinity_find_core().
4049     ncores++;
4050   }
4051   return ncores;
4052 }
4053 
4054 // This function finds to which cluster/core given processing unit is bound.
4055 static int __kmp_affinity_find_core(const AddrUnsPair *address2os, int proc,
4056                                     int bottom_level, int core_level) {
4057   return __kmp_affinity_compute_ncores(address2os, proc + 1, bottom_level,
4058                                        core_level) -
4059          1;
4060 }
4061 
4062 // This function finds maximal number of processing units bound to a
4063 // cluster/core at given level.
4064 static int __kmp_affinity_max_proc_per_core(const AddrUnsPair *address2os,
4065                                             int nprocs, int bottom_level,
4066                                             int core_level) {
4067   int maxprocpercore = 0;
4068 
4069   if (core_level < bottom_level) {
4070     for (int i = 0; i < nprocs; i++) {
4071       int percore = address2os[i].first.labels[core_level + 1] + 1;
4072 
4073       if (percore > maxprocpercore) {
4074         maxprocpercore = percore;
4075       }
4076     }
4077   } else {
4078     maxprocpercore = 1;
4079   }
4080   return maxprocpercore;
4081 }
4082 
4083 static AddrUnsPair *address2os = NULL;
4084 static int *procarr = NULL;
4085 static int __kmp_aff_depth = 0;
4086 
4087 #if KMP_USE_HIER_SCHED
4088 #define KMP_EXIT_AFF_NONE                                                      \
4089   KMP_ASSERT(__kmp_affinity_type == affinity_none);                            \
4090   KMP_ASSERT(address2os == NULL);                                              \
4091   __kmp_apply_thread_places(NULL, 0);                                          \
4092   __kmp_create_affinity_none_places();                                         \
4093   __kmp_dispatch_set_hierarchy_values();                                       \
4094   return;
4095 #else
4096 #define KMP_EXIT_AFF_NONE                                                      \
4097   KMP_ASSERT(__kmp_affinity_type == affinity_none);                            \
4098   KMP_ASSERT(address2os == NULL);                                              \
4099   __kmp_apply_thread_places(NULL, 0);                                          \
4100   __kmp_create_affinity_none_places();                                         \
4101   return;
4102 #endif
4103 
4104 // Create a one element mask array (set of places) which only contains the
4105 // initial process's affinity mask
4106 static void __kmp_create_affinity_none_places() {
4107   KMP_ASSERT(__kmp_affin_fullMask != NULL);
4108   KMP_ASSERT(__kmp_affinity_type == affinity_none);
4109   __kmp_affinity_num_masks = 1;
4110   KMP_CPU_ALLOC_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
4111   kmp_affin_mask_t *dest = KMP_CPU_INDEX(__kmp_affinity_masks, 0);
4112   KMP_CPU_COPY(dest, __kmp_affin_fullMask);
4113 }
4114 
4115 static int __kmp_affinity_cmp_Address_child_num(const void *a, const void *b) {
4116   const Address *aa = &(((const AddrUnsPair *)a)->first);
4117   const Address *bb = &(((const AddrUnsPair *)b)->first);
4118   unsigned depth = aa->depth;
4119   unsigned i;
4120   KMP_DEBUG_ASSERT(depth == bb->depth);
4121   KMP_DEBUG_ASSERT((unsigned)__kmp_affinity_compact <= depth);
4122   KMP_DEBUG_ASSERT(__kmp_affinity_compact >= 0);
4123   for (i = 0; i < (unsigned)__kmp_affinity_compact; i++) {
4124     int j = depth - i - 1;
4125     if (aa->childNums[j] < bb->childNums[j])
4126       return -1;
4127     if (aa->childNums[j] > bb->childNums[j])
4128       return 1;
4129   }
4130   for (; i < depth; i++) {
4131     int j = i - __kmp_affinity_compact;
4132     if (aa->childNums[j] < bb->childNums[j])
4133       return -1;
4134     if (aa->childNums[j] > bb->childNums[j])
4135       return 1;
4136   }
4137   return 0;
4138 }
4139 
4140 static void __kmp_aux_affinity_initialize(void) {
4141   if (__kmp_affinity_masks != NULL) {
4142     KMP_ASSERT(__kmp_affin_fullMask != NULL);
4143     return;
4144   }
4145 
4146   // Create the "full" mask - this defines all of the processors that we
4147   // consider to be in the machine model. If respect is set, then it is the
4148   // initialization thread's affinity mask. Otherwise, it is all processors that
4149   // we know about on the machine.
4150   if (__kmp_affin_fullMask == NULL) {
4151     KMP_CPU_ALLOC(__kmp_affin_fullMask);
4152   }
4153   if (KMP_AFFINITY_CAPABLE()) {
4154     if (__kmp_affinity_respect_mask) {
4155       __kmp_get_system_affinity(__kmp_affin_fullMask, TRUE);
4156 
4157       // Count the number of available processors.
4158       unsigned i;
4159       __kmp_avail_proc = 0;
4160       KMP_CPU_SET_ITERATE(i, __kmp_affin_fullMask) {
4161         if (!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) {
4162           continue;
4163         }
4164         __kmp_avail_proc++;
4165       }
4166       if (__kmp_avail_proc > __kmp_xproc) {
4167         if (__kmp_affinity_verbose ||
4168             (__kmp_affinity_warnings &&
4169              (__kmp_affinity_type != affinity_none))) {
4170           KMP_WARNING(ErrorInitializeAffinity);
4171         }
4172         __kmp_affinity_type = affinity_none;
4173         KMP_AFFINITY_DISABLE();
4174         return;
4175       }
4176     } else {
4177       __kmp_affinity_entire_machine_mask(__kmp_affin_fullMask);
4178       __kmp_avail_proc = __kmp_xproc;
4179     }
4180   }
4181 
4182   if (__kmp_affinity_gran == affinity_gran_tile &&
4183       // check if user's request is valid
4184       __kmp_affinity_dispatch->get_api_type() == KMPAffinity::NATIVE_OS) {
4185     KMP_WARNING(AffTilesNoHWLOC, "KMP_AFFINITY");
4186     __kmp_affinity_gran = affinity_gran_package;
4187   }
4188 
4189   int depth = -1;
4190   kmp_i18n_id_t msg_id = kmp_i18n_null;
4191 
4192   // For backward compatibility, setting KMP_CPUINFO_FILE =>
4193   // KMP_TOPOLOGY_METHOD=cpuinfo
4194   if ((__kmp_cpuinfo_file != NULL) &&
4195       (__kmp_affinity_top_method == affinity_top_method_all)) {
4196     __kmp_affinity_top_method = affinity_top_method_cpuinfo;
4197   }
4198 
4199   if (__kmp_affinity_top_method == affinity_top_method_all) {
4200     // In the default code path, errors are not fatal - we just try using
4201     // another method. We only emit a warning message if affinity is on, or the
4202     // verbose flag is set, an the nowarnings flag was not set.
4203     const char *file_name = NULL;
4204     int line = 0;
4205 #if KMP_USE_HWLOC
4206     if (depth < 0 &&
4207         __kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC) {
4208       if (__kmp_affinity_verbose) {
4209         KMP_INFORM(AffUsingHwloc, "KMP_AFFINITY");
4210       }
4211       if (!__kmp_hwloc_error) {
4212         depth = __kmp_affinity_create_hwloc_map(&address2os, &msg_id);
4213         if (depth == 0) {
4214           KMP_EXIT_AFF_NONE;
4215         } else if (depth < 0 && __kmp_affinity_verbose) {
4216           KMP_INFORM(AffIgnoringHwloc, "KMP_AFFINITY");
4217         }
4218       } else if (__kmp_affinity_verbose) {
4219         KMP_INFORM(AffIgnoringHwloc, "KMP_AFFINITY");
4220       }
4221     }
4222 #endif
4223 
4224 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
4225 
4226     if (depth < 0) {
4227       if (__kmp_affinity_verbose) {
4228         KMP_INFORM(AffInfoStr, "KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
4229       }
4230 
4231       file_name = NULL;
4232       depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
4233       if (depth == 0) {
4234         KMP_EXIT_AFF_NONE;
4235       }
4236 
4237       if (depth < 0) {
4238         if (__kmp_affinity_verbose) {
4239           if (msg_id != kmp_i18n_null) {
4240             KMP_INFORM(AffInfoStrStr, "KMP_AFFINITY",
4241                        __kmp_i18n_catgets(msg_id),
4242                        KMP_I18N_STR(DecodingLegacyAPIC));
4243           } else {
4244             KMP_INFORM(AffInfoStr, "KMP_AFFINITY",
4245                        KMP_I18N_STR(DecodingLegacyAPIC));
4246           }
4247         }
4248 
4249         file_name = NULL;
4250         depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
4251         if (depth == 0) {
4252           KMP_EXIT_AFF_NONE;
4253         }
4254       }
4255     }
4256 
4257 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
4258 
4259 #if KMP_OS_LINUX
4260 
4261     if (depth < 0) {
4262       if (__kmp_affinity_verbose) {
4263         if (msg_id != kmp_i18n_null) {
4264           KMP_INFORM(AffStrParseFilename, "KMP_AFFINITY",
4265                      __kmp_i18n_catgets(msg_id), "/proc/cpuinfo");
4266         } else {
4267           KMP_INFORM(AffParseFilename, "KMP_AFFINITY", "/proc/cpuinfo");
4268         }
4269       }
4270 
4271       FILE *f = fopen("/proc/cpuinfo", "r");
4272       if (f == NULL) {
4273         msg_id = kmp_i18n_str_CantOpenCpuinfo;
4274       } else {
4275         file_name = "/proc/cpuinfo";
4276         depth =
4277             __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
4278         fclose(f);
4279         if (depth == 0) {
4280           KMP_EXIT_AFF_NONE;
4281         }
4282       }
4283     }
4284 
4285 #endif /* KMP_OS_LINUX */
4286 
4287 #if KMP_GROUP_AFFINITY
4288 
4289     if ((depth < 0) && (__kmp_num_proc_groups > 1)) {
4290       if (__kmp_affinity_verbose) {
4291         KMP_INFORM(AffWindowsProcGroupMap, "KMP_AFFINITY");
4292       }
4293 
4294       depth = __kmp_affinity_create_proc_group_map(&address2os, &msg_id);
4295       KMP_ASSERT(depth != 0);
4296     }
4297 
4298 #endif /* KMP_GROUP_AFFINITY */
4299 
4300     if (depth < 0) {
4301       if (__kmp_affinity_verbose && (msg_id != kmp_i18n_null)) {
4302         if (file_name == NULL) {
4303           KMP_INFORM(UsingFlatOS, __kmp_i18n_catgets(msg_id));
4304         } else if (line == 0) {
4305           KMP_INFORM(UsingFlatOSFile, file_name, __kmp_i18n_catgets(msg_id));
4306         } else {
4307           KMP_INFORM(UsingFlatOSFileLine, file_name, line,
4308                      __kmp_i18n_catgets(msg_id));
4309         }
4310       }
4311       // FIXME - print msg if msg_id = kmp_i18n_null ???
4312 
4313       file_name = "";
4314       depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
4315       if (depth == 0) {
4316         KMP_EXIT_AFF_NONE;
4317       }
4318       KMP_ASSERT(depth > 0);
4319       KMP_ASSERT(address2os != NULL);
4320     }
4321   }
4322 
4323 #if KMP_USE_HWLOC
4324   else if (__kmp_affinity_top_method == affinity_top_method_hwloc) {
4325     KMP_ASSERT(__kmp_affinity_dispatch->get_api_type() == KMPAffinity::HWLOC);
4326     if (__kmp_affinity_verbose) {
4327       KMP_INFORM(AffUsingHwloc, "KMP_AFFINITY");
4328     }
4329     depth = __kmp_affinity_create_hwloc_map(&address2os, &msg_id);
4330     if (depth == 0) {
4331       KMP_EXIT_AFF_NONE;
4332     }
4333   }
4334 #endif // KMP_USE_HWLOC
4335 
4336 // If the user has specified that a paricular topology discovery method is to be
4337 // used, then we abort if that method fails. The exception is group affinity,
4338 // which might have been implicitly set.
4339 
4340 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
4341 
4342   else if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
4343     if (__kmp_affinity_verbose) {
4344       KMP_INFORM(AffInfoStr, "KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
4345     }
4346 
4347     depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
4348     if (depth == 0) {
4349       KMP_EXIT_AFF_NONE;
4350     }
4351     if (depth < 0) {
4352       KMP_ASSERT(msg_id != kmp_i18n_null);
4353       KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4354     }
4355   } else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
4356     if (__kmp_affinity_verbose) {
4357       KMP_INFORM(AffInfoStr, "KMP_AFFINITY", KMP_I18N_STR(DecodingLegacyAPIC));
4358     }
4359 
4360     depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
4361     if (depth == 0) {
4362       KMP_EXIT_AFF_NONE;
4363     }
4364     if (depth < 0) {
4365       KMP_ASSERT(msg_id != kmp_i18n_null);
4366       KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4367     }
4368   }
4369 
4370 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
4371 
4372   else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
4373     const char *filename;
4374     if (__kmp_cpuinfo_file != NULL) {
4375       filename = __kmp_cpuinfo_file;
4376     } else {
4377       filename = "/proc/cpuinfo";
4378     }
4379 
4380     if (__kmp_affinity_verbose) {
4381       KMP_INFORM(AffParseFilename, "KMP_AFFINITY", filename);
4382     }
4383 
4384     FILE *f = fopen(filename, "r");
4385     if (f == NULL) {
4386       int code = errno;
4387       if (__kmp_cpuinfo_file != NULL) {
4388         __kmp_fatal(KMP_MSG(CantOpenFileForReading, filename), KMP_ERR(code),
4389                     KMP_HNT(NameComesFrom_CPUINFO_FILE), __kmp_msg_null);
4390       } else {
4391         __kmp_fatal(KMP_MSG(CantOpenFileForReading, filename), KMP_ERR(code),
4392                     __kmp_msg_null);
4393       }
4394     }
4395     int line = 0;
4396     depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
4397     fclose(f);
4398     if (depth < 0) {
4399       KMP_ASSERT(msg_id != kmp_i18n_null);
4400       if (line > 0) {
4401         KMP_FATAL(FileLineMsgExiting, filename, line,
4402                   __kmp_i18n_catgets(msg_id));
4403       } else {
4404         KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
4405       }
4406     }
4407     if (__kmp_affinity_type == affinity_none) {
4408       KMP_ASSERT(depth == 0);
4409       KMP_EXIT_AFF_NONE;
4410     }
4411   }
4412 
4413 #if KMP_GROUP_AFFINITY
4414 
4415   else if (__kmp_affinity_top_method == affinity_top_method_group) {
4416     if (__kmp_affinity_verbose) {
4417       KMP_INFORM(AffWindowsProcGroupMap, "KMP_AFFINITY");
4418     }
4419 
4420     depth = __kmp_affinity_create_proc_group_map(&address2os, &msg_id);
4421     KMP_ASSERT(depth != 0);
4422     if (depth < 0) {
4423       KMP_ASSERT(msg_id != kmp_i18n_null);
4424       KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
4425     }
4426   }
4427 
4428 #endif /* KMP_GROUP_AFFINITY */
4429 
4430   else if (__kmp_affinity_top_method == affinity_top_method_flat) {
4431     if (__kmp_affinity_verbose) {
4432       KMP_INFORM(AffUsingFlatOS, "KMP_AFFINITY");
4433     }
4434 
4435     depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
4436     if (depth == 0) {
4437       KMP_EXIT_AFF_NONE;
4438     }
4439     // should not fail
4440     KMP_ASSERT(depth > 0);
4441     KMP_ASSERT(address2os != NULL);
4442   }
4443 
4444 #if KMP_USE_HIER_SCHED
4445   __kmp_dispatch_set_hierarchy_values();
4446 #endif
4447 
4448   if (address2os == NULL) {
4449     if (KMP_AFFINITY_CAPABLE() &&
4450         (__kmp_affinity_verbose ||
4451          (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none)))) {
4452       KMP_WARNING(ErrorInitializeAffinity);
4453     }
4454     __kmp_affinity_type = affinity_none;
4455     __kmp_create_affinity_none_places();
4456     KMP_AFFINITY_DISABLE();
4457     return;
4458   }
4459 
4460   if (__kmp_affinity_gran == affinity_gran_tile
4461 #if KMP_USE_HWLOC
4462       && __kmp_tile_depth == 0
4463 #endif
4464       ) {
4465     // tiles requested but not detected, warn user on this
4466     KMP_WARNING(AffTilesNoTiles, "KMP_AFFINITY");
4467   }
4468 
4469   __kmp_apply_thread_places(&address2os, depth);
4470 
4471   // Create the table of masks, indexed by thread Id.
4472   unsigned maxIndex;
4473   unsigned numUnique;
4474   kmp_affin_mask_t *osId2Mask =
4475       __kmp_create_masks(&maxIndex, &numUnique, address2os, __kmp_avail_proc);
4476   if (__kmp_affinity_gran_levels == 0) {
4477     KMP_DEBUG_ASSERT((int)numUnique == __kmp_avail_proc);
4478   }
4479 
4480   // Set the childNums vector in all Address objects. This must be done before
4481   // we can sort using __kmp_affinity_cmp_Address_child_num(), which takes into
4482   // account the setting of __kmp_affinity_compact.
4483   __kmp_affinity_assign_child_nums(address2os, __kmp_avail_proc);
4484 
4485   switch (__kmp_affinity_type) {
4486 
4487   case affinity_explicit:
4488     KMP_DEBUG_ASSERT(__kmp_affinity_proclist != NULL);
4489 #if OMP_40_ENABLED
4490     if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
4491 #endif
4492     {
4493       __kmp_affinity_process_proclist(
4494           &__kmp_affinity_masks, &__kmp_affinity_num_masks,
4495           __kmp_affinity_proclist, osId2Mask, maxIndex);
4496     }
4497 #if OMP_40_ENABLED
4498     else {
4499       __kmp_affinity_process_placelist(
4500           &__kmp_affinity_masks, &__kmp_affinity_num_masks,
4501           __kmp_affinity_proclist, osId2Mask, maxIndex);
4502     }
4503 #endif
4504     if (__kmp_affinity_num_masks == 0) {
4505       if (__kmp_affinity_verbose ||
4506           (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none))) {
4507         KMP_WARNING(AffNoValidProcID);
4508       }
4509       __kmp_affinity_type = affinity_none;
4510       __kmp_create_affinity_none_places();
4511       return;
4512     }
4513     break;
4514 
4515   // The other affinity types rely on sorting the Addresses according to some
4516   // permutation of the machine topology tree. Set __kmp_affinity_compact and
4517   // __kmp_affinity_offset appropriately, then jump to a common code fragment
4518   // to do the sort and create the array of affinity masks.
4519 
4520   case affinity_logical:
4521     __kmp_affinity_compact = 0;
4522     if (__kmp_affinity_offset) {
4523       __kmp_affinity_offset =
4524           __kmp_nThreadsPerCore * __kmp_affinity_offset % __kmp_avail_proc;
4525     }
4526     goto sortAddresses;
4527 
4528   case affinity_physical:
4529     if (__kmp_nThreadsPerCore > 1) {
4530       __kmp_affinity_compact = 1;
4531       if (__kmp_affinity_compact >= depth) {
4532         __kmp_affinity_compact = 0;
4533       }
4534     } else {
4535       __kmp_affinity_compact = 0;
4536     }
4537     if (__kmp_affinity_offset) {
4538       __kmp_affinity_offset =
4539           __kmp_nThreadsPerCore * __kmp_affinity_offset % __kmp_avail_proc;
4540     }
4541     goto sortAddresses;
4542 
4543   case affinity_scatter:
4544     if (__kmp_affinity_compact >= depth) {
4545       __kmp_affinity_compact = 0;
4546     } else {
4547       __kmp_affinity_compact = depth - 1 - __kmp_affinity_compact;
4548     }
4549     goto sortAddresses;
4550 
4551   case affinity_compact:
4552     if (__kmp_affinity_compact >= depth) {
4553       __kmp_affinity_compact = depth - 1;
4554     }
4555     goto sortAddresses;
4556 
4557   case affinity_balanced:
4558     if (depth <= 1) {
4559       if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
4560         KMP_WARNING(AffBalancedNotAvail, "KMP_AFFINITY");
4561       }
4562       __kmp_affinity_type = affinity_none;
4563       __kmp_create_affinity_none_places();
4564       return;
4565     } else if (!__kmp_affinity_uniform_topology()) {
4566       // Save the depth for further usage
4567       __kmp_aff_depth = depth;
4568 
4569       int core_level = __kmp_affinity_find_core_level(
4570           address2os, __kmp_avail_proc, depth - 1);
4571       int ncores = __kmp_affinity_compute_ncores(address2os, __kmp_avail_proc,
4572                                                  depth - 1, core_level);
4573       int maxprocpercore = __kmp_affinity_max_proc_per_core(
4574           address2os, __kmp_avail_proc, depth - 1, core_level);
4575 
4576       int nproc = ncores * maxprocpercore;
4577       if ((nproc < 2) || (nproc < __kmp_avail_proc)) {
4578         if (__kmp_affinity_verbose || __kmp_affinity_warnings) {
4579           KMP_WARNING(AffBalancedNotAvail, "KMP_AFFINITY");
4580         }
4581         __kmp_affinity_type = affinity_none;
4582         return;
4583       }
4584 
4585       procarr = (int *)__kmp_allocate(sizeof(int) * nproc);
4586       for (int i = 0; i < nproc; i++) {
4587         procarr[i] = -1;
4588       }
4589 
4590       int lastcore = -1;
4591       int inlastcore = 0;
4592       for (int i = 0; i < __kmp_avail_proc; i++) {
4593         int proc = address2os[i].second;
4594         int core =
4595             __kmp_affinity_find_core(address2os, i, depth - 1, core_level);
4596 
4597         if (core == lastcore) {
4598           inlastcore++;
4599         } else {
4600           inlastcore = 0;
4601         }
4602         lastcore = core;
4603 
4604         procarr[core * maxprocpercore + inlastcore] = proc;
4605       }
4606     }
4607     if (__kmp_affinity_compact >= depth) {
4608       __kmp_affinity_compact = depth - 1;
4609     }
4610 
4611   sortAddresses:
4612     // Allocate the gtid->affinity mask table.
4613     if (__kmp_affinity_dups) {
4614       __kmp_affinity_num_masks = __kmp_avail_proc;
4615     } else {
4616       __kmp_affinity_num_masks = numUnique;
4617     }
4618 
4619 #if OMP_40_ENABLED
4620     if ((__kmp_nested_proc_bind.bind_types[0] != proc_bind_intel) &&
4621         (__kmp_affinity_num_places > 0) &&
4622         ((unsigned)__kmp_affinity_num_places < __kmp_affinity_num_masks)) {
4623       __kmp_affinity_num_masks = __kmp_affinity_num_places;
4624     }
4625 #endif
4626 
4627     KMP_CPU_ALLOC_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
4628 
4629     // Sort the address2os table according to the current setting of
4630     // __kmp_affinity_compact, then fill out __kmp_affinity_masks.
4631     qsort(address2os, __kmp_avail_proc, sizeof(*address2os),
4632           __kmp_affinity_cmp_Address_child_num);
4633     {
4634       int i;
4635       unsigned j;
4636       for (i = 0, j = 0; i < __kmp_avail_proc; i++) {
4637         if ((!__kmp_affinity_dups) && (!address2os[i].first.leader)) {
4638           continue;
4639         }
4640         unsigned osId = address2os[i].second;
4641         kmp_affin_mask_t *src = KMP_CPU_INDEX(osId2Mask, osId);
4642         kmp_affin_mask_t *dest = KMP_CPU_INDEX(__kmp_affinity_masks, j);
4643         KMP_ASSERT(KMP_CPU_ISSET(osId, src));
4644         KMP_CPU_COPY(dest, src);
4645         if (++j >= __kmp_affinity_num_masks) {
4646           break;
4647         }
4648       }
4649       KMP_DEBUG_ASSERT(j == __kmp_affinity_num_masks);
4650     }
4651     break;
4652 
4653   default:
4654     KMP_ASSERT2(0, "Unexpected affinity setting");
4655   }
4656 
4657   KMP_CPU_FREE_ARRAY(osId2Mask, maxIndex + 1);
4658   machine_hierarchy.init(address2os, __kmp_avail_proc);
4659 }
4660 #undef KMP_EXIT_AFF_NONE
4661 
4662 void __kmp_affinity_initialize(void) {
4663   // Much of the code above was written assumming that if a machine was not
4664   // affinity capable, then __kmp_affinity_type == affinity_none.  We now
4665   // explicitly represent this as __kmp_affinity_type == affinity_disabled.
4666   // There are too many checks for __kmp_affinity_type == affinity_none
4667   // in this code.  Instead of trying to change them all, check if
4668   // __kmp_affinity_type == affinity_disabled, and if so, slam it with
4669   // affinity_none, call the real initialization routine, then restore
4670   // __kmp_affinity_type to affinity_disabled.
4671   int disabled = (__kmp_affinity_type == affinity_disabled);
4672   if (!KMP_AFFINITY_CAPABLE()) {
4673     KMP_ASSERT(disabled);
4674   }
4675   if (disabled) {
4676     __kmp_affinity_type = affinity_none;
4677   }
4678   __kmp_aux_affinity_initialize();
4679   if (disabled) {
4680     __kmp_affinity_type = affinity_disabled;
4681   }
4682 }
4683 
4684 void __kmp_affinity_uninitialize(void) {
4685   if (__kmp_affinity_masks != NULL) {
4686     KMP_CPU_FREE_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
4687     __kmp_affinity_masks = NULL;
4688   }
4689   if (__kmp_affin_fullMask != NULL) {
4690     KMP_CPU_FREE(__kmp_affin_fullMask);
4691     __kmp_affin_fullMask = NULL;
4692   }
4693   __kmp_affinity_num_masks = 0;
4694   __kmp_affinity_type = affinity_default;
4695 #if OMP_40_ENABLED
4696   __kmp_affinity_num_places = 0;
4697 #endif
4698   if (__kmp_affinity_proclist != NULL) {
4699     __kmp_free(__kmp_affinity_proclist);
4700     __kmp_affinity_proclist = NULL;
4701   }
4702   if (address2os != NULL) {
4703     __kmp_free(address2os);
4704     address2os = NULL;
4705   }
4706   if (procarr != NULL) {
4707     __kmp_free(procarr);
4708     procarr = NULL;
4709   }
4710 #if KMP_USE_HWLOC
4711   if (__kmp_hwloc_topology != NULL) {
4712     hwloc_topology_destroy(__kmp_hwloc_topology);
4713     __kmp_hwloc_topology = NULL;
4714   }
4715 #endif
4716   KMPAffinity::destroy_api();
4717 }
4718 
4719 void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
4720   if (!KMP_AFFINITY_CAPABLE()) {
4721     return;
4722   }
4723 
4724   kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4725   if (th->th.th_affin_mask == NULL) {
4726     KMP_CPU_ALLOC(th->th.th_affin_mask);
4727   } else {
4728     KMP_CPU_ZERO(th->th.th_affin_mask);
4729   }
4730 
4731   // Copy the thread mask to the kmp_info_t strucuture. If
4732   // __kmp_affinity_type == affinity_none, copy the "full" mask, i.e. one that
4733   // has all of the OS proc ids set, or if __kmp_affinity_respect_mask is set,
4734   // then the full mask is the same as the mask of the initialization thread.
4735   kmp_affin_mask_t *mask;
4736   int i;
4737 
4738 #if OMP_40_ENABLED
4739   if (KMP_AFFINITY_NON_PROC_BIND)
4740 #endif
4741   {
4742     if ((__kmp_affinity_type == affinity_none) ||
4743         (__kmp_affinity_type == affinity_balanced)) {
4744 #if KMP_GROUP_AFFINITY
4745       if (__kmp_num_proc_groups > 1) {
4746         return;
4747       }
4748 #endif
4749       KMP_ASSERT(__kmp_affin_fullMask != NULL);
4750       i = 0;
4751       mask = __kmp_affin_fullMask;
4752     } else {
4753       KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0);
4754       i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
4755       mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
4756     }
4757   }
4758 #if OMP_40_ENABLED
4759   else {
4760     if ((!isa_root) ||
4761         (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
4762 #if KMP_GROUP_AFFINITY
4763       if (__kmp_num_proc_groups > 1) {
4764         return;
4765       }
4766 #endif
4767       KMP_ASSERT(__kmp_affin_fullMask != NULL);
4768       i = KMP_PLACE_ALL;
4769       mask = __kmp_affin_fullMask;
4770     } else {
4771       // int i = some hash function or just a counter that doesn't
4772       // always start at 0.  Use gtid for now.
4773       KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0);
4774       i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
4775       mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
4776     }
4777   }
4778 #endif
4779 
4780 #if OMP_40_ENABLED
4781   th->th.th_current_place = i;
4782   if (isa_root) {
4783     th->th.th_new_place = i;
4784     th->th.th_first_place = 0;
4785     th->th.th_last_place = __kmp_affinity_num_masks - 1;
4786   } else if (KMP_AFFINITY_NON_PROC_BIND) {
4787     // When using a Non-OMP_PROC_BIND affinity method,
4788     // set all threads' place-partition-var to the entire place list
4789     th->th.th_first_place = 0;
4790     th->th.th_last_place = __kmp_affinity_num_masks - 1;
4791   }
4792 
4793   if (i == KMP_PLACE_ALL) {
4794     KA_TRACE(100, ("__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4795                    gtid));
4796   } else {
4797     KA_TRACE(100, ("__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4798                    gtid, i));
4799   }
4800 #else
4801   if (i == -1) {
4802     KA_TRACE(
4803         100,
4804         ("__kmp_affinity_set_init_mask: binding T#%d to __kmp_affin_fullMask\n",
4805          gtid));
4806   } else {
4807     KA_TRACE(100, ("__kmp_affinity_set_init_mask: binding T#%d to mask %d\n",
4808                    gtid, i));
4809   }
4810 #endif /* OMP_40_ENABLED */
4811 
4812   KMP_CPU_COPY(th->th.th_affin_mask, mask);
4813 
4814   if (__kmp_affinity_verbose
4815       /* to avoid duplicate printing (will be correctly printed on barrier) */
4816       && (__kmp_affinity_type == affinity_none ||
4817           (i != KMP_PLACE_ALL && __kmp_affinity_type != affinity_balanced))) {
4818     char buf[KMP_AFFIN_MASK_PRINT_LEN];
4819     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4820                               th->th.th_affin_mask);
4821     KMP_INFORM(BoundToOSProcSet, "KMP_AFFINITY", (kmp_int32)getpid(),
4822                __kmp_gettid(), gtid, buf);
4823   }
4824 
4825 #if KMP_OS_WINDOWS
4826   // On Windows* OS, the process affinity mask might have changed. If the user
4827   // didn't request affinity and this call fails, just continue silently.
4828   // See CQ171393.
4829   if (__kmp_affinity_type == affinity_none) {
4830     __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4831   } else
4832 #endif
4833     __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4834 }
4835 
4836 #if OMP_40_ENABLED
4837 
4838 void __kmp_affinity_set_place(int gtid) {
4839   if (!KMP_AFFINITY_CAPABLE()) {
4840     return;
4841   }
4842 
4843   kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4844 
4845   KA_TRACE(100, ("__kmp_affinity_set_place: binding T#%d to place %d (current "
4846                  "place = %d)\n",
4847                  gtid, th->th.th_new_place, th->th.th_current_place));
4848 
4849   // Check that the new place is within this thread's partition.
4850   KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4851   KMP_ASSERT(th->th.th_new_place >= 0);
4852   KMP_ASSERT((unsigned)th->th.th_new_place <= __kmp_affinity_num_masks);
4853   if (th->th.th_first_place <= th->th.th_last_place) {
4854     KMP_ASSERT((th->th.th_new_place >= th->th.th_first_place) &&
4855                (th->th.th_new_place <= th->th.th_last_place));
4856   } else {
4857     KMP_ASSERT((th->th.th_new_place <= th->th.th_first_place) ||
4858                (th->th.th_new_place >= th->th.th_last_place));
4859   }
4860 
4861   // Copy the thread mask to the kmp_info_t strucuture,
4862   // and set this thread's affinity.
4863   kmp_affin_mask_t *mask =
4864       KMP_CPU_INDEX(__kmp_affinity_masks, th->th.th_new_place);
4865   KMP_CPU_COPY(th->th.th_affin_mask, mask);
4866   th->th.th_current_place = th->th.th_new_place;
4867 
4868   if (__kmp_affinity_verbose) {
4869     char buf[KMP_AFFIN_MASK_PRINT_LEN];
4870     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4871                               th->th.th_affin_mask);
4872     KMP_INFORM(BoundToOSProcSet, "OMP_PROC_BIND", (kmp_int32)getpid(),
4873                __kmp_gettid(), gtid, buf);
4874   }
4875   __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4876 }
4877 
4878 #endif /* OMP_40_ENABLED */
4879 
4880 int __kmp_aux_set_affinity(void **mask) {
4881   int gtid;
4882   kmp_info_t *th;
4883   int retval;
4884 
4885   if (!KMP_AFFINITY_CAPABLE()) {
4886     return -1;
4887   }
4888 
4889   gtid = __kmp_entry_gtid();
4890   KA_TRACE(1000, (""); {
4891     char buf[KMP_AFFIN_MASK_PRINT_LEN];
4892     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4893                               (kmp_affin_mask_t *)(*mask));
4894     __kmp_debug_printf(
4895         "kmp_set_affinity: setting affinity mask for thread %d = %s\n", gtid,
4896         buf);
4897   });
4898 
4899   if (__kmp_env_consistency_check) {
4900     if ((mask == NULL) || (*mask == NULL)) {
4901       KMP_FATAL(AffinityInvalidMask, "kmp_set_affinity");
4902     } else {
4903       unsigned proc;
4904       int num_procs = 0;
4905 
4906       KMP_CPU_SET_ITERATE(proc, ((kmp_affin_mask_t *)(*mask))) {
4907         if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
4908           KMP_FATAL(AffinityInvalidMask, "kmp_set_affinity");
4909         }
4910         if (!KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4911           continue;
4912         }
4913         num_procs++;
4914       }
4915       if (num_procs == 0) {
4916         KMP_FATAL(AffinityInvalidMask, "kmp_set_affinity");
4917       }
4918 
4919 #if KMP_GROUP_AFFINITY
4920       if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4921         KMP_FATAL(AffinityInvalidMask, "kmp_set_affinity");
4922       }
4923 #endif /* KMP_GROUP_AFFINITY */
4924     }
4925   }
4926 
4927   th = __kmp_threads[gtid];
4928   KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4929   retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4930   if (retval == 0) {
4931     KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4932   }
4933 
4934 #if OMP_40_ENABLED
4935   th->th.th_current_place = KMP_PLACE_UNDEFINED;
4936   th->th.th_new_place = KMP_PLACE_UNDEFINED;
4937   th->th.th_first_place = 0;
4938   th->th.th_last_place = __kmp_affinity_num_masks - 1;
4939 
4940   // Turn off 4.0 affinity for the current tread at this parallel level.
4941   th->th.th_current_task->td_icvs.proc_bind = proc_bind_false;
4942 #endif
4943 
4944   return retval;
4945 }
4946 
4947 int __kmp_aux_get_affinity(void **mask) {
4948   int gtid;
4949   int retval;
4950   kmp_info_t *th;
4951 
4952   if (!KMP_AFFINITY_CAPABLE()) {
4953     return -1;
4954   }
4955 
4956   gtid = __kmp_entry_gtid();
4957   th = __kmp_threads[gtid];
4958   KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4959 
4960   KA_TRACE(1000, (""); {
4961     char buf[KMP_AFFIN_MASK_PRINT_LEN];
4962     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4963                               th->th.th_affin_mask);
4964     __kmp_printf("kmp_get_affinity: stored affinity mask for thread %d = %s\n",
4965                  gtid, buf);
4966   });
4967 
4968   if (__kmp_env_consistency_check) {
4969     if ((mask == NULL) || (*mask == NULL)) {
4970       KMP_FATAL(AffinityInvalidMask, "kmp_get_affinity");
4971     }
4972   }
4973 
4974 #if !KMP_OS_WINDOWS
4975 
4976   retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4977   KA_TRACE(1000, (""); {
4978     char buf[KMP_AFFIN_MASK_PRINT_LEN];
4979     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4980                               (kmp_affin_mask_t *)(*mask));
4981     __kmp_printf("kmp_get_affinity: system affinity mask for thread %d = %s\n",
4982                  gtid, buf);
4983   });
4984   return retval;
4985 
4986 #else
4987 
4988   KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4989   return 0;
4990 
4991 #endif /* KMP_OS_WINDOWS */
4992 }
4993 
4994 int __kmp_aux_get_affinity_max_proc() {
4995   if (!KMP_AFFINITY_CAPABLE()) {
4996     return 0;
4997   }
4998 #if KMP_GROUP_AFFINITY
4999   if (__kmp_num_proc_groups > 1) {
5000     return (int)(__kmp_num_proc_groups * sizeof(DWORD_PTR) * CHAR_BIT);
5001   }
5002 #endif
5003   return __kmp_xproc;
5004 }
5005 
5006 int __kmp_aux_set_affinity_mask_proc(int proc, void **mask) {
5007   if (!KMP_AFFINITY_CAPABLE()) {
5008     return -1;
5009   }
5010 
5011   KA_TRACE(1000, (""); {
5012     int gtid = __kmp_entry_gtid();
5013     char buf[KMP_AFFIN_MASK_PRINT_LEN];
5014     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5015                               (kmp_affin_mask_t *)(*mask));
5016     __kmp_debug_printf("kmp_set_affinity_mask_proc: setting proc %d in "
5017                        "affinity mask for thread %d = %s\n",
5018                        proc, gtid, buf);
5019   });
5020 
5021   if (__kmp_env_consistency_check) {
5022     if ((mask == NULL) || (*mask == NULL)) {
5023       KMP_FATAL(AffinityInvalidMask, "kmp_set_affinity_mask_proc");
5024     }
5025   }
5026 
5027   if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5028     return -1;
5029   }
5030   if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5031     return -2;
5032   }
5033 
5034   KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
5035   return 0;
5036 }
5037 
5038 int __kmp_aux_unset_affinity_mask_proc(int proc, void **mask) {
5039   if (!KMP_AFFINITY_CAPABLE()) {
5040     return -1;
5041   }
5042 
5043   KA_TRACE(1000, (""); {
5044     int gtid = __kmp_entry_gtid();
5045     char buf[KMP_AFFIN_MASK_PRINT_LEN];
5046     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5047                               (kmp_affin_mask_t *)(*mask));
5048     __kmp_debug_printf("kmp_unset_affinity_mask_proc: unsetting proc %d in "
5049                        "affinity mask for thread %d = %s\n",
5050                        proc, gtid, buf);
5051   });
5052 
5053   if (__kmp_env_consistency_check) {
5054     if ((mask == NULL) || (*mask == NULL)) {
5055       KMP_FATAL(AffinityInvalidMask, "kmp_unset_affinity_mask_proc");
5056     }
5057   }
5058 
5059   if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5060     return -1;
5061   }
5062   if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5063     return -2;
5064   }
5065 
5066   KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
5067   return 0;
5068 }
5069 
5070 int __kmp_aux_get_affinity_mask_proc(int proc, void **mask) {
5071   if (!KMP_AFFINITY_CAPABLE()) {
5072     return -1;
5073   }
5074 
5075   KA_TRACE(1000, (""); {
5076     int gtid = __kmp_entry_gtid();
5077     char buf[KMP_AFFIN_MASK_PRINT_LEN];
5078     __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
5079                               (kmp_affin_mask_t *)(*mask));
5080     __kmp_debug_printf("kmp_get_affinity_mask_proc: getting proc %d in "
5081                        "affinity mask for thread %d = %s\n",
5082                        proc, gtid, buf);
5083   });
5084 
5085   if (__kmp_env_consistency_check) {
5086     if ((mask == NULL) || (*mask == NULL)) {
5087       KMP_FATAL(AffinityInvalidMask, "kmp_get_affinity_mask_proc");
5088     }
5089   }
5090 
5091   if ((proc < 0) || (proc >= __kmp_aux_get_affinity_max_proc())) {
5092     return -1;
5093   }
5094   if (!KMP_CPU_ISSET(proc, __kmp_affin_fullMask)) {
5095     return 0;
5096   }
5097 
5098   return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
5099 }
5100 
5101 // Dynamic affinity settings - Affinity balanced
5102 void __kmp_balanced_affinity(kmp_info_t *th, int nthreads) {
5103   KMP_DEBUG_ASSERT(th);
5104   bool fine_gran = true;
5105   int tid = th->th.th_info.ds.ds_tid;
5106 
5107   switch (__kmp_affinity_gran) {
5108   case affinity_gran_fine:
5109   case affinity_gran_thread:
5110     break;
5111   case affinity_gran_core:
5112     if (__kmp_nThreadsPerCore > 1) {
5113       fine_gran = false;
5114     }
5115     break;
5116   case affinity_gran_package:
5117     if (nCoresPerPkg > 1) {
5118       fine_gran = false;
5119     }
5120     break;
5121   default:
5122     fine_gran = false;
5123   }
5124 
5125   if (__kmp_affinity_uniform_topology()) {
5126     int coreID;
5127     int threadID;
5128     // Number of hyper threads per core in HT machine
5129     int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
5130     // Number of cores
5131     int ncores = __kmp_ncores;
5132     if ((nPackages > 1) && (__kmp_nth_per_core <= 1)) {
5133       __kmp_nth_per_core = __kmp_avail_proc / nPackages;
5134       ncores = nPackages;
5135     }
5136     // How many threads will be bound to each core
5137     int chunk = nthreads / ncores;
5138     // How many cores will have an additional thread bound to it - "big cores"
5139     int big_cores = nthreads % ncores;
5140     // Number of threads on the big cores
5141     int big_nth = (chunk + 1) * big_cores;
5142     if (tid < big_nth) {
5143       coreID = tid / (chunk + 1);
5144       threadID = (tid % (chunk + 1)) % __kmp_nth_per_core;
5145     } else { // tid >= big_nth
5146       coreID = (tid - big_cores) / chunk;
5147       threadID = ((tid - big_cores) % chunk) % __kmp_nth_per_core;
5148     }
5149 
5150     KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
5151                       "Illegal set affinity operation when not capable");
5152 
5153     kmp_affin_mask_t *mask = th->th.th_affin_mask;
5154     KMP_CPU_ZERO(mask);
5155 
5156     if (fine_gran) {
5157       int osID = address2os[coreID * __kmp_nth_per_core + threadID].second;
5158       KMP_CPU_SET(osID, mask);
5159     } else {
5160       for (int i = 0; i < __kmp_nth_per_core; i++) {
5161         int osID;
5162         osID = address2os[coreID * __kmp_nth_per_core + i].second;
5163         KMP_CPU_SET(osID, mask);
5164       }
5165     }
5166     if (__kmp_affinity_verbose) {
5167       char buf[KMP_AFFIN_MASK_PRINT_LEN];
5168       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5169       KMP_INFORM(BoundToOSProcSet, "KMP_AFFINITY", (kmp_int32)getpid(),
5170                  __kmp_gettid(), tid, buf);
5171     }
5172     __kmp_set_system_affinity(mask, TRUE);
5173   } else { // Non-uniform topology
5174 
5175     kmp_affin_mask_t *mask = th->th.th_affin_mask;
5176     KMP_CPU_ZERO(mask);
5177 
5178     int core_level = __kmp_affinity_find_core_level(
5179         address2os, __kmp_avail_proc, __kmp_aff_depth - 1);
5180     int ncores = __kmp_affinity_compute_ncores(address2os, __kmp_avail_proc,
5181                                                __kmp_aff_depth - 1, core_level);
5182     int nth_per_core = __kmp_affinity_max_proc_per_core(
5183         address2os, __kmp_avail_proc, __kmp_aff_depth - 1, core_level);
5184 
5185     // For performance gain consider the special case nthreads ==
5186     // __kmp_avail_proc
5187     if (nthreads == __kmp_avail_proc) {
5188       if (fine_gran) {
5189         int osID = address2os[tid].second;
5190         KMP_CPU_SET(osID, mask);
5191       } else {
5192         int core = __kmp_affinity_find_core(address2os, tid,
5193                                             __kmp_aff_depth - 1, core_level);
5194         for (int i = 0; i < __kmp_avail_proc; i++) {
5195           int osID = address2os[i].second;
5196           if (__kmp_affinity_find_core(address2os, i, __kmp_aff_depth - 1,
5197                                        core_level) == core) {
5198             KMP_CPU_SET(osID, mask);
5199           }
5200         }
5201       }
5202     } else if (nthreads <= ncores) {
5203 
5204       int core = 0;
5205       for (int i = 0; i < ncores; i++) {
5206         // Check if this core from procarr[] is in the mask
5207         int in_mask = 0;
5208         for (int j = 0; j < nth_per_core; j++) {
5209           if (procarr[i * nth_per_core + j] != -1) {
5210             in_mask = 1;
5211             break;
5212           }
5213         }
5214         if (in_mask) {
5215           if (tid == core) {
5216             for (int j = 0; j < nth_per_core; j++) {
5217               int osID = procarr[i * nth_per_core + j];
5218               if (osID != -1) {
5219                 KMP_CPU_SET(osID, mask);
5220                 // For fine granularity it is enough to set the first available
5221                 // osID for this core
5222                 if (fine_gran) {
5223                   break;
5224                 }
5225               }
5226             }
5227             break;
5228           } else {
5229             core++;
5230           }
5231         }
5232       }
5233     } else { // nthreads > ncores
5234       // Array to save the number of processors at each core
5235       int *nproc_at_core = (int *)KMP_ALLOCA(sizeof(int) * ncores);
5236       // Array to save the number of cores with "x" available processors;
5237       int *ncores_with_x_procs =
5238           (int *)KMP_ALLOCA(sizeof(int) * (nth_per_core + 1));
5239       // Array to save the number of cores with # procs from x to nth_per_core
5240       int *ncores_with_x_to_max_procs =
5241           (int *)KMP_ALLOCA(sizeof(int) * (nth_per_core + 1));
5242 
5243       for (int i = 0; i <= nth_per_core; i++) {
5244         ncores_with_x_procs[i] = 0;
5245         ncores_with_x_to_max_procs[i] = 0;
5246       }
5247 
5248       for (int i = 0; i < ncores; i++) {
5249         int cnt = 0;
5250         for (int j = 0; j < nth_per_core; j++) {
5251           if (procarr[i * nth_per_core + j] != -1) {
5252             cnt++;
5253           }
5254         }
5255         nproc_at_core[i] = cnt;
5256         ncores_with_x_procs[cnt]++;
5257       }
5258 
5259       for (int i = 0; i <= nth_per_core; i++) {
5260         for (int j = i; j <= nth_per_core; j++) {
5261           ncores_with_x_to_max_procs[i] += ncores_with_x_procs[j];
5262         }
5263       }
5264 
5265       // Max number of processors
5266       int nproc = nth_per_core * ncores;
5267       // An array to keep number of threads per each context
5268       int *newarr = (int *)__kmp_allocate(sizeof(int) * nproc);
5269       for (int i = 0; i < nproc; i++) {
5270         newarr[i] = 0;
5271       }
5272 
5273       int nth = nthreads;
5274       int flag = 0;
5275       while (nth > 0) {
5276         for (int j = 1; j <= nth_per_core; j++) {
5277           int cnt = ncores_with_x_to_max_procs[j];
5278           for (int i = 0; i < ncores; i++) {
5279             // Skip the core with 0 processors
5280             if (nproc_at_core[i] == 0) {
5281               continue;
5282             }
5283             for (int k = 0; k < nth_per_core; k++) {
5284               if (procarr[i * nth_per_core + k] != -1) {
5285                 if (newarr[i * nth_per_core + k] == 0) {
5286                   newarr[i * nth_per_core + k] = 1;
5287                   cnt--;
5288                   nth--;
5289                   break;
5290                 } else {
5291                   if (flag != 0) {
5292                     newarr[i * nth_per_core + k]++;
5293                     cnt--;
5294                     nth--;
5295                     break;
5296                   }
5297                 }
5298               }
5299             }
5300             if (cnt == 0 || nth == 0) {
5301               break;
5302             }
5303           }
5304           if (nth == 0) {
5305             break;
5306           }
5307         }
5308         flag = 1;
5309       }
5310       int sum = 0;
5311       for (int i = 0; i < nproc; i++) {
5312         sum += newarr[i];
5313         if (sum > tid) {
5314           if (fine_gran) {
5315             int osID = procarr[i];
5316             KMP_CPU_SET(osID, mask);
5317           } else {
5318             int coreID = i / nth_per_core;
5319             for (int ii = 0; ii < nth_per_core; ii++) {
5320               int osID = procarr[coreID * nth_per_core + ii];
5321               if (osID != -1) {
5322                 KMP_CPU_SET(osID, mask);
5323               }
5324             }
5325           }
5326           break;
5327         }
5328       }
5329       __kmp_free(newarr);
5330     }
5331 
5332     if (__kmp_affinity_verbose) {
5333       char buf[KMP_AFFIN_MASK_PRINT_LEN];
5334       __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
5335       KMP_INFORM(BoundToOSProcSet, "KMP_AFFINITY", (kmp_int32)getpid(),
5336                  __kmp_gettid(), tid, buf);
5337     }
5338     __kmp_set_system_affinity(mask, TRUE);
5339   }
5340 }
5341 
5342 #if KMP_OS_LINUX
5343 // We don't need this entry for Windows because
5344 // there is GetProcessAffinityMask() api
5345 //
5346 // The intended usage is indicated by these steps:
5347 // 1) The user gets the current affinity mask
5348 // 2) Then sets the affinity by calling this function
5349 // 3) Error check the return value
5350 // 4) Use non-OpenMP parallelization
5351 // 5) Reset the affinity to what was stored in step 1)
5352 #ifdef __cplusplus
5353 extern "C"
5354 #endif
5355     int
5356     kmp_set_thread_affinity_mask_initial()
5357 // the function returns 0 on success,
5358 //   -1 if we cannot bind thread
5359 //   >0 (errno) if an error happened during binding
5360 {
5361   int gtid = __kmp_get_gtid();
5362   if (gtid < 0) {
5363     // Do not touch non-omp threads
5364     KA_TRACE(30, ("kmp_set_thread_affinity_mask_initial: "
5365                   "non-omp thread, returning\n"));
5366     return -1;
5367   }
5368   if (!KMP_AFFINITY_CAPABLE() || !__kmp_init_middle) {
5369     KA_TRACE(30, ("kmp_set_thread_affinity_mask_initial: "
5370                   "affinity not initialized, returning\n"));
5371     return -1;
5372   }
5373   KA_TRACE(30, ("kmp_set_thread_affinity_mask_initial: "
5374                 "set full mask for thread %d\n",
5375                 gtid));
5376   KMP_DEBUG_ASSERT(__kmp_affin_fullMask != NULL);
5377   return __kmp_set_system_affinity(__kmp_affin_fullMask, FALSE);
5378 }
5379 #endif
5380 
5381 #endif // KMP_AFFINITY_SUPPORTED
5382