Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
runtime/src/kmp_affinity.cpp
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
void KMPAffinity::destroy_api() { | void KMPAffinity::destroy_api() { | ||||
if (__kmp_affinity_dispatch != NULL) { | if (__kmp_affinity_dispatch != NULL) { | ||||
delete __kmp_affinity_dispatch; | delete __kmp_affinity_dispatch; | ||||
__kmp_affinity_dispatch = NULL; | __kmp_affinity_dispatch = NULL; | ||||
picked_api = false; | picked_api = false; | ||||
} | } | ||||
} | } | ||||
#define KMP_ADVANCE_SCAN(scan) \ | |||||
while (*scan != '\0') { \ | |||||
scan++; \ | |||||
} | |||||
// Print the affinity mask to the character array in a pretty format. | // Print the affinity mask to the character array in a pretty format. | ||||
// The format is a comma separated list of non-negative integers or integer | |||||
// ranges: e.g., 1,2,3-5,7,9-15 | |||||
// The format can also be the string "{<empty>}" if no bits are set in mask | |||||
char *__kmp_affinity_print_mask(char *buf, int buf_len, | char *__kmp_affinity_print_mask(char *buf, int buf_len, | ||||
kmp_affin_mask_t *mask) { | kmp_affin_mask_t *mask) { | ||||
int start = 0, finish = 0, previous = 0; | |||||
bool first_range; | |||||
KMP_ASSERT(buf); | |||||
KMP_ASSERT(buf_len >= 40); | KMP_ASSERT(buf_len >= 40); | ||||
KMP_ASSERT(mask); | |||||
char *scan = buf; | char *scan = buf; | ||||
char *end = buf + buf_len - 1; | char *end = buf + buf_len - 1; | ||||
// Find first element / check for empty set. | // Check for empty set. | ||||
int i; | if (mask->begin() == mask->end()) { | ||||
i = mask->begin(); | |||||
if (i == mask->end()) { | |||||
KMP_SNPRINTF(scan, end - scan + 1, "{<empty>}"); | KMP_SNPRINTF(scan, end - scan + 1, "{<empty>}"); | ||||
while (*scan != '\0') | KMP_ADVANCE_SCAN(scan); | ||||
scan++; | |||||
KMP_ASSERT(scan <= end); | KMP_ASSERT(scan <= end); | ||||
return buf; | return buf; | ||||
} | } | ||||
KMP_SNPRINTF(scan, end - scan + 1, "{%d", i); | first_range = true; | ||||
while (*scan != '\0') | start = mask->begin(); | ||||
scan++; | while (1) { | ||||
i++; | // Find next range | ||||
for (; i != mask->end(); i = mask->next(i)) { | // [start, previous] is inclusive range of contiguous bits in mask | ||||
if (!KMP_CPU_ISSET(i, mask)) { | for (finish = mask->next(start), previous = start; | ||||
continue; | finish == previous + 1 && finish != mask->end(); | ||||
finish = mask->next(finish)) { | |||||
previous = finish; | |||||
} | |||||
// The first range does not need a comma printed before it, but the rest | |||||
// of the ranges do need a comma beforehand | |||||
if (!first_range) { | |||||
KMP_SNPRINTF(scan, end - scan + 1, "%s", ","); | |||||
KMP_ADVANCE_SCAN(scan); | |||||
} else { | |||||
first_range = false; | |||||
} | |||||
// Range with three or more contiguous bits in the affinity mask | |||||
if (previous - start > 1) { | |||||
KMP_SNPRINTF(scan, end - scan + 1, "%d-%d", static_cast<int>(start), | |||||
static_cast<int>(previous)); | |||||
} else { | |||||
// Range with one or two contiguous bits in the affinity mask | |||||
KMP_SNPRINTF(scan, end - scan + 1, "%d", static_cast<int>(start)); | |||||
KMP_ADVANCE_SCAN(scan); | |||||
if (previous - start > 0) { | |||||
KMP_SNPRINTF(scan, end - scan + 1, ",%d", static_cast<int>(previous)); | |||||
} | |||||
} | |||||
KMP_ADVANCE_SCAN(scan); | |||||
// Start over with new start point | |||||
start = finish; | |||||
if (start == mask->end()) | |||||
break; | |||||
// Check for overflow | |||||
if (end - scan < 2) | |||||
break; | |||||
} | } | ||||
// Check for buffer overflow. A string of the form ",<n>" will have at most | // Check for overflow | ||||
// 10 characters, plus we want to leave room to print ",...}" if the set is | |||||
// too large to print for a total of 15 characters. We already left room for | |||||
// '\0' in setting end. | |||||
if (end - scan < 15) { | |||||
break; | |||||
} | |||||
KMP_SNPRINTF(scan, end - scan + 1, ",%-d", i); | |||||
while (*scan != '\0') | |||||
scan++; | |||||
} | |||||
if (i != mask->end()) { | |||||
KMP_SNPRINTF(scan, end - scan + 1, ",..."); | |||||
while (*scan != '\0') | |||||
scan++; | |||||
} | |||||
KMP_SNPRINTF(scan, end - scan + 1, "}"); | |||||
while (*scan != '\0') | |||||
scan++; | |||||
KMP_ASSERT(scan <= end); | KMP_ASSERT(scan <= end); | ||||
return buf; | return buf; | ||||
} | } | ||||
#undef KMP_ADVANCE_SCAN | |||||
// Print the affinity mask to the string buffer object in a pretty format | |||||
// The format is a comma separated list of non-negative integers or integer | |||||
// ranges: e.g., 1,2,3-5,7,9-15 | |||||
// The format can also be the string "{<empty>}" if no bits are set in mask | |||||
kmp_str_buf_t *__kmp_affinity_str_buf_mask(kmp_str_buf_t *buf, | |||||
kmp_affin_mask_t *mask) { | |||||
int start = 0, finish = 0, previous = 0; | |||||
bool first_range; | |||||
KMP_ASSERT(buf); | |||||
KMP_ASSERT(mask); | |||||
__kmp_str_buf_clear(buf); | |||||
// Check for empty set. | |||||
if (mask->begin() == mask->end()) { | |||||
__kmp_str_buf_print(buf, "%s", "{<empty>}"); | |||||
return buf; | |||||
} | |||||
first_range = true; | |||||
start = mask->begin(); | |||||
while (1) { | |||||
// Find next range | |||||
// [start, previous] is inclusive range of contiguous bits in mask | |||||
for (finish = mask->next(start), previous = start; | |||||
finish == previous + 1 && finish != mask->end(); | |||||
finish = mask->next(finish)) { | |||||
previous = finish; | |||||
} | |||||
// The first range does not need a comma printed before it, but the rest | |||||
// of the ranges do need a comma beforehand | |||||
if (!first_range) { | |||||
__kmp_str_buf_print(buf, "%s", ","); | |||||
} else { | |||||
first_range = false; | |||||
} | |||||
// Range with three or more contiguous bits in the affinity mask | |||||
if (previous - start > 1) { | |||||
__kmp_str_buf_print(buf, "%d-%d", static_cast<int>(start), | |||||
static_cast<int>(previous)); | |||||
} else { | |||||
// Range with one or two contiguous bits in the affinity mask | |||||
__kmp_str_buf_print(buf, "%d", static_cast<int>(start)); | |||||
if (previous - start > 0) { | |||||
__kmp_str_buf_print(buf, ",%d", static_cast<int>(previous)); | |||||
} | |||||
} | |||||
// Start over with new start point | |||||
start = finish; | |||||
if (start == mask->end()) | |||||
break; | |||||
} | |||||
return buf; | |||||
} | |||||
void __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) { | void __kmp_affinity_entire_machine_mask(kmp_affin_mask_t *mask) { | ||||
KMP_CPU_ZERO(mask); | KMP_CPU_ZERO(mask); | ||||
#if KMP_GROUP_AFFINITY | #if KMP_GROUP_AFFINITY | ||||
if (__kmp_num_proc_groups > 1) { | if (__kmp_num_proc_groups > 1) { | ||||
int group; | int group; | ||||
▲ Show 20 Lines • Show All 5,152 Lines • Show Last 20 Lines |