CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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,
97  std::shared_ptr<ActivityRegistry> areg,
98  std::shared_ptr<ProcessConfiguration> 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_;
159  };
160 
161  template <typename T>
163  typename T::TransitionInfoType& transitionInfo,
164  ServiceToken const& token,
165  bool cleaningUpAfterException) {
166  auto const& principal = transitionInfo.principal();
167 
168  // Caught exception is propagated via WaitingTaskHolder
169  CMS_SA_ALLOW try {
170  //need the doneTask to own the memory
171  auto globalContext = std::make_shared<GlobalContext>(T::makeGlobalContext(principal, processContext_));
172 
173  if (actReg_) {
174  //Services may depend upon each other
175  ServiceRegistry::Operate op(token);
176  T::preScheduleSignal(actReg_.get(), globalContext.get());
177  }
178 
179  ServiceWeakToken weakToken = token;
180  auto doneTask = make_waiting_task(
181  [this, iHolder, cleaningUpAfterException, globalContext, weakToken](std::exception_ptr const* iPtr) mutable {
182  std::exception_ptr excpt;
183  if (iPtr) {
184  excpt = *iPtr;
185  //add context information to the exception and print message
186  try {
187  convertException::wrap([&]() { std::rethrow_exception(excpt); });
188  } catch (cms::Exception& ex) {
189  //TODO: should add the transition type info
190  std::ostringstream ost;
191  if (ex.context().empty()) {
192  ost << "Processing " << T::transitionName() << " ";
193  }
194  ServiceRegistry::Operate op(weakToken.lock());
195  addContextAndPrintException(ost.str().c_str(), ex, cleaningUpAfterException);
196  excpt = std::current_exception();
197  }
198  if (actReg_) {
199  ServiceRegistry::Operate op(weakToken.lock());
200  actReg_->preGlobalEarlyTerminationSignal_(*globalContext, TerminationOrigin::ExceptionFromThisContext);
201  }
202  }
203  if (actReg_) {
204  // Caught exception is propagated via WaitingTaskHolder
205  CMS_SA_ALLOW try {
206  ServiceRegistry::Operate op(weakToken.lock());
207  T::postScheduleSignal(actReg_.get(), globalContext.get());
208  } catch (...) {
209  if (not excpt) {
210  excpt = std::current_exception();
211  }
212  }
213  }
214  iHolder.doneWaiting(excpt);
215  });
216  WorkerManager& workerManager = workerManagers_[principal.index()];
217  workerManager.resetAll();
218 
219  ParentContext parentContext(globalContext.get());
220  //make sure the ProductResolvers know about their
221  // workers to allow proper data dependency handling
222  workerManager.setupResolvers(transitionInfo.principal());
223 
224  //make sure the task doesn't get run until all workers have beens started
225  WaitingTaskHolder holdForLoop(*iHolder.group(), doneTask);
226  auto& aw = workerManager.allWorkers();
227  for (Worker* worker : boost::adaptors::reverse(aw)) {
228  worker->doWorkAsync<T>(
229  holdForLoop, transitionInfo, token, StreamID::invalidStreamID(), parentContext, globalContext.get());
230  }
231  } catch (...) {
232  iHolder.doneWaiting(std::current_exception());
233  }
234  }
235 } // namespace edm
236 
237 #endif
ServiceToken lock() const
Definition: ServiceToken.h:101
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_
std::vector< Worker * > AllWorkers
void deleteModule(std::string const &iLabel)
Delete the module with label iLabel.
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
processConfiguration
Definition: Schedule.cc:687
static StreamID invalidStreamID()
Definition: StreamID.h:45
actions
Definition: Schedule.cc:687
bool terminate() const
Return whether each output module has reached its maximum count.
oneapi::tbb::task_group * group() const noexcept
void doneWaiting(std::exception_ptr iExcept)
std::vector< Worker * > Workers
AllWorkers const & allWorkers() const
returns the collection of pointers to workers
std::list< std::string > const & context() const
Definition: Exception.cc:147
std::shared_ptr< Worker > WorkerPtr
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:101
void endJob(ExceptionCollector &collector)
areg
Definition: Schedule.cc:687
std::shared_ptr< ActivityRegistry > actReg_
PreGlobalEarlyTermination preGlobalEarlyTerminationSignal_
std::vector< ModuleDescription const * > getAllModuleDescriptions() const
void beginJob(ProductRegistry const &, eventsetup::ESRecordsToProxyIndices const &, ProcessBlockHelperBase const &)
SendTerminationSignalIfException(edm::ActivityRegistry *iReg, edm::GlobalContext const *iContext)
ExceptionToActionTable const & actionTable() const
returns the action table
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_
long double T
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 > processConfiguration, ProcessContext const *processContext)
prealloc
Definition: Schedule.cc:687
std::vector< edm::propagate_const< WorkerPtr > > extraWorkers_