LLVM OpenMP 19.0.0git
capacity_mix_threads.cpp
Go to the documentation of this file.
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
17void 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
23int 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}
#define N
Definition: bug54082.c:13
void dummy_root()
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void * data
#define i
Definition: kmp_stub.cpp:87
int omp_get_max_threads()
int main()
Definition: test-touch.c:21