[all-commits] [llvm/llvm-project] f43958: [OpenMP] Fixed a crash in hidden helper thread

Shilei Tian via All-commits all-commits at lists.llvm.org
Mon Mar 29 16:30:29 PDT 2021


  Branch: refs/heads/release/12.x
  Home:   https://github.com/llvm/llvm-project
  Commit: f43958b7c497c526b238607624ee0069888f4c98
      https://github.com/llvm/llvm-project/commit/f43958b7c497c526b238607624ee0069888f4c98
  Author: Shilei Tian <tianshilei1992 at gmail.com>
  Date:   2021-03-29 (Mon, 29 Mar 2021)

  Changed paths:
    M openmp/runtime/src/kmp_runtime.cpp
    M openmp/runtime/src/kmp_settings.cpp
    A openmp/runtime/test/tasking/hidden_helper_task/capacity_mix_threads.cpp
    A openmp/runtime/test/tasking/hidden_helper_task/capacity_nthreads.cpp

  Log Message:
  -----------
  [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)




More information about the All-commits mailing list