14 typedef int (Getter)(void);
15 typedef void (Setter)(
int);
17 Getter* getOpenBlasThreads = NULL;
18 Setter* setOpenBlasThreads = NULL;
19 Getter* getMklThreads = NULL;
20 Setter* setMklThreads = NULL;
28 getOpenBlasThreads = loadSymbol<Getter>(
"libopenblas",
"goto_get_num_procs");
34 setOpenBlasThreads = loadSymbol<Setter>(
"libopenblas",
"openblas_set_num_threads");
35 }
catch (LibraryException
const&) {
53 getMklThreads = loadSymbol<Getter>(
"libiomp5",
"omp_get_max_threads");
54 setMklThreads = loadSymbol<Setter>(
"libiomp5",
"omp_set_num_threads");
55 }
catch (LibraryException
const&) {
61 bool disableImplicitThreadingImpl(
68 for (
auto&& ss : envvars) {
73 unsigned int numThreads = getter();
74 if (numThreads <= 1) {
78 std::cerr <<
"WARNING: You are using " <<
package << " with multiple threads (" << numThreads <<
79 "), but have not\n" <<
80 "specified the number of threads using one of the " << package <<
81 " environment variables:\n";
87 for (
auto iter = envvars.begin() + 1; iter != envvars.end(); ++iter) {
92 "This may indicate that you are unintentionally using multiple threads, which may\n" 93 "cause problems. WE HAVE THEREFORE DISABLED " <<
package << " THREADING. If you know\n" <<
94 "what you are doing and want threads enabled implicitly, set the environment\n" <<
95 "variable " << allowEnvvar << ".\n";
109 if (!haveOpenBlas && !haveMkl && numThreads != 0 && numThreads != 1) {
113 setOpenBlasThreads(numThreads);
116 setMklThreads(numThreads);
122 unsigned int numThreads = 0;
124 numThreads =
std::max(numThreads, static_cast<unsigned int>(getOpenBlasThreads()));
127 numThreads =
std::max(numThreads, static_cast<unsigned int>(getMklThreads()));
138 bool intervened =
false;
140 intervened |= disableImplicitThreadingImpl(
142 {
"OPENBLAS_NUM_THREADS",
"GOTO_NUM_THREADS",
"OMP_NUM_THREADS"},
148 intervened |= disableImplicitThreadingImpl(
150 {
"MKL_NUM_THREADS",
"MKL_DOMAIN_NUM_THREADS",
"OMP_NUM_THREADS"},
bool disableImplicitThreading()
Disable threading that has not been set explicitly.
bool const haveMkl
Is MKL available?
No threading library is available.
A base class for image defects.
std::string const allowEnvvar
Environment variable to allow implicit threading.
void setNumThreads(unsigned int numThreads)
Set number of threads to use.
unsigned int getNumThreads()
Get maximum number of threads we might use.
bool canLoadLibrary(std::string const &libName)
Return whether we can load a library.
bool const haveOpenBlas
Is OpenBLAS available?