diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h --- a/openmp/runtime/src/kmp.h +++ b/openmp/runtime/src/kmp.h @@ -4072,6 +4072,18 @@ #define KMP_GTID_TO_SHADOW_GTID(gtid) \ ((gtid) % (__kmp_hidden_helper_threads_num - 1) + 2) +// Return the adjusted gtid value by subtracting from gtid the number +// of hidden helper threads. This adjusted value is the gtid the thread would +// have received if there were no hidden helper threads. +static inline int __kmp_adjust_gtid_for_hidden_helpers(int gtid) { + int adjusted_gtid = gtid; + if (__kmp_enable_hidden_helper && __kmp_hidden_helper_threads_num > 0 && + gtid > 0 && adjusted_gtid - __kmp_hidden_helper_threads_num >= 0) { + adjusted_gtid -= __kmp_hidden_helper_threads_num; + } + return adjusted_gtid; +} + // Support for error directive typedef enum kmp_severity_t { severity_warning = 1, diff --git a/openmp/runtime/src/kmp_affinity.cpp b/openmp/runtime/src/kmp_affinity.cpp --- a/openmp/runtime/src/kmp_affinity.cpp +++ b/openmp/runtime/src/kmp_affinity.cpp @@ -3950,8 +3950,9 @@ i = 0; mask = __kmp_affin_fullMask; } else { + int mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid); KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0); - i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks; + i = (mask_idx + __kmp_affinity_offset) % __kmp_affinity_num_masks; mask = KMP_CPU_INDEX(__kmp_affinity_masks, i); } } else { @@ -3967,9 +3968,10 @@ mask = __kmp_affin_fullMask; } else { // int i = some hash function or just a counter that doesn't - // always start at 0. Use gtid for now. + // always start at 0. Use adjusted gtid for now. + int mask_idx = __kmp_adjust_gtid_for_hidden_helpers(gtid); KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0); - i = (gtid + __kmp_affinity_offset) % __kmp_affinity_num_masks; + i = (mask_idx + __kmp_affinity_offset) % __kmp_affinity_num_masks; mask = KMP_CPU_INDEX(__kmp_affinity_masks, i); } }