Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-28 22:48:25

0001 #include "FWCore/Framework/interface/WorkerManager.h"
0002 #include "UnscheduledConfigurator.h"
0003 
0004 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0005 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0006 #include "FWCore/Framework/interface/maker/Worker.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/Utilities/interface/Algorithms.h"
0009 #include "FWCore/Utilities/interface/ConvertException.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "FWCore/Utilities/interface/ExceptionCollector.h"
0012 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0013 
0014 #include <exception>
0015 #include <functional>
0016 
0017 static const std::string kFilterType("EDFilter");
0018 static const std::string kProducerType("EDProducer");
0019 
0020 namespace edm {
0021   // -----------------------------
0022 
0023   WorkerManager::WorkerManager(std::shared_ptr<ActivityRegistry> areg,
0024                                ExceptionToActionTable const& actions,
0025                                ModuleTypeResolverMaker const* typeResolverMaker)
0026       : workerReg_(areg, typeResolverMaker),
0027         actionTable_(&actions),
0028         allWorkers_(),
0029         unscheduled_(*areg),
0030         lastSetupEventPrincipal_(nullptr) {}  // WorkerManager::WorkerManager
0031 
0032   WorkerManager::WorkerManager(std::shared_ptr<ModuleRegistry> modReg,
0033                                std::shared_ptr<ActivityRegistry> areg,
0034                                ExceptionToActionTable const& actions)
0035       : workerReg_(areg, modReg),
0036         actionTable_(&actions),
0037         allWorkers_(),
0038         unscheduled_(*areg),
0039         lastSetupEventPrincipal_(nullptr) {}  // WorkerManager::WorkerManager
0040 
0041   void WorkerManager::deleteModuleIfExists(std::string const& moduleLabel) {
0042     auto worker = workerReg_.get(moduleLabel);
0043     if (worker != nullptr) {
0044       auto eraseBeg = std::remove(allWorkers_.begin(), allWorkers_.end(), worker);
0045       allWorkers_.erase(eraseBeg, allWorkers_.end());
0046       unscheduled_.removeWorker(worker);
0047       workerReg_.deleteModule(moduleLabel);
0048     }
0049   }
0050 
0051   Worker* WorkerManager::getWorker(ParameterSet& pset,
0052                                    ProductRegistry& preg,
0053                                    PreallocationConfiguration const* prealloc,
0054                                    std::shared_ptr<ProcessConfiguration const> processConfiguration,
0055                                    std::string const& label) {
0056     WorkerParams params(&pset, preg, prealloc, processConfiguration, *actionTable_);
0057     return workerReg_.getWorker(params, label);
0058   }
0059 
0060   void WorkerManager::addToUnscheduledWorkers(ParameterSet& pset,
0061                                               ProductRegistry& preg,
0062                                               PreallocationConfiguration const* prealloc,
0063                                               std::shared_ptr<ProcessConfiguration const> processConfiguration,
0064                                               std::string label,
0065                                               std::set<std::string>& unscheduledLabels,
0066                                               std::vector<std::string>& shouldBeUsedLabels) {
0067     //Need to
0068     // 1) create worker
0069     // 2) if it is a WorkerT<EDProducer>, add it to our list
0070     auto modType = pset.getParameter<std::string>("@module_edm_type");
0071     if (modType == kProducerType || modType == kFilterType) {
0072       Worker* newWorker = getWorker(pset, preg, prealloc, processConfiguration, label);
0073       assert(newWorker->moduleType() == Worker::kProducer || newWorker->moduleType() == Worker::kFilter);
0074       unscheduledLabels.insert(label);
0075       unscheduled_.addWorker(newWorker);
0076       //add to list so it gets reset each new event
0077       addToAllWorkers(newWorker);
0078     } else {
0079       shouldBeUsedLabels.push_back(label);
0080     }
0081   }
0082 
0083   void WorkerManager::beginJob(ProductRegistry const& iRegistry,
0084                                eventsetup::ESRecordsToProductResolverIndices const& iESIndices,
0085                                ProcessBlockHelperBase const& processBlockHelperBase,
0086                                GlobalContext const& globalContext) {
0087     std::exception_ptr exceptionPtr;
0088     CMS_SA_ALLOW try {
0089       auto const processBlockLookup = iRegistry.productLookup(InProcess);
0090       auto const runLookup = iRegistry.productLookup(InRun);
0091       auto const lumiLookup = iRegistry.productLookup(InLumi);
0092       auto const eventLookup = iRegistry.productLookup(InEvent);
0093       if (!allWorkers_.empty()) {
0094         auto const& processName = allWorkers_[0]->description()->processName();
0095         auto processBlockModuleToIndicies = processBlockLookup->indiciesForModulesInProcess(processName);
0096         auto runModuleToIndicies = runLookup->indiciesForModulesInProcess(processName);
0097         auto lumiModuleToIndicies = lumiLookup->indiciesForModulesInProcess(processName);
0098         auto eventModuleToIndicies = eventLookup->indiciesForModulesInProcess(processName);
0099         for (auto& worker : allWorkers_) {
0100           worker->updateLookup(InProcess, *processBlockLookup);
0101           worker->updateLookup(InRun, *runLookup);
0102           worker->updateLookup(InLumi, *lumiLookup);
0103           worker->updateLookup(InEvent, *eventLookup);
0104           worker->updateLookup(iESIndices);
0105           worker->resolvePutIndicies(InProcess, processBlockModuleToIndicies);
0106           worker->resolvePutIndicies(InRun, runModuleToIndicies);
0107           worker->resolvePutIndicies(InLumi, lumiModuleToIndicies);
0108           worker->resolvePutIndicies(InEvent, eventModuleToIndicies);
0109           worker->selectInputProcessBlocks(iRegistry, processBlockHelperBase);
0110         }
0111       }
0112     } catch (...) {
0113       exceptionPtr = std::current_exception();
0114     }
0115 
0116     for (auto& worker : allWorkers_) {
0117       CMS_SA_ALLOW try { worker->beginJob(globalContext); } catch (...) {
0118         if (!exceptionPtr) {
0119           exceptionPtr = std::current_exception();
0120         }
0121       }
0122     }
0123     if (exceptionPtr) {
0124       std::rethrow_exception(exceptionPtr);
0125     }
0126   }
0127 
0128   void WorkerManager::endJob(ExceptionCollector& collector, GlobalContext const& globalContext) {
0129     for (auto& worker : allWorkers_) {
0130       try {
0131         convertException::wrap([&worker, &globalContext]() { worker->endJob(globalContext); });
0132       } catch (cms::Exception const& ex) {
0133         collector.addException(ex);
0134       }
0135     }
0136   }
0137 
0138   void WorkerManager::beginStream(StreamID streamID, StreamContext const& streamContext) {
0139     std::exception_ptr exceptionPtr;
0140     for (auto& worker : allWorkers_) {
0141       CMS_SA_ALLOW try { worker->beginStream(streamID, streamContext); } catch (...) {
0142         if (!exceptionPtr) {
0143           exceptionPtr = std::current_exception();
0144         }
0145       }
0146     }
0147     if (exceptionPtr) {
0148       std::rethrow_exception(exceptionPtr);
0149     }
0150   }
0151 
0152   void WorkerManager::endStream(StreamID streamID,
0153                                 StreamContext const& streamContext,
0154                                 ExceptionCollector& collector,
0155                                 std::mutex& collectorMutex) noexcept {
0156     for (auto& worker : allWorkers_) {
0157       CMS_SA_ALLOW try { worker->endStream(streamID, streamContext); } catch (...) {
0158         std::exception_ptr exceptionPtr = std::current_exception();
0159         std::lock_guard<std::mutex> collectorLock(collectorMutex);
0160         collector.call([&exceptionPtr]() { std::rethrow_exception(exceptionPtr); });
0161       }
0162     }
0163   }
0164 
0165   void WorkerManager::resetAll() { for_all(allWorkers_, std::bind(&Worker::reset, std::placeholders::_1)); }
0166 
0167   void WorkerManager::addToAllWorkers(Worker* w) {
0168     if (!search_all(allWorkers_, w)) {
0169       allWorkers_.push_back(w);
0170     }
0171   }
0172 
0173   void WorkerManager::setupResolvers(Principal& ep) {
0174     this->resetAll();
0175     if (&ep != lastSetupEventPrincipal_) {
0176       UnscheduledConfigurator config(allWorkers_.begin(), allWorkers_.end(), &(unscheduled_.auxiliary()));
0177       ep.setupUnscheduled(config);
0178       lastSetupEventPrincipal_ = &ep;
0179     }
0180   }
0181 
0182   void WorkerManager::setupOnDemandSystem(EventTransitionInfo const& info) {
0183     unscheduled_.setEventTransitionInfo(info);
0184   }
0185 
0186 }  // namespace edm