CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EDLooperBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: <package>
4 // Module: EDLooperBase
5 //
6 // Author: Valentin Kuznetsov
7 // Created: Wed Jul 5 11:44:26 EDT 2006
8 
35 #include "tbb/global_control.h"
36 
37 namespace edm {
38 
40  : iCounter_(0),
41  act_table_(nullptr),
42  moduleChanger_(nullptr),
43  moduleDescription_("Looper", "looper"),
44  moduleCallingContext_(&moduleDescription_) {}
46 
48 
50  const edm::EventSetupImpl& esi,
51  edm::ProcessingController& ioController,
52  StreamContext* streamContext) {
54  streamContext->setEventID(eventPrincipal.id());
55  streamContext->setRunIndex(eventPrincipal.luminosityBlockPrincipal().runPrincipal().index());
56  streamContext->setLuminosityBlockIndex(eventPrincipal.luminosityBlockPrincipal().index());
57  streamContext->setTimestamp(eventPrincipal.time());
58  ParentContext parentContext(streamContext);
59  ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
61  event.setConsumer(this);
62 
64  try {
66  const EventSetup es{
67  esi, static_cast<unsigned int>(Transition::Event), esGetTokenIndices(Transition::Event), parentC, false};
68  status = duringLoop(event, es, ioController);
69  } catch (cms::Exception& e) {
70  e.addContext("Calling the 'duringLoop' method of a looper");
72  if (action != exception_actions::Rethrow) {
73  edm::printCmsExceptionWarning("SkipEvent", e);
74  } else {
75  throw;
76  }
77  }
78  return status;
79  }
80 
83  const EventSetup es{
84  esi, static_cast<unsigned int>(Transition::EndRun), esGetTokenIndices(Transition::EndRun), parentC, false};
85  return endOfLoop(es, iCounter_);
86  }
87 
89  ++iCounter_;
90 
91  std::set<edm::eventsetup::EventSetupRecordKey> const& keys = modifyingRecords();
92  for_all(keys,
93  std::bind(&eventsetup::EventSetupProvider::resetRecordPlusDependentRecords, esp, std::placeholders::_1));
94  }
95 
98  beginOfJob(EventSetup{iImpl,
99  static_cast<unsigned int>(Transition::BeginRun),
101  parentC,
102  false});
103  }
106 
108 
109  void EDLooperBase::doBeginRun(RunPrincipal& iRP, EventSetupImpl const& iES, ProcessContext* processContext) {
111  LuminosityBlockID(iRP.run(), 0),
112  iRP.index(),
114  iRP.beginTime(),
115  processContext);
116  ParentContext parentContext(&globalContext);
117  ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
119  run.setConsumer(this);
121  const EventSetup es{
122  iES, static_cast<unsigned int>(Transition::BeginRun), esGetTokenIndices(Transition::BeginRun), parentC, false};
123  beginRun(run, es);
124  }
125 
126  void EDLooperBase::doEndRun(RunPrincipal& iRP, EventSetupImpl const& iES, ProcessContext* processContext) {
128  LuminosityBlockID(iRP.run(), 0),
129  iRP.index(),
131  iRP.endTime(),
132  processContext);
133  ParentContext parentContext(&globalContext);
134  ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
136  run.setConsumer(this);
138  const EventSetup es{
139  iES, static_cast<unsigned int>(Transition::EndRun), esGetTokenIndices(Transition::EndRun), parentC, false};
140  endRun(run, es);
141  }
143  EventSetupImpl const& iES,
144  ProcessContext* processContext) {
146  iLB.id(),
147  iLB.runPrincipal().index(),
148  iLB.index(),
149  iLB.beginTime(),
150  processContext);
151  ParentContext parentContext(&globalContext);
152  ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
153  LuminosityBlock luminosityBlock(iLB, moduleDescription_, &moduleCallingContext_, false);
154  luminosityBlock.setConsumer(this);
156  const EventSetup es{iES,
157  static_cast<unsigned int>(Transition::BeginLuminosityBlock),
159  parentC,
160  false};
161  beginLuminosityBlock(luminosityBlock, es);
162  }
164  EventSetupImpl const& iES,
165  ProcessContext* processContext) {
167  iLB.id(),
168  iLB.runPrincipal().index(),
169  iLB.index(),
170  iLB.beginTime(),
171  processContext);
172  ParentContext parentContext(&globalContext);
173  ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
174  LuminosityBlock luminosityBlock(iLB, moduleDescription_, &moduleCallingContext_, true);
175  luminosityBlock.setConsumer(this);
177  const EventSetup es{iES,
178  static_cast<unsigned int>(Transition::EndLuminosityBlock),
180  parentC,
181  false};
182  endLuminosityBlock(luminosityBlock, es);
183  }
184 
185  void EDLooperBase::beginRun(Run const&, EventSetup const&) {}
186  void EDLooperBase::endRun(Run const&, EventSetup const&) {}
189 
191 
193  ServiceToken const& token,
194  Transition iTrans,
195  Principal const& iPrincipal,
196  EventSetupImpl const& iImpl) const {
197  esPrefetchAsync(iTask, iImpl, iTrans, token);
198  edPrefetchAsync(std::move(iTask), token, iPrincipal);
199  }
200 
202  ServiceToken const& token,
203  Principal const& iPrincipal) const {
204  //Based on Worker edPrefetchAsync
205 
206  // Prefetch products the module declares it consumes
207  std::vector<ProductResolverIndexAndSkipBit> const& items = itemsToGetFrom(iPrincipal.branchType());
208 
209  for (auto const& item : items) {
210  ProductResolverIndex productResolverIndex = item.productResolverIndex();
211  bool skipCurrentProcess = item.skipCurrentProcess();
212  if (productResolverIndex != ProductResolverIndexAmbiguous) {
213  iPrincipal.prefetchAsync(iTask, productResolverIndex, skipCurrentProcess, token, &moduleCallingContext_);
214  }
215  }
216  }
217 
219  EventSetupImpl const& iImpl,
220  Transition iTrans,
221  ServiceToken const& iToken) const {
222  //Based on Worker::esPrefetchAsync
224  return;
225  }
226  auto const& recs = esGetTokenRecordIndicesVector(iTrans);
227  auto const& items = esGetTokenIndicesVector(iTrans);
228 
229  assert(items.size() == recs.size());
230  if (items.empty()) {
231  return;
232  }
233 
234  //Thread case of 1 thread special. The master thread is doing a wait_for_all on the
235  // default tbb arena. It will not process any tasks on the es arena. We need to add a
236  // task that will synchronously do a wait_for_all in the es arena to be sure prefetching
237  // will work.
238 
239  if UNLIKELY (tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism) == 1) {
240  auto taskGroup = iTask.group();
241  ServiceWeakToken weakToken = iToken;
242  taskGroup->run([this, task = std::move(iTask), iTrans, &iImpl, weakToken]() {
243  std::exception_ptr exceptPtr{};
244  iImpl.taskArena()->execute([this, iTrans, &iImpl, weakToken, &exceptPtr]() {
245  exceptPtr = syncWait([&](WaitingTaskHolder&& iHolder) {
246  auto const& recs = esGetTokenRecordIndicesVector(iTrans);
247  auto const& items = esGetTokenIndicesVector(iTrans);
248  auto hWaitTask = std::move(iHolder);
249  auto token = weakToken.lock();
250  for (size_t i = 0; i != items.size(); ++i) {
251  if (recs[i] != ESRecordIndex{}) {
252  auto rec = iImpl.findImpl(recs[i]);
253  if (rec) {
254  rec->prefetchAsync(hWaitTask, items[i], &iImpl, token, ESParentContext(&moduleCallingContext_));
255  }
256  }
257  }
258  }); //syncWait
259  }); //esTaskArena().execute
260  //note use of a copy gets around declaring the lambda as mutable
261  auto tempTask = task;
262  tempTask.doneWaiting(exceptPtr);
263  }); //group.run
264  } else {
265  auto group = iTask.group();
266  //We need iTask to run in the default arena since it is not an ES task
267  auto task = make_waiting_task(
268  [holder = WaitingTaskWithArenaHolder(std::move(iTask))](std::exception_ptr const* iExcept) mutable {
269  if (iExcept) {
270  holder.doneWaiting(*iExcept);
271  } else {
272  holder.doneWaiting(std::exception_ptr{});
273  }
274  });
275 
276  WaitingTaskHolder tempH(*group, task);
277  iImpl.taskArena()->execute([&]() {
278  for (size_t i = 0; i != items.size(); ++i) {
279  if (recs[i] != ESRecordIndex{}) {
280  auto rec = iImpl.findImpl(recs[i]);
281  if (rec) {
282  rec->prefetchAsync(tempH, items[i], &iImpl, iToken, ESParentContext(&moduleCallingContext_));
283  }
284  }
285  }
286  });
287  }
288  }
289 
290  std::set<eventsetup::EventSetupRecordKey> EDLooperBase::modifyingRecords() const {
291  return std::set<eventsetup::EventSetupRecordKey>();
292  }
293 
294  void EDLooperBase::copyInfo(const ScheduleInfo& iInfo) { scheduleInfo_ = std::make_unique<ScheduleInfo>(iInfo); }
296 
298  const ScheduleInfo* EDLooperBase::scheduleInfo() const { return scheduleInfo_.get(); }
299 
300 } // namespace edm
RunPrincipal const & runPrincipal() const
ServiceToken lock() const
Definition: ServiceToken.h:101
void setConsumer(EDConsumerBase const *iConsumer)
void setTimestamp(Timestamp const &v)
Definition: StreamContext.h:69
virtual Status endOfLoop(EventSetup const &, unsigned int iCounter)=0
void doEndLuminosityBlock(LuminosityBlockPrincipal &, EventSetupImpl const &, ProcessContext *)
unsigned int ProductResolverIndex
ModuleCallingContext moduleCallingContext_
Definition: EDLooperBase.h:171
Timestamp const & beginTime() const
std::exception_ptr syncWait(F &&iFunc)
void prefetchAsync(WaitingTaskHolder iTask, ESProxyIndex iProxyIndex, EventSetupImpl const *, ServiceToken const &, ESParentContext) const
prefetch the data to setup for subsequent calls to getImplementation
unsigned int iCounter_
Definition: EDLooperBase.h:164
EventID const & id() const
list status
Definition: mps_update.py:107
Status doEndOfLoop(EventSetupImpl const &es)
Definition: EDLooperBase.cc:81
std::string const & category() const
Definition: Exception.cc:143
exception_actions::ActionCodes find(const std::string &category) const
LuminosityBlockIndex index() const
std::vector< ESProxyIndex > const & esGetTokenIndicesVector(edm::Transition iTrans) const
RunNumber_t run() const
Definition: RunPrincipal.h:61
assert(be >=bs)
virtual Status duringLoop(Event const &, EventSetup const &, ProcessingController &)=0
virtual std::set< eventsetup::EventSetupRecordKey > modifyingRecords() const
virtual void beginLuminosityBlock(LuminosityBlock const &, EventSetup const &)
Called after all event modules have processed the begin of a LuminosityBlock.
void prefetchAsync(WaitingTaskHolder iTask, ServiceToken const &token, Transition iTrans, Principal const &iPrincipal, EventSetupImpl const &iImpl) const
void setTransition(Transition v)
Definition: StreamContext.h:65
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:14
Timestamp const & time() const
void prefetchAsync(WaitingTaskHolder waitTask, ProductResolverIndex index, bool skipCurrentProcess, ServiceToken const &token, ModuleCallingContext const *mcc) const
Definition: Principal.cc:639
std::vector< ProductResolverIndexAndSkipBit > const & itemsToGetFrom(BranchType iType) const
ModuleDescription moduleDescription_
Definition: EDLooperBase.h:170
void setLuminosityBlockIndex(LuminosityBlockIndex const &v)
Definition: StreamContext.h:68
LuminosityBlockPrincipal const & luminosityBlockPrincipal() const
Timestamp const & beginTime() const
Definition: RunPrincipal.h:67
BranchType const & branchType() const
Definition: Principal.h:181
Status doDuringLoop(EventPrincipal &eventPrincipal, EventSetupImpl const &es, ProcessingController &, StreamContext *)
Definition: EDLooperBase.cc:49
def move
Definition: eostools.py:511
tbb::task_arena * taskArena() const
Timestamp const & endTime() const
Definition: RunPrincipal.h:69
Transition
Definition: Transition.h:12
void edPrefetchAsync(WaitingTaskHolder iTask, ServiceToken const &token, Principal const &iPrincipal) const
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:101
virtual void endOfJob()
void doBeginRun(RunPrincipal &, EventSetupImpl const &, ProcessContext *)
void resetRecordPlusDependentRecords(EventSetupRecordKey const &)
Used when we need to force a Record to reset all its proxies.
~EDLooperBase() noexcept(false) override
Definition: EDLooperBase.cc:45
ScheduleInfo const * scheduleInfo() const
This returns a non-zero value after the constructor has been called.
static LuminosityBlockIndex invalidLuminosityBlockIndex()
void setModuleChanger(ModuleChanger *)
edm::propagate_const< std::unique_ptr< ScheduleInfo > > scheduleInfo_
Definition: EDLooperBase.h:167
ExceptionToActionTable const * act_table_
Definition: EDLooperBase.h:165
void copyInfo(ScheduleInfo const &)
void doBeginLuminosityBlock(LuminosityBlockPrincipal &, EventSetupImpl const &, ProcessContext *)
tuple group
Definition: watchdog.py:82
virtual void beginOfJob()
void addContext(std::string const &context)
Definition: Exception.cc:165
virtual void endRun(Run const &, EventSetup const &)
Called after all event modules have processed the end of a Run.
void prepareForNextLoop(eventsetup::EventSetupProvider *esp)
Definition: EDLooperBase.cc:88
virtual void beginRun(Run const &, EventSetup const &)
Called after all event modules have processed the begin of a Run.
string action
Definition: mps_fire.py:183
tbb::task_group * group() const noexcept
RunIndex index() const
Definition: RunPrincipal.h:57
virtual void startingNewLoop(unsigned int)=0
void setEventID(EventID const &v)
Definition: StreamContext.h:66
std::vector< ESRecordIndex > const & esGetTokenRecordIndicesVector(edm::Transition iTrans) const
void esPrefetchAsync(WaitingTaskHolder iTask, EventSetupImpl const &iImpl, Transition iTrans, ServiceToken const &iToken) const
ModuleChanger * moduleChanger()
This only returns a non-zero value during the call to endOfLoop.
edm::propagate_const< ModuleChanger * > moduleChanger_
Definition: EDLooperBase.h:168
void doEndRun(RunPrincipal &, EventSetupImpl const &, ProcessContext *)
#define UNLIKELY(x)
Definition: Likely.h:21
void setConsumer(EDConsumerBase const *iConsumer)
Definition: Run.h:52
eventsetup::EventSetupRecordImpl const * findImpl(const eventsetup::EventSetupRecordKey &) const
void setRunIndex(RunIndex const &v)
Definition: StreamContext.h:67
virtual void attachTo(ActivityRegistry &)
Override this method if you need to monitor the state of the processing.
void doStartingNewLoop()
Definition: EDLooperBase.cc:47
Definition: Run.h:45
void printCmsExceptionWarning(char const *behavior, cms::Exception const &e)
ESProxyIndex const * esGetTokenIndices(edm::Transition iTrans) const
virtual void endLuminosityBlock(LuminosityBlock const &, EventSetup const &)
Called after all event modules have processed the end of a LuminosityBlock.