Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Public Member Functions | Private Types | Private Attributes | List of all members
AthRNGSvc Class Reference

A service to manage multiple RNG streams in thread-safe way. More...

#include <AthRNGSvc.h>

Inheritance diagram for AthRNGSvc:
Collaboration diagram for AthRNGSvc:

Public Member Functions

 AthRNGSvc (const std::string &name, ISvcLocator *svc)
 Standard constructor. More...
 
virtual ~AthRNGSvc ()
 
StatusCode initialize () override final
 Initialize the service. More...
 
virtual ATHRNG::RNGWrappergetEngine (const INamedInterface *client, const std::string &streamName="") override final
 IAthRNGSvc method to retrieve the random number wrapper. More...
 
virtual void printEngineState (const INamedInterface *client, const std::string &streamName="") override final
 Print engine state. More...
 

Private Types

typedef std::function< CLHEP::HepRandomEngine *(void)> factoryFunc
 Factory function which constructs a HepRandomEngine. More...
 

Private Attributes

std::string m_rngType
 Random number engine type (e.g. dSFMT, ranecu) More...
 
std::unordered_map< std::string, ATHRNG::RNGWrapper * > m_wrappers
 The structure for storing the RNGWrappers. More...
 
factoryFunc m_fact
 
std::mutex m_mutex
 Mutex for protecting access to the wrapper structure. More...
 

Detailed Description

A service to manage multiple RNG streams in thread-safe way.

The random engines are provided via the RNGWrapper which dereferences to the appropriate slot-local engine.

Todo:
Move from manual pointer management to smart pointer management.

Definition at line 33 of file AthRNGSvc.h.

Member Typedef Documentation

◆ factoryFunc

typedef std::function<CLHEP::HepRandomEngine*(void)> AthRNGSvc::factoryFunc
private

Factory function which constructs a HepRandomEngine.

Definition at line 62 of file AthRNGSvc.h.

Constructor & Destructor Documentation

◆ AthRNGSvc()

AthRNGSvc::AthRNGSvc ( const std::string &  name,
ISvcLocator *  svc 
)

Standard constructor.

Definition at line 15 of file AthRNGSvc.cxx.

16  : base_class(name, svc),
17  m_rngType("dSFMT")
18 {
19  declareProperty("EngineType", m_rngType, "CLHEP RandomEngine type");
20 }

◆ ~AthRNGSvc()

AthRNGSvc::~AthRNGSvc ( )
virtual

Definition at line 97 of file AthRNGSvc.cxx.

98 {
99  // Clean up the RNGWrappers and HepRandomEngines
100  for(auto& wrapperPair : m_wrappers) {
101  delete wrapperPair.second;
102  }
103 }

Member Function Documentation

◆ getEngine()

ATHRNG::RNGWrapper * AthRNGSvc::getEngine ( const INamedInterface *  client,
const std::string &  streamName = "" 
)
finaloverridevirtual

IAthRNGSvc method to retrieve the random number wrapper.

Definition at line 49 of file AthRNGSvc.cxx.

51 {
52  // Retrieve number of event slots
53  size_t numSlots = SG::getNSlots();
54 
55  // The name key combines the client name and optional stream name
56  std::string rngName = client->name();
57  if(!streamName.empty()) rngName += "/" + streamName;
58 
59  // Look for an existing RNG stream
60  std::lock_guard<std::mutex> lock(m_mutex);
61  auto it = m_wrappers.find(rngName);
62 
63  // Construct a new RNGWrapper if none exists
64  if(it == m_wrappers.end()){
65  ATH_MSG_INFO("Creating engine " << rngName);
66  auto wrp = new ATHRNG::RNGWrapper(m_fact, numSlots);
67  m_wrappers.insert( std::make_pair(rngName, wrp) );
68  return wrp;
69  }
70 
71  ATH_MSG_DEBUG("Returning engine " << rngName);
72  return it->second;
73 }

◆ initialize()

StatusCode AthRNGSvc::initialize ( )
finaloverride

Initialize the service.

Definition at line 22 of file AthRNGSvc.cxx.

23 {
24  if(m_rngType == "dSFMT") {
25  m_fact = [](void)->CLHEP::HepRandomEngine*{
26  return new CLHEP::dSFMTEngine();
27  };
28  } else if(m_rngType == "Ranlux64") {
29  m_fact = [](void)->CLHEP::HepRandomEngine*{
30  return new CLHEP::Ranlux64Engine();
31  };
32  } else if(m_rngType == "Ranecu") {
33  m_fact = [](void)->CLHEP::HepRandomEngine*{
34  return new CLHEP::RanecuEngine();
35  };
36  } else if(m_rngType == "MixMax") {
37  m_fact = [](void)->CLHEP::HepRandomEngine*{
38  return new CLHEP::MixMaxRng();
39  };
40  } else {
41  ATH_MSG_WARNING("Supported Generator types are 'dSFMT', 'Ranlux64', 'Ranecu', 'MixMax'");
42  ATH_MSG_FATAL("Generator type \"" << m_rngType << "\" is not known. Check Joboptions");
43  return StatusCode::FAILURE;
44  }
45 
46  return StatusCode::SUCCESS;
47 }

◆ printEngineState()

void AthRNGSvc::printEngineState ( const INamedInterface *  client,
const std::string &  streamName = "" 
)
finaloverridevirtual

Print engine state.

Definition at line 75 of file AthRNGSvc.cxx.

77 {
78  // Retrieve the current slot's engine
80  CLHEP::HepRandomEngine* engine( *wrapper );
81 
82  // Extract the engine state numbers
83  std::vector<unsigned long> rngStates = engine->put();
84 
85  // Print the state numbers
86  std::string rngName = client->name();
87  if(!streamName.empty()) rngName += "/" + streamName;
88  std::lock_guard<std::mutex> lock(m_mutex);
89  msg(MSG::ALWAYS) << rngName << " ";
90  // We mask 32 bits because the other bits are garbage and unused
91  for(const unsigned long s : rngStates) {
92  msg() << (s & 0xffffffffu) << " ";
93  }
94  msg() << endmsg;
95 }

Member Data Documentation

◆ m_fact

factoryFunc AthRNGSvc::m_fact
private

Definition at line 63 of file AthRNGSvc.h.

◆ m_mutex

std::mutex AthRNGSvc::m_mutex
private

Mutex for protecting access to the wrapper structure.

Definition at line 66 of file AthRNGSvc.h.

◆ m_rngType

std::string AthRNGSvc::m_rngType
private

Random number engine type (e.g. dSFMT, ranecu)

Definition at line 56 of file AthRNGSvc.h.

◆ m_wrappers

std::unordered_map<std::string, ATHRNG::RNGWrapper*> AthRNGSvc::m_wrappers
private

The structure for storing the RNGWrappers.

Definition at line 59 of file AthRNGSvc.h.


The documentation for this class was generated from the following files:
CLHEP::dSFMTEngine
Definition: dSFMTEngine.h:31
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
skel.it
it
Definition: skel.GENtoEVGEN.py:407
AthRNGSvc::getEngine
virtual ATHRNG::RNGWrapper * getEngine(const INamedInterface *client, const std::string &streamName="") override final
IAthRNGSvc method to retrieve the random number wrapper.
Definition: AthRNGSvc.cxx:49
AthRNGSvc::m_fact
factoryFunc m_fact
Definition: AthRNGSvc.h:63
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
rerun_display.client
client
Definition: rerun_display.py:31
TrigConf::MSGTC::ALWAYS
@ ALWAYS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:29
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthRNGSvc::m_rngType
std::string m_rngType
Random number engine type (e.g. dSFMT, ranecu)
Definition: AthRNGSvc.h:56
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AthRNGSvc::m_wrappers
std::unordered_map< std::string, ATHRNG::RNGWrapper * > m_wrappers
The structure for storing the RNGWrappers.
Definition: AthRNGSvc.h:59
AthRNGSvc::m_mutex
std::mutex m_mutex
Mutex for protecting access to the wrapper structure.
Definition: AthRNGSvc.h:66
SG::getNSlots
size_t getNSlots()
Return the number of event slots.
Definition: SlotSpecificObj.cxx:64
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7