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,
"{%d", 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,
",%-d", 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)
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)
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(__kmp_affinity_compact <= depth);
323 for (i = 0; i < __kmp_affinity_compact; i++) {
324 int j = depth - i - 1;
325 if (aa->childNums[j] < bb->childNums[j])
return -1;
326 if (aa->childNums[j] > bb->childNums[j])
return 1;
328 for (; i < depth; i++) {
329 int j = i - __kmp_affinity_compact;
330 if (aa->childNums[j] < bb->childNums[j])
return -1;
331 if (aa->childNums[j] > bb->childNums[j])
return 1;
351 __kmp_affinity_assign_child_nums(AddrUnsPair *address2os,
354 KMP_DEBUG_ASSERT(numAddrs > 0);
355 int depth = address2os->first.depth;
356 unsigned *counts = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
357 unsigned *lastLabel = (
unsigned *)__kmp_allocate(depth
360 for (labCt = 0; labCt < depth; labCt++) {
361 address2os[0].first.childNums[labCt] = counts[labCt] = 0;
362 lastLabel[labCt] = address2os[0].first.labels[labCt];
365 for (i = 1; i < numAddrs; i++) {
366 for (labCt = 0; labCt < depth; labCt++) {
367 if (address2os[i].first.labels[labCt] != lastLabel[labCt]) {
369 for (labCt2 = labCt + 1; labCt2 < depth; labCt2++) {
371 lastLabel[labCt2] = address2os[i].first.labels[labCt2];
374 lastLabel[labCt] = address2os[i].first.labels[labCt];
378 for (labCt = 0; labCt < depth; labCt++) {
379 address2os[i].first.childNums[labCt] = counts[labCt];
381 for (; labCt < Address::maxDepth; labCt++) {
382 address2os[i].first.childNums[labCt] = 0;
400 static kmp_affin_mask_t *fullMask = NULL;
403 __kmp_affinity_get_fullMask() {
return fullMask; }
406 static int nCoresPerPkg, nPackages;
407 int __kmp_nThreadsPerCore;
416 __kmp_affinity_uniform_topology()
418 return __kmp_avail_proc == (__kmp_nThreadsPerCore * nCoresPerPkg * nPackages);
427 __kmp_affinity_print_topology(AddrUnsPair *address2os,
int len,
int depth,
428 int pkgLevel,
int coreLevel,
int threadLevel)
432 KMP_INFORM(OSProcToPhysicalThreadMap,
"KMP_AFFINITY");
433 for (proc = 0; proc < len; proc++) {
436 __kmp_str_buf_init(&buf);
437 for (level = 0; level < depth; level++) {
438 if (level == threadLevel) {
439 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Thread));
441 else if (level == coreLevel) {
442 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Core));
444 else if (level == pkgLevel) {
445 __kmp_str_buf_print(&buf,
"%s ", KMP_I18N_STR(Package));
447 else if (level > pkgLevel) {
448 __kmp_str_buf_print(&buf,
"%s_%d ", KMP_I18N_STR(Node),
449 level - pkgLevel - 1);
452 __kmp_str_buf_print(&buf,
"L%d ", level);
454 __kmp_str_buf_print(&buf,
"%d ",
455 address2os[proc].first.labels[level]);
457 KMP_INFORM(OSProcMapToPack,
"KMP_AFFINITY", address2os[proc].second,
459 __kmp_str_buf_free(&buf);
470 __kmp_affinity_create_flat_map(AddrUnsPair **address2os,
471 kmp_i18n_id_t *
const msg_id)
474 *msg_id = kmp_i18n_null;
481 if (! KMP_AFFINITY_CAPABLE()) {
482 KMP_ASSERT(__kmp_affinity_type == affinity_none);
483 __kmp_ncores = nPackages = __kmp_xproc;
484 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
485 __kmp_ht_enabled = FALSE;
486 if (__kmp_affinity_verbose) {
487 KMP_INFORM(AffFlatTopology,
"KMP_AFFINITY");
488 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
489 KMP_INFORM(Uniform,
"KMP_AFFINITY");
490 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
491 __kmp_nThreadsPerCore, __kmp_ncores);
502 __kmp_ncores = nPackages = __kmp_avail_proc;
503 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
504 __kmp_ht_enabled = FALSE;
505 if (__kmp_affinity_verbose) {
506 char buf[KMP_AFFIN_MASK_PRINT_LEN];
507 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
509 KMP_INFORM(AffCapableUseFlat,
"KMP_AFFINITY");
510 if (__kmp_affinity_respect_mask) {
511 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
513 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
515 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
516 KMP_INFORM(Uniform,
"KMP_AFFINITY");
517 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
518 __kmp_nThreadsPerCore, __kmp_ncores);
520 if (__kmp_affinity_type == affinity_none) {
527 *address2os = (AddrUnsPair*)
528 __kmp_allocate(
sizeof(**address2os) * __kmp_avail_proc);
531 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
535 if (! KMP_CPU_ISSET(i, fullMask)) {
541 (*address2os)[avail_ct++] = AddrUnsPair(addr,i);
543 if (__kmp_affinity_verbose) {
544 KMP_INFORM(OSProcToPackage,
"KMP_AFFINITY");
547 if (__kmp_affinity_gran_levels < 0) {
552 if (__kmp_affinity_gran > affinity_gran_package) {
553 __kmp_affinity_gran_levels = 1;
556 __kmp_affinity_gran_levels = 0;
563 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
574 __kmp_affinity_create_proc_group_map(AddrUnsPair **address2os,
575 kmp_i18n_id_t *
const msg_id)
578 *msg_id = kmp_i18n_null;
584 if ((! KMP_AFFINITY_CAPABLE()) || (__kmp_get_proc_group(fullMask) >= 0)) {
592 *address2os = (AddrUnsPair*)
593 __kmp_allocate(
sizeof(**address2os) * __kmp_avail_proc);
596 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
600 if (! KMP_CPU_ISSET(i, fullMask)) {
605 addr.labels[0] = i / (CHAR_BIT *
sizeof(DWORD_PTR));
606 addr.labels[1] = i % (CHAR_BIT *
sizeof(DWORD_PTR));
607 (*address2os)[avail_ct++] = AddrUnsPair(addr,i);
609 if (__kmp_affinity_verbose) {
610 KMP_INFORM(AffOSProcToGroup,
"KMP_AFFINITY", i, addr.labels[0],
615 if (__kmp_affinity_gran_levels < 0) {
616 if (__kmp_affinity_gran == affinity_gran_group) {
617 __kmp_affinity_gran_levels = 1;
619 else if ((__kmp_affinity_gran == affinity_gran_fine)
620 || (__kmp_affinity_gran == affinity_gran_thread)) {
621 __kmp_affinity_gran_levels = 0;
624 const char *gran_str = NULL;
625 if (__kmp_affinity_gran == affinity_gran_core) {
628 else if (__kmp_affinity_gran == affinity_gran_package) {
629 gran_str =
"package";
631 else if (__kmp_affinity_gran == affinity_gran_node) {
639 __kmp_affinity_gran_levels = 0;
648 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
651 __kmp_cpuid_mask_width(
int count) {
654 while((1<<r) < count)
660 class apicThreadInfo {
664 unsigned maxCoresPerPkg;
665 unsigned maxThreadsPerPkg;
673 __kmp_affinity_cmp_apicThreadInfo_os_id(
const void *a,
const void *b)
675 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
676 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
677 if (aa->osId < bb->osId)
return -1;
678 if (aa->osId > bb->osId)
return 1;
684 __kmp_affinity_cmp_apicThreadInfo_phys_id(
const void *a,
const void *b)
686 const apicThreadInfo *aa = (
const apicThreadInfo *)a;
687 const apicThreadInfo *bb = (
const apicThreadInfo *)b;
688 if (aa->pkgId < bb->pkgId)
return -1;
689 if (aa->pkgId > bb->pkgId)
return 1;
690 if (aa->coreId < bb->coreId)
return -1;
691 if (aa->coreId > bb->coreId)
return 1;
692 if (aa->threadId < bb->threadId)
return -1;
693 if (aa->threadId > bb->threadId)
return 1;
705 __kmp_affinity_create_apicid_map(AddrUnsPair **address2os,
706 kmp_i18n_id_t *
const msg_id)
710 *msg_id = kmp_i18n_null;
718 __kmp_x86_cpuid(0, 0, &buf);
720 *msg_id = kmp_i18n_str_NoLeaf4Support;
736 if (! KMP_AFFINITY_CAPABLE()) {
741 KMP_ASSERT(__kmp_affinity_type == affinity_none);
752 __kmp_x86_cpuid(1, 0, &buf);
753 int maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
754 if (maxThreadsPerPkg == 0) {
755 maxThreadsPerPkg = 1;
771 __kmp_x86_cpuid(0, 0, &buf);
773 __kmp_x86_cpuid(4, 0, &buf);
774 nCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
797 __kmp_ncores = __kmp_xproc;
798 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
799 __kmp_nThreadsPerCore = 1;
800 __kmp_ht_enabled = FALSE;
801 if (__kmp_affinity_verbose) {
802 KMP_INFORM(AffNotCapableUseLocCpuid,
"KMP_AFFINITY");
803 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
804 if (__kmp_affinity_uniform_topology()) {
805 KMP_INFORM(Uniform,
"KMP_AFFINITY");
807 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
809 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
810 __kmp_nThreadsPerCore, __kmp_ncores);
825 kmp_affin_mask_t *oldMask;
826 KMP_CPU_ALLOC(oldMask);
827 KMP_ASSERT(oldMask != NULL);
828 __kmp_get_system_affinity(oldMask, TRUE);
863 apicThreadInfo *threadInfo = (apicThreadInfo *)__kmp_allocate(
864 __kmp_avail_proc *
sizeof(apicThreadInfo));
866 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
870 if (! KMP_CPU_ISSET(i, fullMask)) {
873 KMP_DEBUG_ASSERT(nApics < __kmp_avail_proc);
875 __kmp_affinity_bind_thread(i);
876 threadInfo[nApics].osId = i;
882 __kmp_x86_cpuid(1, 0, &buf);
883 if (! (buf.edx >> 9) & 1) {
884 __kmp_set_system_affinity(oldMask, TRUE);
885 __kmp_free(threadInfo);
886 KMP_CPU_FREE(oldMask);
887 *msg_id = kmp_i18n_str_ApicNotPresent;
890 threadInfo[nApics].apicId = (buf.ebx >> 24) & 0xff;
891 threadInfo[nApics].maxThreadsPerPkg = (buf.ebx >> 16) & 0xff;
892 if (threadInfo[nApics].maxThreadsPerPkg == 0) {
893 threadInfo[nApics].maxThreadsPerPkg = 1;
904 __kmp_x86_cpuid(0, 0, &buf);
906 __kmp_x86_cpuid(4, 0, &buf);
907 threadInfo[nApics].maxCoresPerPkg = ((buf.eax >> 26) & 0x3f) + 1;
910 threadInfo[nApics].maxCoresPerPkg = 1;
917 int widthCT = __kmp_cpuid_mask_width(
918 threadInfo[nApics].maxThreadsPerPkg);
919 threadInfo[nApics].pkgId = threadInfo[nApics].apicId >> widthCT;
921 int widthC = __kmp_cpuid_mask_width(
922 threadInfo[nApics].maxCoresPerPkg);
923 int widthT = widthCT - widthC;
930 __kmp_set_system_affinity(oldMask, TRUE);
931 __kmp_free(threadInfo);
932 KMP_CPU_FREE(oldMask);
933 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
937 int maskC = (1 << widthC) - 1;
938 threadInfo[nApics].coreId = (threadInfo[nApics].apicId >> widthT)
941 int maskT = (1 << widthT) - 1;
942 threadInfo[nApics].threadId = threadInfo[nApics].apicId &maskT;
951 __kmp_set_system_affinity(oldMask, TRUE);
963 KMP_ASSERT(nApics > 0);
965 __kmp_ncores = nPackages = 1;
966 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
967 __kmp_ht_enabled = FALSE;
968 if (__kmp_affinity_verbose) {
969 char buf[KMP_AFFIN_MASK_PRINT_LEN];
970 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
972 KMP_INFORM(AffUseGlobCpuid,
"KMP_AFFINITY");
973 if (__kmp_affinity_respect_mask) {
974 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
976 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
978 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
979 KMP_INFORM(Uniform,
"KMP_AFFINITY");
980 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
981 __kmp_nThreadsPerCore, __kmp_ncores);
984 if (__kmp_affinity_type == affinity_none) {
985 __kmp_free(threadInfo);
986 KMP_CPU_FREE(oldMask);
990 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair));
992 addr.labels[0] = threadInfo[0].pkgId;
993 (*address2os)[0] = AddrUnsPair(addr, threadInfo[0].osId);
995 if (__kmp_affinity_gran_levels < 0) {
996 __kmp_affinity_gran_levels = 0;
999 if (__kmp_affinity_verbose) {
1000 __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
1003 __kmp_free(threadInfo);
1004 KMP_CPU_FREE(oldMask);
1011 qsort(threadInfo, nApics,
sizeof(*threadInfo),
1012 __kmp_affinity_cmp_apicThreadInfo_phys_id);
1031 __kmp_nThreadsPerCore = 1;
1032 unsigned nCores = 1;
1035 unsigned lastPkgId = threadInfo[0].pkgId;
1036 unsigned coreCt = 1;
1037 unsigned lastCoreId = threadInfo[0].coreId;
1038 unsigned threadCt = 1;
1039 unsigned lastThreadId = threadInfo[0].threadId;
1042 unsigned prevMaxCoresPerPkg = threadInfo[0].maxCoresPerPkg;
1043 unsigned prevMaxThreadsPerPkg = threadInfo[0].maxThreadsPerPkg;
1045 for (i = 1; i < nApics; i++) {
1046 if (threadInfo[i].pkgId != lastPkgId) {
1049 lastPkgId = threadInfo[i].pkgId;
1050 if (coreCt > nCoresPerPkg) nCoresPerPkg = coreCt;
1052 lastCoreId = threadInfo[i].coreId;
1053 if (threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1055 lastThreadId = threadInfo[i].threadId;
1062 prevMaxCoresPerPkg = threadInfo[i].maxCoresPerPkg;
1063 prevMaxThreadsPerPkg = threadInfo[i].maxThreadsPerPkg;
1067 if (threadInfo[i].coreId != lastCoreId) {
1070 lastCoreId = threadInfo[i].coreId;
1071 if (threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1073 lastThreadId = threadInfo[i].threadId;
1075 else if (threadInfo[i].threadId != lastThreadId) {
1077 lastThreadId = threadInfo[i].threadId;
1080 __kmp_free(threadInfo);
1081 KMP_CPU_FREE(oldMask);
1082 *msg_id = kmp_i18n_str_LegacyApicIDsNotUnique;
1090 if ((prevMaxCoresPerPkg != threadInfo[i].maxCoresPerPkg)
1091 || (prevMaxThreadsPerPkg != threadInfo[i].maxThreadsPerPkg)) {
1092 __kmp_free(threadInfo);
1093 KMP_CPU_FREE(oldMask);
1094 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1099 if (coreCt > nCoresPerPkg) nCoresPerPkg = coreCt;
1100 if (threadCt > __kmp_nThreadsPerCore) __kmp_nThreadsPerCore = threadCt;
1108 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1109 __kmp_ncores = nCores;
1110 if (__kmp_affinity_verbose) {
1111 char buf[KMP_AFFIN_MASK_PRINT_LEN];
1112 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1114 KMP_INFORM(AffUseGlobCpuid,
"KMP_AFFINITY");
1115 if (__kmp_affinity_respect_mask) {
1116 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
1118 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
1120 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1121 if (__kmp_affinity_uniform_topology()) {
1122 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1124 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1126 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1127 __kmp_nThreadsPerCore, __kmp_ncores);
1131 if (__kmp_affinity_type == affinity_none) {
1132 __kmp_free(threadInfo);
1133 KMP_CPU_FREE(oldMask);
1143 int coreLevel = (nCoresPerPkg <= 1) ? -1 : 1;
1144 int threadLevel = (__kmp_nThreadsPerCore <= 1) ? -1 : ((coreLevel >= 0) ? 2 : 1);
1145 unsigned depth = (pkgLevel >= 0) + (coreLevel >= 0) + (threadLevel >= 0);
1147 KMP_ASSERT(depth > 0);
1148 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair) * nApics);
1150 for (i = 0; i < nApics; ++i) {
1151 Address addr(depth);
1152 unsigned os = threadInfo[i].osId;
1155 if (pkgLevel >= 0) {
1156 addr.labels[d++] = threadInfo[i].pkgId;
1158 if (coreLevel >= 0) {
1159 addr.labels[d++] = threadInfo[i].coreId;
1161 if (threadLevel >= 0) {
1162 addr.labels[d++] = threadInfo[i].threadId;
1164 (*address2os)[i] = AddrUnsPair(addr, os);
1167 if (__kmp_affinity_gran_levels < 0) {
1172 __kmp_affinity_gran_levels = 0;
1173 if ((threadLevel >= 0)
1174 && (__kmp_affinity_gran > affinity_gran_thread)) {
1175 __kmp_affinity_gran_levels++;
1177 if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1178 __kmp_affinity_gran_levels++;
1180 if ((pkgLevel >= 0) && (__kmp_affinity_gran > affinity_gran_package)) {
1181 __kmp_affinity_gran_levels++;
1185 if (__kmp_affinity_verbose) {
1186 __kmp_affinity_print_topology(*address2os, nApics, depth, pkgLevel,
1187 coreLevel, threadLevel);
1190 __kmp_free(threadInfo);
1191 KMP_CPU_FREE(oldMask);
1202 __kmp_affinity_create_x2apicid_map(AddrUnsPair **address2os,
1203 kmp_i18n_id_t *
const msg_id)
1208 *msg_id = kmp_i18n_null;
1213 __kmp_x86_cpuid(0, 0, &buf);
1215 *msg_id = kmp_i18n_str_NoLeaf11Support;
1218 __kmp_x86_cpuid(11, 0, &buf);
1220 *msg_id = kmp_i18n_str_NoLeaf11Support;
1231 int threadLevel = -1;
1234 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
1236 for (level = 0;; level++) {
1249 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1252 __kmp_x86_cpuid(11, level, &buf);
1263 int kind = (buf.ecx >> 8) & 0xff;
1268 threadLevel = level;
1271 __kmp_nThreadsPerCore = buf.ebx & 0xff;
1272 if (__kmp_nThreadsPerCore == 0) {
1273 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1277 else if (kind == 2) {
1283 nCoresPerPkg = buf.ebx & 0xff;
1284 if (nCoresPerPkg == 0) {
1285 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1291 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1294 if (pkgLevel >= 0) {
1298 nPackages = buf.ebx & 0xff;
1299 if (nPackages == 0) {
1300 *msg_id = kmp_i18n_str_InvalidCpuidInfo;
1313 if (threadLevel >= 0) {
1314 threadLevel = depth - threadLevel - 1;
1316 if (coreLevel >= 0) {
1317 coreLevel = depth - coreLevel - 1;
1319 KMP_DEBUG_ASSERT(pkgLevel >= 0);
1320 pkgLevel = depth - pkgLevel - 1;
1329 if (! KMP_AFFINITY_CAPABLE())
1335 KMP_ASSERT(__kmp_affinity_type == affinity_none);
1337 __kmp_ncores = __kmp_xproc / __kmp_nThreadsPerCore;
1338 nPackages = (__kmp_xproc + nCoresPerPkg - 1) / nCoresPerPkg;
1339 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1340 if (__kmp_affinity_verbose) {
1341 KMP_INFORM(AffNotCapableUseLocCpuidL11,
"KMP_AFFINITY");
1342 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1343 if (__kmp_affinity_uniform_topology()) {
1344 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1346 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1348 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1349 __kmp_nThreadsPerCore, __kmp_ncores);
1364 kmp_affin_mask_t *oldMask;
1365 KMP_CPU_ALLOC(oldMask);
1366 __kmp_get_system_affinity(oldMask, TRUE);
1371 AddrUnsPair *retval = (AddrUnsPair *)
1372 __kmp_allocate(
sizeof(AddrUnsPair) * __kmp_avail_proc);
1380 for (proc = 0; proc < KMP_CPU_SETSIZE; ++proc) {
1384 if (! KMP_CPU_ISSET(proc, fullMask)) {
1387 KMP_DEBUG_ASSERT(nApics < __kmp_avail_proc);
1389 __kmp_affinity_bind_thread(proc);
1395 Address addr(depth);
1398 for (level = 0; level < depth; level++) {
1399 __kmp_x86_cpuid(11, level, &buf);
1400 unsigned apicId = buf.edx;
1402 if (level != depth - 1) {
1403 KMP_CPU_FREE(oldMask);
1404 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1407 addr.labels[depth - level - 1] = apicId >> prev_shift;
1411 int shift = buf.eax & 0x1f;
1412 int mask = (1 << shift) - 1;
1413 addr.labels[depth - level - 1] = (apicId & mask) >> prev_shift;
1416 if (level != depth) {
1417 KMP_CPU_FREE(oldMask);
1418 *msg_id = kmp_i18n_str_InconsistentCpuidInfo;
1422 retval[nApics] = AddrUnsPair(addr, proc);
1430 __kmp_set_system_affinity(oldMask, TRUE);
1435 KMP_ASSERT(nApics > 0);
1437 __kmp_ncores = nPackages = 1;
1438 __kmp_nThreadsPerCore = nCoresPerPkg = 1;
1439 __kmp_ht_enabled = FALSE;
1440 if (__kmp_affinity_verbose) {
1441 char buf[KMP_AFFIN_MASK_PRINT_LEN];
1442 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1444 KMP_INFORM(AffUseGlobCpuidL11,
"KMP_AFFINITY");
1445 if (__kmp_affinity_respect_mask) {
1446 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
1448 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
1450 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1451 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1452 KMP_INFORM(Topology,
"KMP_AFFINITY", nPackages, nCoresPerPkg,
1453 __kmp_nThreadsPerCore, __kmp_ncores);
1456 if (__kmp_affinity_type == affinity_none) {
1458 KMP_CPU_FREE(oldMask);
1466 addr.labels[0] = retval[0].first.labels[pkgLevel];
1467 retval[0].first = addr;
1469 if (__kmp_affinity_gran_levels < 0) {
1470 __kmp_affinity_gran_levels = 0;
1473 if (__kmp_affinity_verbose) {
1474 __kmp_affinity_print_topology(retval, 1, 1, 0, -1, -1);
1477 *address2os = retval;
1478 KMP_CPU_FREE(oldMask);
1485 qsort(retval, nApics,
sizeof(*retval), __kmp_affinity_cmp_Address_labels);
1490 unsigned *totals = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1491 unsigned *counts = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1492 unsigned *maxCt = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1493 unsigned *last = (
unsigned *)__kmp_allocate(depth *
sizeof(
unsigned));
1494 for (level = 0; level < depth; level++) {
1498 last[level] = retval[0].first.labels[level];
1507 for (proc = 1; proc < nApics; proc++) {
1509 for (level = 0; level < depth; level++) {
1510 if (retval[proc].first.labels[level] != last[level]) {
1512 for (j = level + 1; j < depth; j++) {
1522 last[j] = retval[proc].first.labels[j];
1526 if (counts[level] > maxCt[level]) {
1527 maxCt[level] = counts[level];
1529 last[level] = retval[proc].first.labels[level];
1532 else if (level == depth - 1) {
1538 KMP_CPU_FREE(oldMask);
1539 *msg_id = kmp_i18n_str_x2ApicIDsNotUnique;
1551 if (threadLevel >= 0) {
1552 __kmp_nThreadsPerCore = maxCt[threadLevel];
1555 __kmp_nThreadsPerCore = 1;
1557 __kmp_ht_enabled = (__kmp_nThreadsPerCore > 1);
1559 nPackages = totals[pkgLevel];
1561 if (coreLevel >= 0) {
1562 __kmp_ncores = totals[coreLevel];
1563 nCoresPerPkg = maxCt[coreLevel];
1566 __kmp_ncores = nPackages;
1573 unsigned prod = maxCt[0];
1574 for (level = 1; level < depth; level++) {
1575 prod *= maxCt[level];
1577 bool uniform = (prod == totals[level - 1]);
1582 if (__kmp_affinity_verbose) {
1583 char mask[KMP_AFFIN_MASK_PRINT_LEN];
1584 __kmp_affinity_print_mask(mask, KMP_AFFIN_MASK_PRINT_LEN, oldMask);
1586 KMP_INFORM(AffUseGlobCpuidL11,
"KMP_AFFINITY");
1587 if (__kmp_affinity_respect_mask) {
1588 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", mask);
1590 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", mask);
1592 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
1594 KMP_INFORM(Uniform,
"KMP_AFFINITY");
1596 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
1600 __kmp_str_buf_init(&buf);
1602 __kmp_str_buf_print(&buf,
"%d", totals[0]);
1603 for (level = 1; level <= pkgLevel; level++) {
1604 __kmp_str_buf_print(&buf,
" x %d", maxCt[level]);
1606 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, nCoresPerPkg,
1607 __kmp_nThreadsPerCore, __kmp_ncores);
1609 __kmp_str_buf_free(&buf);
1612 if (__kmp_affinity_type == affinity_none) {
1618 KMP_CPU_FREE(oldMask);
1627 for (level = 0; level < depth; level++) {
1628 if ((maxCt[level] == 1) && (level != pkgLevel)) {
1638 if (new_depth != depth) {
1639 AddrUnsPair *new_retval = (AddrUnsPair *)__kmp_allocate(
1640 sizeof(AddrUnsPair) * nApics);
1641 for (proc = 0; proc < nApics; proc++) {
1642 Address addr(new_depth);
1643 new_retval[proc] = AddrUnsPair(addr, retval[proc].second);
1646 for (level = 0; level < depth; level++) {
1647 if ((maxCt[level] == 1) && (level != pkgLevel)) {
1648 if (level == threadLevel) {
1651 else if ((threadLevel >= 0) && (level < threadLevel)) {
1654 if (level == coreLevel) {
1657 else if ((coreLevel >= 0) && (level < coreLevel)) {
1660 if (level < pkgLevel) {
1665 for (proc = 0; proc < nApics; proc++) {
1666 new_retval[proc].first.labels[new_level]
1667 = retval[proc].first.labels[level];
1673 retval = new_retval;
1677 if (__kmp_affinity_gran_levels < 0) {
1682 __kmp_affinity_gran_levels = 0;
1683 if ((threadLevel >= 0) && (__kmp_affinity_gran > affinity_gran_thread)) {
1684 __kmp_affinity_gran_levels++;
1686 if ((coreLevel >= 0) && (__kmp_affinity_gran > affinity_gran_core)) {
1687 __kmp_affinity_gran_levels++;
1689 if (__kmp_affinity_gran > affinity_gran_package) {
1690 __kmp_affinity_gran_levels++;
1694 if (__kmp_affinity_verbose) {
1695 __kmp_affinity_print_topology(retval, nApics, depth, pkgLevel,
1696 coreLevel, threadLevel);
1703 KMP_CPU_FREE(oldMask);
1704 *address2os = retval;
1713 #define threadIdIndex 1
1714 #define coreIdIndex 2
1715 #define pkgIdIndex 3
1716 #define nodeIdIndex 4
1718 typedef unsigned *ProcCpuInfo;
1719 static unsigned maxIndex = pkgIdIndex;
1723 __kmp_affinity_cmp_ProcCpuInfo_os_id(
const void *a,
const void *b)
1725 const unsigned *aa = (
const unsigned *)a;
1726 const unsigned *bb = (
const unsigned *)b;
1727 if (aa[osIdIndex] < bb[osIdIndex])
return -1;
1728 if (aa[osIdIndex] > bb[osIdIndex])
return 1;
1734 __kmp_affinity_cmp_ProcCpuInfo_phys_id(
const void *a,
const void *b)
1737 const unsigned *aa = *((
const unsigned **)a);
1738 const unsigned *bb = *((
const unsigned **)b);
1739 for (i = maxIndex; ; i--) {
1740 if (aa[i] < bb[i])
return -1;
1741 if (aa[i] > bb[i])
return 1;
1742 if (i == osIdIndex)
break;
1753 __kmp_affinity_create_cpuinfo_map(AddrUnsPair **address2os,
int *line,
1754 kmp_i18n_id_t *
const msg_id, FILE *f)
1757 *msg_id = kmp_i18n_null;
1764 unsigned num_records = 0;
1766 buf[
sizeof(buf) - 1] = 1;
1767 if (! fgets(buf,
sizeof(buf), f)) {
1774 char s1[] =
"processor";
1775 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
1784 if (sscanf(buf,
"node_%d id", &level) == 1) {
1785 if (nodeIdIndex + level >= maxIndex) {
1786 maxIndex = nodeIdIndex + level;
1797 if (num_records == 0) {
1799 *msg_id = kmp_i18n_str_NoProcRecords;
1802 if (num_records > __kmp_xproc) {
1804 *msg_id = kmp_i18n_str_TooManyProcRecords;
1815 if (fseek(f, 0, SEEK_SET) != 0) {
1817 *msg_id = kmp_i18n_str_CantRewindCpuinfo;
1825 unsigned **threadInfo = (
unsigned **)__kmp_allocate((num_records + 1)
1826 *
sizeof(
unsigned *));
1828 for (i = 0; i <= num_records; i++) {
1829 threadInfo[i] = (
unsigned *)__kmp_allocate((maxIndex + 1)
1830 *
sizeof(unsigned));
1833 #define CLEANUP_THREAD_INFO \
1834 for (i = 0; i <= num_records; i++) { \
1835 __kmp_free(threadInfo[i]); \
1837 __kmp_free(threadInfo);
1844 #define INIT_PROC_INFO(p) \
1845 for (__index = 0; __index <= maxIndex; __index++) { \
1846 (p)[__index] = UINT_MAX; \
1849 for (i = 0; i <= num_records; i++) {
1850 INIT_PROC_INFO(threadInfo[i]);
1853 unsigned num_avail = 0;
1863 buf[
sizeof(buf) - 1] = 1;
1864 bool long_line =
false;
1865 if (! fgets(buf,
sizeof(buf), f)) {
1873 for (i = 0; i <= maxIndex; i++) {
1874 if (threadInfo[num_avail][i] != UINT_MAX) {
1882 }
else if (!buf[
sizeof(buf) - 1]) {
1889 #define CHECK_LINE \
1891 CLEANUP_THREAD_INFO; \
1892 *msg_id = kmp_i18n_str_LongLineCpuinfo; \
1898 char s1[] =
"processor";
1899 if (strncmp(buf, s1,
sizeof(s1) - 1) == 0) {
1901 char *p = strchr(buf +
sizeof(s1) - 1,
':');
1903 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1904 if (threadInfo[num_avail][osIdIndex] != UINT_MAX)
goto dup_field;
1905 threadInfo[num_avail][osIdIndex] = val;
1908 char s2[] =
"physical id";
1909 if (strncmp(buf, s2,
sizeof(s2) - 1) == 0) {
1911 char *p = strchr(buf +
sizeof(s2) - 1,
':');
1913 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1914 if (threadInfo[num_avail][pkgIdIndex] != UINT_MAX)
goto dup_field;
1915 threadInfo[num_avail][pkgIdIndex] = val;
1918 char s3[] =
"core id";
1919 if (strncmp(buf, s3,
sizeof(s3) - 1) == 0) {
1921 char *p = strchr(buf +
sizeof(s3) - 1,
':');
1923 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1924 if (threadInfo[num_avail][coreIdIndex] != UINT_MAX)
goto dup_field;
1925 threadInfo[num_avail][coreIdIndex] = val;
1928 char s4[] =
"thread id";
1929 if (strncmp(buf, s4,
sizeof(s4) - 1) == 0) {
1931 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1933 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1934 if (threadInfo[num_avail][threadIdIndex] != UINT_MAX)
goto dup_field;
1935 threadInfo[num_avail][threadIdIndex] = val;
1939 if (sscanf(buf,
"node_%d id", &level) == 1) {
1941 char *p = strchr(buf +
sizeof(s4) - 1,
':');
1943 if ((p == NULL) || (sscanf(p + 1,
"%u\n", &val) != 1))
goto no_val;
1944 KMP_ASSERT(nodeIdIndex + level <= maxIndex);
1945 if (threadInfo[num_avail][nodeIdIndex + level] != UINT_MAX)
goto dup_field;
1946 threadInfo[num_avail][nodeIdIndex + level] = val;
1955 if ((*buf != 0) && (*buf !=
'\n')) {
1962 while (((ch = fgetc(f)) != EOF) && (ch !=
'\n'));
1971 if (num_avail == __kmp_xproc) {
1972 CLEANUP_THREAD_INFO;
1973 *msg_id = kmp_i18n_str_TooManyEntries;
1981 if (threadInfo[num_avail][osIdIndex] == UINT_MAX) {
1982 CLEANUP_THREAD_INFO;
1983 *msg_id = kmp_i18n_str_MissingProcField;
1986 if (threadInfo[0][pkgIdIndex] == UINT_MAX) {
1987 CLEANUP_THREAD_INFO;
1988 *msg_id = kmp_i18n_str_MissingPhysicalIDField;
1995 if (! KMP_CPU_ISSET(threadInfo[num_avail][osIdIndex], fullMask)) {
1996 INIT_PROC_INFO(threadInfo[num_avail]);
2005 KMP_ASSERT(num_avail <= num_records);
2006 INIT_PROC_INFO(threadInfo[num_avail]);
2011 CLEANUP_THREAD_INFO;
2012 *msg_id = kmp_i18n_str_MissingValCpuinfo;
2016 CLEANUP_THREAD_INFO;
2017 *msg_id = kmp_i18n_str_DuplicateFieldCpuinfo;
2022 # if KMP_MIC && REDUCE_TEAM_SIZE
2023 unsigned teamSize = 0;
2024 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2038 KMP_ASSERT(num_avail > 0);
2039 KMP_ASSERT(num_avail <= num_records);
2040 if (num_avail == 1) {
2042 __kmp_nThreadsPerCore = nCoresPerPkg = nPackages = 1;
2043 __kmp_ht_enabled = FALSE;
2044 if (__kmp_affinity_verbose) {
2045 if (! KMP_AFFINITY_CAPABLE()) {
2046 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2047 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2048 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2051 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2052 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
2054 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2055 if (__kmp_affinity_respect_mask) {
2056 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2058 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2060 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2061 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2065 __kmp_str_buf_init(&buf);
2066 __kmp_str_buf_print(&buf,
"1");
2067 for (index = maxIndex - 1; index > pkgIdIndex; index--) {
2068 __kmp_str_buf_print(&buf,
" x 1");
2070 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, 1, 1, 1);
2071 __kmp_str_buf_free(&buf);
2074 if (__kmp_affinity_type == affinity_none) {
2075 CLEANUP_THREAD_INFO;
2079 *address2os = (AddrUnsPair*)__kmp_allocate(
sizeof(AddrUnsPair));
2081 addr.labels[0] = threadInfo[0][pkgIdIndex];
2082 (*address2os)[0] = AddrUnsPair(addr, threadInfo[0][osIdIndex]);
2084 if (__kmp_affinity_gran_levels < 0) {
2085 __kmp_affinity_gran_levels = 0;
2088 if (__kmp_affinity_verbose) {
2089 __kmp_affinity_print_topology(*address2os, 1, 1, 0, -1, -1);
2092 CLEANUP_THREAD_INFO;
2099 qsort(threadInfo, num_avail,
sizeof(*threadInfo),
2100 __kmp_affinity_cmp_ProcCpuInfo_phys_id);
2113 unsigned *counts = (
unsigned *)__kmp_allocate((maxIndex + 1)
2114 *
sizeof(unsigned));
2115 unsigned *maxCt = (
unsigned *)__kmp_allocate((maxIndex + 1)
2116 *
sizeof(unsigned));
2117 unsigned *totals = (
unsigned *)__kmp_allocate((maxIndex + 1)
2118 *
sizeof(unsigned));
2119 unsigned *lastId = (
unsigned *)__kmp_allocate((maxIndex + 1)
2120 *
sizeof(unsigned));
2122 bool assign_thread_ids =
false;
2126 restart_radix_check:
2132 if (assign_thread_ids) {
2133 if (threadInfo[0][threadIdIndex] == UINT_MAX) {
2134 threadInfo[0][threadIdIndex] = threadIdCt++;
2136 else if (threadIdCt <= threadInfo[0][threadIdIndex]) {
2137 threadIdCt = threadInfo[0][threadIdIndex] + 1;
2140 for (index = 0; index <= maxIndex; index++) {
2144 lastId[index] = threadInfo[0][index];;
2150 for (i = 1; i < num_avail; i++) {
2155 for (index = maxIndex; index >= threadIdIndex; index--) {
2156 if (assign_thread_ids && (index == threadIdIndex)) {
2160 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2161 threadInfo[i][threadIdIndex] = threadIdCt++;
2169 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2170 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2173 if (threadInfo[i][index] != lastId[index]) {
2182 for (index2 = threadIdIndex; index2 < index; index2++) {
2184 if (counts[index2] > maxCt[index2]) {
2185 maxCt[index2] = counts[index2];
2188 lastId[index2] = threadInfo[i][index2];
2192 lastId[index] = threadInfo[i][index];
2194 if (assign_thread_ids && (index > threadIdIndex)) {
2196 # if KMP_MIC && REDUCE_TEAM_SIZE
2201 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2202 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2212 if (threadInfo[i][threadIdIndex] == UINT_MAX) {
2213 threadInfo[i][threadIdIndex] = threadIdCt++;
2221 else if (threadIdCt <= threadInfo[i][threadIdIndex]) {
2222 threadIdCt = threadInfo[i][threadIdIndex] + 1;
2228 if (index < threadIdIndex) {
2234 if ((threadInfo[i][threadIdIndex] != UINT_MAX)
2235 || assign_thread_ids) {
2240 CLEANUP_THREAD_INFO;
2241 *msg_id = kmp_i18n_str_PhysicalIDsNotUnique;
2250 assign_thread_ids =
true;
2251 goto restart_radix_check;
2255 # if KMP_MIC && REDUCE_TEAM_SIZE
2260 teamSize += ( threadIdCt <= 2 ) ? ( threadIdCt ) : ( threadIdCt - 1 );
2261 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2263 for (index = threadIdIndex; index <= maxIndex; index++) {
2264 if (counts[index] > maxCt[index]) {
2265 maxCt[index] = counts[index];
2269 __kmp_nThreadsPerCore = maxCt[threadIdIndex];
2270 nCoresPerPkg = maxCt[coreIdIndex];
2271 nPackages = totals[pkgIdIndex];
2276 unsigned prod = totals[maxIndex];
2277 for (index = threadIdIndex; index < maxIndex; index++) {
2278 prod *= maxCt[index];
2280 bool uniform = (prod == totals[threadIdIndex]);
2288 __kmp_ht_enabled = (maxCt[threadIdIndex] > 1);
2289 __kmp_ncores = totals[coreIdIndex];
2291 if (__kmp_affinity_verbose) {
2292 if (! KMP_AFFINITY_CAPABLE()) {
2293 KMP_INFORM(AffNotCapableUseCpuinfo,
"KMP_AFFINITY");
2294 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2296 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2298 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2302 char buf[KMP_AFFIN_MASK_PRINT_LEN];
2303 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, fullMask);
2304 KMP_INFORM(AffCapableUseCpuinfo,
"KMP_AFFINITY");
2305 if (__kmp_affinity_respect_mask) {
2306 KMP_INFORM(InitOSProcSetRespect,
"KMP_AFFINITY", buf);
2308 KMP_INFORM(InitOSProcSetNotRespect,
"KMP_AFFINITY", buf);
2310 KMP_INFORM(AvailableOSProc,
"KMP_AFFINITY", __kmp_avail_proc);
2312 KMP_INFORM(Uniform,
"KMP_AFFINITY");
2314 KMP_INFORM(NonUniform,
"KMP_AFFINITY");
2318 __kmp_str_buf_init(&buf);
2320 __kmp_str_buf_print(&buf,
"%d", totals[maxIndex]);
2321 for (index = maxIndex - 1; index >= pkgIdIndex; index--) {
2322 __kmp_str_buf_print(&buf,
" x %d", maxCt[index]);
2324 KMP_INFORM(TopologyExtra,
"KMP_AFFINITY", buf.str, maxCt[coreIdIndex],
2325 maxCt[threadIdIndex], __kmp_ncores);
2327 __kmp_str_buf_free(&buf);
2330 # if KMP_MIC && REDUCE_TEAM_SIZE
2334 if ((__kmp_dflt_team_nth == 0) && (teamSize > 0)) {
2335 __kmp_dflt_team_nth = teamSize;
2336 KA_TRACE(20, (
"__kmp_affinity_create_cpuinfo_map: setting __kmp_dflt_team_nth = %d\n",
2337 __kmp_dflt_team_nth));
2339 # endif // KMP_MIC && REDUCE_TEAM_SIZE
2341 if (__kmp_affinity_type == affinity_none) {
2346 CLEANUP_THREAD_INFO;
2357 bool *inMap = (
bool *)__kmp_allocate((maxIndex + 1) *
sizeof(bool));
2359 for (index = threadIdIndex; index < maxIndex; index++) {
2360 KMP_ASSERT(totals[index] >= totals[index + 1]);
2361 inMap[index] = (totals[index] > totals[index + 1]);
2363 inMap[maxIndex] = (totals[maxIndex] > 1);
2364 inMap[pkgIdIndex] =
true;
2367 for (index = threadIdIndex; index <= maxIndex; index++) {
2372 KMP_ASSERT(depth > 0);
2377 *address2os = (AddrUnsPair*)
2378 __kmp_allocate(
sizeof(AddrUnsPair) * num_avail);
2381 int threadLevel = -1;
2383 for (i = 0; i < num_avail; ++i) {
2384 Address addr(depth);
2385 unsigned os = threadInfo[i][osIdIndex];
2389 for (src_index = maxIndex; src_index >= threadIdIndex; src_index--) {
2390 if (! inMap[src_index]) {
2393 addr.labels[dst_index] = threadInfo[i][src_index];
2394 if (src_index == pkgIdIndex) {
2395 pkgLevel = dst_index;
2397 else if (src_index == coreIdIndex) {
2398 coreLevel = dst_index;
2400 else if (src_index == threadIdIndex) {
2401 threadLevel = dst_index;
2405 (*address2os)[i] = AddrUnsPair(addr, os);
2408 if (__kmp_affinity_gran_levels < 0) {
2414 __kmp_affinity_gran_levels = 0;
2415 for (src_index = threadIdIndex; src_index <= maxIndex; src_index++) {
2416 if (! inMap[src_index]) {
2419 switch (src_index) {
2421 if (__kmp_affinity_gran > affinity_gran_thread) {
2422 __kmp_affinity_gran_levels++;
2427 if (__kmp_affinity_gran > affinity_gran_core) {
2428 __kmp_affinity_gran_levels++;
2433 if (__kmp_affinity_gran > affinity_gran_package) {
2434 __kmp_affinity_gran_levels++;
2441 if (__kmp_affinity_verbose) {
2442 __kmp_affinity_print_topology(*address2os, num_avail, depth, pkgLevel,
2443 coreLevel, threadLevel);
2451 CLEANUP_THREAD_INFO;
2461 static kmp_affin_mask_t *
2462 __kmp_create_masks(
unsigned *maxIndex,
unsigned *numUnique,
2463 AddrUnsPair *address2os,
unsigned numAddrs)
2472 KMP_ASSERT(numAddrs > 0);
2473 depth = address2os[0].first.depth;
2476 for (i = 0; i < numAddrs; i++) {
2477 unsigned osId = address2os[i].second;
2478 if (osId > maxOsId) {
2482 kmp_affin_mask_t *osId2Mask = (kmp_affin_mask_t *)__kmp_allocate(
2483 (maxOsId + 1) * __kmp_affin_mask_size);
2490 qsort(address2os, numAddrs,
sizeof(*address2os),
2491 __kmp_affinity_cmp_Address_labels);
2493 KMP_ASSERT(__kmp_affinity_gran_levels >= 0);
2494 if (__kmp_affinity_verbose && (__kmp_affinity_gran_levels > 0)) {
2495 KMP_INFORM(ThreadsMigrate,
"KMP_AFFINITY", __kmp_affinity_gran_levels);
2497 if (__kmp_affinity_gran_levels >= depth) {
2498 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2499 && (__kmp_affinity_type != affinity_none))) {
2500 KMP_WARNING(AffThreadsMayMigrate);
2510 unsigned unique = 0;
2512 unsigned leader = 0;
2513 Address *leaderAddr = &(address2os[0].first);
2514 kmp_affin_mask_t *sum
2515 = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
2517 KMP_CPU_SET(address2os[0].second, sum);
2518 for (i = 1; i < numAddrs; i++) {
2524 if (leaderAddr->isClose(address2os[i].first,
2525 __kmp_affinity_gran_levels)) {
2526 KMP_CPU_SET(address2os[i].second, sum);
2535 for (; j < i; j++) {
2536 unsigned osId = address2os[j].second;
2537 KMP_DEBUG_ASSERT(osId <= maxOsId);
2538 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2539 KMP_CPU_COPY(mask, sum);
2540 address2os[j].first.leader = (j == leader);
2548 leaderAddr = &(address2os[i].first);
2550 KMP_CPU_SET(address2os[i].second, sum);
2557 for (; j < i; j++) {
2558 unsigned osId = address2os[j].second;
2559 KMP_DEBUG_ASSERT(osId <= maxOsId);
2560 kmp_affin_mask_t *mask = KMP_CPU_INDEX(osId2Mask, osId);
2561 KMP_CPU_COPY(mask, sum);
2562 address2os[j].first.leader = (j == leader);
2566 *maxIndex = maxOsId;
2567 *numUnique = unique;
2577 static kmp_affin_mask_t *newMasks;
2578 static int numNewMasks;
2579 static int nextNewMask;
2581 #define ADD_MASK(_mask) \
2583 if (nextNewMask >= numNewMasks) { \
2585 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_REALLOC(newMasks, \
2586 numNewMasks * __kmp_affin_mask_size); \
2588 KMP_CPU_COPY(KMP_CPU_INDEX(newMasks, nextNewMask), (_mask)); \
2592 #define ADD_MASK_OSID(_osId,_osId2Mask,_maxOsId) \
2594 if (((_osId) > _maxOsId) || \
2595 (! KMP_CPU_ISSET((_osId), KMP_CPU_INDEX(_osId2Mask, (_osId))))) {\
2596 if (__kmp_affinity_verbose || (__kmp_affinity_warnings \
2597 && (__kmp_affinity_type != affinity_none))) { \
2598 KMP_WARNING(AffIgnoreInvalidProcID, _osId); \
2602 ADD_MASK(KMP_CPU_INDEX(_osId2Mask, (_osId))); \
2612 __kmp_affinity_process_proclist(kmp_affin_mask_t **out_masks,
2613 unsigned int *out_numMasks,
const char *proclist,
2614 kmp_affin_mask_t *osId2Mask,
int maxOsId)
2616 const char *scan = proclist;
2617 const char *next = proclist;
2624 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
2625 * __kmp_affin_mask_size);
2627 kmp_affin_mask_t *sumMask = (kmp_affin_mask_t *)__kmp_allocate(
2628 __kmp_affin_mask_size);
2632 int start, end, stride;
2636 if (*next ==
'\0') {
2650 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2653 num = __kmp_str_to_int(scan, *next);
2654 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2659 if ((num > maxOsId) ||
2660 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2661 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2662 && (__kmp_affinity_type != affinity_none))) {
2663 KMP_WARNING(AffIgnoreInvalidProcID, num);
2665 KMP_CPU_ZERO(sumMask);
2668 KMP_CPU_COPY(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2694 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2695 "bad explicit proc list");
2698 num = __kmp_str_to_int(scan, *next);
2699 KMP_ASSERT2(num >= 0,
"bad explicit proc list");
2704 if ((num > maxOsId) ||
2705 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
2706 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2707 && (__kmp_affinity_type != affinity_none))) {
2708 KMP_WARNING(AffIgnoreInvalidProcID, num);
2712 KMP_CPU_UNION(sumMask, KMP_CPU_INDEX(osId2Mask, num));
2731 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2733 start = __kmp_str_to_int(scan, *next);
2734 KMP_ASSERT2(start >= 0,
"bad explicit proc list");
2741 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2759 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
"bad explicit proc list");
2761 end = __kmp_str_to_int(scan, *next);
2762 KMP_ASSERT2(end >= 0,
"bad explicit proc list");
2783 KMP_ASSERT2((*next >=
'0') && (*next <=
'9'),
2784 "bad explicit proc list");
2786 stride = __kmp_str_to_int(scan, *next);
2787 KMP_ASSERT2(stride >= 0,
"bad explicit proc list");
2794 KMP_ASSERT2(stride != 0,
"bad explicit proc list");
2796 KMP_ASSERT2(start <= end,
"bad explicit proc list");
2799 KMP_ASSERT2(start >= end,
"bad explicit proc list");
2801 KMP_ASSERT2((end - start) / stride <= 65536,
"bad explicit proc list");
2808 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2810 }
while (start <= end);
2814 ADD_MASK_OSID(start, osId2Mask, maxOsId);
2816 }
while (start >= end);
2829 *out_numMasks = nextNewMask;
2830 if (nextNewMask == 0) {
2832 KMP_INTERNAL_FREE(newMasks);
2836 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
2837 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
2838 __kmp_free(sumMask);
2839 KMP_INTERNAL_FREE(newMasks);
2869 __kmp_process_subplace_list(
const char **scan, kmp_affin_mask_t *osId2Mask,
2870 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
2875 int start, count, stride, i;
2881 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2882 "bad explicit places list");
2885 start = __kmp_str_to_int(*scan, *next);
2886 KMP_ASSERT(start >= 0);
2893 if (**scan ==
'}' || **scan ==
',') {
2894 if ((start > maxOsId) ||
2895 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2896 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2897 && (__kmp_affinity_type != affinity_none))) {
2898 KMP_WARNING(AffIgnoreInvalidProcID, start);
2902 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2905 if (**scan ==
'}') {
2911 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2918 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2919 "bad explicit places list");
2922 count = __kmp_str_to_int(*scan, *next);
2923 KMP_ASSERT(count >= 0);
2930 if (**scan ==
'}' || **scan ==
',') {
2931 for (i = 0; i < count; i++) {
2932 if ((start > maxOsId) ||
2933 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2934 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2935 && (__kmp_affinity_type != affinity_none))) {
2936 KMP_WARNING(AffIgnoreInvalidProcID, start);
2941 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
2946 if (**scan ==
'}') {
2952 KMP_ASSERT2(**scan ==
':',
"bad explicit places list");
2961 if (**scan ==
'+') {
2965 if (**scan ==
'-') {
2973 KMP_ASSERT2((**scan >=
'0') && (**scan <=
'9'),
2974 "bad explicit places list");
2977 stride = __kmp_str_to_int(*scan, *next);
2978 KMP_ASSERT(stride >= 0);
2986 if (**scan ==
'}' || **scan ==
',') {
2987 for (i = 0; i < count; i++) {
2988 if ((start > maxOsId) ||
2989 (! KMP_CPU_ISSET(start, KMP_CPU_INDEX(osId2Mask, start)))) {
2990 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
2991 && (__kmp_affinity_type != affinity_none))) {
2992 KMP_WARNING(AffIgnoreInvalidProcID, start);
2997 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, start));
3002 if (**scan ==
'}') {
3009 KMP_ASSERT2(0,
"bad explicit places list");
3015 __kmp_process_place(
const char **scan, kmp_affin_mask_t *osId2Mask,
3016 int maxOsId, kmp_affin_mask_t *tempMask,
int *setSize)
3024 if (**scan ==
'{') {
3026 __kmp_process_subplace_list(scan, osId2Mask, maxOsId , tempMask,
3028 KMP_ASSERT2(**scan ==
'}',
"bad explicit places list");
3031 else if (**scan ==
'!') {
3032 __kmp_process_place(scan, osId2Mask, maxOsId, tempMask, setSize);
3033 KMP_CPU_COMPLEMENT(tempMask);
3036 else if ((**scan >=
'0') && (**scan <=
'9')) {
3039 int num = __kmp_str_to_int(*scan, *next);
3040 KMP_ASSERT(num >= 0);
3041 if ((num > maxOsId) ||
3042 (! KMP_CPU_ISSET(num, KMP_CPU_INDEX(osId2Mask, num)))) {
3043 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3044 && (__kmp_affinity_type != affinity_none))) {
3045 KMP_WARNING(AffIgnoreInvalidProcID, num);
3049 KMP_CPU_UNION(tempMask, KMP_CPU_INDEX(osId2Mask, num));
3055 KMP_ASSERT2(0,
"bad explicit places list");
3061 __kmp_affinity_process_placelist(kmp_affin_mask_t **out_masks,
3062 unsigned int *out_numMasks,
const char *placelist,
3063 kmp_affin_mask_t *osId2Mask,
int maxOsId)
3065 const char *scan = placelist;
3066 const char *next = placelist;
3069 newMasks = (kmp_affin_mask_t *)KMP_INTERNAL_MALLOC(numNewMasks
3070 * __kmp_affin_mask_size);
3073 kmp_affin_mask_t *tempMask = (kmp_affin_mask_t *)__kmp_allocate(
3074 __kmp_affin_mask_size);
3075 KMP_CPU_ZERO(tempMask);
3079 int start, count, stride;
3081 __kmp_process_place(&scan, osId2Mask, maxOsId, tempMask, &setSize);
3087 if (*scan ==
'\0' || *scan ==
',') {
3091 KMP_CPU_ZERO(tempMask);
3093 if (*scan ==
'\0') {
3100 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3107 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3108 "bad explicit places list");
3111 count = __kmp_str_to_int(scan, *next);
3112 KMP_ASSERT(count >= 0);
3119 if (*scan ==
'\0' || *scan ==
',') {
3121 for (i = 0; i < count; i++) {
3128 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j > 0; j--) {
3133 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3134 KMP_CPU_SET(j, tempMask);
3138 KMP_CPU_CLR(j, tempMask);
3141 for (; j >= 0; j--) {
3142 KMP_CPU_CLR(j, tempMask);
3145 KMP_CPU_ZERO(tempMask);
3148 if (*scan ==
'\0') {
3155 KMP_ASSERT2(*scan ==
':',
"bad explicit places list");
3176 KMP_ASSERT2((*scan >=
'0') && (*scan <=
'9'),
3177 "bad explicit places list");
3180 stride = __kmp_str_to_int(scan, *next);
3181 KMP_DEBUG_ASSERT(stride >= 0);
3187 for (i = 0; i < count; i++) {
3194 for (j = __kmp_affin_mask_size * CHAR_BIT - 1; j >= stride; j--) {
3195 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3196 KMP_CPU_SET(j, tempMask);
3200 KMP_CPU_CLR(j, tempMask);
3203 for (; j >= 0; j--) {
3204 KMP_CPU_CLR(j, tempMask);
3210 for (i = 0; i < count; i++) {
3217 for (j = 0; j < (__kmp_affin_mask_size * CHAR_BIT) + stride;
3219 if (KMP_CPU_ISSET(j - stride, tempMask)) {
3220 KMP_CPU_SET(j, tempMask);
3224 KMP_CPU_CLR(j, tempMask);
3227 for (; j < __kmp_affin_mask_size * CHAR_BIT; j++) {
3228 KMP_CPU_CLR(j, tempMask);
3232 KMP_CPU_ZERO(tempMask);
3239 if (*scan ==
'\0') {
3247 KMP_ASSERT2(0,
"bad explicit places list");
3250 *out_numMasks = nextNewMask;
3251 if (nextNewMask == 0) {
3253 KMP_INTERNAL_FREE(newMasks);
3257 = (kmp_affin_mask_t *)__kmp_allocate(nextNewMask * __kmp_affin_mask_size);
3258 memcpy(*out_masks, newMasks, nextNewMask * __kmp_affin_mask_size);
3259 __kmp_free(tempMask);
3260 KMP_INTERNAL_FREE(newMasks);
3266 #undef ADD_MASK_OSID
3272 __kmp_apply_thread_places(AddrUnsPair **pAddr,
int depth)
3274 if ( __kmp_place_num_cores == 0 ) {
3275 if ( __kmp_place_num_threads_per_core == 0 ) {
3278 __kmp_place_num_cores = nCoresPerPkg;
3280 if ( !__kmp_affinity_uniform_topology() || depth != 3 ) {
3281 KMP_WARNING( AffThrPlaceUnsupported );
3284 if ( __kmp_place_num_threads_per_core == 0 ) {
3285 __kmp_place_num_threads_per_core = __kmp_nThreadsPerCore;
3287 if ( __kmp_place_core_offset + __kmp_place_num_cores > nCoresPerPkg ) {
3288 KMP_WARNING( AffThrPlaceManyCores );
3292 AddrUnsPair *newAddr = (AddrUnsPair *)__kmp_allocate(
sizeof(AddrUnsPair) *
3293 nPackages * __kmp_place_num_cores * __kmp_place_num_threads_per_core);
3294 int i, j, k, n_old = 0, n_new = 0;
3295 for ( i = 0; i < nPackages; ++i ) {
3296 for ( j = 0; j < nCoresPerPkg; ++j ) {
3297 if ( j < __kmp_place_core_offset || j >= __kmp_place_core_offset + __kmp_place_num_cores ) {
3298 n_old += __kmp_nThreadsPerCore;
3300 for ( k = 0; k < __kmp_nThreadsPerCore; ++k ) {
3301 if ( k < __kmp_place_num_threads_per_core ) {
3302 newAddr[n_new] = (*pAddr)[n_old];
3310 nCoresPerPkg = __kmp_place_num_cores;
3311 __kmp_nThreadsPerCore = __kmp_place_num_threads_per_core;
3312 __kmp_avail_proc = n_new;
3313 __kmp_ncores = nPackages * __kmp_place_num_cores;
3315 __kmp_free( *pAddr );
3322 static AddrUnsPair *address2os = NULL;
3323 static int * procarr = NULL;
3324 static int __kmp_aff_depth = 0;
3327 __kmp_aux_affinity_initialize(
void)
3329 if (__kmp_affinity_masks != NULL) {
3330 KMP_ASSERT(fullMask != NULL);
3340 if (fullMask == NULL) {
3341 fullMask = (kmp_affin_mask_t *)__kmp_allocate(__kmp_affin_mask_size);
3343 if (KMP_AFFINITY_CAPABLE()) {
3344 if (__kmp_affinity_respect_mask) {
3345 __kmp_get_system_affinity(fullMask, TRUE);
3351 __kmp_avail_proc = 0;
3352 for (i = 0; i < KMP_CPU_SETSIZE; ++i) {
3353 if (! KMP_CPU_ISSET(i, fullMask)) {
3358 if (__kmp_avail_proc > __kmp_xproc) {
3359 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3360 && (__kmp_affinity_type != affinity_none))) {
3361 KMP_WARNING(ErrorInitializeAffinity);
3363 __kmp_affinity_type = affinity_none;
3364 __kmp_affin_mask_size = 0;
3369 __kmp_affinity_entire_machine_mask(fullMask);
3370 __kmp_avail_proc = __kmp_xproc;
3375 kmp_i18n_id_t msg_id = kmp_i18n_null;
3381 if ((__kmp_cpuinfo_file != NULL) &&
3382 (__kmp_affinity_top_method == affinity_top_method_all)) {
3383 __kmp_affinity_top_method = affinity_top_method_cpuinfo;
3386 if (__kmp_affinity_top_method == affinity_top_method_all) {
3392 const char *file_name = NULL;
3395 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3397 if (__kmp_affinity_verbose) {
3398 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY", KMP_I18N_STR(Decodingx2APIC));
3402 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3404 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3405 KMP_ASSERT(address2os == NULL);
3410 if ((msg_id != kmp_i18n_null)
3411 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3412 && (__kmp_affinity_type != affinity_none)))) {
3414 if (__kmp_affinity_verbose) {
3415 KMP_INFORM(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3416 KMP_I18N_STR(DecodingLegacyAPIC));
3419 KMP_WARNING(AffInfoStrStr,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
3420 KMP_I18N_STR(DecodingLegacyAPIC));
3425 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3427 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3428 KMP_ASSERT(address2os == NULL);
3438 if ((msg_id != kmp_i18n_null)
3439 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3440 && (__kmp_affinity_type != affinity_none)))) {
3442 if (__kmp_affinity_verbose) {
3443 KMP_INFORM(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3446 KMP_WARNING(AffStrParseFilename,
"KMP_AFFINITY", __kmp_i18n_catgets(msg_id),
"/proc/cpuinfo");
3449 else if (__kmp_affinity_verbose) {
3450 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY",
"/proc/cpuinfo");
3453 FILE *f = fopen(
"/proc/cpuinfo",
"r");
3455 msg_id = kmp_i18n_str_CantOpenCpuinfo;
3458 file_name =
"/proc/cpuinfo";
3459 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3462 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3463 KMP_ASSERT(address2os == NULL);
3472 if (msg_id != kmp_i18n_null
3473 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3474 && (__kmp_affinity_type != affinity_none)))) {
3475 if (file_name == NULL) {
3476 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3478 else if (line == 0) {
3479 KMP_WARNING(UsingFlatOSFile, file_name, __kmp_i18n_catgets(msg_id));
3482 KMP_WARNING(UsingFlatOSFileLine, file_name, line, __kmp_i18n_catgets(msg_id));
3487 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3489 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3490 KMP_ASSERT(address2os == NULL);
3493 KMP_ASSERT(depth > 0);
3494 KMP_ASSERT(address2os != NULL);
3504 # if KMP_ARCH_X86 || KMP_ARCH_X86_64
3506 else if (__kmp_affinity_top_method == affinity_top_method_x2apicid) {
3507 if (__kmp_affinity_verbose) {
3508 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3509 KMP_I18N_STR(Decodingx2APIC));
3512 depth = __kmp_affinity_create_x2apicid_map(&address2os, &msg_id);
3514 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3515 KMP_ASSERT(address2os == NULL);
3520 KMP_ASSERT(msg_id != kmp_i18n_null);
3521 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3524 else if (__kmp_affinity_top_method == affinity_top_method_apicid) {
3525 if (__kmp_affinity_verbose) {
3526 KMP_INFORM(AffInfoStr,
"KMP_AFFINITY",
3527 KMP_I18N_STR(DecodingLegacyAPIC));
3530 depth = __kmp_affinity_create_apicid_map(&address2os, &msg_id);
3532 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3533 KMP_ASSERT(address2os == NULL);
3538 KMP_ASSERT(msg_id != kmp_i18n_null);
3539 KMP_FATAL(MsgExiting, __kmp_i18n_catgets(msg_id));
3545 else if (__kmp_affinity_top_method == affinity_top_method_cpuinfo) {
3546 const char *filename;
3547 if (__kmp_cpuinfo_file != NULL) {
3548 filename = __kmp_cpuinfo_file;
3551 filename =
"/proc/cpuinfo";
3554 if (__kmp_affinity_verbose) {
3555 KMP_INFORM(AffParseFilename,
"KMP_AFFINITY", filename);
3558 FILE *f = fopen(filename,
"r");
3561 if (__kmp_cpuinfo_file != NULL) {
3564 KMP_MSG(CantOpenFileForReading, filename),
3566 KMP_HNT(NameComesFrom_CPUINFO_FILE),
3573 KMP_MSG(CantOpenFileForReading, filename),
3580 depth = __kmp_affinity_create_cpuinfo_map(&address2os, &line, &msg_id, f);
3583 KMP_ASSERT(msg_id != kmp_i18n_null);
3585 KMP_FATAL(FileLineMsgExiting, filename, line, __kmp_i18n_catgets(msg_id));
3588 KMP_FATAL(FileMsgExiting, filename, __kmp_i18n_catgets(msg_id));
3591 if (__kmp_affinity_type == affinity_none) {
3592 KMP_ASSERT(depth == 0);
3593 KMP_ASSERT(address2os == NULL);
3598 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3600 else if (__kmp_affinity_top_method == affinity_top_method_group) {
3601 if (__kmp_affinity_verbose) {
3602 KMP_INFORM(AffWindowsProcGroupMap,
"KMP_AFFINITY");
3605 depth = __kmp_affinity_create_proc_group_map(&address2os, &msg_id);
3606 KMP_ASSERT(depth != 0);
3609 if ((msg_id != kmp_i18n_null)
3610 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3611 && (__kmp_affinity_type != affinity_none)))) {
3612 KMP_WARNING(UsingFlatOS, __kmp_i18n_catgets(msg_id));
3615 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3617 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3618 KMP_ASSERT(address2os == NULL);
3622 KMP_ASSERT(depth > 0);
3623 KMP_ASSERT(address2os != NULL);
3629 else if (__kmp_affinity_top_method == affinity_top_method_flat) {
3630 if (__kmp_affinity_verbose) {
3631 KMP_INFORM(AffUsingFlatOS,
"KMP_AFFINITY");
3634 depth = __kmp_affinity_create_flat_map(&address2os, &msg_id);
3636 KMP_ASSERT(__kmp_affinity_type == affinity_none);
3637 KMP_ASSERT(address2os == NULL);
3641 KMP_ASSERT(depth > 0);
3642 KMP_ASSERT(address2os != NULL);
3645 if (address2os == NULL) {
3646 if (KMP_AFFINITY_CAPABLE()
3647 && (__kmp_affinity_verbose || (__kmp_affinity_warnings
3648 && (__kmp_affinity_type != affinity_none)))) {
3649 KMP_WARNING(ErrorInitializeAffinity);
3651 __kmp_affinity_type = affinity_none;
3652 __kmp_affin_mask_size = 0;
3657 __kmp_apply_thread_places(&address2os, depth);
3665 kmp_affin_mask_t *osId2Mask = __kmp_create_masks(&maxIndex, &numUnique,
3666 address2os, __kmp_avail_proc);
3667 if (__kmp_affinity_gran_levels == 0) {
3668 KMP_DEBUG_ASSERT(numUnique == __kmp_avail_proc);
3676 __kmp_affinity_assign_child_nums(address2os, __kmp_avail_proc);
3678 switch (__kmp_affinity_type) {
3680 case affinity_explicit:
3681 KMP_DEBUG_ASSERT(__kmp_affinity_proclist != NULL);
3683 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3686 __kmp_affinity_process_proclist(&__kmp_affinity_masks,
3687 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3692 __kmp_affinity_process_placelist(&__kmp_affinity_masks,
3693 &__kmp_affinity_num_masks, __kmp_affinity_proclist, osId2Mask,
3697 if (__kmp_affinity_num_masks == 0) {
3698 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
3699 && (__kmp_affinity_type != affinity_none))) {
3700 KMP_WARNING(AffNoValidProcID);
3702 __kmp_affinity_type = affinity_none;
3715 case affinity_logical:
3716 __kmp_affinity_compact = 0;
3717 if (__kmp_affinity_offset) {
3718 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3723 case affinity_physical:
3724 if (__kmp_nThreadsPerCore > 1) {
3725 __kmp_affinity_compact = 1;
3726 if (__kmp_affinity_compact >= depth) {
3727 __kmp_affinity_compact = 0;
3730 __kmp_affinity_compact = 0;
3732 if (__kmp_affinity_offset) {
3733 __kmp_affinity_offset = __kmp_nThreadsPerCore * __kmp_affinity_offset
3738 case affinity_scatter:
3739 if (__kmp_affinity_compact >= depth) {
3740 __kmp_affinity_compact = 0;
3743 __kmp_affinity_compact = depth - 1 - __kmp_affinity_compact;
3747 case affinity_compact:
3748 if (__kmp_affinity_compact >= depth) {
3749 __kmp_affinity_compact = depth - 1;
3754 case affinity_balanced:
3756 if( nPackages > 1 ) {
3757 if( __kmp_affinity_verbose || __kmp_affinity_warnings ) {
3758 KMP_WARNING( AffBalancedNotAvail,
"KMP_AFFINITY" );
3760 __kmp_affinity_type = affinity_none;
3762 }
else if( __kmp_affinity_uniform_topology() ) {
3767 __kmp_aff_depth = depth;
3770 int nth_per_core = __kmp_nThreadsPerCore;
3773 if( nth_per_core > 1 ) {
3774 core_level = depth - 2;
3776 core_level = depth - 1;
3778 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
3779 int nproc = nth_per_core * ncores;
3781 procarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
3782 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 k = core * nth_per_core + thread;
3803 procarr[ k ] = proc;
3815 if (__kmp_affinity_dups) {
3816 __kmp_affinity_num_masks = __kmp_avail_proc;
3819 __kmp_affinity_num_masks = numUnique;
3823 if ( ( __kmp_nested_proc_bind.bind_types[0] != proc_bind_intel )
3824 && ( __kmp_affinity_num_places > 0 )
3825 && ( __kmp_affinity_num_places < __kmp_affinity_num_masks ) ) {
3826 __kmp_affinity_num_masks = __kmp_affinity_num_places;
3830 __kmp_affinity_masks = (kmp_affin_mask_t*)__kmp_allocate(
3831 __kmp_affinity_num_masks * __kmp_affin_mask_size);
3837 qsort(address2os, __kmp_avail_proc,
sizeof(*address2os),
3838 __kmp_affinity_cmp_Address_child_num);
3842 for (i = 0, j = 0; i < __kmp_avail_proc; i++) {
3843 if ((! __kmp_affinity_dups) && (! address2os[i].first.leader)) {
3846 unsigned osId = address2os[i].second;
3847 kmp_affin_mask_t *src = KMP_CPU_INDEX(osId2Mask, osId);
3848 kmp_affin_mask_t *dest
3849 = KMP_CPU_INDEX(__kmp_affinity_masks, j);
3850 KMP_ASSERT(KMP_CPU_ISSET(osId, src));
3851 KMP_CPU_COPY(dest, src);
3852 if (++j >= __kmp_affinity_num_masks) {
3856 KMP_DEBUG_ASSERT(j == __kmp_affinity_num_masks);
3861 KMP_ASSERT2(0,
"Unexpected affinity setting");
3864 __kmp_free(osId2Mask);
3869 __kmp_affinity_initialize(
void)
3882 int disabled = (__kmp_affinity_type == affinity_disabled);
3883 if (! KMP_AFFINITY_CAPABLE()) {
3884 KMP_ASSERT(disabled);
3887 __kmp_affinity_type = affinity_none;
3889 __kmp_aux_affinity_initialize();
3891 __kmp_affinity_type = affinity_disabled;
3897 __kmp_affinity_uninitialize(
void)
3899 if (__kmp_affinity_masks != NULL) {
3900 __kmp_free(__kmp_affinity_masks);
3901 __kmp_affinity_masks = NULL;
3903 if (fullMask != NULL) {
3904 KMP_CPU_FREE(fullMask);
3907 __kmp_affinity_num_masks = 0;
3909 __kmp_affinity_num_places = 0;
3911 if (__kmp_affinity_proclist != NULL) {
3912 __kmp_free(__kmp_affinity_proclist);
3913 __kmp_affinity_proclist = NULL;
3915 if( address2os != NULL ) {
3916 __kmp_free( address2os );
3919 if( procarr != NULL ) {
3920 __kmp_free( procarr );
3927 __kmp_affinity_set_init_mask(
int gtid,
int isa_root)
3929 if (! KMP_AFFINITY_CAPABLE()) {
3933 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
3934 if (th->th.th_affin_mask == NULL) {
3935 KMP_CPU_ALLOC(th->th.th_affin_mask);
3938 KMP_CPU_ZERO(th->th.th_affin_mask);
3948 kmp_affin_mask_t *mask;
3952 if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
3955 if ((__kmp_affinity_type == affinity_none)
3957 || (__kmp_affinity_type == affinity_balanced)
3960 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3961 if (__kmp_num_proc_groups > 1) {
3965 KMP_ASSERT(fullMask != NULL);
3970 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
3971 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3972 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
3978 || (__kmp_nested_proc_bind.bind_types[0] == proc_bind_false)) {
3979 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
3980 if (__kmp_num_proc_groups > 1) {
3984 KMP_ASSERT(fullMask != NULL);
3993 KMP_DEBUG_ASSERT( __kmp_affinity_num_masks > 0 );
3994 i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks;
3995 mask = KMP_CPU_INDEX(__kmp_affinity_masks, i);
4001 th->th.th_current_place = i;
4003 th->th.th_new_place = i;
4004 th->th.th_first_place = 0;
4005 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4008 if (i == KMP_PLACE_ALL) {
4009 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to all places\n",
4013 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to place %d\n",
4018 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to fullMask\n",
4022 KA_TRACE(100, (
"__kmp_affinity_set_init_mask: binding T#%d to mask %d\n",
4027 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4029 if (__kmp_affinity_verbose) {
4030 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4031 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4032 th->th.th_affin_mask);
4033 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", gtid, buf);
4042 if ( __kmp_affinity_type == affinity_none ) {
4043 __kmp_set_system_affinity(th->th.th_affin_mask, FALSE);
4047 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4054 __kmp_affinity_set_place(
int gtid)
4058 if (! KMP_AFFINITY_CAPABLE()) {
4062 kmp_info_t *th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[gtid]);
4064 KA_TRACE(100, (
"__kmp_affinity_set_place: binding T#%d to place %d (current place = %d)\n",
4065 gtid, th->th.th_new_place, th->th.th_current_place));
4070 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4071 KMP_DEBUG_ASSERT(th->th.th_new_place >= 0);
4072 KMP_DEBUG_ASSERT(th->th.th_new_place <= __kmp_affinity_num_masks);
4073 if (th->th.th_first_place <= th->th.th_last_place) {
4074 KMP_DEBUG_ASSERT((th->th.th_new_place >= th->th.th_first_place)
4075 && (th->th.th_new_place <= th->th.th_last_place));
4078 KMP_DEBUG_ASSERT((th->th.th_new_place <= th->th.th_first_place)
4079 || (th->th.th_new_place >= th->th.th_last_place));
4086 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks,
4087 th->th.th_new_place);
4088 KMP_CPU_COPY(th->th.th_affin_mask, mask);
4089 th->th.th_current_place = th->th.th_new_place;
4091 if (__kmp_affinity_verbose) {
4092 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4093 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4094 th->th.th_affin_mask);
4095 KMP_INFORM(BoundToOSProcSet,
"OMP_PROC_BIND", gtid, buf);
4097 __kmp_set_system_affinity(th->th.th_affin_mask, TRUE);
4104 __kmp_aux_set_affinity(
void **mask)
4110 if (! KMP_AFFINITY_CAPABLE()) {
4114 gtid = __kmp_entry_gtid();
4116 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4117 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4118 (kmp_affin_mask_t *)(*mask));
4119 __kmp_debug_printf(
"kmp_set_affinity: setting affinity mask for thread %d = %s\n",
4123 if (__kmp_env_consistency_check) {
4124 if ((mask == NULL) || (*mask == NULL)) {
4125 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4131 for (proc = 0; proc < KMP_CPU_SETSIZE; proc++) {
4132 if (! KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask))) {
4136 if (! KMP_CPU_ISSET(proc, fullMask)) {
4137 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4141 if (num_procs == 0) {
4142 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4145 # if KMP_OS_WINDOWS && KMP_ARCH_X86_64
4146 if (__kmp_get_proc_group((kmp_affin_mask_t *)(*mask)) < 0) {
4147 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
4154 th = __kmp_threads[gtid];
4155 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4156 retval = __kmp_set_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4158 KMP_CPU_COPY(th->th.th_affin_mask, (kmp_affin_mask_t *)(*mask));
4162 th->th.th_current_place = KMP_PLACE_UNDEFINED;
4163 th->th.th_new_place = KMP_PLACE_UNDEFINED;
4164 th->th.th_first_place = 0;
4165 th->th.th_last_place = __kmp_affinity_num_masks - 1;
4173 __kmp_aux_get_affinity(
void **mask)
4179 if (! KMP_AFFINITY_CAPABLE()) {
4183 gtid = __kmp_entry_gtid();
4184 th = __kmp_threads[gtid];
4185 KMP_DEBUG_ASSERT(th->th.th_affin_mask != NULL);
4188 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4189 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4190 th->th.th_affin_mask);
4191 __kmp_printf(
"kmp_get_affinity: stored affinity mask for thread %d = %s\n", gtid, buf);
4194 if (__kmp_env_consistency_check) {
4195 if ((mask == NULL) || (*mask == NULL)) {
4196 KMP_FATAL(AffinityInvalidMask,
"kmp_get_affinity");
4200 # if !KMP_OS_WINDOWS
4202 retval = __kmp_get_system_affinity((kmp_affin_mask_t *)(*mask), FALSE);
4204 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4205 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4206 (kmp_affin_mask_t *)(*mask));
4207 __kmp_printf(
"kmp_get_affinity: system affinity mask for thread %d = %s\n", gtid, buf);
4213 KMP_CPU_COPY((kmp_affin_mask_t *)(*mask), th->th.th_affin_mask);
4222 __kmp_aux_set_affinity_mask_proc(
int proc,
void **mask)
4226 if (! KMP_AFFINITY_CAPABLE()) {
4231 int gtid = __kmp_entry_gtid();
4232 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4233 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4234 (kmp_affin_mask_t *)(*mask));
4235 __kmp_debug_printf(
"kmp_set_affinity_mask_proc: setting proc %d in affinity mask for thread %d = %s\n",
4239 if (__kmp_env_consistency_check) {
4240 if ((mask == NULL) || (*mask == NULL)) {
4241 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4245 if ((proc < 0) || (proc >= KMP_CPU_SETSIZE)) {
4248 if (! KMP_CPU_ISSET(proc, fullMask)) {
4252 KMP_CPU_SET(proc, (kmp_affin_mask_t *)(*mask));
4258 __kmp_aux_unset_affinity_mask_proc(
int proc,
void **mask)
4262 if (! KMP_AFFINITY_CAPABLE()) {
4267 int gtid = __kmp_entry_gtid();
4268 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4269 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4270 (kmp_affin_mask_t *)(*mask));
4271 __kmp_debug_printf(
"kmp_unset_affinity_mask_proc: unsetting proc %d in affinity mask for thread %d = %s\n",
4275 if (__kmp_env_consistency_check) {
4276 if ((mask == NULL) || (*mask == NULL)) {
4277 KMP_FATAL(AffinityInvalidMask,
"kmp_unset_affinity_mask_proc");
4281 if ((proc < 0) || (proc >= KMP_CPU_SETSIZE)) {
4284 if (! KMP_CPU_ISSET(proc, fullMask)) {
4288 KMP_CPU_CLR(proc, (kmp_affin_mask_t *)(*mask));
4294 __kmp_aux_get_affinity_mask_proc(
int proc,
void **mask)
4298 if (! KMP_AFFINITY_CAPABLE()) {
4303 int gtid = __kmp_entry_gtid();
4304 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4305 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN,
4306 (kmp_affin_mask_t *)(*mask));
4307 __kmp_debug_printf(
"kmp_get_affinity_mask_proc: getting proc %d in affinity mask for thread %d = %s\n",
4311 if (__kmp_env_consistency_check) {
4312 if ((mask == NULL) || (*mask == NULL)) {
4313 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity_mask_proc");
4317 if ((proc < 0) || (proc >= KMP_CPU_SETSIZE)) {
4320 if (! KMP_CPU_ISSET(proc, fullMask)) {
4324 return KMP_CPU_ISSET(proc, (kmp_affin_mask_t *)(*mask));
4330 void __kmp_balanced_affinity(
int tid,
int nthreads )
4332 if( __kmp_affinity_uniform_topology() ) {
4336 int __kmp_nth_per_core = __kmp_avail_proc / __kmp_ncores;
4338 int ncores = __kmp_ncores;
4340 int chunk = nthreads / ncores;
4342 int big_cores = nthreads % ncores;
4344 int big_nth = ( chunk + 1 ) * big_cores;
4345 if( tid < big_nth ) {
4346 coreID = tid / (chunk + 1 );
4347 threadID = ( tid % (chunk + 1 ) ) % __kmp_nth_per_core ;
4349 coreID = ( tid - big_cores ) / chunk;
4350 threadID = ( ( tid - big_cores ) % chunk ) % __kmp_nth_per_core ;
4353 KMP_DEBUG_ASSERT2(KMP_AFFINITY_CAPABLE(),
4354 "Illegal set affinity operation when not capable");
4356 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4360 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4361 int osID = address2os[ coreID * __kmp_nth_per_core + threadID ].second;
4362 KMP_CPU_SET( osID, mask);
4363 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4364 for(
int i = 0; i < __kmp_nth_per_core; i++ ) {
4366 osID = address2os[ coreID * __kmp_nth_per_core + i ].second;
4367 KMP_CPU_SET( osID, mask);
4370 if (__kmp_affinity_verbose) {
4371 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4372 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4373 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4375 __kmp_set_system_affinity( mask, TRUE );
4378 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
4382 int nth_per_core = __kmp_nThreadsPerCore;
4384 if( nth_per_core > 1 ) {
4385 core_level = __kmp_aff_depth - 2;
4387 core_level = __kmp_aff_depth - 1;
4391 int ncores = address2os[ __kmp_avail_proc - 1 ].first.labels[ core_level ] + 1;
4394 if( nthreads == __kmp_avail_proc ) {
4395 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4396 int osID = address2os[ tid ].second;
4397 KMP_CPU_SET( osID, mask);
4398 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4399 int coreID = address2os[ tid ].first.labels[ core_level ];
4403 for(
int i = 0; i < __kmp_avail_proc; i++ ) {
4404 int osID = address2os[ i ].second;
4405 int core = address2os[ i ].first.labels[ core_level ];
4406 if( core == coreID ) {
4407 KMP_CPU_SET( osID, mask);
4409 if( cnt == nth_per_core ) {
4415 }
else if( nthreads <= __kmp_ncores ) {
4418 for(
int i = 0; i < ncores; i++ ) {
4421 for(
int j = 0; j < nth_per_core; j++ ) {
4422 if( procarr[ i * nth_per_core + j ] != - 1 ) {
4429 for(
int j = 0; j < nth_per_core; j++ ) {
4430 int osID = procarr[ i * nth_per_core + j ];
4432 KMP_CPU_SET( osID, mask );
4434 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4449 int nproc_at_core[ ncores ];
4451 int ncores_with_x_procs[ nth_per_core + 1 ];
4453 int ncores_with_x_to_max_procs[ nth_per_core + 1 ];
4455 for(
int i = 0; i <= nth_per_core; i++ ) {
4456 ncores_with_x_procs[ i ] = 0;
4457 ncores_with_x_to_max_procs[ i ] = 0;
4460 for(
int i = 0; i < ncores; i++ ) {
4462 for(
int j = 0; j < nth_per_core; j++ ) {
4463 if( procarr[ i * nth_per_core + j ] != -1 ) {
4467 nproc_at_core[ i ] = cnt;
4468 ncores_with_x_procs[ cnt ]++;
4471 for(
int i = 0; i <= nth_per_core; i++ ) {
4472 for(
int j = i; j <= nth_per_core; j++ ) {
4473 ncores_with_x_to_max_procs[ i ] += ncores_with_x_procs[ j ];
4478 int nproc = nth_per_core * ncores;
4480 int * newarr = (
int * )__kmp_allocate(
sizeof(
int ) * nproc );
4481 for(
int i = 0; i < nproc; i++ ) {
4488 for(
int j = 1; j <= nth_per_core; j++ ) {
4489 int cnt = ncores_with_x_to_max_procs[ j ];
4490 for(
int i = 0; i < ncores; i++ ) {
4492 if( nproc_at_core[ i ] == 0 ) {
4495 for(
int k = 0; k < nth_per_core; k++ ) {
4496 if( procarr[ i * nth_per_core + k ] != -1 ) {
4497 if( newarr[ i * nth_per_core + k ] == 0 ) {
4498 newarr[ i * nth_per_core + k ] = 1;
4504 newarr[ i * nth_per_core + k ] ++;
4512 if( cnt == 0 || nth == 0 ) {
4523 for(
int i = 0; i < nproc; i++ ) {
4527 if( __kmp_affinity_gran == affinity_gran_fine || __kmp_affinity_gran == affinity_gran_thread) {
4528 int osID = procarr[ i ];
4529 KMP_CPU_SET( osID, mask);
4530 }
else if( __kmp_affinity_gran == affinity_gran_core ) {
4531 int coreID = i / nth_per_core;
4532 for(
int ii = 0; ii < nth_per_core; ii++ ) {
4533 int osID = procarr[ coreID * nth_per_core + ii ];
4535 KMP_CPU_SET( osID, mask);
4542 __kmp_free( newarr );
4545 if (__kmp_affinity_verbose) {
4546 char buf[KMP_AFFIN_MASK_PRINT_LEN];
4547 __kmp_affinity_print_mask(buf, KMP_AFFIN_MASK_PRINT_LEN, mask);
4548 KMP_INFORM(BoundToOSProcSet,
"KMP_AFFINITY", tid, buf);
4550 __kmp_set_system_affinity( mask, TRUE );
4559 #error "Unknown or unsupported OS"
4560 #endif // KMP_OS_WINDOWS || KMP_OS_LINUX