xref: /llvm-project/openmp/runtime/test/tasking/hidden_helper_task/capacity_mix_threads.cpp (revision 22bba85d82637d0446928ff6ff41f98583f3d3b2)
1 // RUN: %libomp-cxx-compile-and-run
2 
3 #include <omp.h>
4 
5 #include <algorithm>
6 #include <cassert>
7 #include <chrono>
8 #include <thread>
9 #include <vector>
10 
11 // AIX runs out of resource in 32-bit if 4*omp_get_max_threads() is more
12 // than 64 threads with the default stack size.
13 #if defined(_AIX) && !__LP64__
14 #define MAX_THREADS 64
15 #endif
16 
dummy_root()17 void dummy_root() {
18   // omp_get_max_threads() will do middle initialization
19   int nthreads = omp_get_max_threads();
20   std::this_thread::sleep_for(std::chrono::milliseconds(1000));
21 }
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[]) {
24   int N = std::min(std::max(std::max(32, 4 * omp_get_max_threads()),
25                             4 * omp_get_num_procs()),
26                    std::numeric_limits<int>::max());
27 
28 #if defined(_AIX) && !__LP64__
29   if (N > MAX_THREADS)
30     N = MAX_THREADS;
31 #endif
32 
33   std::vector<int> data(N);
34 
35   // Create a new thread to initialize the OpenMP RTL. The new thread will not
36   // be taken as the "initial thread".
37   std::thread root(dummy_root);
38 
39 #pragma omp parallel for num_threads(N)
40   for (unsigned i = 0; i < N; ++i) {
41     data[i] = i;
42   }
43 
44 #pragma omp parallel for num_threads(N + 1)
45   for (unsigned i = 0; i < N; ++i) {
46     data[i] += i;
47   }
48 
49   for (unsigned i = 0; i < N; ++i) {
50     assert(data[i] == 2 * i);
51   }
52 
53   root.join();
54 
55   return 0;
56 }
57