43 #if KMP_OS_WINDOWS || KMP_OS_LINUX
49 __kmp_affinity_print_mask(
char *buf,
int buf_len, kmp_affin_mask_t *mask)
51 KMP_ASSERT(buf_len >= 40);
53 char *end = buf + buf_len - 1;
59 for (i = 0; i < KMP_CPU_SETSIZE; i++) {
60 if (KMP_CPU_ISSET(i, mask)) {
64 if (i == KMP_CPU_SETSIZE) {
65 sprintf(scan,
"{<empty>}");
66 while (*scan !=
'\0') scan++;
67 KMP_ASSERT(scan <= end);
71 sprintf(scan,
"{%ld", i);
72 while (*scan !=
'\0') scan++;
74 for (; i < KMP_CPU_SETSIZE; i++) {
75 if (! KMP_CPU_ISSET(i, mask)) {
85 if (end - scan < 15) {
88 sprintf(scan,
",%-ld", i);
89 while (*scan !=
'\0') scan++;
91 if (i < KMP_CPU_SETSIZE) {
92 sprintf(scan,
",...");
93 while (*scan !=
'\0') scan++;
96 while (*scan !=
'\0') scan++;
97 KMP_ASSERT(scan <= end);
103 __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask)
107 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
109 if (__kmp_num_proc_groups > 1) {
111 struct GROUP_AFFINITY ga;
112 KMP_DEBUG_ASSERT(__kmp_GetActiveProcessorCount != NULL);
113 for (group = 0; group < __kmp_num_proc_groups; group++) {
115 int num = __kmp_GetActiveProcessorCount(group);
116 for (i = 0; i < num; i++) {
117 KMP_CPU_SET(i + group * (CHAR_BIT *
sizeof(DWORD_PTR)), mask);
127 for (proc = 0; proc < __kmp_xproc; proc++) {
128 KMP_CPU_SET(proc, mask);
156 # if !defined(KMP_DEBUG) && !defined(COVER)
160 static const unsigned maxDepth = 32;
161 unsigned labels[maxDepth];
162 unsigned childNums[maxDepth];
165 Address(
unsigned _depth)
166 : depth(_depth), leader(FALSE) {
168 Address &operator=(
const Address &b) {
170 for (
unsigned i = 0; i < depth; i++) {
171 labels[i] = b.labels[i];
172 childNums[i] = b.childNums[i];
177 bool operator==(
const Address &b)
const {
178 if (depth != b.depth)
180 for (
unsigned i = 0; i < depth; i++)
181 if(labels[i] != b.labels[i])
185 bool isClose(
const Address &b,
int level)
const {
186 if (depth != b.depth)
188 if ((
unsigned)level >= depth)
190 for (
unsigned i = 0; i < (depth - level); i++)
191 if(labels[i] != b.labels[i])
195 bool operator!=(
const Address &b)
const {
196 return !operator==(b);
204 AddrUnsPair(Address _first,
unsigned _second)
205 : first(_first), second(_second) {
207 AddrUnsPair &operator=(
const AddrUnsPair &b)
219 static const unsigned maxDepth = 32;
220 unsigned labels[maxDepth];
221 unsigned childNums[maxDepth];
224 Address(
unsigned _depth);
225 Address &operator=(
const Address &b);
226 bool operator==(
const Address &b)
const;
227 bool isClose(
const Address &b,
int level)
const;
228 bool operator!=(
const Address &b)
const;
231 Address::Address(
unsigned _depth)
237 Address &Address::operator=(
const Address &b) {
239 for (
unsigned i = 0; i < depth; i++) {
240 labels[i] = b.labels[i];
241 childNums[i] = b.childNums[i];
247 bool Address::operator==(
const Address &b)
const {
248 if (depth != b.depth)
250 for (
unsigned i = 0; i < depth; i++)
251 if(labels[i] != b.labels[i])
256 bool Address::isClose(
const Address &b,
int level)
const {
257 if (depth != b.depth)
259 if ((
unsigned)level >= depth)
261 for (
unsigned i = 0; i < (depth - level); i++)
262 if(labels[i] != b.labels[i])
267 bool Address::operator!=(
const Address &b)
const {
268 return !operator==(b);
275 AddrUnsPair(Address _first,
unsigned _second);
276 AddrUnsPair &operator=(
const AddrUnsPair &b);
279 AddrUnsPair::AddrUnsPair(Address _first,
unsigned _second)
280 : first(_first), second(_second)
284 AddrUnsPair &AddrUnsPair::operator=(
const AddrUnsPair &b)
295 __kmp_affinity_cmp_Address_labels(
const void *a,
const void *b)
297 const Address *aa = (
const Address *)&(((AddrUnsPair *)a)
299 const Address *bb = (
const Address *)&(((AddrUnsPair *)b)
301 unsigned depth = aa->depth;
303 KMP_DEBUG_ASSERT(depth == bb->depth);
304 for (i = 0; i < depth; i++) {
305 if (aa->labels[i] < bb->labels[i])
return -1;
306 if (aa->labels[i] > bb->labels[i])
return 1;
313 __kmp_affinity_cmp_Address_child_num(
const void *a,
const void *b)
315 const Address *aa = (
const Address *)&(((AddrUnsPair *)a)
317 const Address *bb = (
const Address *)&(((AddrUnsPair *)b)
319 unsigned depth = aa->depth;
321 KMP_DEBUG_ASSERT(depth == bb->depth);
322 KMP_DEBUG_ASSERT((
unsigned)__kmp_affinity_compact <= depth);
323 KMP_DEBUG_ASSERT(__kmp_affinity_compact >= 0);
324 for (i = 0; i < (unsigned)__kmp_affinity_compact; i++) {
325 int j = depth - i - 1;
326 if (aa->childNums[j] < bb->childNums[j])
return -1;
327 if (aa->childNums[j] > bb->childNums[j])
return 1;
329 for (; i < depth; i++) {
330 int j = i - __kmp_affinity_compact;
331 if (aa->childNums[j] < bb->childNums[j])
return -1;
332 if (aa->childNums[j] > bb->childNums[j])
return 1;
352 __kmp_affinity_assign_child_nums(AddrUnsPair *address2os,
355 KMP_DEBUG_ASSERT(numAddrs > 0);
356 int depth = address2os->first.depth;
357 unsigned *counts = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
358 unsigned *lastLabel = (
unsigned *)__kmp_allocate(depth
361 for (labCt = 0; labCt < depth; labCt++) {
362 address2os[0].first.childNums[labCt] = counts[labCt] = 0;
363 lastLabel[labCt] = address2os[0].first.labels[labCt];
366 for (i = 1; i < numAddrs; i++) {
367 for (labCt = 0; labCt < depth; labCt++) {
368 if (address2os[i].first.labels[labCt] != lastLabel[labCt]) {
370 for (labCt2 = labCt + 1; labCt2 < depth; labCt2++) {
372 lastLabel[labCt2] = address2os[i].first.labels[labCt2];
375 lastLabel[labCt] = address2os[i].first.labels[labCt];
379 for (labCt = 0; labCt < depth; labCt++) {
380 address2os[i].first.childNums[labCt] = counts[labCt];
382 for (; labCt < (int)Address::maxDepth; labCt++) {
383 address2os[i].first.childNums[labCt] = 0;
401 static kmp_affin_mask_t *fullMask = NULL;
404 __kmp_affinity_get_fullMask() {
return fullMask; }
407 static int nCoresPerPkg, nPackages;
408 int __kmp_nThreadsPerCore;
417 __kmp_affinity_uniform_topology()
419 return __kmp_avail_proc == (__kmp_nThreadsPerCore * nCoresPerPkg * nPackages);
428 __kmp_affinity_print_topology(AddrUnsPair *address2os,
int len,
int depth,
429 int pkgLevel,
int coreLevel,
int threadLevel)
433 KMP_INFORM(OSProcToPhysicalThreadMap,
"KMP_AFFINITY");
434 for (proc = 0; proc < len; proc++) {
437 __kmp_str_buf_init(&buf);
438 for (level = 0; level < depth; level++) {
439 if (level == threadLevel) {
440 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Thread));
442 else if (level == coreLevel) {
443 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Core));
445 else if (level == pkgLevel) {
446 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Package));
448 else if (level > pkgLevel) {
449 __kmp_str_buf_print(&buf,
"%s_%d ", KMP_I18N_STR(Node),
450 level - pkgLevel - 1);
453 __kmp_str_buf_print(&buf,
"L%d ", level);
455 __kmp_str_buf_print(&buf,
"%d ",
456 address2os[proc].first.labels[level]);
458 KMP_INFORM(OSProcMapToPack,
"KMP_AFFINITY", address2os[proc].second,
460 __kmp_str_buf_free(&buf);
471 __kmp_affinity_create_flat_map(AddrUnsPair **address2os,
472 kmp_i18n_id_t *
const msg_id)
475 *msg_id = kmp_i18n_null;
482 if (! KMP_AFFINITY_CAPABLE()) {
483 KMP_ASSERT(__kmp_affinity_type == affinity_none);
484 __kmp_ncores = nPackages = __kmp_xproc;
485 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
486 __kmp_ht_enabled = FALSE;
487 if (__kmp_affinity_verbose) {
488 KMP_INFORM(AffFlatTopology,
"KMP_AFFINITY");
489 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
490 KMP_INFORM(Uniform,
"KMP_AFFINITY");
491 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
492 __kmp_nThreadsPerCore, __kmp_ncores);
503 __kmp_ncores = nPackages = __kmp_avail_proc;
504 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
505 __kmp_ht_enabled = FALSE;
506 if (__kmp_affinity_verbose) {
507 char buf[KMP_AFFIN_MASK_PRINT_LEN];
508 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
510 KMP_INFORM(AffCapableUseFlat,
"KMP_AFFINITY");
511 if (__kmp_affinity_respect_mask) {
512 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
514 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
516 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
517 KMP_INFORM(Uniform,
"KMP_AFFINITY");
518 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
519 __kmp_nThreadsPerCore, __kmp_ncores);
521 if (__kmp_affinity_type == affinity_none) {
528 *address2os = (AddrUnsPair*)
529 __kmp_allocate(
sizeof(**address2os) * __kmp_avail_proc);
532 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
536 if (! KMP_CPU_ISSET(i, fullMask)) {
542 (*address2os)[avail_ct++] = AddrUnsPair(addr,i);
544 if (__kmp_affinity_verbose) {
545 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
548 if (__kmp_affinity_gran_levels < 0) {
553 if (__kmp_affinity_gran > affinity_gran_package) {
554 __kmp_affinity_gran_levels = 1;
557 __kmp_affinity_gran_levels = 0;
564 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
575 __kmp_affinity_create_proc_group_map(AddrUnsPair **address2os,
576 kmp_i18n_id_t *
const msg_id)
579 *msg_id = kmp_i18n_null;
585 if ((! KMP_AFFINITY_CAPABLE()) || (__kmp_get_proc_group(fullMask) >= 0)) {
593 *address2os = (AddrUnsPair*)
594 __kmp_allocate(
sizeof(**address2os) * __kmp_avail_proc);
597 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
601 if (! KMP_CPU_ISSET(i, fullMask)) {
606 addr.labels[0] = i / (CHAR_BIT *
sizeof(DWORD_PTR));
607 addr.labels[1] = i % (CHAR_BIT *
sizeof(DWORD_PTR));
608 (*address2os)[avail_ct++] = AddrUnsPair(addr,i);
610 if (__kmp_affinity_verbose) {
611 KMP_INFORM(AffOSProcToGroup,
"KMP_AFFINITY", i, addr.labels[0],
616 if (__kmp_affinity_gran_levels < 0) {
617 if (__kmp_affinity_gran == affinity_gran_group) {
618 __kmp_affinity_gran_levels = 1;
620 else if ((__kmp_affinity_gran == affinity_gran_fine)
621 || (__kmp_affinity_gran == affinity_gran_thread)) {
622 __kmp_affinity_gran_levels = 0;
625 const char *gran_str = NULL;
626 if (__kmp_affinity_gran == affinity_gran_core) {
629 else if (__kmp_affinity_gran == affinity_gran_package) {
630 gran_str =
"package";
632 else if (__kmp_affinity_gran == affinity_gran_node) {
640 __kmp_affinity_gran_levels = 0;
649 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
652 __kmp_cpuid_mask_width(
int count) {
655 while((1<<r) < count)
661 class apicThreadInfo {
665 unsigned maxCoresPerPkg;
666 unsigned maxThreadsPerPkg;
674 __kmp_affinity_cmp_apicThreadInfo_os_id(
const void *a,
const void *b)
676 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
677 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
678 if (aa->osId < bb->osId)
return -1;
679 if (aa->osId > bb->osId)
return 1;
685 __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
const void *b)
687 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
688 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
689 if (aa->pkgId < bb->pkgId)
return -1;
690 if (aa->pkgId > bb->pkgId)
return 1;
691 if (aa->coreId < bb->coreId)
return -1;
692 if (aa->coreId > bb->coreId)
return 1;
693 if (aa->threadId < bb->threadId)
return -1;
694 if (aa->threadId > bb->threadId)
return 1;
706 __kmp_affinity_create_apicid_map(AddrUnsPair **address2os,
707 kmp_i18n_id_t *
const msg_id)
711 *msg_id = kmp_i18n_null;
719 __kmp_x86_cpuid(0, 0, &buf);
721 *msg_id = kmp_i18n_str_NoLeaf4Support;
737 if (! KMP_AFFINITY_CAPABLE()) {
742 KMP_ASSERT(__kmp_affinity_type == affinity_none);
753 __kmp_x86_cpuid(1, 0, &buf);
754 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
755 if (maxThreadsPerPkg == 0) {
756 maxThreadsPerPkg = 1;
772 __kmp_x86_cpuid(0, 0, &buf);
774 __kmp_x86_cpuid(4, 0, &buf);
775 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
798 __kmp_ncores = __kmp_xproc;
799 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
800 __kmp_nThreadsPerCore = 1;
801 __kmp_ht_enabled = FALSE;
802 if (__kmp_affinity_verbose) {
803 KMP_INFORM(AffNotCapableUseLocCpuid,
"KMP_AFFINITY");
804 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
805 if (__kmp_affinity_uniform_topology()) {
806 KMP_INFORM(Uniform,
"KMP_AFFINITY");
808 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
810 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
811 __kmp_nThreadsPerCore, __kmp_ncores);
826 kmp_affin_mask_t *oldMask;
827 KMP_CPU_ALLOC(oldMask);
828 KMP_ASSERT(oldMask != NULL);
829 __kmp_get_system_affinity(oldMask, TRUE);
864 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
865 __kmp_avail_proc *
sizeof(apicThreadInfo));
867 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
871 if (! KMP_CPU_ISSET(i, fullMask)) {
874 KMP_DEBUG_ASSERT((
int)nApics < __kmp_avail_proc);
876 __kmp_affinity_bind_thread(i);
877 threadInfo[nApics].osId = i;
883 __kmp_x86_cpuid(1, 0, &buf);
884 if (! (buf.edx >> 9) & 1) {
885 __kmp_set_system_affinity(oldMask, TRUE);
886 __kmp_free(threadInfo);
887 KMP_CPU_FREE(oldMask);
888 *msg_id = kmp_i18n_str_ApicNotPresent;
891 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
892 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
893 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
894 threadInfo[nApics].maxThreadsPerPkg = 1;
905 __kmp_x86_cpuid(0, 0, &buf);
907 __kmp_x86_cpuid(4, 0, &buf);
908 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
911 threadInfo[nApics].maxCoresPerPkg = 1;
918 int widthCT = __kmp_cpuid_mask_width(
919 threadInfo[nApics].maxThreadsPerPkg);
920 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
922 int widthC = __kmp_cpuid_mask_width(
923 threadInfo[nApics].maxCoresPerPkg);
924 int widthT = widthCT - widthC;
931 __kmp_set_system_affinity(oldMask, TRUE);
932 __kmp_free(threadInfo);
933 KMP_CPU_FREE(oldMask);
934 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
938 int maskC = (1 << widthC) - 1;
939 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT)
942 int maskT = (1 << widthT) - 1;
943 threadInfo[nApics].threadId = threadInfo[nApics].apicId &maskT;
952 __kmp_set_system_affinity(oldMask, TRUE);
964 KMP_ASSERT(nApics > 0);
966 __kmp_ncores = nPackages = 1;
967 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
968 __kmp_ht_enabled = FALSE;
969 if (__kmp_affinity_verbose) {
970 char buf[KMP_AFFIN_MASK_PRINT_LEN];
971 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
973 KMP_INFORM(AffUseGlobCpuid,
"KMP_AFFINITY");
974 if (__kmp_affinity_respect_mask) {
975 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
977 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
979 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
980 KMP_INFORM(Uniform,
"KMP_AFFINITY");
981 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
982 __kmp_nThreadsPerCore, __kmp_ncores);
985 if (__kmp_affinity_type == affinity_none) {
986 __kmp_free(threadInfo);
987 KMP_CPU_FREE(oldMask);
991 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair));
993 addr.labels[0] = threadInfo[0].pkgId;
994 (*address2os)[0] = AddrUnsPair(addr, threadInfo[0].osId);
996 if (__kmp_affinity_gran_levels < 0) {
997 __kmp_affinity_gran_levels = 0;
1000 if (__kmp_affinity_verbose) {
1001 __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
1004 __kmp_free(threadInfo);
1005 KMP_CPU_FREE(oldMask);
1012 qsort(threadInfo, nApics,
sizeof(*threadInfo),
1013 __kmp_affinity_cmp_apicThreadInfo_phys_id);
1032 __kmp_nThreadsPerCore = 1;
1033 unsigned nCores = 1;
1036 unsigned lastPkgId = threadInfo[0].pkgId;
1037 unsigned coreCt = 1;
1038 unsigned lastCoreId = threadInfo[0].coreId;
1039 unsigned threadCt = 1;
1040 unsigned lastThreadId = threadInfo[0].threadId;
1043 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
1044 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
1046 for (i = 1; i < nApics; i++) {
1047 if (threadInfo[i].pkgId != lastPkgId) {
1050 lastPkgId = threadInfo[i].pkgId;
1051 if ((
int)coreCt > nCoresPerPkg) nCoresPerPkg = coreCt;
1053 lastCoreId = threadInfo[i].coreId;
1054 if ((
int)threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1056 lastThreadId = threadInfo[i].threadId;
1063 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
1064 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
1068 if (threadInfo[i].coreId != lastCoreId) {
1071 lastCoreId = threadInfo[i].coreId;
1072 if ((
int)threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1074 lastThreadId = threadInfo[i].threadId;
1076 else if (threadInfo[i].threadId != lastThreadId) {
1078 lastThreadId = threadInfo[i].threadId;
1081 __kmp_free(threadInfo);
1082 KMP_CPU_FREE(oldMask);
1083 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
1091 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg)
1092 || (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
1093 __kmp_free(threadInfo);
1094 KMP_CPU_FREE(oldMask);
1095 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1100 if ((
int)coreCt > nCoresPerPkg) nCoresPerPkg = coreCt;
1101 if ((
int)threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1109 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1110 __kmp_ncores = nCores;
1111 if (__kmp_affinity_verbose) {
1112 char buf[KMP_AFFIN_MASK_PRINT_LEN];
1113 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1115 KMP_INFORM(AffUseGlobCpuid,
"KMP_AFFINITY");
1116 if (__kmp_affinity_respect_mask) {
1117 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
1119 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
1121 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1122 if (__kmp_affinity_uniform_topology()) {
1123 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1125 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1127 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1128 __kmp_nThreadsPerCore, __kmp_ncores);
1132 if (__kmp_affinity_type == affinity_none) {
1133 __kmp_free(threadInfo);
1134 KMP_CPU_FREE(oldMask);
1144 int coreLevel = (nCoresPerPkg <= 1) ? -1 : 1;
1145 int threadLevel = (__kmp_nThreadsPerCore <= 1) ? -1 : ((coreLevel >= 0) ? 2 : 1);
1146 unsigned depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
1148 KMP_ASSERT(depth > 0);
1149 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair) * nApics);
1151 for (i = 0; i < nApics; ++i) {
1152 Address addr(depth);
1153 unsigned os = threadInfo[i].osId;
1156 if (pkgLevel >= 0) {
1157 addr.labels[d++] = threadInfo[i].pkgId;
1159 if (coreLevel >= 0) {
1160 addr.labels[d++] = threadInfo[i].coreId;
1162 if (threadLevel >= 0) {
1163 addr.labels[d++] = threadInfo[i].threadId;
1165 (*address2os)[i] = AddrUnsPair(addr, os);
1168 if (__kmp_affinity_gran_levels < 0) {
1173 __kmp_affinity_gran_levels = 0;
1174 if ((threadLevel >= 0)
1175 && (__kmp_affinity_gran > affinity_gran_thread)) {
1176 __kmp_affinity_gran_levels++;
1178 if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1179 __kmp_affinity_gran_levels++;
1181 if ((pkgLevel >= 0) && (__kmp_affinity_gran > affinity_gran_package)) {
1182 __kmp_affinity_gran_levels++;
1186 if (__kmp_affinity_verbose) {
1187 __kmp_affinity_print_topology(*address2os, nApics, depth, pkgLevel,
1188 coreLevel, threadLevel);
1191 __kmp_free(threadInfo);
1192 KMP_CPU_FREE(oldMask);
1203 __kmp_affinity_create_x2apicid_map(AddrUnsPair **address2os,
1204 kmp_i18n_id_t *
const msg_id)
1209 *msg_id = kmp_i18n_null;
1214 __kmp_x86_cpuid(0, 0, &buf);
1216 *msg_id = kmp_i18n_str_NoLeaf11Support;
1219 __kmp_x86_cpuid(11, 0, &buf);
1221 *msg_id = kmp_i18n_str_NoLeaf11Support;
1232 int threadLevel = -1;
1235 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
1237 for (level = 0;; level++) {
1250 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1253 __kmp_x86_cpuid(11, level, &buf);
1264 int kind = (buf.ecx >> 8) & 0xff;
1269 threadLevel = level;
1272 __kmp_nThreadsPerCore = buf.ebx & 0xff;
1273 if (__kmp_nThreadsPerCore == 0) {
1274 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1278 else if (kind == 2) {
1284 nCoresPerPkg = buf.ebx & 0xff;
1285 if (nCoresPerPkg == 0) {
1286 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1292 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1295 if (pkgLevel >= 0) {
1299 nPackages = buf.ebx & 0xff;
1300 if (nPackages == 0) {
1301 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1314 if (threadLevel >= 0) {
1315 threadLevel = depth - threadLevel - 1;
1317 if (coreLevel >= 0) {
1318 coreLevel = depth - coreLevel - 1;
1320 KMP_DEBUG_ASSERT(pkgLevel >= 0);
1321 pkgLevel = depth - pkgLevel - 1;
1330 if (! KMP_AFFINITY_CAPABLE())
1336 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1338 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1339 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1340 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1341 if (__kmp_affinity_verbose) {
1342 KMP_INFORM(AffNotCapableUseLocCpuidL11,
"KMP_AFFINITY");
1343 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1344 if (__kmp_affinity_uniform_topology()) {
1345 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1347 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1349 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1350 __kmp_nThreadsPerCore, __kmp_ncores);
1365 kmp_affin_mask_t *oldMask;
1366 KMP_CPU_ALLOC(oldMask);
1367 __kmp_get_system_affinity(oldMask, TRUE);
1372 AddrUnsPair *retval = (AddrUnsPair *)
1373 __kmp_allocate(
sizeof(AddrUnsPair) * __kmp_avail_proc);
1381 for (proc = 0; proc < KMP_CPU_SETSIZE; ++proc) {
1385 if (! KMP_CPU_ISSET(proc, fullMask)) {
1388 KMP_DEBUG_ASSERT(nApics < __kmp_avail_proc);
1390 __kmp_affinity_bind_thread(proc);
1396 Address addr(depth);
1399 for (level = 0; level < depth; level++) {
1400 __kmp_x86_cpuid(11, level, &buf);
1401 unsigned apicId = buf.edx;
1403 if (level != depth - 1) {
1404 KMP_CPU_FREE(oldMask);
1405 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1408 addr.labels[depth - level - 1] = apicId >> prev_shift;
1412 int shift = buf.eax & 0x1f;
1413 int mask = (1 << shift) - 1;
1414 addr.labels[depth - level - 1] = (apicId & mask) >> prev_shift;
1417 if (level != depth) {
1418 KMP_CPU_FREE(oldMask);
1419 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1423 retval[nApics] = AddrUnsPair(addr, proc);
1431 __kmp_set_system_affinity(oldMask, TRUE);
1436 KMP_ASSERT(nApics > 0);
1438 __kmp_ncores = nPackages = 1;
1439 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1440 __kmp_ht_enabled = FALSE;
1441 if (__kmp_affinity_verbose) {
1442 char buf[KMP_AFFIN_MASK_PRINT_LEN];
1443 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1445 KMP_INFORM(AffUseGlobCpuidL11,
"KMP_AFFINITY");
1446 if (__kmp_affinity_respect_mask) {
1447 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
1449 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
1451 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1452 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1453 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1454 __kmp_nThreadsPerCore, __kmp_ncores);
1457 if (__kmp_affinity_type == affinity_none) {
1459 KMP_CPU_FREE(oldMask);
1467 addr.labels[0] = retval[0].first.labels[pkgLevel];
1468 retval[0].first = addr;
1470 if (__kmp_affinity_gran_levels < 0) {
1471 __kmp_affinity_gran_levels = 0;
1474 if (__kmp_affinity_verbose) {
1475 __kmp_affinity_print_topology(retval, 1, 1, 0, -1, -1);
1478 *address2os = retval;
1479 KMP_CPU_FREE(oldMask);
1486 qsort(retval, nApics,
sizeof(*retval), __kmp_affinity_cmp_Address_labels);
1491 unsigned *totals = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1492 unsigned *counts = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1493 unsigned *maxCt = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1494 unsigned *last = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1495 for (level = 0; level < depth; level++) {
1499 last[level] = retval[0].first.labels[level];
1508 for (proc = 1; (int)proc < nApics; proc++) {
1510 for (level = 0; level < depth; level++) {
1511 if (retval[proc].first.labels[level] != last[level]) {
1513 for (j = level + 1; j < depth; j++) {
1523 last[j] = retval[proc].first.labels[j];
1527 if (counts[level] > maxCt[level]) {
1528 maxCt[level] = counts[level];
1530 last[level] = retval[proc].first.labels[level];
1533 else if (level == depth - 1) {
1539 KMP_CPU_FREE(oldMask);
1540 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
1552 if (threadLevel >= 0) {
1553 __kmp_nThreadsPerCore = maxCt[threadLevel];
1556 __kmp_nThreadsPerCore = 1;
1558 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1560 nPackages = totals[pkgLevel];
1562 if (coreLevel >= 0) {
1563 __kmp_ncores = totals[coreLevel];
1564 nCoresPerPkg = maxCt[coreLevel];
1567 __kmp_ncores = nPackages;
1574 unsigned prod = maxCt[0];
1575 for (level = 1; level < depth; level++) {
1576 prod *= maxCt[level];
1578 bool uniform = (prod == totals[level - 1]);
1583 if (__kmp_affinity_verbose) {
1584 char mask[KMP_AFFIN_MASK_PRINT_LEN];
1585 __kmp_affinity_print_mask(mask, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1587 KMP_INFORM(AffUseGlobCpuidL11,
"KMP_AFFINITY");
1588 if (__kmp_affinity_respect_mask) {
1589 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", mask);
1591 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", mask);
1593 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1595 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1597 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1601 __kmp_str_buf_init(&buf);
1603 __kmp_str_buf_print(&buf,
"%d", totals[0]);
1604 for (level = 1; level <= pkgLevel; level++) {
1605 __kmp_str_buf_print(&buf,
" x %d", maxCt[level]);
1607 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, nCoresPerPkg,
1608 __kmp_nThreadsPerCore, __kmp_ncores);
1610 __kmp_str_buf_free(&buf);
1613 if (__kmp_affinity_type == affinity_none) {
1619 KMP_CPU_FREE(oldMask);
1628 for (level = 0; level < depth; level++) {
1629 if ((maxCt[level] == 1) && (level != pkgLevel)) {
1639 if (new_depth != depth) {
1640 AddrUnsPair *new_retval = (AddrUnsPair *)__kmp_allocate(
1641 sizeof(AddrUnsPair) * nApics);
1642 for (proc = 0; (int)proc < nApics; proc++) {
1643 Address addr(new_depth);
1644 new_retval[proc] = AddrUnsPair(addr, retval[proc].second);
1647 for (level = 0; level < depth; level++) {
1648 if ((maxCt[level] == 1) && (level != pkgLevel)) {
1649 if (level == threadLevel) {
1652 else if ((threadLevel >= 0) && (level < threadLevel)) {
1655 if (level == coreLevel) {
1658 else if ((coreLevel >= 0) && (level < coreLevel)) {
1661 if (level < pkgLevel) {
1666 for (proc = 0; (int)proc < nApics; proc++) {
1667 new_retval[proc].first.labels[new_level]
1668 = retval[proc].first.labels[level];
1674 retval = new_retval;
1678 if (__kmp_affinity_gran_levels < 0) {
1683 __kmp_affinity_gran_levels = 0;
1684 if ((threadLevel >= 0) && (__kmp_affinity_gran > affinity_gran_thread)) {
1685 __kmp_affinity_gran_levels++;
1687 if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1688 __kmp_affinity_gran_levels++;
1690 if (__kmp_affinity_gran > affinity_gran_package) {
1691 __kmp_affinity_gran_levels++;
1695 if (__kmp_affinity_verbose) {
1696 __kmp_affinity_print_topology(retval, nApics, depth, pkgLevel,
1697 coreLevel, threadLevel);
1704 KMP_CPU_FREE(oldMask);
1705 *address2os = retval;
1714 #define threadIdIndex 1
1715 #define coreIdIndex 2
1716 #define pkgIdIndex 3
1717 #define nodeIdIndex 4
1719 typedef unsigned *ProcCpuInfo;
1720 static unsigned maxIndex = pkgIdIndex;
1724 __kmp_affinity_cmp_ProcCpuInfo_os_id(
const void *a,
const void *b)
1726 const unsigned *aa = (
const unsigned *)a;
1727 const unsigned *bb = (
const unsigned *)b;
1728 if (aa[osIdIndex] < bb[osIdIndex])
return -1;
1729 if (aa[osIdIndex] > bb[osIdIndex])
return 1;
1735 __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
const void *b)
1738 const unsigned *aa = *((
const unsigned **)a);
1739 const unsigned *bb = *((
const unsigned **)b);
1740 for (i = maxIndex; ; i--) {
1741 if (aa[i] < bb[i])
return -1;
1742 if (aa[i] > bb[i])
return 1;
1743 if (i == osIdIndex)
break;
1754 __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os,
int *line,
1755 kmp_i18n_id_t *
const msg_id, FILE *f)
1758 *msg_id = kmp_i18n_null;
1765 unsigned num_records = 0;
1767 buf[
sizeof(buf) - 1] = 1;
1768 if (! fgets(buf,
sizeof(buf), f)) {
1775 char s1[] =
"processor";
1776 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
1785 if (sscanf(buf,
"node_%d id", &level) == 1) {
1786 if (nodeIdIndex + level >= maxIndex) {
1787 maxIndex = nodeIdIndex + level;
1798 if (num_records == 0) {
1800 *msg_id = kmp_i18n_str_NoProcRecords;
1803 if (num_records > (
unsigned)__kmp_xproc) {
1805 *msg_id = kmp_i18n_str_TooManyProcRecords;
1816 if (fseek(f, 0, SEEK_SET) != 0) {
1818 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
1826 unsigned **threadInfo = (
unsigned **)__kmp_allocate((num_records + 1)
1827 *
sizeof(
unsigned *));
1829 for (i = 0; i <= num_records; i++) {
1830 threadInfo[i] = (
unsigned *)__kmp_allocate((maxIndex + 1)
1831 *
sizeof(unsigned));
1834 #define CLEANUP_THREAD_INFO \
1835 for (i = 0; i <= num_records; i++) { \
1836 __kmp_free(threadInfo[i]); \
1838 __kmp_free(threadInfo);
1845 #define INIT_PROC_INFO(p) \
1846 for (__index = 0; __index <= maxIndex; __index++) { \
1847 (p)[__index] = UINT_MAX; \
1850 for (i = 0; i <= num_records; i++) {
1851 INIT_PROC_INFO(threadInfo[i]);
1854 unsigned num_avail = 0;
1864 buf[
sizeof(buf) - 1] = 1;
1865 bool long_line =
false;
1866 if (! fgets(buf,
sizeof(buf), f)) {
1874 for (i = 0; i <= maxIndex; i++) {
1875 if (threadInfo[num_avail][i] != UINT_MAX) {
1883 }
else if (!buf[
sizeof(buf) - 1]) {
1890 #define CHECK_LINE \
1892 CLEANUP_THREAD_INFO; \
1893 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
1899 char s1[] =
"processor";
1900 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
1902 char *p = strchr(buf +
sizeof(s1) - 1,
':');
1904 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1905 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
goto dup_field;
1906 threadInfo[num_avail][osIdIndex] = val;
1909 char s2[] =
"physical id";
1910 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
1912 char *p = strchr(buf +
sizeof(s2) - 1,
':');
1914 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1915 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
goto dup_field;
1916 threadInfo[num_avail][pkgIdIndex] = val;
1919 char s3[] =
"core id";
1920 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
1922 char *p = strchr(buf +
sizeof(s3) - 1,
':');
1924 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1925 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
goto dup_field;
1926 threadInfo[num_avail][coreIdIndex] = val;
1929 char s4[] =
"thread id";
1930 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
1932 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1934 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1935 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
goto dup_field;
1936 threadInfo[num_avail][threadIdIndex] = val;
1940 if (sscanf(buf,
"node_%d id", &level) == 1) {
1942 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1944 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1945 KMP_ASSERT(nodeIdIndex + level <= maxIndex);
1946 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
goto dup_field;
1947 threadInfo[num_avail][nodeIdIndex + level] = val;
1956 if ((*buf != 0) && (*buf !=
'\n')) {
1963 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'));
1972 if (num_avail == __kmp_xproc) {
1973 CLEANUP_THREAD_INFO;
1974 *msg_id = kmp_i18n_str_TooManyEntries;
1982 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
1983 CLEANUP_THREAD_INFO;
1984 *msg_id = kmp_i18n_str_MissingProcField;
1987 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
1988 CLEANUP_THREAD_INFO;
1989 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
1996 if (! KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex], fullMask)) {
1997 INIT_PROC_INFO(threadInfo[num_avail]);
2006 KMP_ASSERT(num_avail <= num_records);
2007 INIT_PROC_INFO(threadInfo[num_avail]);
2012 CLEANUP_THREAD_INFO;
2013 *msg_id = kmp_i18n_str_MissingValCpuinfo;
2017 CLEANUP_THREAD_INFO;
2018 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
2023 # if KMP_MIC && REDUCE_TEAM_SIZE
2024 unsigned teamSize = 0;
2025 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2039 KMP_ASSERT(num_avail > 0);
2040 KMP_ASSERT(num_avail <= num_records);
2041 if (num_avail == 1) {
2043 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2044 __kmp_ht_enabled = FALSE;
2045 if (__kmp_affinity_verbose) {
2046 if (! KMP_AFFINITY_CAPABLE()) {
2047 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2048 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2049 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2052 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2053 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
2055 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2056 if (__kmp_affinity_respect_mask) {
2057 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2059 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2061 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2062 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2066 __kmp_str_buf_init(&buf);
2067 __kmp_str_buf_print(&buf,
"1");
2068 for (index = maxIndex - 1; index > pkgIdIndex; index--) {
2069 __kmp_str_buf_print(&buf,
" x 1");
2071 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, 1, 1, 1);
2072 __kmp_str_buf_free(&buf);
2075 if (__kmp_affinity_type == affinity_none) {
2076 CLEANUP_THREAD_INFO;
2080 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair));
2082 addr.labels[0] = threadInfo[0][pkgIdIndex];
2083 (*address2os)[0] = AddrUnsPair(addr, threadInfo[0][osIdIndex]);
2085 if (__kmp_affinity_gran_levels < 0) {
2086 __kmp_affinity_gran_levels = 0;
2089 if (__kmp_affinity_verbose) {
2090 __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
2093 CLEANUP_THREAD_INFO;
2100 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
2101 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
2114 unsigned *counts = (
unsigned *)__kmp_allocate((maxIndex + 1)
2115 *
sizeof(unsigned));
2116 unsigned *maxCt = (
unsigned *)__kmp_allocate((maxIndex + 1)
2117 *
sizeof(unsigned));
2118 unsigned *totals = (
unsigned *)__kmp_allocate((maxIndex + 1)
2119 *
sizeof(unsigned));
2120 unsigned *lastId = (
unsigned *)__kmp_allocate((maxIndex + 1)
2121 *
sizeof(unsigned));
2123 bool assign_thread_ids =
false;
2124 unsigned threadIdCt;
2127 restart_radix_check:
2133 if (assign_thread_ids) {
2134 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
2135 threadInfo[0][threadIdIndex] = threadIdCt++;
2137 else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
2138 threadIdCt = threadInfo[0][threadIdIndex] + 1;
2141 for (index = 0; index <= maxIndex; index++) {
2145 lastId[index] = threadInfo[0][index];;
2151 for (i = 1; i < num_avail; i++) {
2156 for (index = maxIndex; index >= threadIdIndex; index--) {
2157 if (assign_thread_ids && (index == threadIdIndex)) {
2161 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2162 threadInfo[i][threadIdIndex] = threadIdCt++;
2170 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2171 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2174 if (threadInfo[i][index] != lastId[index]) {
2183 for (index2 = threadIdIndex; index2 < index; index2++) {
2185 if (counts[index2] > maxCt[index2]) {
2186 maxCt[index2] = counts[index2];
2189 lastId[index2] = threadInfo[i][index2];
2193 lastId[index] = threadInfo[i][index];
2195 if (assign_thread_ids && (index > threadIdIndex)) {
2197 # if KMP_MIC && REDUCE_TEAM_SIZE
2202 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2203 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2213 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2214 threadInfo[i][threadIdIndex] = threadIdCt++;
2222 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2223 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2229 if (index < threadIdIndex) {
2235 if ((threadInfo[i][threadIdIndex] != UINT_MAX)
2236 || assign_thread_ids) {
2241 CLEANUP_THREAD_INFO;
2242 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2251 assign_thread_ids =
true;
2252 goto restart_radix_check;
2256 # if KMP_MIC && REDUCE_TEAM_SIZE
2261 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2262 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2264 for (index = threadIdIndex; index <= maxIndex; index++) {
2265 if (counts[index] > maxCt[index]) {
2266 maxCt[index] = counts[index];
2270 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
2271 nCoresPerPkg = maxCt[coreIdIndex];
2272 nPackages = totals[pkgIdIndex];
2277 unsigned prod = totals[maxIndex];
2278 for (index = threadIdIndex; index < maxIndex; index++) {
2279 prod *= maxCt[index];
2281 bool uniform = (prod == totals[threadIdIndex]);
2289 __kmp_ht_enabled = (maxCt[threadIdIndex] > 1);
2290 __kmp_ncores = totals[coreIdIndex];
2292 if (__kmp_affinity_verbose) {
2293 if (! KMP_AFFINITY_CAPABLE()) {
2294 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2295 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2297 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2299 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2303 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2304 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
2305 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2306 if (__kmp_affinity_respect_mask) {
2307 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2309 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2311 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2313 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2315 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2319 __kmp_str_buf_init(&buf);
2321 __kmp_str_buf_print(&buf,
"%d", totals[maxIndex]);
2322 for (index = maxIndex - 1; index >= pkgIdIndex; index--) {
2323 __kmp_str_buf_print(&buf,
" x %d", maxCt[index]);
2325 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, maxCt[coreIdIndex],
2326 maxCt[threadIdIndex], __kmp_ncores);
2328 __kmp_str_buf_free(&buf);
2331 # if KMP_MIC && REDUCE_TEAM_SIZE
2335 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
2336 __kmp_dflt_team_nth = teamSize;
2337 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting __kmp_dflt_team_nth = %d\n",
2338 __kmp_dflt_team_nth));
2340 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2342 if (__kmp_affinity_type == affinity_none) {
2347 CLEANUP_THREAD_INFO;
2358 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
2360 for (index = threadIdIndex; index < maxIndex; index++) {
2361 KMP_ASSERT(totals[index] >= totals[index + 1]);
2362 inMap[index] = (totals[index] > totals[index + 1]);
2364 inMap[maxIndex] = (totals[maxIndex] > 1);
2365 inMap[pkgIdIndex] =
true;
2368 for (index = threadIdIndex; index <= maxIndex; index++) {
2373 KMP_ASSERT(depth > 0);
2378 *address2os = (AddrUnsPair*)
2379 __kmp_allocate(
sizeof(AddrUnsPair) * num_avail);
2382 int threadLevel = -1;
2384 for (i = 0; i < num_avail; ++i) {
2385 Address addr(depth);
2386 unsigned os = threadInfo[i][osIdIndex];
2390 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
2391 if (! inMap[src_index]) {
2394 addr.labels[dst_index] = threadInfo[i][src_index];
2395 if (src_index == pkgIdIndex) {
2396 pkgLevel = dst_index;
2398 else if (src_index == coreIdIndex) {
2399 coreLevel = dst_index;
2401 else if (src_index == threadIdIndex) {
2402 threadLevel = dst_index;
2406 (*address2os)[i] = AddrUnsPair(addr, os);
2409 if (__kmp_affinity_gran_levels < 0) {
2415 __kmp_affinity_gran_levels = 0;
2416 for (src_index = threadIdIndex; src_index <= maxIndex; src_index++) {
2417 if (! inMap[src_index]) {
2420 switch (src_index) {
2422 if (__kmp_affinity_gran > affinity_gran_thread) {
2423 __kmp_affinity_gran_levels++;
2428 if (__kmp_affinity_gran > affinity_gran_core) {
2429 __kmp_affinity_gran_levels++;
2434 if (__kmp_affinity_gran > affinity_gran_package) {
2435 __kmp_affinity_gran_levels++;
2442 if (__kmp_affinity_verbose) {
2443 __kmp_affinity_print_topology(*address2os, num_avail, depth, pkgLevel,
2444 coreLevel, threadLevel);
2452 CLEANUP_THREAD_INFO;
2462 static kmp_affin_mask_t *
2463 __kmp_create_masks(
unsigned *maxIndex,
unsigned *numUnique,
2464 AddrUnsPair *address2os,
unsigned numAddrs)
2473 KMP_ASSERT(numAddrs > 0);
2474 depth = address2os[0].first.depth;
2477 for (i = 0; i < numAddrs; i++) {
2478 unsigned osId = address2os[i].second;
2479 if (osId > maxOsId) {
2483 kmp_affin_mask_t *osId2Mask = (kmp_affin_mask_t *)__kmp_allocate(
2484 (maxOsId + 1) * __kmp_affin_mask_size);
2491 qsort(address2os, numAddrs,
sizeof(*address2os),
2492 __kmp_affinity_cmp_Address_labels);
2494 KMP_ASSERT(__kmp_affinity_gran_levels >= 0);
2495 if (__kmp_affinity_verbose && (__kmp_affinity_gran_levels > 0)) {
2496 KMP_INFORM(ThreadsMigrate,
"KMP_AFFINITY", __kmp_affinity_gran_levels);
2498 if (__kmp_affinity_gran_levels >= (
int)depth) {
2499 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2500 && (__kmp_affinity_type != affinity_none))) {
2501 KMP_WARNING(AffThreadsMayMigrate);
2511 unsigned unique = 0;
2513 unsigned leader = 0;
2514 Address *leaderAddr = &(address2os[0].first);
2515 kmp_affin_mask_t *sum
2516 = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
2518 KMP_CPU_SET(address2os[0].second, sum);
2519 for (i = 1; i < numAddrs; i++) {
2525 if (leaderAddr->isClose(address2os[i].first,
2526 __kmp_affinity_gran_levels)) {
2527 KMP_CPU_SET(address2os[i].second, sum);
2536 for (; j < i; j++) {
2537 unsigned osId = address2os[j].second;
2538 KMP_DEBUG_ASSERT(osId <= maxOsId);
2539 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2540 KMP_CPU_COPY(mask, sum);
2541 address2os[j].first.leader = (j == leader);
2549 leaderAddr = &(address2os[i].first);
2551 KMP_CPU_SET(address2os[i].second, sum);
2558 for (; j < i; j++) {
2559 unsigned osId = address2os[j].second;
2560 KMP_DEBUG_ASSERT(osId <= maxOsId);
2561 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2562 KMP_CPU_COPY(mask, sum);
2563 address2os[j].first.leader = (j == leader);
2567 *maxIndex = maxOsId;
2568 *numUnique = unique;
2578 static kmp_affin_mask_t *newMasks;
2579 static int numNewMasks;
2580 static int nextNewMask;
2582 #define ADD_MASK(_mask) \
2584 if (nextNewMask >= numNewMasks) { \
2586 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_REALLOC(newMasks, \
2587 numNewMasks * __kmp_affin_mask_size); \
2589 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
2593 #define ADD_MASK_OSID(_osId,_osId2Mask,_maxOsId) \
2595 if (((_osId) > _maxOsId) || \
2596 (! KMP_CPU_ISSET((_osId), KMP_CPU_INDEX(_osId2Mask, (_osId))))) {\
2597 if (__kmp_affinity_verbose || (__kmp_affinity_warnings \
2598 && (__kmp_affinity_type != affinity_none))) { \
2599 KMP_WARNING(AffIgnoreInvalidProcID, _osId); \
2603 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
2613 __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
2614 unsigned int *out_numMasks,
const char *proclist,
2615 kmp_affin_mask_t *osId2Mask,
int maxOsId)
2617 const char *scan = proclist;
2618 const char *next = proclist;
2625 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
2626 * __kmp_affin_mask_size);
2628 kmp_affin_mask_t *sumMask = (kmp_affin_mask_t *)__kmp_allocate(
2629 __kmp_affin_mask_size);
2633 int start, end, stride;
2637 if (*next ==
'\0') {
2651 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2654 num = __kmp_str_to_int(scan, *next);
2655 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2660 if ((num > maxOsId) ||
2661 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2662 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2663 && (__kmp_affinity_type != affinity_none))) {
2664 KMP_WARNING(AffIgnoreInvalidProcID, num);
2666 KMP_CPU_ZERO(sumMask);
2669 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2695 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2696 "bad explicit proc list");
2699 num = __kmp_str_to_int(scan, *next);
2700 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2705 if ((num > maxOsId) ||
2706 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2707 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2708 && (__kmp_affinity_type != affinity_none))) {
2709 KMP_WARNING(AffIgnoreInvalidProcID, num);
2713 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2732 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2734 start = __kmp_str_to_int(scan, *next);
2735 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
2742 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2760 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2762 end = __kmp_str_to_int(scan, *next);
2763 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
2784 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2785 "bad explicit proc list");
2787 stride = __kmp_str_to_int(scan, *next);
2788 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
2795 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
2797 KMP_ASSERT2(start <= end,
"bad explicit proc list");
2800 KMP_ASSERT2(start >= end,
"bad explicit proc list");
2802 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
2809 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2811 }
while (start <= end);
2815 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2817 }
while (start >= end);
2830 *out_numMasks = nextNewMask;
2831 if (nextNewMask == 0) {
2833 KMP_INTERNAL_FREE(newMasks);
2837 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
2838 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
2839 __kmp_free(sumMask);
2840 KMP_INTERNAL_FREE(newMasks);
2870 __kmp_process_subplace_list(
const char **scan, kmp_affin_mask_t *osId2Mask,
2871 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
2876 int start, count, stride, i;
2882 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2883 "bad explicit places list");
2886 start = __kmp_str_to_int(*scan, *next);
2887 KMP_ASSERT(start >= 0);
2894 if (**scan ==
'}' || **scan ==
',') {
2895 if ((start > maxOsId) ||
2896 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2897 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2898 && (__kmp_affinity_type != affinity_none))) {
2899 KMP_WARNING(AffIgnoreInvalidProcID, start);
2903 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2906 if (**scan ==
'}') {
2912 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2919 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2920 "bad explicit places list");
2923 count = __kmp_str_to_int(*scan, *next);
2924 KMP_ASSERT(count >= 0);
2931 if (**scan ==
'}' || **scan ==
',') {
2932 for (i = 0; i < count; i++) {
2933 if ((start > maxOsId) ||
2934 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2935 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2936 && (__kmp_affinity_type != affinity_none))) {
2937 KMP_WARNING(AffIgnoreInvalidProcID, start);
2942 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2947 if (**scan ==
'}') {
2953 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2962 if (**scan ==
'+') {
2966 if (**scan ==
'-') {
2974 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2975 "bad explicit places list");
2978 stride = __kmp_str_to_int(*scan, *next);
2979 KMP_ASSERT(stride >= 0);
2987 if (**scan ==
'}' || **scan ==
',') {
2988 for (i = 0; i < count; i++) {
2989 if ((start > maxOsId) ||
2990 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2991 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2992 && (__kmp_affinity_type != affinity_none))) {
2993 KMP_WARNING(AffIgnoreInvalidProcID, start);
2998 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3003 if (**scan ==
'}') {
3010 KMP_ASSERT2(0,
"bad explicit places list");
3016 __kmp_process_place(
const char **scan, kmp_affin_mask_t *osId2Mask,
3017 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
3025 if (**scan ==
'{') {
3027 __kmp_process_subplace_list(scan, osId2Mask, maxOsId , tempMask,
3029 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3032 else if (**scan ==
'!') {
3033 __kmp_process_place(scan, osId2Mask, maxOsId, tempMask, setSize);
3034 KMP_CPU_COMPLEMENT(tempMask);
3037 else if ((**scan >=
'0') && (**scan <=
'9')) {
3040 int num = __kmp_str_to_int(*scan, *next);
3041 KMP_ASSERT(num >= 0);
3042 if ((num > maxOsId) ||
3043 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3044 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3045 && (__kmp_affinity_type != affinity_none))) {
3046 KMP_WARNING(AffIgnoreInvalidProcID, num);
3050 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3056 KMP_ASSERT2(0,
"bad explicit places list");
3062 __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
3063 unsigned int *out_numMasks,
const char *placelist,
3064 kmp_affin_mask_t *osId2Mask,
int maxOsId)
3066 const char *scan = placelist;
3067 const char *next = placelist;
3070 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
3071 * __kmp_affin_mask_size);
3074 kmp_affin_mask_t *tempMask = (kmp_affin_mask_t *)__kmp_allocate(
3075 __kmp_affin_mask_size);
3076 KMP_CPU_ZERO(tempMask);
3080 int start, count, stride;
3082 __kmp_process_place(&scan, osId2Mask, maxOsId, tempMask, &setSize);
3088 if (*scan ==
'\0' || *scan ==
',') {
3092 KMP_CPU_ZERO(tempMask);
3094 if (*scan ==
'\0') {
3101 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3108 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3109 "bad explicit places list");
3112 count = __kmp_str_to_int(scan, *next);
3113 KMP_ASSERT(count >= 0);
3120 if (*scan ==
'\0' || *scan ==
',') {
3122 for (i = 0; i < count; i++) {
3129 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j > 0; j--) {
3134 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3135 KMP_CPU_SET(j, tempMask);
3139 KMP_CPU_CLR(j, tempMask);
3142 for (; j >= 0; j--) {
3143 KMP_CPU_CLR(j, tempMask);
3146 KMP_CPU_ZERO(tempMask);
3149 if (*scan ==
'\0') {
3156 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3177 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3178 "bad explicit places list");
3181 stride = __kmp_str_to_int(scan, *next);
3182 KMP_DEBUG_ASSERT(stride >= 0);
3188 for (i = 0; i < count; i++) {
3195 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j >= stride; j--) {
3196 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3197 KMP_CPU_SET(j, tempMask);
3201 KMP_CPU_CLR(j, tempMask);
3204 for (; j >= 0; j--) {
3205 KMP_CPU_CLR(j, tempMask);
3211 for (i = 0; i < count; i++) {
3218 for (j = 0; j < (__kmp_affin_mask_size * CHAR_BIT) + stride;
3220 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3221 KMP_CPU_SET(j, tempMask);
3225 KMP_CPU_CLR(j, tempMask);
3228 for (; j < __kmp_affin_mask_size * CHAR_BIT; j++) {
3229 KMP_CPU_CLR(j, tempMask);
3233 KMP_CPU_ZERO(tempMask);
3240 if (*scan ==
'\0') {
3248 KMP_ASSERT2(0,
"bad explicit places list");
3251 *out_numMasks = nextNewMask;
3252 if (nextNewMask == 0) {
3254 KMP_INTERNAL_FREE(newMasks);
3258 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
3259 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
3260 __kmp_free(tempMask);
3261 KMP_INTERNAL_FREE(newMasks);
3267 #undef ADD_MASK_OSID
3273 __kmp_apply_thread_places(AddrUnsPair **pAddr,
int depth)
3275 if ( __kmp_place_num_cores == 0 ) {
3276 if ( __kmp_place_num_threads_per_core == 0 ) {
3279 __kmp_place_num_cores = nCoresPerPkg;
3281 if ( !__kmp_affinity_uniform_topology() || depth != 3 ) {
3282 KMP_WARNING( AffThrPlaceUnsupported );
3285 if ( __kmp_place_num_threads_per_core == 0 ) {
3286 __kmp_place_num_threads_per_core = __kmp_nThreadsPerCore;
3288 if ( __kmp_place_core_offset + __kmp_place_num_cores > nCoresPerPkg ) {
3289 KMP_WARNING( AffThrPlaceManyCores );
3293 AddrUnsPair *newAddr = (AddrUnsPair *)__kmp_allocate(
sizeof(AddrUnsPair) *
3294 nPackages * __kmp_place_num_cores * __kmp_place_num_threads_per_core);
3295 int i, j, k, n_old = 0, n_new = 0;
3296 for ( i = 0; i < nPackages; ++i ) {
3297 for ( j = 0; j < nCoresPerPkg; ++j ) {
3298 if ( j < __kmp_place_core_offset || j >= __kmp_place_core_offset + __kmp_place_num_cores ) {
3299 n_old += __kmp_nThreadsPerCore;
3301 for ( k = 0; k < __kmp_nThreadsPerCore; ++k ) {
3302 if ( k < __kmp_place_num_threads_per_core ) {
3303 newAddr[n_new] = (*pAddr)[n_old];
3311 nCoresPerPkg = __kmp_place_num_cores;
3312 __kmp_nThreadsPerCore = __kmp_place_num_threads_per_core;
3313 __kmp_avail_proc = n_new;
3314 __kmp_ncores = nPackages * __kmp_place_num_cores;
3316 __kmp_free( *pAddr );
3323 static AddrUnsPair *address2os = NULL;
3324 static int * procarr = NULL;
3325 static int __kmp_aff_depth = 0;
3328 __kmp_aux_affinity_initialize(
void)
3330 if (__kmp_affinity_masks != NULL) {
3331 KMP_ASSERT(fullMask != NULL);
3341 if (fullMask == NULL) {
3342 fullMask = (kmp_affin_mask_t *)__kmp_allocate(__kmp_affin_mask_size);
3344 if (KMP_AFFINITY_CAPABLE()) {
3345 if (__kmp_affinity_respect_mask) {
3346 __kmp_get_system_affinity(fullMask, TRUE);
3352 __kmp_avail_proc = 0;
3353 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
3354 if (! KMP_CPU_ISSET(i, fullMask)) {
3359 if (__kmp_avail_proc > __kmp_xproc) {
3360 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3361 && (__kmp_affinity_type != affinity_none))) {
3362 KMP_WARNING(ErrorInitializeAffinity);
3364 __kmp_affinity_type = affinity_none;
3365 __kmp_affin_mask_size = 0;
3370 __kmp_affinity_entire_machine_mask(fullMask);
3371 __kmp_avail_proc = __kmp_xproc;
3376 kmp_i18n_id_t msg_id = kmp_i18n_null;
3382 if ((__kmp_cpuinfo_file != NULL) &&
3383 (__kmp_affinity_top_method == affinity_top_method_all)) {
3384 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3387 if (__kmp_affinity_top_method == affinity_top_method_all) {
3393 const char *file_name = NULL;
3396 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3398 if (__kmp_affinity_verbose) {
3399 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
3403 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3405 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3406 KMP_ASSERT(address2os == NULL);
3411 if ((msg_id != kmp_i18n_null)
3412 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3413 && (__kmp_affinity_type != affinity_none)))) {
3415 if (__kmp_affinity_verbose) {
3416 KMP_INFORM(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3417 KMP_I18N_STR(DecodingLegacyAPIC));
3420 KMP_WARNING(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3421 KMP_I18N_STR(DecodingLegacyAPIC));
3426 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3428 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3429 KMP_ASSERT(address2os == NULL);
3439 if ((msg_id != kmp_i18n_null)
3440 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3441 && (__kmp_affinity_type != affinity_none)))) {
3443 if (__kmp_affinity_verbose) {
3444 KMP_INFORM(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3447 KMP_WARNING(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3450 else if (__kmp_affinity_verbose) {
3451 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"/proc/cpuinfo");
3454 FILE *f = fopen(
"/proc/cpuinfo",
"r");
3456 msg_id = kmp_i18n_str_CantOpenCpuinfo;
3459 file_name =
"/proc/cpuinfo";
3460 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3463 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3464 KMP_ASSERT(address2os == NULL);
3473 if (msg_id != kmp_i18n_null
3474 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3475 && (__kmp_affinity_type != affinity_none)))) {
3476 if (file_name == NULL) {
3477 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3479 else if (line == 0) {
3480 KMP_WARNING(UsingFlatOSFile, file_name, __kmp_i18n_catgets(msg_id));
3483 KMP_WARNING(UsingFlatOSFileLine, file_name, line, __kmp_i18n_catgets(msg_id));
3488 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3490 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3491 KMP_ASSERT(address2os == NULL);
3494 KMP_ASSERT(depth > 0);
3495 KMP_ASSERT(address2os != NULL);
3505 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3507 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
3508 if (__kmp_affinity_verbose) {
3509 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3510 KMP_I18N_STR(Decodingx2APIC));
3513 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3515 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3516 KMP_ASSERT(address2os == NULL);
3521 KMP_ASSERT(msg_id != kmp_i18n_null);
3522 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3525 else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
3526 if (__kmp_affinity_verbose) {
3527 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3528 KMP_I18N_STR(DecodingLegacyAPIC));
3531 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3533 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3534 KMP_ASSERT(address2os == NULL);
3539 KMP_ASSERT(msg_id != kmp_i18n_null);
3540 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3546 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
3547 const char *filename;
3548 if (__kmp_cpuinfo_file != NULL) {
3549 filename = __kmp_cpuinfo_file;
3552 filename =
"/proc/cpuinfo";
3555 if (__kmp_affinity_verbose) {
3556 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
3559 FILE *f = fopen(filename,
"r");
3562 if (__kmp_cpuinfo_file != NULL) {
3565 KMP_MSG(CantOpenFileForReading, filename),
3567 KMP_HNT(NameComesFrom_CPUINFO_FILE),
3574 KMP_MSG(CantOpenFileForReading, filename),
3581 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3584 KMP_ASSERT(msg_id != kmp_i18n_null);
3586 KMP_FATAL(FileLineMsgExiting, filename, line, __kmp_i18n_catgets(msg_id));
3589 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
3592 if (__kmp_affinity_type == affinity_none) {
3593 KMP_ASSERT(depth == 0);
3594 KMP_ASSERT(address2os == NULL);
3599 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3601 else if (__kmp_affinity_top_method == affinity_top_method_group) {
3602 if (__kmp_affinity_verbose) {
3603 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
3606 depth = __kmp_affinity_create_proc_group_map(&address2os, &msg_id);
3607 KMP_ASSERT(depth != 0);
3610 if ((msg_id != kmp_i18n_null)
3611 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3612 && (__kmp_affinity_type != affinity_none)))) {
3613 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3616 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3618 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3619 KMP_ASSERT(address2os == NULL);
3623 KMP_ASSERT(depth > 0);
3624 KMP_ASSERT(address2os != NULL);
3630 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
3631 if (__kmp_affinity_verbose) {
3632 KMP_INFORM(AffUsingFlatOS,
"KMP_AFFINITY");
3635 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3637 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3638 KMP_ASSERT(address2os == NULL);
3642 KMP_ASSERT(depth > 0);
3643 KMP_ASSERT(address2os != NULL);
3646 if (address2os == NULL) {
3647 if (KMP_AFFINITY_CAPABLE()
3648 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3649 && (__kmp_affinity_type != affinity_none)))) {
3650 KMP_WARNING(ErrorInitializeAffinity);
3652 __kmp_affinity_type = affinity_none;
3653 __kmp_affin_mask_size = 0;
3658 __kmp_apply_thread_places(&address2os, depth);
3666 kmp_affin_mask_t *osId2Mask = __kmp_create_masks(&maxIndex, &numUnique,
3667 address2os, __kmp_avail_proc);
3668 if (__kmp_affinity_gran_levels == 0) {
3669 KMP_DEBUG_ASSERT(numUnique == __kmp_avail_proc);
3677 __kmp_affinity_assign_child_nums(address2os, __kmp_avail_proc);
3679 switch (__kmp_affinity_type) {
3681 case affinity_explicit:
3682 KMP_DEBUG_ASSERT(__kmp_affinity_proclist != NULL);
3684 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3687 __kmp_affinity_process_proclist(&__kmp_affinity_masks,
3688 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3693 __kmp_affinity_process_placelist(&__kmp_affinity_masks,
3694 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3698 if (__kmp_affinity_num_masks == 0) {
3699 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3700 && (__kmp_affinity_type != affinity_none))) {
3701 KMP_WARNING(AffNoValidProcID);
3703 __kmp_affinity_type = affinity_none;
3716 case affinity_logical:
3717 __kmp_affinity_compact = 0;
3718 if (__kmp_affinity_offset) {
3719 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3724 case affinity_physical:
3725 if (__kmp_nThreadsPerCore > 1) {
3726 __kmp_affinity_compact = 1;
3727 if (__kmp_affinity_compact >= depth) {
3728 __kmp_affinity_compact = 0;
3731 __kmp_affinity_compact = 0;
3733 if (__kmp_affinity_offset) {
3734 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3739 case affinity_scatter:
3740 if (__kmp_affinity_compact >= depth) {
3741 __kmp_affinity_compact = 0;
3744 __kmp_affinity_compact = depth - 1 - __kmp_affinity_compact;
3748 case affinity_compact:
3749 if (__kmp_affinity_compact >= depth) {
3750 __kmp_affinity_compact = depth - 1;
3755 case affinity_balanced:
3757 if( nPackages > 1 ) {
3758 if( __kmp_affinity_verbose || __kmp_affinity_warnings ) {
3759 KMP_WARNING( AffBalancedNotAvail,
"KMP_AFFINITY" );
3761 __kmp_affinity_type = affinity_none;
3763 }
else if( __kmp_affinity_uniform_topology() ) {
3768 __kmp_aff_depth = depth;
3771 int nth_per_core = __kmp_nThreadsPerCore;
3774 if( nth_per_core > 1 ) {
3775 core_level = depth - 2;
3777 core_level = depth - 1;
3779 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
3780 int nproc = nth_per_core * ncores;
3782 procarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
3783 for(
int i = 0; i < nproc; i++ ) {
3787 for(
int i = 0; i < __kmp_avail_proc; i++ ) {
3788 int proc = address2os[ i ].second;
3792 int level = depth - 1;
3796 int core = address2os[ i ].first.labels[ level ];
3798 if( nth_per_core > 1 ) {
3799 thread = address2os[ i ].first.labels[ level ] % nth_per_core;
3800 core = address2os[ i ].first.labels[ level - 1 ];
3802 procarr[ core * nth_per_core + thread ] = proc;
3813 if (__kmp_affinity_dups) {
3814 __kmp_affinity_num_masks = __kmp_avail_proc;
3817 __kmp_affinity_num_masks = numUnique;
3821 if ( ( __kmp_nested_proc_bind.bind_types[0] != proc_bind_intel )
3822 && ( __kmp_affinity_num_places > 0 )
3823 && ( (
unsigned)__kmp_affinity_num_places < __kmp_affinity_num_masks ) ) {
3824 __kmp_affinity_num_masks = __kmp_affinity_num_places;
3828 __kmp_affinity_masks = (kmp_affin_mask_t*)__kmp_allocate(
3829 __kmp_affinity_num_masks * __kmp_affin_mask_size);
3835 qsort(address2os, __kmp_avail_proc,
sizeof(*address2os),
3836 __kmp_affinity_cmp_Address_child_num);
3840 for (i = 0, j = 0; i < __kmp_avail_proc; i++) {
3841 if ((! __kmp_affinity_dups) && (! address2os[i].first.leader)) {
3844 unsigned osId = address2os[i].second;
3845 kmp_affin_mask_t *src = KMP_CPU_INDEX(osId2Mask, osId);
3846 kmp_affin_mask_t *dest
3847 = KMP_CPU_INDEX(__kmp_affinity_masks, j);
3848 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
3849 KMP_CPU_COPY(dest, src);
3850 if (++j >= __kmp_affinity_num_masks) {
3854 KMP_DEBUG_ASSERT(j == __kmp_affinity_num_masks);
3859 KMP_ASSERT2(0,
"Unexpected affinity setting");
3862 __kmp_free(osId2Mask);
3867 __kmp_affinity_initialize(
void)
3880 int disabled = (__kmp_affinity_type == affinity_disabled);
3881 if (! KMP_AFFINITY_CAPABLE()) {
3882 KMP_ASSERT(disabled);
3885 __kmp_affinity_type = affinity_none;
3887 __kmp_aux_affinity_initialize();
3889 __kmp_affinity_type = affinity_disabled;
3895 __kmp_affinity_uninitialize(
void)
3897 if (__kmp_affinity_masks != NULL) {
3898 __kmp_free(__kmp_affinity_masks);
3899 __kmp_affinity_masks = NULL;
3901 if (fullMask != NULL) {
3902 KMP_CPU_FREE(fullMask);
3905 __kmp_affinity_num_masks = 0;
3907 __kmp_affinity_num_places = 0;
3909 if (__kmp_affinity_proclist != NULL) {
3910 __kmp_free(__kmp_affinity_proclist);
3911 __kmp_affinity_proclist = NULL;
3913 if( address2os != NULL ) {
3914 __kmp_free( address2os );
3917 if( procarr != NULL ) {
3918 __kmp_free( procarr );
3925 __kmp_affinity_set_init_mask(
int gtid,
int isa_root)
3927 if (! KMP_AFFINITY_CAPABLE()) {
3931 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
3932 if (th->th.th_affin_mask == NULL) {
3933 KMP_CPU_ALLOC(th->th.th_affin_mask);
3936 KMP_CPU_ZERO(th->th.th_affin_mask);
3946 kmp_affin_mask_t *mask;
3950 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3953 if ((__kmp_affinity_type == affinity_none)
3955 || (__kmp_affinity_type == affinity_balanced)
3958 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3959 if (__kmp_num_proc_groups > 1) {
3963 KMP_ASSERT(fullMask != NULL);
3968 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
3969 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3970 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3976 || (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
3977 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3978 if (__kmp_num_proc_groups > 1) {
3982 KMP_ASSERT(fullMask != NULL);
3991 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
3992 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3993 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3999 th->th.th_current_place = i;
4001 th->th.th_new_place = i;
4002 th->th.th_first_place = 0;
4003 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4006 if (i == KMP_PLACE_ALL) {
4007 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4011 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4016 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to fullMask\n",
4020 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to mask %d\n",
4025 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4027 if (__kmp_affinity_verbose) {
4028 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4029 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4030 th->th.th_affin_mask);
4031 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", gtid, buf);
4040 if ( __kmp_affinity_type == affinity_none ) {
4041 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4045 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4052 __kmp_affinity_set_place(
int gtid)
4056 if (! KMP_AFFINITY_CAPABLE()) {
4060 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4062 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current place = %d)\n",
4063 gtid, th->th.th_new_place, th->th.th_current_place));
4068 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4069 KMP_DEBUG_ASSERT(th->th.th_new_place >= 0);
4070 KMP_DEBUG_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity_num_masks);
4071 if (th->th.th_first_place <= th->th.th_last_place) {
4072 KMP_DEBUG_ASSERT((th->th.th_new_place >= th->th.th_first_place)
4073 && (th->th.th_new_place <= th->th.th_last_place));
4076 KMP_DEBUG_ASSERT((th->th.th_new_place <= th->th.th_first_place)
4077 || (th->th.th_new_place >= th->th.th_last_place));
4084 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks,
4085 th->th.th_new_place);
4086 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4087 th->th.th_current_place = th->th.th_new_place;
4089 if (__kmp_affinity_verbose) {
4090 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4091 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4092 th->th.th_affin_mask);
4093 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", gtid, buf);
4095 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4102 __kmp_aux_set_affinity(
void **mask)
4108 if (! KMP_AFFINITY_CAPABLE()) {
4112 gtid = __kmp_entry_gtid();
4114 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4115 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4116 (kmp_affin_mask_t *)(*mask));
4117 __kmp_debug_printf(
"kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4121 if (__kmp_env_consistency_check) {
4122 if ((mask == NULL) || (*mask == NULL)) {
4123 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4129 for (proc = 0; proc < KMP_CPU_SETSIZE; proc++) {
4130 if (! KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4134 if (! KMP_CPU_ISSET(proc, fullMask)) {
4135 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4139 if (num_procs == 0) {
4140 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4143 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
4144 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4145 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4152 th = __kmp_threads[gtid];
4153 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4154 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4156 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4160 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4161 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4162 th->th.th_first_place = 0;
4163 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4171 __kmp_aux_get_affinity(
void **mask)
4177 if (! KMP_AFFINITY_CAPABLE()) {
4181 gtid = __kmp_entry_gtid();
4182 th = __kmp_threads[gtid];
4183 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4186 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4187 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4188 th->th.th_affin_mask);
4189 __kmp_printf(
"kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid, buf);
4192 if (__kmp_env_consistency_check) {
4193 if ((mask == NULL) || (*mask == NULL)) {
4194 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4198 # if !KMP_OS_WINDOWS
4200 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4202 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4203 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4204 (kmp_affin_mask_t *)(*mask));
4205 __kmp_printf(
"kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid, buf);
4211 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4220 __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask)
4224 if (! KMP_AFFINITY_CAPABLE()) {
4229 int gtid = __kmp_entry_gtid();
4230 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4231 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4232 (kmp_affin_mask_t *)(*mask));
4233 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in affinity mask for thread %d = %s\n",
4237 if (__kmp_env_consistency_check) {
4238 if ((mask == NULL) || (*mask == NULL)) {
4239 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4243 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4246 if (! KMP_CPU_ISSET(proc, fullMask)) {
4250 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
4256 __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask)
4260 if (! KMP_AFFINITY_CAPABLE()) {
4265 int gtid = __kmp_entry_gtid();
4266 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4267 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4268 (kmp_affin_mask_t *)(*mask));
4269 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in affinity mask for thread %d = %s\n",
4273 if (__kmp_env_consistency_check) {
4274 if ((mask == NULL) || (*mask == NULL)) {
4275 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
4279 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4282 if (! KMP_CPU_ISSET(proc, fullMask)) {
4286 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
4292 __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask)
4296 if (! KMP_AFFINITY_CAPABLE()) {
4301 int gtid = __kmp_entry_gtid();
4302 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4303 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4304 (kmp_affin_mask_t *)(*mask));
4305 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in affinity mask for thread %d = %s\n",
4309 if (__kmp_env_consistency_check) {
4310 if ((mask == NULL) || (*mask == NULL)) {
4311 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4315 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4318 if (! KMP_CPU_ISSET(proc, fullMask)) {
4322 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
4328 void __kmp_balanced_affinity(
int tid,
int nthreads )
4330 if( __kmp_affinity_uniform_topology() ) {
4334 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
4336 int ncores = __kmp_ncores;
4338 int chunk = nthreads / ncores;
4340 int big_cores = nthreads % ncores;
4342 int big_nth = ( chunk + 1 ) * big_cores;
4343 if( tid < big_nth ) {
4344 coreID = tid / (chunk + 1 );
4345 threadID = ( tid % (chunk + 1 ) ) % __kmp_nth_per_core ;
4347 coreID = ( tid - big_cores ) / chunk;
4348 threadID = ( ( tid - big_cores ) % chunk ) % __kmp_nth_per_core ;
4351 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
4352 "Illegal set affinity operation when not capable");
4354 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4358 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4359 int osID = address2os[ coreID * __kmp_nth_per_core + threadID ].second;
4360 KMP_CPU_SET( osID, mask);
4361 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4362 for(
int i = 0; i < __kmp_nth_per_core; i++ ) {
4364 osID = address2os[ coreID * __kmp_nth_per_core + i ].second;
4365 KMP_CPU_SET( osID, mask);
4368 if (__kmp_affinity_verbose) {
4369 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4370 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4371 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4373 __kmp_set_system_affinity( mask, TRUE );
4376 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4380 int nth_per_core = __kmp_nThreadsPerCore;
4382 if( nth_per_core > 1 ) {
4383 core_level = __kmp_aff_depth - 2;
4385 core_level = __kmp_aff_depth - 1;
4389 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
4392 if( nthreads == __kmp_avail_proc ) {
4393 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4394 int osID = address2os[ tid ].second;
4395 KMP_CPU_SET( osID, mask);
4396 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4397 int coreID = address2os[ tid ].first.labels[ core_level ];
4401 for(
int i = 0; i < __kmp_avail_proc; i++ ) {
4402 int osID = address2os[ i ].second;
4403 int core = address2os[ i ].first.labels[ core_level ];
4404 if( core == coreID ) {
4405 KMP_CPU_SET( osID, mask);
4407 if( cnt == nth_per_core ) {
4413 }
else if( nthreads <= __kmp_ncores ) {
4416 for(
int i = 0; i < ncores; i++ ) {
4419 for(
int j = 0; j < nth_per_core; j++ ) {
4420 if( procarr[ i * nth_per_core + j ] != - 1 ) {
4427 for(
int j = 0; j < nth_per_core; j++ ) {
4428 int osID = procarr[ i * nth_per_core + j ];
4430 KMP_CPU_SET( osID, mask );
4432 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4447 int nproc_at_core[ ncores ];
4449 int ncores_with_x_procs[ nth_per_core + 1 ];
4451 int ncores_with_x_to_max_procs[ nth_per_core + 1 ];
4453 for(
int i = 0; i <= nth_per_core; i++ ) {
4454 ncores_with_x_procs[ i ] = 0;
4455 ncores_with_x_to_max_procs[ i ] = 0;
4458 for(
int i = 0; i < ncores; i++ ) {
4460 for(
int j = 0; j < nth_per_core; j++ ) {
4461 if( procarr[ i * nth_per_core + j ] != -1 ) {
4465 nproc_at_core[ i ] = cnt;
4466 ncores_with_x_procs[ cnt ]++;
4469 for(
int i = 0; i <= nth_per_core; i++ ) {
4470 for(
int j = i; j <= nth_per_core; j++ ) {
4471 ncores_with_x_to_max_procs[ i ] += ncores_with_x_procs[ j ];
4476 int nproc = nth_per_core * ncores;
4478 int * newarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
4479 for(
int i = 0; i < nproc; i++ ) {
4486 for(
int j = 1; j <= nth_per_core; j++ ) {
4487 int cnt = ncores_with_x_to_max_procs[ j ];
4488 for(
int i = 0; i < ncores; i++ ) {
4490 if( nproc_at_core[ i ] == 0 ) {
4493 for(
int k = 0; k < nth_per_core; k++ ) {
4494 if( procarr[ i * nth_per_core + k ] != -1 ) {
4495 if( newarr[ i * nth_per_core + k ] == 0 ) {
4496 newarr[ i * nth_per_core + k ] = 1;
4502 newarr[ i * nth_per_core + k ] ++;
4510 if( cnt == 0 || nth == 0 ) {
4521 for(
int i = 0; i < nproc; i++ ) {
4525 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4526 int osID = procarr[ i ];
4527 KMP_CPU_SET( osID, mask);
4528 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4529 int coreID = i / nth_per_core;
4530 for(
int ii = 0; ii < nth_per_core; ii++ ) {
4531 int osID = procarr[ coreID * nth_per_core + ii ];
4533 KMP_CPU_SET( osID, mask);
4540 __kmp_free( newarr );
4543 if (__kmp_affinity_verbose) {
4544 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4545 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4546 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4548 __kmp_set_system_affinity( mask, TRUE );
4557 #error "Unknown or unsupported OS"
4558 #endif // KMP_OS_WINDOWS || KMP_OS_LINUX