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;
1907 #if KMP_OS_LINUX && USE_SYSFS_INFO
1909 snprintf(path,
sizeof(path),
1910 "/sys/devices/system/cpu/cpu%u/topology/physical_package_id",
1911 threadInfo[num_avail][osIdIndex]);
1912 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][pkgIdIndex]);
1914 snprintf(path,
sizeof(path),
1915 "/sys/devices/system/cpu/cpu%u/topology/core_id",
1916 threadInfo[num_avail][osIdIndex]);
1917 __kmp_read_from_file(path,
"%u", &threadInfo[num_avail][coreIdIndex]);
1921 char s2[] =
"physical id";
1922 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
1924 char *p = strchr(buf +
sizeof(s2) - 1,
':');
1926 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1927 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
goto dup_field;
1928 threadInfo[num_avail][pkgIdIndex] = val;
1931 char s3[] =
"core id";
1932 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
1934 char *p = strchr(buf +
sizeof(s3) - 1,
':');
1936 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1937 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
goto dup_field;
1938 threadInfo[num_avail][coreIdIndex] = val;
1940 #endif // KMP_OS_LINUX && USE_SYSFS_INFO
1942 char s4[] =
"thread id";
1943 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
1945 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1947 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1948 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
goto dup_field;
1949 threadInfo[num_avail][threadIdIndex] = val;
1953 if (sscanf(buf,
"node_%d id", &level) == 1) {
1955 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1957 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1958 KMP_ASSERT(nodeIdIndex + level <= maxIndex);
1959 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
goto dup_field;
1960 threadInfo[num_avail][nodeIdIndex + level] = val;
1969 if ((*buf != 0) && (*buf !=
'\n')) {
1976 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'));
1985 if (num_avail == __kmp_xproc) {
1986 CLEANUP_THREAD_INFO;
1987 *msg_id = kmp_i18n_str_TooManyEntries;
1995 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
1996 CLEANUP_THREAD_INFO;
1997 *msg_id = kmp_i18n_str_MissingProcField;
2000 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
2001 CLEANUP_THREAD_INFO;
2002 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
2009 if (! KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex], fullMask)) {
2010 INIT_PROC_INFO(threadInfo[num_avail]);
2019 KMP_ASSERT(num_avail <= num_records);
2020 INIT_PROC_INFO(threadInfo[num_avail]);
2025 CLEANUP_THREAD_INFO;
2026 *msg_id = kmp_i18n_str_MissingValCpuinfo;
2030 CLEANUP_THREAD_INFO;
2031 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
2036 # if KMP_MIC && REDUCE_TEAM_SIZE
2037 unsigned teamSize = 0;
2038 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2052 KMP_ASSERT(num_avail > 0);
2053 KMP_ASSERT(num_avail <= num_records);
2054 if (num_avail == 1) {
2056 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2057 __kmp_ht_enabled = FALSE;
2058 if (__kmp_affinity_verbose) {
2059 if (! KMP_AFFINITY_CAPABLE()) {
2060 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2061 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2062 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2065 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2066 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
2068 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2069 if (__kmp_affinity_respect_mask) {
2070 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2072 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2074 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2075 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2079 __kmp_str_buf_init(&buf);
2080 __kmp_str_buf_print(&buf,
"1");
2081 for (index = maxIndex - 1; index > pkgIdIndex; index--) {
2082 __kmp_str_buf_print(&buf,
" x 1");
2084 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, 1, 1, 1);
2085 __kmp_str_buf_free(&buf);
2088 if (__kmp_affinity_type == affinity_none) {
2089 CLEANUP_THREAD_INFO;
2093 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair));
2095 addr.labels[0] = threadInfo[0][pkgIdIndex];
2096 (*address2os)[0] = AddrUnsPair(addr, threadInfo[0][osIdIndex]);
2098 if (__kmp_affinity_gran_levels < 0) {
2099 __kmp_affinity_gran_levels = 0;
2102 if (__kmp_affinity_verbose) {
2103 __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
2106 CLEANUP_THREAD_INFO;
2113 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
2114 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
2127 unsigned *counts = (
unsigned *)__kmp_allocate((maxIndex + 1)
2128 *
sizeof(unsigned));
2129 unsigned *maxCt = (
unsigned *)__kmp_allocate((maxIndex + 1)
2130 *
sizeof(unsigned));
2131 unsigned *totals = (
unsigned *)__kmp_allocate((maxIndex + 1)
2132 *
sizeof(unsigned));
2133 unsigned *lastId = (
unsigned *)__kmp_allocate((maxIndex + 1)
2134 *
sizeof(unsigned));
2136 bool assign_thread_ids =
false;
2137 unsigned threadIdCt;
2140 restart_radix_check:
2146 if (assign_thread_ids) {
2147 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
2148 threadInfo[0][threadIdIndex] = threadIdCt++;
2150 else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
2151 threadIdCt = threadInfo[0][threadIdIndex] + 1;
2154 for (index = 0; index <= maxIndex; index++) {
2158 lastId[index] = threadInfo[0][index];;
2164 for (i = 1; i < num_avail; i++) {
2169 for (index = maxIndex; index >= threadIdIndex; index--) {
2170 if (assign_thread_ids && (index == threadIdIndex)) {
2174 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2175 threadInfo[i][threadIdIndex] = threadIdCt++;
2183 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2184 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2187 if (threadInfo[i][index] != lastId[index]) {
2196 for (index2 = threadIdIndex; index2 < index; index2++) {
2198 if (counts[index2] > maxCt[index2]) {
2199 maxCt[index2] = counts[index2];
2202 lastId[index2] = threadInfo[i][index2];
2206 lastId[index] = threadInfo[i][index];
2208 if (assign_thread_ids && (index > threadIdIndex)) {
2210 # if KMP_MIC && REDUCE_TEAM_SIZE
2215 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2216 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2226 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2227 threadInfo[i][threadIdIndex] = threadIdCt++;
2235 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2236 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2242 if (index < threadIdIndex) {
2248 if ((threadInfo[i][threadIdIndex] != UINT_MAX)
2249 || assign_thread_ids) {
2254 CLEANUP_THREAD_INFO;
2255 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2264 assign_thread_ids =
true;
2265 goto restart_radix_check;
2269 # if KMP_MIC && REDUCE_TEAM_SIZE
2274 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2275 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2277 for (index = threadIdIndex; index <= maxIndex; index++) {
2278 if (counts[index] > maxCt[index]) {
2279 maxCt[index] = counts[index];
2283 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
2284 nCoresPerPkg = maxCt[coreIdIndex];
2285 nPackages = totals[pkgIdIndex];
2290 unsigned prod = totals[maxIndex];
2291 for (index = threadIdIndex; index < maxIndex; index++) {
2292 prod *= maxCt[index];
2294 bool uniform = (prod == totals[threadIdIndex]);
2302 __kmp_ht_enabled = (maxCt[threadIdIndex] > 1);
2303 __kmp_ncores = totals[coreIdIndex];
2305 if (__kmp_affinity_verbose) {
2306 if (! KMP_AFFINITY_CAPABLE()) {
2307 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2308 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2310 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2312 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2316 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2317 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
2318 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2319 if (__kmp_affinity_respect_mask) {
2320 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2322 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2324 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2326 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2328 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2332 __kmp_str_buf_init(&buf);
2334 __kmp_str_buf_print(&buf,
"%d", totals[maxIndex]);
2335 for (index = maxIndex - 1; index >= pkgIdIndex; index--) {
2336 __kmp_str_buf_print(&buf,
" x %d", maxCt[index]);
2338 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, maxCt[coreIdIndex],
2339 maxCt[threadIdIndex], __kmp_ncores);
2341 __kmp_str_buf_free(&buf);
2344 # if KMP_MIC && REDUCE_TEAM_SIZE
2348 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
2349 __kmp_dflt_team_nth = teamSize;
2350 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting __kmp_dflt_team_nth = %d\n",
2351 __kmp_dflt_team_nth));
2353 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2355 if (__kmp_affinity_type == affinity_none) {
2360 CLEANUP_THREAD_INFO;
2371 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
2373 for (index = threadIdIndex; index < maxIndex; index++) {
2374 KMP_ASSERT(totals[index] >= totals[index + 1]);
2375 inMap[index] = (totals[index] > totals[index + 1]);
2377 inMap[maxIndex] = (totals[maxIndex] > 1);
2378 inMap[pkgIdIndex] =
true;
2381 for (index = threadIdIndex; index <= maxIndex; index++) {
2386 KMP_ASSERT(depth > 0);
2391 *address2os = (AddrUnsPair*)
2392 __kmp_allocate(
sizeof(AddrUnsPair) * num_avail);
2395 int threadLevel = -1;
2397 for (i = 0; i < num_avail; ++i) {
2398 Address addr(depth);
2399 unsigned os = threadInfo[i][osIdIndex];
2403 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
2404 if (! inMap[src_index]) {
2407 addr.labels[dst_index] = threadInfo[i][src_index];
2408 if (src_index == pkgIdIndex) {
2409 pkgLevel = dst_index;
2411 else if (src_index == coreIdIndex) {
2412 coreLevel = dst_index;
2414 else if (src_index == threadIdIndex) {
2415 threadLevel = dst_index;
2419 (*address2os)[i] = AddrUnsPair(addr, os);
2422 if (__kmp_affinity_gran_levels < 0) {
2428 __kmp_affinity_gran_levels = 0;
2429 for (src_index = threadIdIndex; src_index <= maxIndex; src_index++) {
2430 if (! inMap[src_index]) {
2433 switch (src_index) {
2435 if (__kmp_affinity_gran > affinity_gran_thread) {
2436 __kmp_affinity_gran_levels++;
2441 if (__kmp_affinity_gran > affinity_gran_core) {
2442 __kmp_affinity_gran_levels++;
2447 if (__kmp_affinity_gran > affinity_gran_package) {
2448 __kmp_affinity_gran_levels++;
2455 if (__kmp_affinity_verbose) {
2456 __kmp_affinity_print_topology(*address2os, num_avail, depth, pkgLevel,
2457 coreLevel, threadLevel);
2465 CLEANUP_THREAD_INFO;
2475 static kmp_affin_mask_t *
2476 __kmp_create_masks(
unsigned *maxIndex,
unsigned *numUnique,
2477 AddrUnsPair *address2os,
unsigned numAddrs)
2486 KMP_ASSERT(numAddrs > 0);
2487 depth = address2os[0].first.depth;
2490 for (i = 0; i < numAddrs; i++) {
2491 unsigned osId = address2os[i].second;
2492 if (osId > maxOsId) {
2496 kmp_affin_mask_t *osId2Mask = (kmp_affin_mask_t *)__kmp_allocate(
2497 (maxOsId + 1) * __kmp_affin_mask_size);
2504 qsort(address2os, numAddrs,
sizeof(*address2os),
2505 __kmp_affinity_cmp_Address_labels);
2507 KMP_ASSERT(__kmp_affinity_gran_levels >= 0);
2508 if (__kmp_affinity_verbose && (__kmp_affinity_gran_levels > 0)) {
2509 KMP_INFORM(ThreadsMigrate,
"KMP_AFFINITY", __kmp_affinity_gran_levels);
2511 if (__kmp_affinity_gran_levels >= (
int)depth) {
2512 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2513 && (__kmp_affinity_type != affinity_none))) {
2514 KMP_WARNING(AffThreadsMayMigrate);
2524 unsigned unique = 0;
2526 unsigned leader = 0;
2527 Address *leaderAddr = &(address2os[0].first);
2528 kmp_affin_mask_t *sum
2529 = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
2531 KMP_CPU_SET(address2os[0].second, sum);
2532 for (i = 1; i < numAddrs; i++) {
2538 if (leaderAddr->isClose(address2os[i].first,
2539 __kmp_affinity_gran_levels)) {
2540 KMP_CPU_SET(address2os[i].second, sum);
2549 for (; j < i; j++) {
2550 unsigned osId = address2os[j].second;
2551 KMP_DEBUG_ASSERT(osId <= maxOsId);
2552 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2553 KMP_CPU_COPY(mask, sum);
2554 address2os[j].first.leader = (j == leader);
2562 leaderAddr = &(address2os[i].first);
2564 KMP_CPU_SET(address2os[i].second, sum);
2571 for (; j < i; j++) {
2572 unsigned osId = address2os[j].second;
2573 KMP_DEBUG_ASSERT(osId <= maxOsId);
2574 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2575 KMP_CPU_COPY(mask, sum);
2576 address2os[j].first.leader = (j == leader);
2580 *maxIndex = maxOsId;
2581 *numUnique = unique;
2591 static kmp_affin_mask_t *newMasks;
2592 static int numNewMasks;
2593 static int nextNewMask;
2595 #define ADD_MASK(_mask) \
2597 if (nextNewMask >= numNewMasks) { \
2599 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_REALLOC(newMasks, \
2600 numNewMasks * __kmp_affin_mask_size); \
2602 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
2606 #define ADD_MASK_OSID(_osId,_osId2Mask,_maxOsId) \
2608 if (((_osId) > _maxOsId) || \
2609 (! KMP_CPU_ISSET((_osId), KMP_CPU_INDEX(_osId2Mask, (_osId))))) {\
2610 if (__kmp_affinity_verbose || (__kmp_affinity_warnings \
2611 && (__kmp_affinity_type != affinity_none))) { \
2612 KMP_WARNING(AffIgnoreInvalidProcID, _osId); \
2616 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
2626 __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
2627 unsigned int *out_numMasks,
const char *proclist,
2628 kmp_affin_mask_t *osId2Mask,
int maxOsId)
2630 const char *scan = proclist;
2631 const char *next = proclist;
2638 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
2639 * __kmp_affin_mask_size);
2641 kmp_affin_mask_t *sumMask = (kmp_affin_mask_t *)__kmp_allocate(
2642 __kmp_affin_mask_size);
2646 int start, end, stride;
2650 if (*next ==
'\0') {
2664 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2667 num = __kmp_str_to_int(scan, *next);
2668 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2673 if ((num > maxOsId) ||
2674 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2675 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2676 && (__kmp_affinity_type != affinity_none))) {
2677 KMP_WARNING(AffIgnoreInvalidProcID, num);
2679 KMP_CPU_ZERO(sumMask);
2682 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2708 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2709 "bad explicit proc list");
2712 num = __kmp_str_to_int(scan, *next);
2713 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2718 if ((num > maxOsId) ||
2719 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2720 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2721 && (__kmp_affinity_type != affinity_none))) {
2722 KMP_WARNING(AffIgnoreInvalidProcID, num);
2726 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2745 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2747 start = __kmp_str_to_int(scan, *next);
2748 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
2755 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2773 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2775 end = __kmp_str_to_int(scan, *next);
2776 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
2797 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2798 "bad explicit proc list");
2800 stride = __kmp_str_to_int(scan, *next);
2801 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
2808 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
2810 KMP_ASSERT2(start <= end,
"bad explicit proc list");
2813 KMP_ASSERT2(start >= end,
"bad explicit proc list");
2815 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
2822 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2824 }
while (start <= end);
2828 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2830 }
while (start >= end);
2843 *out_numMasks = nextNewMask;
2844 if (nextNewMask == 0) {
2846 KMP_INTERNAL_FREE(newMasks);
2850 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
2851 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
2852 __kmp_free(sumMask);
2853 KMP_INTERNAL_FREE(newMasks);
2883 __kmp_process_subplace_list(
const char **scan, kmp_affin_mask_t *osId2Mask,
2884 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
2889 int start, count, stride, i;
2895 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2896 "bad explicit places list");
2899 start = __kmp_str_to_int(*scan, *next);
2900 KMP_ASSERT(start >= 0);
2907 if (**scan ==
'}' || **scan ==
',') {
2908 if ((start > maxOsId) ||
2909 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2910 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2911 && (__kmp_affinity_type != affinity_none))) {
2912 KMP_WARNING(AffIgnoreInvalidProcID, start);
2916 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2919 if (**scan ==
'}') {
2925 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2932 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2933 "bad explicit places list");
2936 count = __kmp_str_to_int(*scan, *next);
2937 KMP_ASSERT(count >= 0);
2944 if (**scan ==
'}' || **scan ==
',') {
2945 for (i = 0; i < count; i++) {
2946 if ((start > maxOsId) ||
2947 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2948 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2949 && (__kmp_affinity_type != affinity_none))) {
2950 KMP_WARNING(AffIgnoreInvalidProcID, start);
2955 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2960 if (**scan ==
'}') {
2966 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2975 if (**scan ==
'+') {
2979 if (**scan ==
'-') {
2987 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2988 "bad explicit places list");
2991 stride = __kmp_str_to_int(*scan, *next);
2992 KMP_ASSERT(stride >= 0);
3000 if (**scan ==
'}' || **scan ==
',') {
3001 for (i = 0; i < count; i++) {
3002 if ((start > maxOsId) ||
3003 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
3004 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3005 && (__kmp_affinity_type != affinity_none))) {
3006 KMP_WARNING(AffIgnoreInvalidProcID, start);
3011 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3016 if (**scan ==
'}') {
3023 KMP_ASSERT2(0,
"bad explicit places list");
3029 __kmp_process_place(
const char **scan, kmp_affin_mask_t *osId2Mask,
3030 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
3038 if (**scan ==
'{') {
3040 __kmp_process_subplace_list(scan, osId2Mask, maxOsId , tempMask,
3042 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3045 else if (**scan ==
'!') {
3046 __kmp_process_place(scan, osId2Mask, maxOsId, tempMask, setSize);
3047 KMP_CPU_COMPLEMENT(tempMask);
3050 else if ((**scan >=
'0') && (**scan <=
'9')) {
3053 int num = __kmp_str_to_int(*scan, *next);
3054 KMP_ASSERT(num >= 0);
3055 if ((num > maxOsId) ||
3056 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3057 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3058 && (__kmp_affinity_type != affinity_none))) {
3059 KMP_WARNING(AffIgnoreInvalidProcID, num);
3063 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3069 KMP_ASSERT2(0,
"bad explicit places list");
3075 __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
3076 unsigned int *out_numMasks,
const char *placelist,
3077 kmp_affin_mask_t *osId2Mask,
int maxOsId)
3079 const char *scan = placelist;
3080 const char *next = placelist;
3083 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
3084 * __kmp_affin_mask_size);
3087 kmp_affin_mask_t *tempMask = (kmp_affin_mask_t *)__kmp_allocate(
3088 __kmp_affin_mask_size);
3089 KMP_CPU_ZERO(tempMask);
3093 __kmp_process_place(&scan, osId2Mask, maxOsId, tempMask, &setSize);
3099 if (*scan ==
'\0' || *scan ==
',') {
3103 KMP_CPU_ZERO(tempMask);
3105 if (*scan ==
'\0') {
3112 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3119 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3120 "bad explicit places list");
3123 int count = __kmp_str_to_int(scan, *next);
3124 KMP_ASSERT(count >= 0);
3131 if (*scan ==
'\0' || *scan ==
',') {
3133 for (i = 0; i < count; i++) {
3140 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j > 0; j--) {
3145 if (KMP_CPU_ISSET(j - 1, tempMask)) {
3146 KMP_CPU_SET(j, tempMask);
3150 KMP_CPU_CLR(j, tempMask);
3153 for (; j >= 0; j--) {
3154 KMP_CPU_CLR(j, tempMask);
3157 KMP_CPU_ZERO(tempMask);
3160 if (*scan ==
'\0') {
3167 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3188 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3189 "bad explicit places list");
3192 int stride = __kmp_str_to_int(scan, *next);
3193 KMP_DEBUG_ASSERT(stride >= 0);
3199 for (i = 0; i < count; i++) {
3206 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j >= stride; j--) {
3207 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3208 KMP_CPU_SET(j, tempMask);
3212 KMP_CPU_CLR(j, tempMask);
3215 for (; j >= 0; j--) {
3216 KMP_CPU_CLR(j, tempMask);
3222 for (i = 0; i < count; i++) {
3229 for (j = 0; j < (__kmp_affin_mask_size * CHAR_BIT) + stride;
3231 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3232 KMP_CPU_SET(j, tempMask);
3236 KMP_CPU_CLR(j, tempMask);
3239 for (; j < __kmp_affin_mask_size * CHAR_BIT; j++) {
3240 KMP_CPU_CLR(j, tempMask);
3244 KMP_CPU_ZERO(tempMask);
3251 if (*scan ==
'\0') {
3259 KMP_ASSERT2(0,
"bad explicit places list");
3262 *out_numMasks = nextNewMask;
3263 if (nextNewMask == 0) {
3265 KMP_INTERNAL_FREE(newMasks);
3269 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
3270 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
3271 __kmp_free(tempMask);
3272 KMP_INTERNAL_FREE(newMasks);
3278 #undef ADD_MASK_OSID
3284 __kmp_apply_thread_places(AddrUnsPair **pAddr,
int depth)
3286 if ( __kmp_place_num_cores == 0 ) {
3287 if ( __kmp_place_num_threads_per_core == 0 ) {
3290 __kmp_place_num_cores = nCoresPerPkg;
3292 if ( !__kmp_affinity_uniform_topology() || depth != 3 ) {
3293 KMP_WARNING( AffThrPlaceUnsupported );
3296 if ( __kmp_place_num_threads_per_core == 0 ) {
3297 __kmp_place_num_threads_per_core = __kmp_nThreadsPerCore;
3299 if ( __kmp_place_core_offset + __kmp_place_num_cores > nCoresPerPkg ) {
3300 KMP_WARNING( AffThrPlaceManyCores );
3304 AddrUnsPair *newAddr = (AddrUnsPair *)__kmp_allocate(
sizeof(AddrUnsPair) *
3305 nPackages * __kmp_place_num_cores * __kmp_place_num_threads_per_core);
3306 int i, j, k, n_old = 0, n_new = 0;
3307 for ( i = 0; i < nPackages; ++i ) {
3308 for ( j = 0; j < nCoresPerPkg; ++j ) {
3309 if ( j < __kmp_place_core_offset || j >= __kmp_place_core_offset + __kmp_place_num_cores ) {
3310 n_old += __kmp_nThreadsPerCore;
3312 for ( k = 0; k < __kmp_nThreadsPerCore; ++k ) {
3313 if ( k < __kmp_place_num_threads_per_core ) {
3314 newAddr[n_new] = (*pAddr)[n_old];
3322 nCoresPerPkg = __kmp_place_num_cores;
3323 __kmp_nThreadsPerCore = __kmp_place_num_threads_per_core;
3324 __kmp_avail_proc = n_new;
3325 __kmp_ncores = nPackages * __kmp_place_num_cores;
3327 __kmp_free( *pAddr );
3334 static AddrUnsPair *address2os = NULL;
3335 static int * procarr = NULL;
3336 static int __kmp_aff_depth = 0;
3339 __kmp_aux_affinity_initialize(
void)
3341 if (__kmp_affinity_masks != NULL) {
3342 KMP_ASSERT(fullMask != NULL);
3352 if (fullMask == NULL) {
3353 fullMask = (kmp_affin_mask_t *)__kmp_allocate(__kmp_affin_mask_size);
3355 if (KMP_AFFINITY_CAPABLE()) {
3356 if (__kmp_affinity_respect_mask) {
3357 __kmp_get_system_affinity(fullMask, TRUE);
3363 __kmp_avail_proc = 0;
3364 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
3365 if (! KMP_CPU_ISSET(i, fullMask)) {
3370 if (__kmp_avail_proc > __kmp_xproc) {
3371 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3372 && (__kmp_affinity_type != affinity_none))) {
3373 KMP_WARNING(ErrorInitializeAffinity);
3375 __kmp_affinity_type = affinity_none;
3376 __kmp_affin_mask_size = 0;
3381 __kmp_affinity_entire_machine_mask(fullMask);
3382 __kmp_avail_proc = __kmp_xproc;
3387 kmp_i18n_id_t msg_id = kmp_i18n_null;
3393 if ((__kmp_cpuinfo_file != NULL) &&
3394 (__kmp_affinity_top_method == affinity_top_method_all)) {
3395 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3398 if (__kmp_affinity_top_method == affinity_top_method_all) {
3404 const char *file_name = NULL;
3407 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3409 if (__kmp_affinity_verbose) {
3410 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
3414 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3416 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3417 KMP_ASSERT(address2os == NULL);
3422 if ((msg_id != kmp_i18n_null)
3423 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3424 && (__kmp_affinity_type != affinity_none)))) {
3426 if (__kmp_affinity_verbose) {
3427 KMP_INFORM(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3428 KMP_I18N_STR(DecodingLegacyAPIC));
3431 KMP_WARNING(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3432 KMP_I18N_STR(DecodingLegacyAPIC));
3437 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3439 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3440 KMP_ASSERT(address2os == NULL);
3450 if ((msg_id != kmp_i18n_null)
3451 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3452 && (__kmp_affinity_type != affinity_none)))) {
3454 if (__kmp_affinity_verbose) {
3455 KMP_INFORM(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3458 KMP_WARNING(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3461 else if (__kmp_affinity_verbose) {
3462 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"/proc/cpuinfo");
3465 FILE *f = fopen(
"/proc/cpuinfo",
"r");
3467 msg_id = kmp_i18n_str_CantOpenCpuinfo;
3470 file_name =
"/proc/cpuinfo";
3471 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3474 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3475 KMP_ASSERT(address2os == NULL);
3484 if (msg_id != kmp_i18n_null
3485 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3486 && (__kmp_affinity_type != affinity_none)))) {
3487 if (file_name == NULL) {
3488 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3490 else if (line == 0) {
3491 KMP_WARNING(UsingFlatOSFile, file_name, __kmp_i18n_catgets(msg_id));
3494 KMP_WARNING(UsingFlatOSFileLine, file_name, line, __kmp_i18n_catgets(msg_id));
3499 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3501 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3502 KMP_ASSERT(address2os == NULL);
3505 KMP_ASSERT(depth > 0);
3506 KMP_ASSERT(address2os != NULL);
3516 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3518 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
3519 if (__kmp_affinity_verbose) {
3520 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3521 KMP_I18N_STR(Decodingx2APIC));
3524 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3526 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3527 KMP_ASSERT(address2os == NULL);
3532 KMP_ASSERT(msg_id != kmp_i18n_null);
3533 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3536 else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
3537 if (__kmp_affinity_verbose) {
3538 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3539 KMP_I18N_STR(DecodingLegacyAPIC));
3542 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3544 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3545 KMP_ASSERT(address2os == NULL);
3550 KMP_ASSERT(msg_id != kmp_i18n_null);
3551 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3557 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
3558 const char *filename;
3559 if (__kmp_cpuinfo_file != NULL) {
3560 filename = __kmp_cpuinfo_file;
3563 filename =
"/proc/cpuinfo";
3566 if (__kmp_affinity_verbose) {
3567 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
3570 FILE *f = fopen(filename,
"r");
3573 if (__kmp_cpuinfo_file != NULL) {
3576 KMP_MSG(CantOpenFileForReading, filename),
3578 KMP_HNT(NameComesFrom_CPUINFO_FILE),
3585 KMP_MSG(CantOpenFileForReading, filename),
3592 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3595 KMP_ASSERT(msg_id != kmp_i18n_null);
3597 KMP_FATAL(FileLineMsgExiting, filename, line, __kmp_i18n_catgets(msg_id));
3600 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
3603 if (__kmp_affinity_type == affinity_none) {
3604 KMP_ASSERT(depth == 0);
3605 KMP_ASSERT(address2os == NULL);
3610 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3612 else if (__kmp_affinity_top_method == affinity_top_method_group) {
3613 if (__kmp_affinity_verbose) {
3614 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
3617 depth = __kmp_affinity_create_proc_group_map(&address2os, &msg_id);
3618 KMP_ASSERT(depth != 0);
3621 if ((msg_id != kmp_i18n_null)
3622 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3623 && (__kmp_affinity_type != affinity_none)))) {
3624 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3627 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3629 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3630 KMP_ASSERT(address2os == NULL);
3634 KMP_ASSERT(depth > 0);
3635 KMP_ASSERT(address2os != NULL);
3641 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
3642 if (__kmp_affinity_verbose) {
3643 KMP_INFORM(AffUsingFlatOS,
"KMP_AFFINITY");
3646 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3648 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3649 KMP_ASSERT(address2os == NULL);
3653 KMP_ASSERT(depth > 0);
3654 KMP_ASSERT(address2os != NULL);
3657 if (address2os == NULL) {
3658 if (KMP_AFFINITY_CAPABLE()
3659 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3660 && (__kmp_affinity_type != affinity_none)))) {
3661 KMP_WARNING(ErrorInitializeAffinity);
3663 __kmp_affinity_type = affinity_none;
3664 __kmp_affin_mask_size = 0;
3669 __kmp_apply_thread_places(&address2os, depth);
3677 kmp_affin_mask_t *osId2Mask = __kmp_create_masks(&maxIndex, &numUnique,
3678 address2os, __kmp_avail_proc);
3679 if (__kmp_affinity_gran_levels == 0) {
3680 KMP_DEBUG_ASSERT(numUnique == __kmp_avail_proc);
3688 __kmp_affinity_assign_child_nums(address2os, __kmp_avail_proc);
3690 switch (__kmp_affinity_type) {
3692 case affinity_explicit:
3693 KMP_DEBUG_ASSERT(__kmp_affinity_proclist != NULL);
3695 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3698 __kmp_affinity_process_proclist(&__kmp_affinity_masks,
3699 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3704 __kmp_affinity_process_placelist(&__kmp_affinity_masks,
3705 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3709 if (__kmp_affinity_num_masks == 0) {
3710 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3711 && (__kmp_affinity_type != affinity_none))) {
3712 KMP_WARNING(AffNoValidProcID);
3714 __kmp_affinity_type = affinity_none;
3727 case affinity_logical:
3728 __kmp_affinity_compact = 0;
3729 if (__kmp_affinity_offset) {
3730 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3735 case affinity_physical:
3736 if (__kmp_nThreadsPerCore > 1) {
3737 __kmp_affinity_compact = 1;
3738 if (__kmp_affinity_compact >= depth) {
3739 __kmp_affinity_compact = 0;
3742 __kmp_affinity_compact = 0;
3744 if (__kmp_affinity_offset) {
3745 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3750 case affinity_scatter:
3751 if (__kmp_affinity_compact >= depth) {
3752 __kmp_affinity_compact = 0;
3755 __kmp_affinity_compact = depth - 1 - __kmp_affinity_compact;
3759 case affinity_compact:
3760 if (__kmp_affinity_compact >= depth) {
3761 __kmp_affinity_compact = depth - 1;
3766 case affinity_balanced:
3768 if( nPackages > 1 ) {
3769 if( __kmp_affinity_verbose || __kmp_affinity_warnings ) {
3770 KMP_WARNING( AffBalancedNotAvail,
"KMP_AFFINITY" );
3772 __kmp_affinity_type = affinity_none;
3774 }
else if( __kmp_affinity_uniform_topology() ) {
3779 __kmp_aff_depth = depth;
3782 int nth_per_core = __kmp_nThreadsPerCore;
3785 if( nth_per_core > 1 ) {
3786 core_level = depth - 2;
3788 core_level = depth - 1;
3790 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
3791 int nproc = nth_per_core * ncores;
3793 procarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
3794 for(
int i = 0; i < nproc; i++ ) {
3798 for(
int i = 0; i < __kmp_avail_proc; i++ ) {
3799 int proc = address2os[ i ].second;
3803 int level = depth - 1;
3807 int core = address2os[ i ].first.labels[ level ];
3809 if( nth_per_core > 1 ) {
3810 thread = address2os[ i ].first.labels[ level ] % nth_per_core;
3811 core = address2os[ i ].first.labels[ level - 1 ];
3813 procarr[ core * nth_per_core + thread ] = proc;
3824 if (__kmp_affinity_dups) {
3825 __kmp_affinity_num_masks = __kmp_avail_proc;
3828 __kmp_affinity_num_masks = numUnique;
3832 if ( ( __kmp_nested_proc_bind.bind_types[0] != proc_bind_intel )
3833 && ( __kmp_affinity_num_places > 0 )
3834 && ( (
unsigned)__kmp_affinity_num_places < __kmp_affinity_num_masks ) ) {
3835 __kmp_affinity_num_masks = __kmp_affinity_num_places;
3839 __kmp_affinity_masks = (kmp_affin_mask_t*)__kmp_allocate(
3840 __kmp_affinity_num_masks * __kmp_affin_mask_size);
3846 qsort(address2os, __kmp_avail_proc,
sizeof(*address2os),
3847 __kmp_affinity_cmp_Address_child_num);
3851 for (i = 0, j = 0; i < __kmp_avail_proc; i++) {
3852 if ((! __kmp_affinity_dups) && (! address2os[i].first.leader)) {
3855 unsigned osId = address2os[i].second;
3856 kmp_affin_mask_t *src = KMP_CPU_INDEX(osId2Mask, osId);
3857 kmp_affin_mask_t *dest
3858 = KMP_CPU_INDEX(__kmp_affinity_masks, j);
3859 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
3860 KMP_CPU_COPY(dest, src);
3861 if (++j >= __kmp_affinity_num_masks) {
3865 KMP_DEBUG_ASSERT(j == __kmp_affinity_num_masks);
3870 KMP_ASSERT2(0,
"Unexpected affinity setting");
3873 __kmp_free(osId2Mask);
3878 __kmp_affinity_initialize(
void)
3891 int disabled = (__kmp_affinity_type == affinity_disabled);
3892 if (! KMP_AFFINITY_CAPABLE()) {
3893 KMP_ASSERT(disabled);
3896 __kmp_affinity_type = affinity_none;
3898 __kmp_aux_affinity_initialize();
3900 __kmp_affinity_type = affinity_disabled;
3906 __kmp_affinity_uninitialize(
void)
3908 if (__kmp_affinity_masks != NULL) {
3909 __kmp_free(__kmp_affinity_masks);
3910 __kmp_affinity_masks = NULL;
3912 if (fullMask != NULL) {
3913 KMP_CPU_FREE(fullMask);
3916 __kmp_affinity_num_masks = 0;
3918 __kmp_affinity_num_places = 0;
3920 if (__kmp_affinity_proclist != NULL) {
3921 __kmp_free(__kmp_affinity_proclist);
3922 __kmp_affinity_proclist = NULL;
3924 if( address2os != NULL ) {
3925 __kmp_free( address2os );
3928 if( procarr != NULL ) {
3929 __kmp_free( procarr );
3936 __kmp_affinity_set_init_mask(
int gtid,
int isa_root)
3938 if (! KMP_AFFINITY_CAPABLE()) {
3942 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
3943 if (th->th.th_affin_mask == NULL) {
3944 KMP_CPU_ALLOC(th->th.th_affin_mask);
3947 KMP_CPU_ZERO(th->th.th_affin_mask);
3957 kmp_affin_mask_t *mask;
3961 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3964 if ((__kmp_affinity_type == affinity_none)
3966 || (__kmp_affinity_type == affinity_balanced)
3969 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3970 if (__kmp_num_proc_groups > 1) {
3974 KMP_ASSERT(fullMask != NULL);
3979 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
3980 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3981 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3987 || (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
3988 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3989 if (__kmp_num_proc_groups > 1) {
3993 KMP_ASSERT(fullMask != NULL);
4002 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
4003 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
4004 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
4010 th->th.th_current_place = i;
4012 th->th.th_new_place = i;
4013 th->th.th_first_place = 0;
4014 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4017 if (i == KMP_PLACE_ALL) {
4018 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4022 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4027 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to fullMask\n",
4031 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to mask %d\n",
4036 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4038 if (__kmp_affinity_verbose) {
4039 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4040 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4041 th->th.th_affin_mask);
4042 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", gtid, buf);
4051 if ( __kmp_affinity_type == affinity_none ) {
4052 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4056 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4063 __kmp_affinity_set_place(
int gtid)
4067 if (! KMP_AFFINITY_CAPABLE()) {
4071 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4073 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current place = %d)\n",
4074 gtid, th->th.th_new_place, th->th.th_current_place));
4079 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4080 KMP_DEBUG_ASSERT(th->th.th_new_place >= 0);
4081 KMP_DEBUG_ASSERT((
unsigned)th->th.th_new_place <= __kmp_affinity_num_masks);
4082 if (th->th.th_first_place <= th->th.th_last_place) {
4083 KMP_DEBUG_ASSERT((th->th.th_new_place >= th->th.th_first_place)
4084 && (th->th.th_new_place <= th->th.th_last_place));
4087 KMP_DEBUG_ASSERT((th->th.th_new_place <= th->th.th_first_place)
4088 || (th->th.th_new_place >= th->th.th_last_place));
4095 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks,
4096 th->th.th_new_place);
4097 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4098 th->th.th_current_place = th->th.th_new_place;
4100 if (__kmp_affinity_verbose) {
4101 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4102 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4103 th->th.th_affin_mask);
4104 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", gtid, buf);
4106 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4113 __kmp_aux_set_affinity(
void **mask)
4119 if (! KMP_AFFINITY_CAPABLE()) {
4123 gtid = __kmp_entry_gtid();
4125 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4126 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4127 (kmp_affin_mask_t *)(*mask));
4128 __kmp_debug_printf(
"kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4132 if (__kmp_env_consistency_check) {
4133 if ((mask == NULL) || (*mask == NULL)) {
4134 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4140 for (proc = 0; proc < KMP_CPU_SETSIZE; proc++) {
4141 if (! KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4145 if (! KMP_CPU_ISSET(proc, fullMask)) {
4146 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4150 if (num_procs == 0) {
4151 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4154 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
4155 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4156 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4163 th = __kmp_threads[gtid];
4164 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4165 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4167 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4171 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4172 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4173 th->th.th_first_place = 0;
4174 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4182 __kmp_aux_get_affinity(
void **mask)
4188 if (! KMP_AFFINITY_CAPABLE()) {
4192 gtid = __kmp_entry_gtid();
4193 th = __kmp_threads[gtid];
4194 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4197 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4198 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4199 th->th.th_affin_mask);
4200 __kmp_printf(
"kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid, buf);
4203 if (__kmp_env_consistency_check) {
4204 if ((mask == NULL) || (*mask == NULL)) {
4205 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4209 # if !KMP_OS_WINDOWS
4211 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4213 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4214 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4215 (kmp_affin_mask_t *)(*mask));
4216 __kmp_printf(
"kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid, buf);
4222 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4231 __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask)
4235 if (! KMP_AFFINITY_CAPABLE()) {
4240 int gtid = __kmp_entry_gtid();
4241 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4242 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4243 (kmp_affin_mask_t *)(*mask));
4244 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in affinity mask for thread %d = %s\n",
4248 if (__kmp_env_consistency_check) {
4249 if ((mask == NULL) || (*mask == NULL)) {
4250 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4254 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4257 if (! KMP_CPU_ISSET(proc, fullMask)) {
4261 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
4267 __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask)
4271 if (! KMP_AFFINITY_CAPABLE()) {
4276 int gtid = __kmp_entry_gtid();
4277 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4278 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4279 (kmp_affin_mask_t *)(*mask));
4280 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in affinity mask for thread %d = %s\n",
4284 if (__kmp_env_consistency_check) {
4285 if ((mask == NULL) || (*mask == NULL)) {
4286 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
4290 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4293 if (! KMP_CPU_ISSET(proc, fullMask)) {
4297 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
4303 __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask)
4307 if (! KMP_AFFINITY_CAPABLE()) {
4312 int gtid = __kmp_entry_gtid();
4313 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4314 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4315 (kmp_affin_mask_t *)(*mask));
4316 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in affinity mask for thread %d = %s\n",
4320 if (__kmp_env_consistency_check) {
4321 if ((mask == NULL) || (*mask == NULL)) {
4322 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4326 if ((proc < 0) || ((
unsigned)proc >= KMP_CPU_SETSIZE)) {
4329 if (! KMP_CPU_ISSET(proc, fullMask)) {
4333 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
4339 void __kmp_balanced_affinity(
int tid,
int nthreads )
4341 if( __kmp_affinity_uniform_topology() ) {
4345 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
4347 int ncores = __kmp_ncores;
4349 int chunk = nthreads / ncores;
4351 int big_cores = nthreads % ncores;
4353 int big_nth = ( chunk + 1 ) * big_cores;
4354 if( tid < big_nth ) {
4355 coreID = tid / (chunk + 1 );
4356 threadID = ( tid % (chunk + 1 ) ) % __kmp_nth_per_core ;
4358 coreID = ( tid - big_cores ) / chunk;
4359 threadID = ( ( tid - big_cores ) % chunk ) % __kmp_nth_per_core ;
4362 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
4363 "Illegal set affinity operation when not capable");
4365 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4369 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4370 int osID = address2os[ coreID * __kmp_nth_per_core + threadID ].second;
4371 KMP_CPU_SET( osID, mask);
4372 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4373 for(
int i = 0; i < __kmp_nth_per_core; i++ ) {
4375 osID = address2os[ coreID * __kmp_nth_per_core + i ].second;
4376 KMP_CPU_SET( osID, mask);
4379 if (__kmp_affinity_verbose) {
4380 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4381 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4382 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4384 __kmp_set_system_affinity( mask, TRUE );
4387 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4391 int nth_per_core = __kmp_nThreadsPerCore;
4393 if( nth_per_core > 1 ) {
4394 core_level = __kmp_aff_depth - 2;
4396 core_level = __kmp_aff_depth - 1;
4400 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
4403 if( nthreads == __kmp_avail_proc ) {
4404 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4405 int osID = address2os[ tid ].second;
4406 KMP_CPU_SET( osID, mask);
4407 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4408 int coreID = address2os[ tid ].first.labels[ core_level ];
4412 for(
int i = 0; i < __kmp_avail_proc; i++ ) {
4413 int osID = address2os[ i ].second;
4414 int core = address2os[ i ].first.labels[ core_level ];
4415 if( core == coreID ) {
4416 KMP_CPU_SET( osID, mask);
4418 if( cnt == nth_per_core ) {
4424 }
else if( nthreads <= __kmp_ncores ) {
4427 for(
int i = 0; i < ncores; i++ ) {
4430 for(
int j = 0; j < nth_per_core; j++ ) {
4431 if( procarr[ i * nth_per_core + j ] != - 1 ) {
4438 for(
int j = 0; j < nth_per_core; j++ ) {
4439 int osID = procarr[ i * nth_per_core + j ];
4441 KMP_CPU_SET( osID, mask );
4443 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4458 int nproc_at_core[ ncores ];
4460 int ncores_with_x_procs[ nth_per_core + 1 ];
4462 int ncores_with_x_to_max_procs[ nth_per_core + 1 ];
4464 for(
int i = 0; i <= nth_per_core; i++ ) {
4465 ncores_with_x_procs[ i ] = 0;
4466 ncores_with_x_to_max_procs[ i ] = 0;
4469 for(
int i = 0; i < ncores; i++ ) {
4471 for(
int j = 0; j < nth_per_core; j++ ) {
4472 if( procarr[ i * nth_per_core + j ] != -1 ) {
4476 nproc_at_core[ i ] = cnt;
4477 ncores_with_x_procs[ cnt ]++;
4480 for(
int i = 0; i <= nth_per_core; i++ ) {
4481 for(
int j = i; j <= nth_per_core; j++ ) {
4482 ncores_with_x_to_max_procs[ i ] += ncores_with_x_procs[ j ];
4487 int nproc = nth_per_core * ncores;
4489 int * newarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
4490 for(
int i = 0; i < nproc; i++ ) {
4497 for(
int j = 1; j <= nth_per_core; j++ ) {
4498 int cnt = ncores_with_x_to_max_procs[ j ];
4499 for(
int i = 0; i < ncores; i++ ) {
4501 if( nproc_at_core[ i ] == 0 ) {
4504 for(
int k = 0; k < nth_per_core; k++ ) {
4505 if( procarr[ i * nth_per_core + k ] != -1 ) {
4506 if( newarr[ i * nth_per_core + k ] == 0 ) {
4507 newarr[ i * nth_per_core + k ] = 1;
4513 newarr[ i * nth_per_core + k ] ++;
4521 if( cnt == 0 || nth == 0 ) {
4532 for(
int i = 0; i < nproc; i++ ) {
4536 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4537 int osID = procarr[ i ];
4538 KMP_CPU_SET( osID, mask);
4539 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4540 int coreID = i / nth_per_core;
4541 for(
int ii = 0; ii < nth_per_core; ii++ ) {
4542 int osID = procarr[ coreID * nth_per_core + ii ];
4544 KMP_CPU_SET( osID, mask);
4551 __kmp_free( newarr );
4554 if (__kmp_affinity_verbose) {
4555 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4556 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4557 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4559 __kmp_set_system_affinity( mask, TRUE );
4568 #error "Unknown or unsupported OS"
4569 #endif // KMP_OS_WINDOWS || KMP_OS_LINUX