OSDN Git Service

[OpenMP] Fixed a crash in hidden helper thread
authorShilei Tian <tianshilei1992@gmail.com>
Thu, 18 Mar 2021 22:25:21 +0000 (18:25 -0400)
committerTom Stellard <tstellar@redhat.com>
Mon, 29 Mar 2021 23:29:53 +0000 (16:29 -0700)
commitf43958b7c497c526b238607624ee0069888f4c98
tree8238f8b3f16bd660fef732cb4f3be86f7ef20eaa
parente94372d1b395a6461e7d973917b3a3c29563a5e6
[OpenMP] Fixed a crash in hidden helper thread

It is reported that after enabling hidden helper thread, the program
can hit the assertion `new_gtid < __kmp_threads_capacity` sometimes. The root
cause is explained as follows. Let's say the default `__kmp_threads_capacity` is
`N`. If hidden helper thread is enabled, `__kmp_threads_capacity` will be offset
to `N+8` by default. If the number of threads we need exceeds `N+8`, e.g. via
`num_threads` clause, we need to expand `__kmp_threads`. In
`__kmp_expand_threads`, the expansion starts from `__kmp_threads_capacity`, and
repeatedly doubling it until the new capacity meets the requirement. Let's
assume the new requirement is `Y`.  If `Y` happens to meet the constraint
`(N+8)*2^X=Y` where `X` is the number of iterations, the new capacity is not
enough because we have 8 slots for hidden helper threads.

Here is an example.
```
#include <vector>

int main(int argc, char *argv[]) {
  constexpr const size_t N = 1344;
  std::vector<int> 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;
  }

  return 0;
}
```
My CPU is 20C40T, then `__kmp_threads_capacity` is 160. After offset,
`__kmp_threads_capacity` becomes 168. `1344 = (160+8)*2^3`, then the assertions
hit.

Reviewed By: protze.joachim

Differential Revision: https://reviews.llvm.org/D98838

(cherry picked from commit 2df65f87c1ea81008768e14522e5d9277234ba70)
openmp/runtime/src/kmp_runtime.cpp
openmp/runtime/src/kmp_settings.cpp
openmp/runtime/test/tasking/hidden_helper_task/capacity_mix_threads.cpp [new file with mode: 0644]
openmp/runtime/test/tasking/hidden_helper_task/capacity_nthreads.cpp [new file with mode: 0644]