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 
24 
25 #include <map>
26 #include <memory>
27 #include <set>
28 #include <string>
29 #include <vector>
30 #include <sstream>
31 #include "boost/range/adaptor/reversed.hpp"
32 
33 
34 namespace edm {
35 
36  namespace {
37  template <typename T>
38  class GlobalScheduleSignalSentry {
39  public:
40  GlobalScheduleSignalSentry(ActivityRegistry* a, typename T::Context const* context) :
41  a_(a), context_(context),
42  allowThrow_(false) {
43  if (a_) T::preScheduleSignal(a_, context_);
44  }
45  ~GlobalScheduleSignalSentry() noexcept(false) {
46  try {
47  if (a_) T::postScheduleSignal(a_, context_);
48  } catch(...) {
49  if(allowThrow_) {throw;}
50  }
51  }
52 
53  void allowThrow() {
54  allowThrow_ = true;
55  }
56 
57  private:
58  // We own none of these resources.
59  ActivityRegistry* a_; // We do not use propagate_const because the registry itself is mutable.
60  typename T::Context const* context_;
61  bool allowThrow_;
62  };
63  }
64 
65  class ActivityRegistry;
66  class EventSetup;
67  class ExceptionCollector;
68  class ProcessContext;
69  class PreallocationConfiguration;
70  class ModuleRegistry;
71  class TriggerResultInserter;
72  class PathStatusInserter;
73  class EndPathStatusInserter;
74 
76  public:
77  typedef std::vector<std::string> vstring;
78  typedef std::vector<Worker*> AllWorkers;
79  typedef std::shared_ptr<Worker> WorkerPtr;
80  typedef std::vector<Worker*> Workers;
81 
82  GlobalSchedule(std::shared_ptr<TriggerResultInserter> inserter,
83  std::vector<edm::propagate_const<std::shared_ptr<PathStatusInserter>>>& pathStatusInserters,
84  std::vector<edm::propagate_const<std::shared_ptr<EndPathStatusInserter>>>& endPathStatusInserters,
85  std::shared_ptr<ModuleRegistry> modReg,
86  std::vector<std::string> const& modulesToUse,
87  ParameterSet& proc_pset,
88  ProductRegistry& pregistry,
89  PreallocationConfiguration const& prealloc,
91  std::shared_ptr<ActivityRegistry> areg,
92  std::shared_ptr<ProcessConfiguration> processConfiguration,
93  ProcessContext const* processContext);
94  GlobalSchedule(GlobalSchedule const&) = delete;
95 
96  template <typename T>
97  void processOneGlobal(typename T::MyPrincipal& principal,
98  EventSetup const& eventSetup,
99  bool cleaningUpAfterException = false);
100 
101  template <typename T>
102  void processOneGlobalAsync(WaitingTaskHolder holder,
103  typename T::MyPrincipal& principal,
104  EventSetup const& eventSetup,
105  bool cleaningUpAfterException = false);
106 
107  void beginJob(ProductRegistry const&);
108  void endJob(ExceptionCollector & collector);
109 
112 
116  std::vector<ModuleDescription const*> getAllModuleDescriptions() const;
117 
120  void getTriggerReport(TriggerReport& rep) const;
121 
123  bool terminate() const;
124 
126  void replaceModule(maker::ModuleHolder* iMod, std::string const& iLabel);
127 
129  AllWorkers const& allWorkers() const {
130  return workerManager_.allWorkers();
131  }
132 
133  private:
134  //Sentry class to only send a signal if an
135  // exception occurs. An exception is identified
136  // by the destructor being called without first
137  // calling completedSuccessfully().
139  public:
141  reg_(iReg),
142  context_(iContext){}
144  if(reg_) {
145  reg_->preGlobalEarlyTerminationSignal_(*context_,TerminationOrigin::ExceptionFromThisContext);
146  }
147  }
149  reg_ = nullptr;
150  }
151  private:
152  edm::ActivityRegistry* reg_; // We do not use propagate_const because the registry itself is mutable.
154  };
155 
156 
157  template<typename T>
158  void runNow(typename T::MyPrincipal const& p, EventSetup const& es,
159  GlobalContext const* context);
160 
163  return workerManager_.actionTable();
164  }
165 
166  void addToAllWorkers(Worker* w);
167 
169  std::shared_ptr<ActivityRegistry> actReg_; // We do not use propagate_const because the registry itself is mutable.
171  std::vector<edm::propagate_const<WorkerPtr>> pathStatusInserterWorkers_;
172  std::vector<edm::propagate_const<WorkerPtr>> endPathStatusInserterWorkers_;
173 
175  };
176 
177 
178  template <typename T>
179  void
180  GlobalSchedule::processOneGlobal(typename T::MyPrincipal& ep,
181  EventSetup const& es,
182  bool cleaningUpAfterException) {
183  GlobalContext globalContext = T::makeGlobalContext(ep, processContext_);
184 
185  GlobalScheduleSignalSentry<T> sentry(actReg_.get(), &globalContext);
186 
187  SendTerminationSignalIfException terminationSentry(actReg_.get(), &globalContext);
188 
189  //If we are in an end transition, we need to reset failed items since they might
190  // be set this time around
191  if( not T::begin_) {
192  ep.resetFailedFromThisProcess();
193  }
194  // This call takes care of the unscheduled processing.
195  workerManager_.processOneOccurrence<T>(ep, es, StreamID::invalidStreamID(), &globalContext, &globalContext, cleaningUpAfterException);
196 
197  try {
198  convertException::wrap([&]() {
199  runNow<T>(ep,es,&globalContext);
200  });
201  }
202  catch(cms::Exception& ex) {
203  if (ex.context().empty()) {
204  addContextAndPrintException("Calling function GlobalSchedule::processOneGlobal", ex, cleaningUpAfterException);
205  } else {
206  addContextAndPrintException("", ex, cleaningUpAfterException);
207  }
208  throw;
209  }
210  terminationSentry.completedSuccessfully();
211 
212  //If we got here no other exception has happened so we can propogate any Service related exceptions
213  sentry.allowThrow();
214  }
215 
216  template <typename T>
217  void
219  typename T::MyPrincipal& ep,
220  EventSetup const& es,
221  bool cleaningUpAfterException) {
223 
224  //need the doneTask to own the memory
225  auto globalContext = std::make_shared<GlobalContext>(T::makeGlobalContext(ep, processContext_));
226 
227  if(actReg_) {
228  T::preScheduleSignal(actReg_.get(), globalContext.get());
229  }
230 
231 
232  //If we are in an end transition, we need to reset failed items since they might
233  // be set this time around
234  if( not T::begin_) {
235  ep.resetFailedFromThisProcess();
236  }
237 
238  auto doneTask = make_waiting_task(tbb::task::allocate_root(),
239  [this,iHolder, cleaningUpAfterException, globalContext, token](std::exception_ptr const* iPtr) mutable
240  {
241  ServiceRegistry::Operate op(token);
242  std::exception_ptr excpt;
243  if(iPtr) {
244  excpt = *iPtr;
245  //add context information to the exception and print message
246  try {
247  convertException::wrap([&]() {
248  std::rethrow_exception(excpt);
249  });
250  } catch(cms::Exception& ex) {
251  //TODO: should add the transition type info
252  std::ostringstream ost;
253  if(ex.context().empty()) {
254  ost<<"Processing "<<T::transitionName()<<" ";
255  }
256  addContextAndPrintException(ost.str().c_str(), ex, cleaningUpAfterException);
257  excpt = std::current_exception();
258  }
259  if(actReg_) {
260  actReg_->preGlobalEarlyTerminationSignal_(*globalContext,TerminationOrigin::ExceptionFromThisContext);
261  }
262  }
263  if(actReg_) {
264  try {
265  T::postScheduleSignal(actReg_.get(), globalContext.get());
266  } catch(...) {
267  if(not excpt) {
268  excpt = std::current_exception();
269  }
270  }
271  }
272  iHolder.doneWaiting(excpt);
273 
274  });
275  workerManager_.resetAll();
276 
277  ParentContext parentContext(globalContext.get());
278  //make sure the ProductResolvers know about their
279  // workers to allow proper data dependency handling
280  workerManager_.setupOnDemandSystem(ep,es);
281 
282  //make sure the task doesn't get run until all workers have beens started
283  WaitingTaskHolder holdForLoop(doneTask);
284  for(auto& worker: boost::adaptors::reverse((allWorkers()))) {
285  worker->doWorkAsync<T>(doneTask,ep,es,StreamID::invalidStreamID(),parentContext,globalContext.get());
286  }
287 
288  }
289 
290  template <typename T>
291  void
292  GlobalSchedule::runNow(typename T::MyPrincipal const& p, EventSetup const& es,
293  GlobalContext const* context) {
294  //do nothing for event since we will run when requested
295  for(auto & worker: allWorkers()) {
296  try {
297  ParentContext parentContext(context);
298  worker->doWork<T>(p, es,StreamID::invalidStreamID(), parentContext, context);
299  }
300  catch (cms::Exception & ex) {
301  if(ex.context().empty()) {
302  std::ostringstream ost;
303  ost << "Processing " <<T::transitionName()<<" "<< p.id();
304  ex.addContext(ost.str());
305  }
306  throw;
307  }
308  }
309  }
310 }
311 
312 #endif
void processOneGlobal(typename T::MyPrincipal &principal, EventSetup const &eventSetup, bool cleaningUpAfterException=false)
std::vector< std::string > vstring
edm::propagate_const< WorkerPtr > results_inserter_
std::vector< edm::propagate_const< WorkerPtr > > endPathStatusInserterWorkers_
roAction_t actions[nactions]
Definition: GenABIO.cc:187
const double w
Definition: UKUtility.cc:23
std::vector< Worker * > AllWorkers
#define noexcept
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
void runNow(typename T::MyPrincipal const &p, EventSetup const &es, GlobalContext const *context)
static StreamID invalidStreamID()
Definition: StreamID.h:48
ServiceToken presentToken() const
void beginJob()
Definition: Breakpoints.cc:15
void processOneGlobalAsync(WaitingTaskHolder holder, typename T::MyPrincipal &principal, EventSetup const &eventSetup, bool cleaningUpAfterException=false)
def principal(options)
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:191
static ServiceRegistry & instance()
std::shared_ptr< Worker > WorkerPtr
rep
Definition: cuy.py:1188
std::shared_ptr< ActivityRegistry > actReg_
FunctorWaitingTask< F > * make_waiting_task(ALLOC &&iAlloc, F f)
Definition: WaitingTask.h:90
void addContext(std::string const &context)
Definition: Exception.cc:227
SendTerminationSignalIfException(edm::ActivityRegistry *iReg, edm::GlobalContext const *iContext)
ExceptionToActionTable const & actionTable() const
returns the action table
HLT enums.
double a
Definition: hdecay.h:121
std::vector< edm::propagate_const< WorkerPtr > > pathStatusInserterWorkers_
auto wrap(F iFunc) -> decltype(iFunc())
ProcessContext const * processContext_
WorkerManager workerManager_
long double T