OGS
OgsAsmThreads.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "OgsAsmThreads.h"
5
6#include <algorithm>
7#include <cstdlib>
8#include <sstream>
9
10#ifdef _OPENMP
11#include <omp.h>
12#endif
13
14#include "Error.h"
15#include "StringTools.h"
16
17namespace BaseLib
18{
19int getNumberOfThreadsFromEnv(char const* env_var_name)
20{
21 char const* const num_threads_env = std::getenv(env_var_name);
22
23 if (!num_threads_env)
24 {
25 return 1;
26 }
27
28 if (std::strlen(num_threads_env) == 0)
29 {
30 OGS_FATAL("The environment variable {} is set but empty.",
31 env_var_name);
32 }
33
34 std::string num_threads_str{num_threads_env};
35 BaseLib::trim(num_threads_str);
36
37 std::istringstream num_threads_iss{num_threads_str};
38 int num_threads = -1;
39
40 num_threads_iss >> num_threads;
41
42 if (!num_threads_iss)
43 {
44 OGS_FATAL("Error parsing {} (= \"{}\").", env_var_name,
45 num_threads_env);
46 }
47
48 if (!num_threads_iss.eof())
49 {
51 "Error parsing {} (= \"{}\"): not read entirely, the "
52 "remainder is \"{}\"",
53 env_var_name,
54 num_threads_env,
55 num_threads_iss.str().substr(num_threads_iss.tellg()));
56 }
57
58 if (num_threads < 1)
59 {
60 OGS_FATAL("You asked (via {}) to use {} < 1 thread.", env_var_name,
61 num_threads);
62 }
63
64 return num_threads;
65}
66
68{
69 return getNumberOfThreadsFromEnv("OGS_ASM_THREADS");
70}
71
73{
74#ifdef _OPENMP
75 int const num_omp_threads = omp_get_max_threads();
76#else
77 int const num_omp_threads = 1;
78#endif
79 return std::max(getNumberOfAssemblyThreads(), num_omp_threads);
80}
81} // namespace BaseLib
#define OGS_FATAL(...)
Definition Error.h:10
int getNumberOfAssemblyThreads()
void trim(std::string &str, char ch)
int getNumberOfThreadsFromEnv(char const *env_var_name)
int getNumberOfThreads()