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