Scheduler.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
8 #include "tbb/global_control.h"
9 
10 #include <cstdlib>
11 #include <string>
12 
14 
15 namespace {
16  unsigned
17  adjust_num_threads(unsigned const specified_num_threads)
18  {
19  char const* p = std::getenv("OMP_NUM_THREADS");
20  if (p == nullptr) {
21  return specified_num_threads;
22  }
23 
24  auto const max_threads = std::stoul(p);
25  if (specified_num_threads == 0) {
26  return max_threads;
27  }
28  if (specified_num_threads <= max_threads) {
29  return specified_num_threads;
30  }
31 
32  cet::HorizontalRule const rule{80};
33  mf::LogAbsolute("MTconfig")
34  << rule('=') << '\n'
35  << "The specified number of threads (" << specified_num_threads
36  << ") exceeds the allowed number (" << max_threads << ").\n"
37  << "The allowed number of threads (" << max_threads
38  << ") will be used for this job.\n"
39  << rule('=');
40  return max_threads;
41  }
42 
43  constexpr auto max_parallelism = tbb::global_control::max_allowed_parallelism;
44  constexpr auto thread_stack_size = tbb::global_control::thread_stack_size;
45 }
46 
47 namespace art {
48 
49  Scheduler::Scheduler(Parameters const& ps)
50  : actionTable_{ps().actionTable()}
51  , nThreads_{adjust_num_threads(ps().num_threads())}
52  , nSchedules_{ps().num_schedules()}
53  , stackSize_{ps().stack_size()}
54  , handleEmptyRuns_{ps().handleEmptyRuns()}
55  , handleEmptySubRuns_{ps().handleEmptySubRuns()}
56  , errorOnMissingConsumes_{ps().errorOnMissingConsumes()}
57  , wantSummary_{ps().wantSummary()}
58  , dataDependencyGraph_{ps().dataDependencyGraph()}
59  {
60  auto& globals = *Globals::instance();
61  globals.setNThreads(nThreads_);
62  globals.setNSchedules(nSchedules_);
63  }
64 
65  std::unique_ptr<GlobalTaskGroup>
67  {
68  using tbb::global_control;
69  auto value_of = [](auto const field) {
70  return global_control::active_value(field);
71  };
72  auto group = std::make_unique<GlobalTaskGroup>(nThreads_, stackSize_);
73  mf::LogInfo("MTdiagnostics")
74  << "TBB has been configured to use:\n"
75  << " - a maximum of " << value_of(max_parallelism) << " threads\n"
76  << " - a stack size of " << value_of(thread_stack_size) << " bytes";
77  return group;
78  }
79 }
std::unique_ptr< GlobalTaskGroup > global_task_group()
Definition: Scheduler.cc:66
unsigned num_threads() const noexcept
Definition: Scheduler.h:79
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::string const dataDependencyGraph_
Definition: Scheduler.h:127
bool const wantSummary_
Definition: Scheduler.h:126
bool const errorOnMissingConsumes_
Definition: Scheduler.h:125
unsigned const stackSize_
Definition: Scheduler.h:122
unsigned const nSchedules_
Definition: Scheduler.h:121
std::string getenv(std::string const &name)
Definition: getenv.cc:15
bool const handleEmptySubRuns_
Definition: Scheduler.h:124
p
Definition: test.py:223
static constexpr double ps
Definition: Units.h:99
bool const handleEmptyRuns_
Definition: Scheduler.h:123
unsigned const nThreads_
Definition: Scheduler.h:120
static Globals * instance()
Definition: Globals.cc:17
MaybeLogger_< ELseverityLevel::ELsev_severe, true > LogAbsolute