ATLAS Offline Software
Loading...
Searching...
No Matches
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.
virtual ~AthRNGSvc ()
StatusCode initialize () override final
 Initialize the service.
virtual ATHRNG::RNGWrappergetEngine (const INamedInterface *client, const std::string &streamName="") override final
 IAthRNGSvc method to retrieve the random number wrapper.
virtual void printEngineState (const INamedInterface *client, const std::string &streamName="") override final
 Print engine state.

Private Types

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

Private Attributes

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

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}
std::string m_rngType
Random number engine type (e.g. dSFMT, ranecu)
Definition AthRNGSvc.h:56

◆ ~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}
std::unordered_map< std::string, ATHRNG::RNGWrapper * > m_wrappers
The structure for storing the RNGWrappers.
Definition AthRNGSvc.h:59

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}
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
std::mutex m_mutex
Mutex for protecting access to the wrapper structure.
Definition AthRNGSvc.h:66
factoryFunc m_fact
Definition AthRNGSvc.h:63
size_t getNSlots()
Return the number of event slots.

◆ 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}
#define ATH_MSG_FATAL(x)
#define ATH_MSG_WARNING(x)

◆ 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
79 ATHRNG::RNGWrapper* wrapper = getEngine(client, streamName);
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}
#define endmsg
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
MsgStream & msg
Definition testRead.cxx:32

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: