|
9 | 9 | #include <omp.h> |
10 | 10 |
|
11 | 11 | // CLI override (default -1 -> auto-detect) |
12 | | -inline int cli_core_count = -1; |
| 12 | +inline int cli_threads = -1; |
13 | 13 |
|
14 | 14 | /** |
15 | 15 | * @brief Detects/sets the number of OpenMP threads |
16 | | - * @param requested Optional requested thread count; uses \a cli_core_count by |
| 16 | + * @param requested Optional requested thread count; uses \a cli_threads by |
17 | 17 | * default |
18 | 18 | * |
19 | 19 | * @return Final number of threads used |
20 | 20 | */ |
21 | 21 | inline int set_openmp_threads(int requested = -1) { |
22 | | - int core_count = (requested >= 0) ? requested : cli_core_count; |
| 22 | + int threads = (requested >= 0) ? requested : cli_threads; |
23 | 23 |
|
24 | 24 | const char* env = std::getenv("OMP_NUM_THREADS"); |
25 | 25 | const bool env_set = (env && std::atoi(env) > 0); |
26 | 26 |
|
27 | 27 | // 1. Environment variable has highest priority |
28 | 28 | if (env_set) { |
29 | | - core_count = std::atoi(env); |
30 | | - std::cout << "OpenMP enabled, core_count=" << core_count |
| 29 | + threads = std::atoi(env); |
| 30 | + std::cout << "OpenMP enabled, threads=" << threads |
31 | 31 | << " set by OMP_NUM_THREADS\n"; |
32 | 32 | } |
33 | 33 | // 2. If NOT set by OMP_NUM_THREADS |
34 | | - else if (core_count <= 0) { |
35 | | - core_count = omp_get_max_threads(); |
36 | | - std::cout << "OpenMP enabled, core_count=" << core_count |
| 34 | + else if (threads <= 0) { |
| 35 | + threads = omp_get_max_threads(); |
| 36 | + std::cout << "OpenMP enabled, threads=" << threads |
37 | 37 | << " auto-detected by OpenMP\n"; |
38 | 38 | } |
39 | | - // 3. Otherwise (core_count > 0) and NOT set by OMP_NUM_THREADS |
| 39 | + // 3. Otherwise (threads > 0) and NOT set by OMP_NUM_THREADS |
40 | 40 | else { |
41 | | - std::cout << "OpenMP enabled, core_count=" << core_count |
| 41 | + std::cout << "OpenMP enabled, threads=" << threads |
42 | 42 | << " set by the CLI\n"; |
43 | 43 | } |
44 | 44 |
|
45 | | - omp_set_num_threads(core_count); |
| 45 | + omp_set_num_threads(threads); |
46 | 46 |
|
47 | | - return core_count; |
| 47 | + return threads; |
48 | 48 | } |
49 | 49 | #endif // QPP_OPENMP |
50 | 50 |
|
|
0 commit comments