CMS 3D CMS Logo

PythonEventProcessor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Subsystem/Package
4 // Class : PythonEventProcessor
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: root
10 // Created: Fri, 20 Jan 2017 16:36:41 GMT
11 //
12 
13 // system include files
14 #include <mutex>
15 #include "oneapi/tbb/task_arena.h"
16 
17 // user include files
20 
24 
27 
29 
34 
35 namespace {
36  std::once_flag pluginFlag;
37  int setupPluginSystem() {
38  std::call_once(pluginFlag, []() { edmplugin::PluginManager::configure(edmplugin::standard::config()); });
39  return 0;
40  }
41 
42  std::shared_ptr<edm::ProcessDesc> addDefaultServicesToProcessDesc(std::shared_ptr<edm::ProcessDesc> iDesc) {
43  iDesc->addServices(edm::defaultCmsRunServices());
44  return iDesc;
45  }
46 
47  //Only one ThreadsController can be active at a time
48  CMS_THREAD_SAFE std::unique_ptr<edm::ThreadsController> tsiPtr;
49  CMS_THREAD_SAFE int nThreads;
50 
51  std::shared_ptr<edm::ProcessDesc> setupThreading(std::shared_ptr<edm::ProcessDesc> iDesc) {
52  // check the "options" ParameterSet
53  std::shared_ptr<edm::ParameterSet> pset = iDesc->getProcessPSet();
54  auto threadsInfo = threadOptions(*pset);
55 
56  threadsInfo.nThreads_ = edm::setNThreads(threadsInfo.nThreads_, threadsInfo.stackSize_, tsiPtr);
57  nThreads = threadsInfo.nThreads_;
58 
59  // update the numberOfThreads and sizeOfStackForThreadsInKB in the "options" ParameterSet
60  setThreadOptions(threadsInfo, *pset);
61 
62  return iDesc;
63  }
64 
65  edm::ServiceToken createJobReport() {
68  std::make_unique<edm::JobReport>(nullptr)));
69  }
70 } // namespace
71 
72 //
73 // constants, enums and typedefs
74 //
75 
76 //
77 // static data member definitions
78 //
79 
80 //
81 // constructors and destructor
82 //
84  : forcePluginSetupFirst_(setupPluginSystem()),
85  processor_(addDefaultServicesToProcessDesc(setupThreading(iDesc.processDesc())),
86  createJobReport(),
87  edm::serviceregistry::kOverlapIsError) {}
88 
89 namespace {
90  class TaskCleanupSentry {
91  public:
92  TaskCleanupSentry(edm::EventProcessor* ep) : ep_(ep) {}
93  ~TaskCleanupSentry() { ep_->taskCleanup(); }
94 
95  private:
97  };
98 } // namespace
99 
101  auto gil = PyEval_SaveThread();
102  // Protects the destructor from throwing exceptions.
103  CMS_SA_ALLOW try {
104  oneapi::tbb::task_arena{nThreads}.execute([this]() {
105  TaskCleanupSentry s(&processor_);
106  processor_.endJob();
107  });
108  } catch (...) {
109  }
110  PyEval_RestoreThread(gil);
111 }
112 
114  auto gil = PyEval_SaveThread();
115  try {
116  oneapi::tbb::task_arena{nThreads}.execute([this]() { (void)processor_.runToCompletion(); });
117  } catch (...) {
118  }
119  PyEval_RestoreThread(gil);
120 }
std::vector< std::string > defaultCmsRunServices()
void setThreadOptions(ThreadsInfo const &threadsInfo, edm::ParameterSet &pset)
Definition: ThreadsInfo.cc:27
#define CMS_SA_ALLOW
static PluginManager & configure(const Config &)
edm::EventProcessor processor_
unsigned int setNThreads(unsigned int iNThreads, unsigned int iStackSize, std::unique_ptr< ThreadsController > &oPtr)
Definition: setNThreads.cc:11
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
PythonEventProcessor(PyBind11ProcessDesc const &)
PluginManager::Config config()
Definition: standard.cc:21
StatusCode runToCompletion()
static ServiceToken createContaining(std::unique_ptr< T > iService)
create a service token that holds the service defined by iService
#define CMS_THREAD_SAFE
ThreadsInfo threadOptions(edm::ParameterSet const &pset)
Definition: ThreadsInfo.cc:11
HLT enums.