CMS 3D CMS Logo

Classes | Typedefs | Functions | Variables
externalGenerator.cc File Reference
#include "boost/program_options.hpp"
#include <atomic>
#include <csignal>
#include <iostream>
#include <string>
#include <thread>
#include "FWCore/TestProcessor/interface/TestProcessor.h"
#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/ExternalGeneratorEventInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/ExternalGeneratorLumiInfo.h"
#include "FWCore/Services/interface/ExternalRandomNumberGeneratorService.h"
#include "FWCore/SharedMemory/interface/WriteBuffer.h"
#include "FWCore/SharedMemory/interface/ReadBuffer.h"
#include "FWCore/SharedMemory/interface/WorkerChannel.h"
#include "FWCore/SharedMemory/interface/ROOTSerializer.h"
#include "FWCore/SharedMemory/interface/ROOTDeserializer.h"
#include "FWCore/SharedMemory/interface/WorkerMonitorThread.h"

Go to the source code of this file.

Classes

class  Harness
 

Typedefs

template<typename T >
using Serializer = ROOTSerializer< T, WriteBuffer >
 

Functions

int main (int argc, char *argv[])
 

Variables

static char const *const kHelpCommandOpt = "help,h"
 
static char const *const kHelpOpt = "help"
 
static char const *const kMemoryNameCommandOpt = "memory-name,m"
 
static char const *const kMemoryNameOpt = "memory-name"
 
static char const *const kUniqueIDCommandOpt = "unique-id,i"
 
static char const *const kUniqueIDOpt = "unique-id"
 
static char const *const kVerboseCommandOpt = "verbose,v"
 
static char const *const kVerboseOpt = "verbose"
 

Typedef Documentation

template<typename T >
using Serializer = ROOTSerializer<T, WriteBuffer>

Definition at line 76 of file externalGenerator.cc.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 78 of file externalGenerator.cc.

References edm::shared_memory::WorkerChannel::accessLock(), edm::BeginLuminosityBlock, edm::BeginRun, EnergyCorrector::c, MessageLogger_cfi::cerr, cmsCodeRulesChecker::configuration, counter, gather_cfg::cout, edm::ServiceRegistry::createContaining(), edm::shared_memory::ROOTDeserializer< T, READBUFFER >::deserialize(), edm::EndLuminosityBlock, edm::EndRun, relativeConstraints::error, edm::Event, cppFunctionSkipper::exception, edm::shared_memory::WorkerChannel::fromWorkerBufferInfo(), Harness::getBeginLumiValue(), Harness::getEndLumiValue(), Harness::getEndRunValue(), Harness::getEventValue(), edm::ExternalRandomNumberGeneratorService::getState(), edm::shared_memory::WorkerChannel::handleTransitions(), mps_fire::i, kHelpCommandOpt, kHelpOpt, kMemoryNameCommandOpt, kMemoryNameOpt, kUniqueIDCommandOpt, kUniqueIDOpt, kVerboseCommandOpt, kVerboseOpt, edm::ExternalRandomNumberGeneratorService::mySeed(), AlcaSiPixelAliHarvester0T_cff::options, AlCaHLTBitMon_ParallelJobs::p, writedatasetfile::run, edm::shared_memory::ROOTSerializer< T, WRITEBUFFER >::serialize(), edm::shared_memory::WorkerMonitorThread::setAction(), edm::ExternalRandomNumberGeneratorService::setState(), edm::shared_memory::WorkerMonitorThread::setupSignalHandling(), edm::shared_memory::WorkerMonitorThread::startThread(), AlCaHLTBitMon_QueryRunRegistry::string, edm::shared_memory::WorkerChannel::toWorkerBufferInfo(), and edm::shared_memory::WorkerChannel::workerSetupDone().

78  {
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")(
86  kMemoryNameCommandOpt, boost::program_options::value<std::string>(), "memory name")(
87  kUniqueIDCommandOpt, boost::program_options::value<std::string>(), "unique id")(kVerboseCommandOpt,
88  "verbose output");
89 
90  boost::program_options::positional_options_description p;
91  p.add(kMemoryNameOpt, 1);
92  p.add(kUniqueIDOpt, 2);
93 
94  boost::program_options::options_description all_options("All Options");
95  all_options.add(desc);
96 
97  boost::program_options::variables_map vm;
98  try {
99  store(boost::program_options::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm);
100  notify(vm);
101  } catch (boost::program_options::error const& iException) {
102  std::cout << argv[0] << ": Error while trying to process command line arguments:\n"
103  << iException.what() << "\nFor usage and an options list, please do 'cmsRun --help'.";
104  return 1;
105  }
106 
107  if (vm.count(kHelpOpt)) {
108  std::cout << desc << std::endl;
109  return 0;
110  }
111 
112  bool verbose = false;
113  if (vm.count(kVerboseOpt)) {
114  verbose = true;
115  }
116 
117  if (!vm.count(kMemoryNameOpt)) {
118  std::cout << " no argument given" << std::endl;
119  return 1;
120  }
121 
122  if (!vm.count(kUniqueIDOpt)) {
123  std::cout << " no second argument given" << std::endl;
124  return 1;
125  }
126 
127  WorkerMonitorThread monitorThread;
128 
129  monitorThread.startThread();
130 
131  try {
132  std::string const memoryName(vm[kMemoryNameOpt].as<std::string>());
133  std::string const uniqueID(vm[kUniqueIDOpt].as<std::string>());
134  {
135  //This class is holding the lock
136  WorkerChannel communicationChannel(memoryName, uniqueID);
137 
138  WriteBuffer sm_buffer{memoryName, communicationChannel.fromWorkerBufferInfo()};
139  ReadBuffer sm_readbuffer{std::string("Rand") + memoryName, communicationChannel.toWorkerBufferInfo()};
140  int counter = 0;
141 
142  //The lock must be released if there is a catastrophic signal
143  auto lockPtr = communicationChannel.accessLock();
144  monitorThread.setAction([lockPtr]() {
145  if (lockPtr) {
146  std::cerr << "SIGNAL CAUGHT: unlock\n";
147  lockPtr->unlock();
148  }
149  });
150 
151  Serializer<ExternalGeneratorEventInfo> serializer(sm_buffer);
152  Serializer<ExternalGeneratorLumiInfo> bl_serializer(sm_buffer);
153  Serializer<GenLumiInfoProduct> el_serializer(sm_buffer);
154  Serializer<GenRunInfoProduct> er_serializer(sm_buffer);
155 
156  ROOTDeserializer<edm::RandomNumberGeneratorState, ReadBuffer> random_deserializer(sm_readbuffer);
157 
158  std::cerr << uniqueID << " process: initializing " << std::endl;
159  int nlines;
160  std::cin >> nlines;
161 
163  for (int i = 0; i < nlines; ++i) {
164  std::string c;
165  std::getline(std::cin, c);
166  if (verbose) {
167  std::cerr << c << "\n";
168  }
169  configuration += c + "\n";
170  }
171 
173  auto serviceToken =
174  edm::ServiceRegistry::createContaining(std::unique_ptr<edm::RandomNumberGenerator>(randomService));
175  Harness harness(configuration, serviceToken);
176 
177  //Either ROOT or the Framework are overriding the signal handlers
178  monitorThread.setupSignalHandling();
179 
180  if (verbose) {
181  std::cerr << uniqueID << " process: done initializing" << std::endl;
182  }
183  communicationChannel.workerSetupDone();
184 
185  if (verbose)
186  std::cerr << uniqueID << " process: waiting " << counter << std::endl;
187  communicationChannel.handleTransitions([&](edm::Transition iTransition, unsigned long long iTransitionID) {
188  ++counter;
189  switch (iTransition) {
191  if (verbose)
192  std::cerr << uniqueID << " process: start beginRun " << std::endl;
193  if (verbose)
194  std::cerr << uniqueID << " process: end beginRun " << std::endl;
195 
196  break;
197  }
199  if (verbose)
200  std::cerr << uniqueID << " process: start beginLumi " << std::endl;
201  auto randState = random_deserializer.deserialize();
202  if (verbose)
203  std::cerr << uniqueID << " random " << randState.state_.size() << " " << randState.seed_ << std::endl;
204  randomService->setState(randState.state_, randState.seed_);
205  auto value = harness.getBeginLumiValue(iTransitionID);
206  value.randomState_.state_ = randomService->getState();
207  value.randomState_.seed_ = randomService->mySeed();
208 
209  bl_serializer.serialize(value);
210  if (verbose)
211  std::cerr << uniqueID << " process: end beginLumi " << std::endl;
212  if (verbose)
213  std::cerr << uniqueID << " rand " << value.randomState_.state_.size() << " " << value.randomState_.seed_
214  << std::endl;
215  break;
216  }
217  case edm::Transition::Event: {
218  if (verbose)
219  std::cerr << uniqueID << " process: event " << counter << std::endl;
220  auto randState = random_deserializer.deserialize();
221  randomService->setState(randState.state_, randState.seed_);
222  auto value = harness.getEventValue();
223  value.randomState_.state_ = randomService->getState();
224  value.randomState_.seed_ = randomService->mySeed();
225 
226  if (verbose)
227  std::cerr << uniqueID << " process: event " << counter << std::endl;
228 
229  serializer.serialize(value);
230  if (verbose)
231  std::cerr << uniqueID << " process: "
232  << " " << counter << std::endl;
233  //usleep(10000000);
234  break;
235  }
237  if (verbose)
238  std::cerr << uniqueID << " process: start endLumi " << std::endl;
239  auto value = harness.getEndLumiValue();
240 
241  el_serializer.serialize(value);
242  if (verbose)
243  std::cerr << uniqueID << " process: end endLumi " << std::endl;
244 
245  break;
246  }
248  if (verbose)
249  std::cerr << uniqueID << " process: start endRun " << std::endl;
250  auto value = harness.getEndRunValue();
251 
252  er_serializer.serialize(value);
253  if (verbose)
254  std::cerr << uniqueID << " process: end endRun " << std::endl;
255 
256  break;
257  }
258  default: {
259  assert(false);
260  }
261  }
262  if (verbose)
263  std::cerr << uniqueID << " process: notifying and waiting " << counter << std::endl;
264  });
265  }
266  } catch (std::exception const& iExcept) {
267  std::cerr << "caught exception \n" << iExcept.what() << "\n";
268  return 1;
269  } catch (...) {
270  std::cerr << "caught unknown exception";
271  return 1;
272  }
273  return 0;
274 }
static char const *const kHelpOpt
static char const *const kMemoryNameCommandOpt
void setState(std::vector< unsigned long > const &, long seed)
void setupSignalHandling()
Sets the unix signal handler which communicates with the thread.
static char const *const kMemoryNameOpt
void setAction(std::function< void()> iFunc)
static char const *const kHelpCommandOpt
Transition
Definition: Transition.h:12
static ServiceToken createContaining(std::unique_ptr< T > iService)
create a service token that holds the service defined by iService
static char const *const kVerboseOpt
Definition: value.py:1
static char const *const kVerboseCommandOpt
static char const *const kUniqueIDCommandOpt
static char const *const kUniqueIDOpt
static std::atomic< unsigned int > counter

Variable Documentation

char const* const kHelpCommandOpt = "help,h"
static

Definition at line 33 of file externalGenerator.cc.

Referenced by main().

char const* const kHelpOpt = "help"
static

Definition at line 32 of file externalGenerator.cc.

Referenced by main().

char const* const kMemoryNameCommandOpt = "memory-name,m"
static

Definition at line 29 of file externalGenerator.cc.

Referenced by main().

char const* const kMemoryNameOpt = "memory-name"
static

Definition at line 28 of file externalGenerator.cc.

Referenced by main().

char const* const kUniqueIDCommandOpt = "unique-id,i"
static

Definition at line 31 of file externalGenerator.cc.

Referenced by main().

char const* const kUniqueIDOpt = "unique-id"
static

Definition at line 30 of file externalGenerator.cc.

Referenced by main().

char const* const kVerboseCommandOpt = "verbose,v"
static

Definition at line 35 of file externalGenerator.cc.

Referenced by main().

char const* const kVerboseOpt = "verbose"
static

Definition at line 34 of file externalGenerator.cc.

Referenced by main().