2#define TBB_USE_CAPTURED_EXCEPTION 0
14#include "tbb/task_arena.h"
15#define TBB_PREVIEW_GLOBAL_CONTROL 1
16#include "tbb/global_control.h"
52 if (
const char *envMaxThreads =
gSystem->
Getenv(
"ROOT_MAX_THREADS")) {
53 char *str_end =
nullptr;
54 long maxThreads = std::strtol(envMaxThreads, &str_end, 0 );
55 if (str_end == envMaxThreads && maxThreads == 0) {
56 Error(
"ROOT::Internal::LogicalCPUBandwidthControl()",
57 "cannot parse number in environment variable ROOT_MAX_THREADS; ignoring.");
58 }
else if (maxThreads < 1) {
59 Error(
"ROOT::Internal::LogicalCPUBandwidthControl()",
60 "environment variable ROOT_MAX_THREADS must be >= 1, but set to %ld; ignoring.",
68 std::ifstream
f(
"/sys/fs/cgroup/cpuacct/cpu.cfs_quota_us");
74 f.open(
"/sys/fs/cgroup/cpuacct/cpu.cfs_period_us");
78 return static_cast<int>(std::ceil(cfs_quota / cfs_period));
82 return std::thread::hardware_concurrency();
95 const unsigned tbbDefaultNumberThreads =
fTBBArena->max_concurrency();
96 maxConcurrency = maxConcurrency > 0 ? std::min(maxConcurrency, tbbDefaultNumberThreads) : tbbDefaultNumberThreads;
98 if (maxConcurrency > bcCpus) {
99 Warning(
"RTaskArenaWrapper",
"CPU Bandwith Control Active. Proceeding with %d threads accordingly", bcCpus);
100 maxConcurrency = bcCpus;
102 if (maxConcurrency > tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism)) {
103 Warning(
"RTaskArenaWrapper",
"tbb::global_control is active, limiting the number of parallel workers"
104 "from this task arena available for execution.");
132 static std::weak_ptr<ROOT::Internal::RTaskArenaWrapper> weak_GTAWrapper;
135 const std::lock_guard<std::mutex> lock{
m};
136 if (
auto sp = weak_GTAWrapper.lock()) {
137 if (maxConcurrency && (sp->TaskArenaSize() != maxConcurrency)) {
138 Warning(
"RTaskArenaWrapper",
"There's already an active task arena. Proceeding with the current %d threads",
139 sp->TaskArenaSize());
144 weak_GTAWrapper = sp;
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.
R__EXTERN TSystem * gSystem
Wrapper for tbb::task_arena.
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
virtual const char * Getenv(const char *env)
Get environment variable.
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...