CMS 3D CMS Logo

externalGenerator.cc
Go to the documentation of this file.
1 #include "boost/program_options.hpp"
2 
3 #include <atomic>
4 #include <csignal>
5 #include <iostream>
6 #include <string>
7 #include <thread>
8 
10 
18 
20 
27 
28 static char const* const kMemoryNameOpt = "memory-name";
29 static char const* const kMemoryNameCommandOpt = "memory-name,m";
30 static char const* const kUniqueIDOpt = "unique-id";
31 static char const* const kUniqueIDCommandOpt = "unique-id,i";
32 static char const* const kHelpOpt = "help";
33 static char const* const kHelpCommandOpt = "help,h";
34 static char const* const kVerboseOpt = "verbose";
35 static char const* const kVerboseCommandOpt = "verbose,v";
36 
37 //NOTE: Can use TestProcessor as the harness for the worker
38 
39 using namespace edm::shared_memory;
40 class Harness {
41 public:
42  Harness(std::string const& iConfig, edm::ServiceToken iToken)
43  : tester_(edm::test::TestProcessor::Config{iConfig}, iToken) {}
44 
46  auto lumi = tester_.testBeginLuminosityBlock(iLumi);
47  ExternalGeneratorLumiInfo returnValue;
48  returnValue.header_ = *lumi.get<GenLumiInfoHeader>();
49  return returnValue;
50  }
51 
53  ExternalGeneratorEventInfo returnValue;
54  auto event = tester_.test();
55  returnValue.hepmc_ = *event.get<edm::HepMCProduct>("unsmeared");
56  returnValue.eventInfo_ = *event.get<GenEventInfoProduct>();
57  returnValue.keepEvent_ = event.modulePassed();
58  return returnValue;
59  }
60 
62  auto lumi = tester_.testEndLuminosityBlock();
63  return *lumi.get<GenLumiInfoProduct>();
64  }
65 
67  auto run = tester_.testEndRun();
68  return *run.get<GenRunInfoProduct>();
69  }
70 
71 private:
73 };
74 
75 template <typename T>
77 
78 int main(int argc, char* argv[]) {
79  std::string descString(argv[0]);
80  descString += " [--";
81  descString += kMemoryNameOpt;
82  descString += "] memory_name";
83  boost::program_options::options_description desc(descString);
84 
85  desc.add_options()(kHelpCommandOpt, "produce help message")(
87  boost::program_options::value<std::string>(),
88  "memory name")(kUniqueIDCommandOpt, boost::program_options::value<std::string>(), "unique id")(kVerboseCommandOpt,
89  "verbose output");
90 
91  boost::program_options::positional_options_description p;
92  p.add(kMemoryNameOpt, 1);
93  p.add(kUniqueIDOpt, 2);
94 
95  boost::program_options::options_description all_options("All Options");
96  all_options.add(desc);
97 
98  boost::program_options::variables_map vm;
99  try {
100  store(boost::program_options::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm);
101  notify(vm);
102  } catch (boost::program_options::error const& iException) {
103  std::cout << argv[0] << ": Error while trying to process command line arguments:\n"
104  << iException.what() << "\nFor usage and an options list, please do 'cmsRun --help'.";
105  return 1;
106  }
107 
108  if (vm.count(kHelpOpt)) {
109  std::cout << desc << std::endl;
110  return 0;
111  }
112 
113  bool verbose = false;
114  if (vm.count(kVerboseOpt)) {
115  verbose = true;
116  }
117 
118  if (!vm.count(kMemoryNameOpt)) {
119  std::cout << " no argument given" << std::endl;
120  return 1;
121  }
122 
123  if (!vm.count(kUniqueIDOpt)) {
124  std::cout << " no second argument given" << std::endl;
125  return 1;
126  }
127 
128  WorkerMonitorThread monitorThread;
129 
130  monitorThread.startThread();
131 
132  CMS_SA_ALLOW try {
133  std::string const memoryName(vm[kMemoryNameOpt].as<std::string>());
134  std::string const uniqueID(vm[kUniqueIDOpt].as<std::string>());
135  {
136  //This class is holding the lock
137  WorkerChannel communicationChannel(memoryName, uniqueID);
138 
139  WriteBuffer sm_buffer{memoryName, communicationChannel.fromWorkerBufferInfo()};
140  ReadBuffer sm_readbuffer{std::string("Rand") + memoryName, communicationChannel.toWorkerBufferInfo()};
141  int counter = 0;
142 
143  //The lock must be released if there is a catastrophic signal
144  auto lockPtr = communicationChannel.accessLock();
145  monitorThread.setAction([lockPtr]() {
146  if (lockPtr) {
147  std::cerr << "SIGNAL CAUGHT: unlock\n";
148  lockPtr->unlock();
149  }
150  });
151 
152  Serializer<ExternalGeneratorEventInfo> serializer(sm_buffer);
153  Serializer<ExternalGeneratorLumiInfo> bl_serializer(sm_buffer);
154  Serializer<GenLumiInfoProduct> el_serializer(sm_buffer);
155  Serializer<GenRunInfoProduct> er_serializer(sm_buffer);
156 
157  ROOTDeserializer<edm::RandomNumberGeneratorState, ReadBuffer> random_deserializer(sm_readbuffer);
158 
159  std::cerr << uniqueID << " process: initializing " << std::endl;
160  int nlines;
161  std::cin >> nlines;
162 
164  for (int i = 0; i < nlines; ++i) {
165  std::string c;
166  std::getline(std::cin, c);
167  if (verbose) {
168  std::cerr << c << "\n";
169  }
170  configuration += c + "\n";
171  }
172 
174  auto serviceToken =
175  edm::ServiceRegistry::createContaining(std::unique_ptr<edm::RandomNumberGenerator>(randomService));
176  Harness harness(configuration, serviceToken);
177 
178  //Either ROOT or the Framework are overriding the signal handlers
179  monitorThread.setupSignalHandling();
180 
181  if (verbose) {
182  std::cerr << uniqueID << " process: done initializing" << std::endl;
183  }
184  communicationChannel.workerSetupDone();
185 
186  if (verbose)
187  std::cerr << uniqueID << " process: waiting " << counter << std::endl;
188  communicationChannel.handleTransitions([&](edm::Transition iTransition, unsigned long long iTransitionID) {
189  ++counter;
190  switch (iTransition) {
192  if (verbose)
193  std::cerr << uniqueID << " process: start beginRun " << std::endl;
194  if (verbose)
195  std::cerr << uniqueID << " process: end beginRun " << std::endl;
196 
197  break;
198  }
200  if (verbose)
201  std::cerr << uniqueID << " process: start beginLumi " << std::endl;
202  auto randState = random_deserializer.deserialize();
203  if (verbose)
204  std::cerr << uniqueID << " random " << randState.state_.size() << " " << randState.seed_ << std::endl;
205  randomService->setState(randState.state_, randState.seed_);
206  auto value = harness.getBeginLumiValue(iTransitionID);
207  value.randomState_.state_ = randomService->getState();
208  value.randomState_.seed_ = randomService->mySeed();
209 
210  bl_serializer.serialize(value);
211  if (verbose)
212  std::cerr << uniqueID << " process: end beginLumi " << std::endl;
213  if (verbose)
214  std::cerr << uniqueID << " rand " << value.randomState_.state_.size() << " " << value.randomState_.seed_
215  << std::endl;
216  break;
217  }
218  case edm::Transition::Event: {
219  if (verbose)
220  std::cerr << uniqueID << " process: event " << counter << std::endl;
221  auto randState = random_deserializer.deserialize();
222  randomService->setState(randState.state_, randState.seed_);
223  auto value = harness.getEventValue();
224  value.randomState_.state_ = randomService->getState();
225  value.randomState_.seed_ = randomService->mySeed();
226 
227  if (verbose)
228  std::cerr << uniqueID << " process: event " << counter << std::endl;
229 
230  serializer.serialize(value);
231  if (verbose)
232  std::cerr << uniqueID << " process: "
233  << " " << counter << std::endl;
234  //usleep(10000000);
235  break;
236  }
238  if (verbose)
239  std::cerr << uniqueID << " process: start endLumi " << std::endl;
240  auto value = harness.getEndLumiValue();
241 
242  el_serializer.serialize(value);
243  if (verbose)
244  std::cerr << uniqueID << " process: end endLumi " << std::endl;
245 
246  break;
247  }
249  if (verbose)
250  std::cerr << uniqueID << " process: start endRun " << std::endl;
251  auto value = harness.getEndRunValue();
252 
253  er_serializer.serialize(value);
254  if (verbose)
255  std::cerr << uniqueID << " process: end endRun " << std::endl;
256 
257  break;
258  }
259  default: {
260  assert(false);
261  }
262  }
263  if (verbose)
264  std::cerr << uniqueID << " process: notifying and waiting " << counter << std::endl;
265  });
266  }
267  } catch (std::exception const& iExcept) {
268  std::cerr << "caught exception \n" << iExcept.what() << "\n";
269  return 1;
270  } catch (...) {
271  std::cerr << "caught unknown exception";
272  return 1;
273  }
274  return 0;
275 }
GenEventInfoProduct
Definition: GenEventInfoProduct.h:17
counter
Definition: counter.py:1
cmsBatch.argv
argv
Definition: cmsBatch.py:279
Harness::getEndRunValue
edmtest::ThingCollection getEndRunValue()
Definition: interprocess.cc:52
mps_fire.i
i
Definition: mps_fire.py:428
kUniqueIDOpt
static char const *const kUniqueIDOpt
Definition: externalGenerator.cc:30
dir2webdir.argc
argc
Definition: dir2webdir.py:39
ReadBuffer.h
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::Transition::Event
kUniqueIDCommandOpt
static char const *const kUniqueIDCommandOpt
Definition: externalGenerator.cc:31
TestProcessor
gather_cfg.cout
cout
Definition: gather_cfg.py:144
cms::cuda::assert
assert(be >=bs)
edm::shared_memory::WorkerMonitorThread
Definition: WorkerMonitorThread.h:32
edm::ExternalRandomNumberGeneratorService::mySeed
std::uint32_t mySeed() const final
Definition: ExternalRandomNumberGeneratorService.cc:69
kMemoryNameCommandOpt
static char const *const kMemoryNameCommandOpt
Definition: externalGenerator.cc:29
Harness::getBeginLumiValue
ExternalGeneratorLumiInfo getBeginLumiValue(unsigned int iLumi)
Definition: externalGenerator.cc:45
edm::shared_memory::ROOTSerializer
Definition: ROOTSerializer.h:31
Harness
Definition: interprocess.cc:28
relativeConstraints.error
error
Definition: relativeConstraints.py:53
GenRunInfoProduct.h
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
ExternalGeneratorEventInfo.h
kMemoryNameOpt
static char const *const kMemoryNameOpt
Definition: externalGenerator.cc:28
edm::shared_memory::WorkerChannel::fromWorkerBufferInfo
BufferInfo * fromWorkerBufferInfo()
This can be used with WriteBuffer to keep Controller and Worker in sync.
Definition: WorkerChannel.h:53
TestProcessor.h
edm::shared_memory::ROOTDeserializer
Definition: ROOTDeserializer.h:31
edm::shared_memory::WriteBuffer
Definition: WriteBuffer.h:37
edm::shared_memory::WorkerChannel::accessLock
boost::interprocess::scoped_lock< boost::interprocess::named_mutex > * accessLock()
the lock is made accessible so that the WorkerMonitorThread can be used to unlock it in the event of ...
Definition: WorkerChannel.h:48
edm::shared_memory::WorkerMonitorThread::setupSignalHandling
void setupSignalHandling()
Sets the unix signal handler which communicates with the thread.
Definition: WorkerMonitorThread.cc:102
ExternalGeneratorEventInfo::hepmc_
edm::HepMCProduct hepmc_
Definition: ExternalGeneratorEventInfo.h:15
edm::ServiceToken
Definition: ServiceToken.h:42
test
Definition: SmallWORMDict.h:13
edm::ExternalRandomNumberGeneratorService::setState
void setState(std::vector< unsigned long > const &, long seed)
Definition: ExternalRandomNumberGeneratorService.cc:24
ExternalGeneratorLumiInfo::header_
GenLumiInfoHeader header_
Definition: ExternalGeneratorLumiInfo.h:14
kHelpCommandOpt
static char const *const kHelpCommandOpt
Definition: externalGenerator.cc:33
edm::shared_memory::WorkerMonitorThread::startThread
void startThread()
Definition: WorkerMonitorThread.cc:78
Harness::Harness
Harness(std::string const &iConfig, edm::ServiceToken iToken)
Definition: externalGenerator.cc:42
main
int main(int argc, char *argv[])
Definition: externalGenerator.cc:78
edm::shared_memory::WorkerMonitorThread::setAction
void setAction(std::function< void()> iFunc)
Definition: WorkerMonitorThread.h:48
verbose
static constexpr int verbose
Definition: HLTExoticaSubAnalysis.cc:25
kVerboseCommandOpt
static char const *const kVerboseCommandOpt
Definition: externalGenerator.cc:35
edm::Transition::BeginLuminosityBlock
GenRunInfoProduct
Definition: GenRunInfoProduct.h:8
Harness::getBeginLumiValue
edmtest::ThingCollection getBeginLumiValue(unsigned int iLumi)
Definition: interprocess.cc:37
edm::shared_memory::ROOTSerializer::serialize
void serialize(T &iValue)
Definition: ROOTSerializer.h:44
kVerboseOpt
static char const *const kVerboseOpt
Definition: externalGenerator.cc:34
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
edm::Transition
Transition
Definition: Transition.h:12
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
GenEventInfoProduct.h
Harness::getEndRunValue
GenRunInfoProduct getEndRunValue()
Definition: externalGenerator.cc:66
ctppsRawToDigi_cff.configuration
configuration
Definition: ctppsRawToDigi_cff.py:11
ExternalGeneratorEventInfo::eventInfo_
GenEventInfoProduct eventInfo_
Definition: ExternalGeneratorEventInfo.h:16
Harness::getEventValue
edmtest::ThingCollection getEventValue()
Definition: interprocess.cc:42
WorkerChannel.h
Harness::getEventValue
ExternalGeneratorEventInfo getEventValue()
Definition: externalGenerator.cc:52
ExternalRandomNumberGeneratorService.h
value
Definition: value.py:1
GenLumiInfoProduct
Definition: GenLumiInfoProduct.h:11
GenLumiInfoHeader.h
counter
static std::atomic< unsigned int > counter
Definition: SharedResourceNames.cc:18
edm::test::TestProcessor
Definition: TestProcessor.h:159
edm::shared_memory
Definition: buffer_names.h:27
ExternalGeneratorLumiInfo
Definition: ExternalGeneratorLumiInfo.h:13
edm::shared_memory::ROOTDeserializer::deserialize
T deserialize()
Definition: ROOTDeserializer.h:44
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::shared_memory::WorkerChannel::handleTransitions
void handleTransitions(F &&iF)
Definition: WorkerChannel.h:65
Harness::getEndLumiValue
edmtest::ThingCollection getEndLumiValue()
Definition: interprocess.cc:47
edm::shared_memory::WorkerChannel
Definition: WorkerChannel.h:35
ExternalGeneratorEventInfo::keepEvent_
bool keepEvent_
Definition: ExternalGeneratorEventInfo.h:18
edm::ExternalRandomNumberGeneratorService::getState
std::vector< unsigned long > getState() const
Definition: ExternalRandomNumberGeneratorService.cc:32
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
edm::Transition::EndLuminosityBlock
writedatasetfile.run
run
Definition: writedatasetfile.py:27
edm::ServiceRegistry::createContaining
static ServiceToken createContaining(std::unique_ptr< T > iService)
create a service token that holds the service defined by iService
Definition: ServiceRegistry.h:99
edm::Transition::BeginRun
ROOTDeserializer.h
AlcaSiPixelAliHarvester0T_cff.options
options
Definition: AlcaSiPixelAliHarvester0T_cff.py:42
ExternalGeneratorLumiInfo.h
WriteBuffer.h
Harness::getEndLumiValue
GenLumiInfoProduct getEndLumiValue()
Definition: externalGenerator.cc:61
WorkerMonitorThread.h
edm::shared_memory::ReadBuffer
Definition: ReadBuffer.h:34
edm::HepMCProduct
Definition: HepMCProduct.h:21
kHelpOpt
static char const *const kHelpOpt
Definition: externalGenerator.cc:32
HepMCProduct.h
Config
Definition: Config.py:1
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
edm::ExternalRandomNumberGeneratorService
Definition: ExternalRandomNumberGeneratorService.h:20
edm::Transition::EndRun
edm::shared_memory::WorkerChannel::workerSetupDone
void workerSetupDone()
Matches the ControllerChannel::setupWorker call.
Definition: WorkerChannel.h:56
GenLumiInfoProduct.h
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
lumi
Definition: LumiSectionData.h:20
edm::shared_memory::WorkerChannel::toWorkerBufferInfo
BufferInfo * toWorkerBufferInfo()
This can be used with ReadBuffer to keep Controller and Worker in sync.
Definition: WorkerChannel.h:51
GenLumiInfoHeader
Definition: GenLumiInfoHeader.h:12
ROOTSerializer.h
ExternalGeneratorEventInfo
Definition: ExternalGeneratorEventInfo.h:14