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 <memory>
#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 77 of file externalGenerator.cc.

Function Documentation

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

Definition at line 91 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().

91  {
92  std::string descString(argv[0]);
93  descString += " [--";
94  descString += kMemoryNameOpt;
95  descString += "] memory_name";
96  boost::program_options::options_description desc(descString);
97 
98  desc.add_options()(kHelpCommandOpt, "produce help message")(
99  kMemoryNameCommandOpt, boost::program_options::value<std::string>(), "memory name")(
100  kUniqueIDCommandOpt, boost::program_options::value<std::string>(), "unique id")(kVerboseCommandOpt,
101  "verbose output");
102 
103  boost::program_options::positional_options_description p;
104  p.add(kMemoryNameOpt, 1);
105  p.add(kUniqueIDOpt, 2);
106 
107  boost::program_options::options_description all_options("All Options");
108  all_options.add(desc);
109 
110  boost::program_options::variables_map vm;
111  try {
112  store(boost::program_options::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm);
113  notify(vm);
114  } catch (boost::program_options::error const& iException) {
115  std::cout << argv[0] << ": Error while trying to process command line arguments:\n"
116  << iException.what() << "\nFor usage and an options list, please do 'cmsRun --help'.";
117  return 1;
118  }
119 
120  if (vm.count(kHelpOpt)) {
121  std::cout << desc << std::endl;
122  return 0;
123  }
124 
125  bool verbose = false;
126  if (vm.count(kVerboseOpt)) {
127  verbose = true;
128  }
129 
130  if (!vm.count(kMemoryNameOpt)) {
131  std::cout << " no argument given" << std::endl;
132  return 1;
133  }
134 
135  if (!vm.count(kUniqueIDOpt)) {
136  std::cout << " no second argument given" << std::endl;
137  return 1;
138  }
139 
140  WorkerMonitorThread monitorThread;
141 
142  monitorThread.startThread();
143 
144  try {
145  std::string const memoryName(vm[kMemoryNameOpt].as<std::string>());
146  std::string const uniqueID(vm[kUniqueIDOpt].as<std::string>());
147  {
148  //This class is holding the lock
149  WorkerChannel communicationChannel(memoryName, uniqueID);
150 
151  WriteBuffer sm_buffer{memoryName, communicationChannel.fromWorkerBufferInfo()};
152  ReadBuffer sm_readbuffer{std::string("Rand") + memoryName, communicationChannel.toWorkerBufferInfo()};
153  int counter = 0;
154 
155  //The lock must be released if there is a catastrophic signal
156  auto lockPtr = communicationChannel.accessLock();
157 
158  monitorThread.setAction([lockPtr]() {
159  if (lockPtr) {
160  std::cerr << "SIGNAL CAUGHT: unlock\n";
161  lockPtr->unlock();
162  }
163  });
164 
165  //be sure to unset the address of the shared lock before the lock goes away
166  s_sharedLock = lockPtr;
167  auto unsetLockPtr = [](void*) { s_sharedLock = nullptr; };
168  std::unique_ptr<decltype(s_sharedLock), decltype(unsetLockPtr)> sharedLockGuard{&s_sharedLock, unsetLockPtr};
169  std::atexit(atexit_handler);
170  auto releaseLock = []() {
171  if (s_sharedLock) {
172  std::cerr << "terminate called: unlock\n";
173  s_sharedLock->unlock();
174  s_sharedLock = nullptr;
175  //deactivate the abort signal
176 
177  struct sigaction act;
178  act.sa_sigaction = nullptr;
179  act.sa_flags = SA_SIGINFO;
180  sigemptyset(&act.sa_mask);
181  sigaction(SIGABRT, &act, nullptr);
182  std::abort();
183  }
184  };
185  std::set_terminate(releaseLock);
186 
187  Serializer<ExternalGeneratorEventInfo> serializer(sm_buffer);
188  Serializer<ExternalGeneratorLumiInfo> bl_serializer(sm_buffer);
189  Serializer<GenLumiInfoProduct> el_serializer(sm_buffer);
190  Serializer<GenRunInfoProduct> er_serializer(sm_buffer);
191 
192  ROOTDeserializer<edm::RandomNumberGeneratorState, ReadBuffer> random_deserializer(sm_readbuffer);
193 
194  std::cerr << uniqueID << " process: initializing " << std::endl;
195  int nlines;
196  std::cin >> nlines;
197 
199  for (int i = 0; i < nlines; ++i) {
200  std::string c;
201  std::getline(std::cin, c);
202  if (verbose) {
203  std::cerr << c << "\n";
204  }
205  configuration += c + "\n";
206  }
207 
209  auto serviceToken =
210  edm::ServiceRegistry::createContaining(std::unique_ptr<edm::RandomNumberGenerator>(randomService));
211  Harness harness(configuration, serviceToken);
212 
213  //Some generator libraries override the signal handlers
214  monitorThread.setupSignalHandling();
215  std::set_terminate(releaseLock);
216 
217  if (verbose) {
218  std::cerr << uniqueID << " process: done initializing" << std::endl;
219  }
220  communicationChannel.workerSetupDone();
221 
222  if (verbose)
223  std::cerr << uniqueID << " process: waiting " << counter << std::endl;
224  communicationChannel.handleTransitions([&](edm::Transition iTransition, unsigned long long iTransitionID) {
225  ++counter;
226  switch (iTransition) {
228  if (verbose)
229  std::cerr << uniqueID << " process: start beginRun " << std::endl;
230  if (verbose)
231  std::cerr << uniqueID << " process: end beginRun " << std::endl;
232 
233  break;
234  }
236  if (verbose)
237  std::cerr << uniqueID << " process: start beginLumi " << std::endl;
238  auto randState = random_deserializer.deserialize();
239  if (verbose)
240  std::cerr << uniqueID << " random " << randState.state_.size() << " " << randState.seed_ << std::endl;
241  randomService->setState(randState.state_, randState.seed_);
242  auto value = harness.getBeginLumiValue(iTransitionID);
243  value.randomState_.state_ = randomService->getState();
244  value.randomState_.seed_ = randomService->mySeed();
245 
246  bl_serializer.serialize(value);
247  if (verbose)
248  std::cerr << uniqueID << " process: end beginLumi " << std::endl;
249  if (verbose)
250  std::cerr << uniqueID << " rand " << value.randomState_.state_.size() << " " << value.randomState_.seed_
251  << std::endl;
252  break;
253  }
254  case edm::Transition::Event: {
255  if (verbose)
256  std::cerr << uniqueID << " process: event " << counter << std::endl;
257  auto randState = random_deserializer.deserialize();
258  randomService->setState(randState.state_, randState.seed_);
259  auto value = harness.getEventValue();
260  value.randomState_.state_ = randomService->getState();
261  value.randomState_.seed_ = randomService->mySeed();
262 
263  if (verbose)
264  std::cerr << uniqueID << " process: event " << counter << std::endl;
265 
266  serializer.serialize(value);
267  if (verbose)
268  std::cerr << uniqueID << " process: "
269  << " " << counter << std::endl;
270  //usleep(10000000);
271  break;
272  }
274  if (verbose)
275  std::cerr << uniqueID << " process: start endLumi " << std::endl;
276  auto value = harness.getEndLumiValue();
277 
278  el_serializer.serialize(value);
279  if (verbose)
280  std::cerr << uniqueID << " process: end endLumi " << std::endl;
281 
282  break;
283  }
285  if (verbose)
286  std::cerr << uniqueID << " process: start endRun " << std::endl;
287  auto value = harness.getEndRunValue();
288 
289  er_serializer.serialize(value);
290  if (verbose)
291  std::cerr << uniqueID << " process: end endRun " << std::endl;
292 
293  break;
294  }
295  default: {
296  assert(false);
297  }
298  }
299  if (verbose)
300  std::cerr << uniqueID << " process: notifying and waiting " << counter << std::endl;
301  });
302  }
303  } catch (std::exception const& iExcept) {
304  std::cerr << "caught exception \n" << iExcept.what() << "\n";
305  return 1;
306  } catch (...) {
307  std::cerr << "caught unknown exception";
308  return 1;
309  }
310  return 0;
311 }
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 34 of file externalGenerator.cc.

Referenced by main().

char const* const kHelpOpt = "help"
static

Definition at line 33 of file externalGenerator.cc.

Referenced by main().

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

Definition at line 30 of file externalGenerator.cc.

Referenced by main().

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

Definition at line 29 of file externalGenerator.cc.

Referenced by main().

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

Definition at line 32 of file externalGenerator.cc.

Referenced by main().

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

Definition at line 31 of file externalGenerator.cc.

Referenced by main().

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

Definition at line 36 of file externalGenerator.cc.

Referenced by main().

char const* const kVerboseOpt = "verbose"
static

Definition at line 35 of file externalGenerator.cc.

Referenced by main().