Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
threads.cpp
Go to the documentation of this file.
2
3#include <cstdlib>
4#include <thread>
5
6#include "storm/io/file.h"
7
8namespace storm::utility {
9
10namespace detail {
11static uint num_threads = 0u;
12
14 auto val = std::getenv("SLURM_CPUS_PER_TASK");
15 if (val != nullptr) {
16 char* end;
17 auto i = std::strtoul(val, &end, 10);
18 if (val != end) {
19 STORM_LOG_WARN("Detected thread limitation via Slurm (max. " << i << " threads.)");
20 return static_cast<uint>(i);
21 }
22 }
23 return 0;
24}
25
27 std::string const filename = "/sys/fs/cgroup/cpu.max";
29 std::ifstream inputFileStream;
30 storm::io::openFile(filename, inputFileStream);
31 std::string contents;
32 storm::io::getline(inputFileStream, contents);
33 storm::io::closeFile(inputFileStream);
34
35 auto pos1 = contents.data();
36 char* pos2;
37 double quota = std::strtod(pos1, &pos2);
38 if (pos1 != pos2 && quota > 0.0) {
39 pos1 = pos2;
40 double period = std::strtod(pos1, &pos2);
41 if (pos1 != pos2 && period > 0.0) {
42 auto res = static_cast<uint>(std::ceil(quota / period));
43 STORM_LOG_WARN("Detected thread limitation via cgroup (max. " << res << " threads, quota=" << quota << ", period=" << period << ").");
44 return res;
45 }
46 }
47 }
48 return 0u;
49}
50
51} // namespace detail
52
54 if (detail::num_threads == 0) {
55 // try to obtain a sensible number of threads we can use
56 auto numHardwareThreads = std::max(1u, std::thread::hardware_concurrency());
57 auto numSlurmThreads = detail::tryReadFromSlurm();
58 auto numCgroupsThreads = detail::tryReadFromCgroups();
59 detail::num_threads = numHardwareThreads;
60 for (auto i : {numSlurmThreads, numCgroupsThreads}) {
61 if (i > 0 && i < detail::num_threads) {
63 }
64 }
65 }
67}
68} // namespace storm::utility
#define STORM_LOG_WARN(message)
Definition logging.h:30
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
Definition file.h:80
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
bool fileExistsAndIsReadable(std::string const &filename)
Tests whether the given file exists and is readable.
Definition file.h:66
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
static uint num_threads
Definition threads.cpp:11
uint getNumberOfThreads()
Definition threads.cpp:53