diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp --- a/openmp/runtime/src/kmp_runtime.cpp +++ b/openmp/runtime/src/kmp_runtime.cpp @@ -3547,6 +3547,10 @@ newCapacity = newCapacity <= (__kmp_sys_max_nth >> 1) ? (newCapacity << 1) : __kmp_sys_max_nth; } while (newCapacity < minimumRequiredCapacity); + // If hidden helper thread is enabled, we also need to count the number + if (__kmp_enable_hidden_helper) { + newCapacity += __kmp_hidden_helper_threads_num; + } newThreads = (kmp_info_t **)__kmp_allocate( (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * newCapacity + CACHE_LINE); newRoot = diff --git a/openmp/runtime/src/kmp_settings.cpp b/openmp/runtime/src/kmp_settings.cpp --- a/openmp/runtime/src/kmp_settings.cpp +++ b/openmp/runtime/src/kmp_settings.cpp @@ -504,9 +504,10 @@ nth = (4 * __kmp_xproc); // If hidden helper task is enabled, we initialize the thread capacity with - // extra - // __kmp_hidden_helper_threads_num. - nth += __kmp_hidden_helper_threads_num; + // extra __kmp_hidden_helper_threads_num. + if (__kmp_enable_hidden_helper) { + nth += __kmp_hidden_helper_threads_num; + } if (nth > __kmp_max_nth) nth = __kmp_max_nth; diff --git a/openmp/runtime/test/tasking/hidden_helper_task/num_threads.cpp b/openmp/runtime/test/tasking/hidden_helper_task/num_threads.cpp new file mode 100644 --- /dev/null +++ b/openmp/runtime/test/tasking/hidden_helper_task/num_threads.cpp @@ -0,0 +1,35 @@ +// RUN: %libomp-cxx-compile-and-run + +#include + +#include +#include +#include + +int main(int argc, char *argv[]) { + constexpr const int __kmp_hidden_helper_threads_num = 8; + const int __kmp_threads_capacity = + std::min(std::max(std::max(32, 4 * omp_get_num_threads()), + 4 * omp_get_num_procs()), + std::numeric_limits::max()); + const int capacity = __kmp_threads_capacity + __kmp_hidden_helper_threads_num; + const int N = 2 * capacity; + + std::vector data(N); + +#pragma omp parallel for + for (unsigned i = 0; i < N; ++i) { + data[i] = i; + } + +#pragma omp parallel for num_threads(N) + for (unsigned i = 0; i < N; ++i) { + data[i] += i; + } + + for (unsigned i = 0; i < N; ++i) { + assert(data[i] == 2 * i); + } + + return 0; +}