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> 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
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
edm::GlobalSchedule::allWorkers
AllWorkers const & allWorkers() const
returns the collection of pointers to workers
Definition: GlobalSchedule.h:129
WorkerManager.h
edm::TerminationOrigin::ExceptionFromThisContext
edm::eventsetup::ESRecordsToProxyIndices
Definition: ESRecordsToProxyIndices.h:35
funct::false
false
Definition: Factorize.h:29
edm::GlobalSchedule::SendTerminationSignalIfException::completedSuccessfully
void completedSuccessfully()
Definition: GlobalSchedule.h:145
edm::ServiceWeakToken
Definition: ServiceToken.h:86
BranchType.h
ActivityRegistry
propagate_const.h
WaitingTaskHolder.h
edm::GlobalSchedule::beginJob
void beginJob(ProductRegistry const &, eventsetup::ESRecordsToProxyIndices const &, ProcessBlockHelperBase const &)
Definition: GlobalSchedule.cc:93
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ProcessContext
Definition: ProcessContext.h:27
edm::GlobalSchedule::terminate
bool terminate() const
Return whether each output module has reached its maximum count.
Algorithms.h
ProcessBlockPrincipal.h
edm::GlobalSchedule::deleteModule
void deleteModule(std::string const &iLabel)
Delete the module with label iLabel.
Definition: GlobalSchedule.cc:117
edm::maker::ModuleHolder
Definition: ModuleHolder.h:37
edm::GlobalSchedule::vstring
std::vector< std::string > vstring
Definition: GlobalSchedule.h:83
edm::GlobalSchedule::Workers
std::vector< Worker * > Workers
Definition: GlobalSchedule.h:86
edm::WaitingTaskHolder::doneWaiting
void doneWaiting(std::exception_ptr iExcept)
Definition: WaitingTaskHolder.h:93
edm::GlobalSchedule::SendTerminationSignalIfException::reg_
edm::ActivityRegistry * reg_
Definition: GlobalSchedule.h:148
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
groupFilesInBlocks.reverse
reverse
Definition: groupFilesInBlocks.py:131
edm::ProductRegistry
Definition: ProductRegistry.h:37
ModuleDescription.h
visDQMUpload.context
context
Definition: visDQMUpload.py:37
edm::ServiceToken
Definition: ServiceToken.h:42
edm::propagate_const
Definition: propagate_const.h:32
edm::GlobalSchedule::SendTerminationSignalIfException::~SendTerminationSignalIfException
~SendTerminationSignalIfException()
Definition: GlobalSchedule.h:140
edm::GlobalSchedule::SendTerminationSignalIfException::context_
GlobalContext const * context_
Definition: GlobalSchedule.h:149
edm::GlobalSchedule::actReg_
std::shared_ptr< ActivityRegistry > actReg_
Definition: GlobalSchedule.h:156
edm::GlobalSchedule::getAllModuleDescriptions
std::vector< ModuleDescription const * > getAllModuleDescriptions() const
Definition: GlobalSchedule.cc:123
edm::ExceptionCollector
Definition: ExceptionCollector.h:33
EventPrincipal.h
edm::convertException::wrap
auto wrap(F iFunc) -> decltype(iFunc())
Definition: ConvertException.h:19
edm::ActivityRegistry
Definition: ActivityRegistry.h:134
edm::GlobalSchedule
Definition: GlobalSchedule.h:81
ConvertException.h
edm::Worker
Definition: Worker.h:91
edm::GlobalSchedule::processContext_
ProcessContext const * processContext_
Definition: GlobalSchedule.h:158
edm::GlobalSchedule::workerManagers_
std::vector< WorkerManager > workerManagers_
Definition: GlobalSchedule.h:155
edm::ParentContext
Definition: ParentContext.h:27
edm::GlobalSchedule::endJob
void endJob(ExceptionCollector &collector)
Definition: GlobalSchedule.cc:91
edm::GlobalContext
Definition: GlobalContext.h:29
edm::ProcessBlockHelperBase
Definition: ProcessBlockHelperBase.h:18
edm::ParameterSet
Definition: ParameterSet.h:47
a
double a
Definition: hdecay.h:119
GlobalContext.h
cms::Exception::context
std::list< std::string > const & context() const
Definition: Exception.cc:147
edm::make_waiting_task
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:101
edm::WaitingTaskHolder
Definition: WaitingTaskHolder.h:32
ExceptionMessages.h
thread_safety_macros.h
RunPrincipal.h
edm::StreamID::invalidStreamID
static StreamID invalidStreamID()
Definition: StreamID.h:45
edm::GlobalSchedule::actionTable
ExceptionToActionTable const & actionTable() const
returns the action table
Definition: GlobalSchedule.h:153
edm::addContextAndPrintException
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
Definition: ExceptionHelpers.cc:11
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
ExceptionActions.h
edm::GlobalSchedule::extraWorkers_
std::vector< edm::propagate_const< WorkerPtr > > extraWorkers_
Definition: GlobalSchedule.h:157
edm::GlobalSchedule::processOneGlobalAsync
void processOneGlobalAsync(WaitingTaskHolder holder, typename T::TransitionInfoType &, ServiceToken const &token, bool cleaningUpAfterException=false)
Definition: GlobalSchedule.h:162
edm::ServiceWeakToken::lock
ServiceToken lock() const
Definition: ServiceToken.h:101
edm::ExceptionToActionTable
Definition: ExceptionActions.h:16
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
LuminosityBlockPrincipal.h
edm::GlobalSchedule::GlobalSchedule
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)
Definition: GlobalSchedule.cc:22
OccurrenceTraits.h
edm::PreallocationConfiguration
Definition: PreallocationConfiguration.h:27
edm::GlobalSchedule::SendTerminationSignalIfException::SendTerminationSignalIfException
SendTerminationSignalIfException(edm::ActivityRegistry *iReg, edm::GlobalContext const *iContext)
Definition: GlobalSchedule.h:138
edm::GlobalSchedule::WorkerPtr
std::shared_ptr< Worker > WorkerPtr
Definition: GlobalSchedule.h:85
ExceptionHelpers.h
Frameworkfwd.h
T
long double T
Definition: Basic3DVectorLD.h:48
Worker.h
WorkerRegistry.h
Exception.h
edm::GlobalSchedule::SendTerminationSignalIfException
Definition: GlobalSchedule.h:136
edm::GlobalSchedule::replaceModule
void replaceModule(maker::ModuleHolder *iMod, std::string const &iLabel)
clone the type of module with label iLabel but configure with iPSet.
Definition: GlobalSchedule.cc:99
actions
roAction_t actions[nactions]
Definition: GenABIO.cc:181
cms::Exception
Definition: Exception.h:70
StreamID.h
edm::ServiceRegistry::Operate
Definition: ServiceRegistry.h:40
edm::WorkerManager
Definition: WorkerManager.h:36
edm::WaitingTaskHolder::group
tbb::task_group * group() const noexcept
Definition: WaitingTaskHolder.h:77
edm::GlobalSchedule::AllWorkers
std::vector< Worker * > AllWorkers
Definition: GlobalSchedule.h:84
FWCoreCommonFwd.h
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:316
edm::ActivityRegistry::preGlobalEarlyTerminationSignal_
PreGlobalEarlyTermination preGlobalEarlyTerminationSignal_
Definition: ActivityRegistry.h:507