CMS 3D CMS Logo

GlobalSchedule.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_GlobalSchedule_h
2 #define FWCore_Framework_GlobalSchedule_h
3 
4 /*
5 */
6 
30 
31 #include <map>
32 #include <memory>
33 #include <set>
34 #include <string>
35 #include <vector>
36 #include <sstream>
37 #include "boost/range/adaptor/reversed.hpp"
38 
39 namespace edm {
40 
41  namespace {
42  template <typename T>
43  class GlobalScheduleSignalSentry {
44  public:
45  GlobalScheduleSignalSentry(ActivityRegistry* a, typename T::Context const* context)
46  : a_(a), context_(context), allowThrow_(false) {
47  if (a_)
48  T::preScheduleSignal(a_, context_);
49  }
50  ~GlobalScheduleSignalSentry() noexcept(false) {
51  // Caught exception is rethrown
52  CMS_SA_ALLOW try {
53  if (a_)
54  T::postScheduleSignal(a_, context_);
55  } catch (...) {
56  if (allowThrow_) {
57  throw;
58  }
59  }
60  }
61 
62  void allowThrow() { allowThrow_ = true; }
63 
64  private:
65  // We own none of these resources.
66  ActivityRegistry* a_; // We do not use propagate_const because the registry itself is mutable.
67  typename T::Context const* context_;
68  bool allowThrow_;
69  };
70  } // namespace
71 
72  class ActivityRegistry;
73  class ExceptionCollector;
74  class ProcessContext;
75  class PreallocationConfiguration;
76  class ModuleRegistry;
77  class TriggerResultInserter;
78  class PathStatusInserter;
79  class EndPathStatusInserter;
80 
82  public:
83  typedef std::vector<std::string> vstring;
84  typedef std::vector<Worker*> AllWorkers;
85  typedef std::shared_ptr<Worker> WorkerPtr;
86  typedef std::vector<Worker*> Workers;
87 
88  GlobalSchedule(std::shared_ptr<TriggerResultInserter> inserter,
89  std::vector<edm::propagate_const<std::shared_ptr<PathStatusInserter>>>& pathStatusInserters,
90  std::vector<edm::propagate_const<std::shared_ptr<EndPathStatusInserter>>>& endPathStatusInserters,
91  std::shared_ptr<ModuleRegistry> modReg,
92  std::vector<std::string> const& modulesToUse,
93  ParameterSet& proc_pset,
94  ProductRegistry& pregistry,
95  PreallocationConfiguration const& prealloc,
97  std::shared_ptr<ActivityRegistry> areg,
98  std::shared_ptr<ProcessConfiguration const> processConfiguration,
99  ProcessContext const* processContext);
100  GlobalSchedule(GlobalSchedule const&) = delete;
101 
102  template <typename T>
104  typename T::TransitionInfoType&,
105  ServiceToken const& token,
106  bool cleaningUpAfterException = false);
107 
109  void endJob(ExceptionCollector& collector);
110 
113 
117  std::vector<ModuleDescription const*> getAllModuleDescriptions() const;
118 
120  bool terminate() const;
121 
123  void replaceModule(maker::ModuleHolder* iMod, std::string const& iLabel);
124 
126  void deleteModule(std::string const& iLabel);
127 
129  AllWorkers const& allWorkers() const { return workerManagers_[0].allWorkers(); }
130 
131  private:
132  //Sentry class to only send a signal if an
133  // exception occurs. An exception is identified
134  // by the destructor being called without first
135  // calling completedSuccessfully().
137  public:
139  : reg_(iReg), context_(iContext) {}
141  if (reg_) {
143  }
144  }
145  void completedSuccessfully() { reg_ = nullptr; }
146 
147  private:
148  edm::ActivityRegistry* reg_; // We do not use propagate_const because the registry itself is mutable.
150  };
151 
153  ExceptionToActionTable const& actionTable() const { return workerManagers_[0].actionTable(); }
154 
155  std::vector<WorkerManager> workerManagers_;
156  std::shared_ptr<ActivityRegistry> actReg_; // We do not use propagate_const because the registry itself is mutable.
157  std::vector<edm::propagate_const<WorkerPtr>> extraWorkers_;
160  };
161 
162  template <typename T>
164  typename T::TransitionInfoType& transitionInfo,
165  ServiceToken const& token,
166  bool cleaningUpAfterException) {
167  auto const& principal = transitionInfo.principal();
168 
169  // Caught exception is propagated via WaitingTaskHolder
170  CMS_SA_ALLOW try {
171  //need the doneTask to own the memory
172  auto globalContext = std::make_shared<GlobalContext>(T::makeGlobalContext(principal, processContext_));
173 
174  if (actReg_) {
175  //Services may depend upon each other
177  T::preScheduleSignal(actReg_.get(), globalContext.get());
178  }
179 
180  ServiceWeakToken weakToken = token;
181  auto doneTask = make_waiting_task(
182  [this, iHolder, cleaningUpAfterException, globalContext, weakToken](std::exception_ptr const* iPtr) mutable {
183  std::exception_ptr excpt;
184  if (iPtr) {
185  excpt = *iPtr;
186  //add context information to the exception and print message
187  try {
188  convertException::wrap([&]() { std::rethrow_exception(excpt); });
189  } catch (cms::Exception& ex) {
190  //TODO: should add the transition type info
191  std::ostringstream ost;
192  if (ex.context().empty()) {
193  ost << "Processing " << T::transitionName() << " ";
194  }
195  ServiceRegistry::Operate op(weakToken.lock());
196  addContextAndPrintException(ost.str().c_str(), ex, cleaningUpAfterException);
197  excpt = std::current_exception();
198  }
199  if (actReg_) {
200  ServiceRegistry::Operate op(weakToken.lock());
201  actReg_->preGlobalEarlyTerminationSignal_(*globalContext, TerminationOrigin::ExceptionFromThisContext);
202  }
203  }
204  if (actReg_) {
205  // Caught exception is propagated via WaitingTaskHolder
206  CMS_SA_ALLOW try {
207  ServiceRegistry::Operate op(weakToken.lock());
208  T::postScheduleSignal(actReg_.get(), globalContext.get());
209  } catch (...) {
210  if (not excpt) {
211  excpt = std::current_exception();
212  }
213  }
214  }
215  iHolder.doneWaiting(excpt);
216  });
217  unsigned int managerIndex = principal.index();
218  if constexpr (T::branchType_ == InRun) {
219  managerIndex += numberOfConcurrentLumis_;
220  }
221  WorkerManager& workerManager = workerManagers_[managerIndex];
222  workerManager.resetAll();
223 
224  ParentContext parentContext(globalContext.get());
225  //make sure the ProductResolvers know about their
226  // workers to allow proper data dependency handling
227  workerManager.setupResolvers(transitionInfo.principal());
228 
229  //make sure the task doesn't get run until all workers have beens started
230  WaitingTaskHolder holdForLoop(*iHolder.group(), doneTask);
231  auto& aw = workerManager.allWorkers();
232  for (Worker* worker : boost::adaptors::reverse(aw)) {
233  worker->doWorkAsync<T>(
234  holdForLoop, transitionInfo, token, StreamID::invalidStreamID(), parentContext, globalContext.get());
235  }
236  } catch (...) {
237  iHolder.doneWaiting(std::current_exception());
238  }
239  }
240 } // namespace edm
241 
242 #endif
std::string_view transitionName(GlobalContext::Transition)
std::vector< std::string > vstring
#define CMS_SA_ALLOW
void replaceModule(maker::ModuleHolder *iMod, std::string const &iLabel)
clone the type of module with label iLabel but configure with iPSet.
std::vector< WorkerManager > workerManagers_
roAction_t actions[nactions]
Definition: GenABIO.cc:181
AllWorkers const & allWorkers() const
returns the collection of pointers to workers
std::vector< Worker * > AllWorkers
ExceptionToActionTable const & actionTable() const
returns the action table
void deleteModule(std::string const &iLabel)
Delete the module with label iLabel.
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
static StreamID invalidStreamID()
Definition: StreamID.h:45
std::vector< ModuleDescription const * > getAllModuleDescriptions() const
oneapi::tbb::task_group * group() const noexcept
bool terminate() const
Return whether each output module has reached its maximum count.
void doneWaiting(std::exception_ptr iExcept)
std::vector< Worker * > Workers
GlobalSchedule(std::shared_ptr< TriggerResultInserter > inserter, std::vector< edm::propagate_const< std::shared_ptr< PathStatusInserter >>> &pathStatusInserters, std::vector< edm::propagate_const< std::shared_ptr< EndPathStatusInserter >>> &endPathStatusInserters, std::shared_ptr< ModuleRegistry > modReg, std::vector< std::string > const &modulesToUse, ParameterSet &proc_pset, ProductRegistry &pregistry, PreallocationConfiguration const &prealloc, ExceptionToActionTable const &actions, std::shared_ptr< ActivityRegistry > areg, std::shared_ptr< ProcessConfiguration const > processConfiguration, ProcessContext const *processContext)
std::shared_ptr< Worker > WorkerPtr
AllWorkers const & allWorkers() const
Definition: WorkerManager.h:85
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:92
void endJob(ExceptionCollector &collector)
ServiceToken lock() const
Definition: ServiceToken.h:101
std::shared_ptr< ActivityRegistry > actReg_
PreGlobalEarlyTermination preGlobalEarlyTerminationSignal_
void beginJob(ProductRegistry const &, eventsetup::ESRecordsToProxyIndices const &, ProcessBlockHelperBase const &)
SendTerminationSignalIfException(edm::ActivityRegistry *iReg, edm::GlobalContext const *iContext)
HLT enums.
void setupResolvers(Principal &principal)
void processOneGlobalAsync(WaitingTaskHolder holder, typename T::TransitionInfoType &, ServiceToken const &token, bool cleaningUpAfterException=false)
double a
Definition: hdecay.h:119
auto wrap(F iFunc) -> decltype(iFunc())
ProcessContext const * processContext_
std::list< std::string > const & context() const
Definition: Exception.cc:147
long double T
unsigned int numberOfConcurrentLumis_
std::vector< edm::propagate_const< WorkerPtr > > extraWorkers_