12#include "tbb/task_arena.h"
13#include "tbb/global_control.h"
48 if (
const char *envMaxThreads =
gSystem->Getenv(
"ROOT_MAX_THREADS")) {
49 char *str_end =
nullptr;
50 long maxThreads = std::strtol(envMaxThreads, &str_end, 0 );
51 if (str_end == envMaxThreads && maxThreads == 0) {
52 Error(
"ROOT::Internal::LogicalCPUBandwidthControl()",
53 "cannot parse number in environment variable ROOT_MAX_THREADS; ignoring.");
54 }
else if (maxThreads < 1) {
55 Error(
"ROOT::Internal::LogicalCPUBandwidthControl()",
56 "environment variable ROOT_MAX_THREADS must be >= 1, but set to %ld; ignoring.",
64 std::ifstream
f(
"/sys/fs/cgroup/cpuacct/cpu.cfs_quota_us");
70 f.open(
"/sys/fs/cgroup/cpuacct/cpu.cfs_period_us");
74 return static_cast<int>(std::ceil(cfs_quota / cfs_period));
78 return std::thread::hardware_concurrency();
91 const unsigned tbbDefaultNumberThreads =
fTBBArena->max_concurrency();
92 maxConcurrency = maxConcurrency > 0 ? std::min(maxConcurrency, tbbDefaultNumberThreads) : tbbDefaultNumberThreads;
94 if (maxConcurrency > bcCpus) {
95 Warning(
"RTaskArenaWrapper",
"CPU Bandwith Control Active. Proceeding with %d threads accordingly", bcCpus);
96 maxConcurrency = bcCpus;
98 if (maxConcurrency > tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism)) {
99 Warning(
"RTaskArenaWrapper",
"tbb::global_control is active, limiting the number of parallel workers"
100 "from this task arena available for execution.");
116 fTBBArena->initialize(tbb::task_arena::attach{});
140std::shared_ptr<ROOT::Internal::RTaskArenaWrapper>
143 static std::weak_ptr<ROOT::Internal::RTaskArenaWrapper> weak_GTAWrapper;
146 const std::lock_guard<std::mutex> lock{
m};
147 if (
auto sp = weak_GTAWrapper.lock()) {
148 if (maxConcurrency && (sp->TaskArenaSize() != maxConcurrency)) {
149 Warning(
"RTaskArenaWrapper",
"There's already an active task arena. Proceeding with the current %d threads",
150 sp->TaskArenaSize());
154 std::shared_ptr<ROOT::Internal::RTaskArenaWrapper> sp;
161 sp = std::make_shared<ROOT::Internal::RTaskArenaWrapper>(maxConcurrency);
163 weak_GTAWrapper = sp;
170 ::Fatal(
"ROOT::Internal::GetGlobalTaskArena",
171 "Unsupported enum value %d", (
int)config);
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
void Fatal(const char *location, const char *msgfmt,...)
Use this function in case of a fatal error. It will abort the program.
R__EXTERN TSystem * gSystem
ROOT::ROpaqueTaskArena & Access()
Provides access to the wrapped tbb::task_arena.
RTaskArenaWrapper(unsigned maxConcurrency=0)
Initializes the tbb::task_arena within RTaskArenaWrapper.
static unsigned fNWorkers
static unsigned TaskArenaSize()
std::unique_ptr< ROOT::ROpaqueTaskArena > fTBBArena
int LogicalCPUBandwidthControl()
Returns the available number of logical cores.
std::shared_ptr< ROOT::Internal::RTaskArenaWrapper > GetGlobalTaskArena(unsigned maxConcurrency=0)
Factory function returning a shared pointer to the instance of the global RTaskArenaWrapper.
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
@ kWholeMachine
Default configuration.
@ kNumConfigs
Number of support IMT semantic configurations.
@ kExistingTBBArena
Use the existing TBB arena.
Marker for attaching to an existing tbb::task_arena.