57 lines · cpp
1// RUN: %libomp-cxx-compile-and-run2 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 more12// than 64 threads with the default stack size.13#if defined(_AIX) && !__LP64__14#define MAX_THREADS 6415#endif16 17void dummy_root() {18 // omp_get_max_threads() will do middle initialization19 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#endif32 33 std::vector<int> data(N);34 35 // Create a new thread to initialize the OpenMP RTL. The new thread will not36 // 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