Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
threads.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <cstdlib>
6#include <thread>
7
8#include "storm/io/file.h"
9
10namespace storm::utility {
11
12namespace detail {
13static uint64_t num_threads = 0u;
14
15uint64_t tryReadFromSlurm() {
16 auto val = std::getenv("SLURM_CPUS_PER_TASK");
17 if (val != nullptr) {
18 char* end;
19 auto i = std::strtoul(val, &end, 10);
20 if (val != end) {
21 STORM_LOG_WARN("Detected thread limitation via Slurm (max. " << i << " threads.)");
22 return static_cast<uint64_t>(i);
23 }
24 }
25 return 0;
26}
27
29 std::string const filename = "/sys/fs/cgroup/cpu.max";
31 std::ifstream inputFileStream;
32 storm::io::openFile(filename, inputFileStream);
33 std::string contents;
34 storm::io::getline(inputFileStream, contents);
35 storm::io::closeFile(inputFileStream);
36
37 auto pos1 = contents.data();
38 char* pos2;
39 double quota = std::strtod(pos1, &pos2);
40 if (pos1 != pos2 && quota > 0.0) {
41 pos1 = pos2;
42 double period = std::strtod(pos1, &pos2);
43 if (pos1 != pos2 && period > 0.0) {
44 auto res = static_cast<uint64_t>(std::ceil(quota / period));
45 STORM_LOG_WARN("Detected thread limitation via cgroup (max. " << res << " threads, quota=" << quota << ", period=" << period << ").");
46 return res;
47 }
48 }
49 }
50 return 0u;
51}
52
53} // namespace detail
54
56 if (detail::num_threads == 0) {
57 // try to obtain a sensible number of threads we can use
58 auto numHardwareThreads = std::max(1u, std::thread::hardware_concurrency());
59 auto numSlurmThreads = detail::tryReadFromSlurm();
60 auto numCgroupsThreads = detail::tryReadFromCgroups();
61 detail::num_threads = numHardwareThreads;
62 for (auto i : {numSlurmThreads, numCgroupsThreads}) {
63 if (i > 0 && i < detail::num_threads) {
65 }
66 }
67 }
69}
70} // namespace storm::utility
#define STORM_LOG_WARN(message)
Definition logging.h:28
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 uint64_t num_threads
Definition threads.cpp:13
uint64_t tryReadFromSlurm()
Definition threads.cpp:15
uint64_t tryReadFromCgroups()
Definition threads.cpp:28
uint64_t getNumberOfThreads()
Definition threads.cpp:55