LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
rndm::SeedMaster< SEED > Class Template Reference

A class to assist in the distribution of guaranteed unique seeds to all engine IDs. More...

#include "SeedMaster.h"

Classes

struct  EngineInfo_t
 Information for each engine. More...
 

Public Types

using seed_t = SEED
 type of served seeds More...
 
using SeedMaster_t = SeedMaster< SEED >
 type of this class More...
 
using EngineId = SeedMasterHelper::EngineId
 type of engine ID More...
 
using Seeder_t = std::function< void(EngineId const &, seed_t)>
 type of a function setting a seed More...
 
using EventData_t = typename PolicyImpl_t::EventData_t
 type of data used for event seeds More...
 
using Policy = details::Policy
 Enumeration of the available policies. More...
 
using EngineInfoIteratorBox = NuRandomServiceHelper::MapKeyConstIteratorBox< EngineData_t >
 An iterator to the configured engine IDs. More...
 

Public Member Functions

 SeedMaster (const fhicl::ParameterSet &)
 
bool hasEngine (EngineId const &id) const
 Returns whether the specified engine is already registered. More...
 
bool hasSeeder (EngineId const &id) const
 Returns whether the specified engine has a valid seeder. More...
 
seed_t getSeed (std::string moduleLabel)
 Returns the seed value for this module label. More...
 
seed_t getSeed (std::string moduleLabel, std::string instanceName)
 Returns the seed value for this module label and instance name. More...
 
seed_t getSeed (EngineId const &)
 Returns the seed value for the engine with the specified ID. More...
 
seed_t getCurrentSeed (EngineId const &id) const
 Returns the last computed seed value for the specified engine ID. More...
 
void registerSeeder (EngineId const &id, Seeder_t seeder)
 Register the specified function to reseed the engine id. More...
 
void registerNewSeeder (EngineId const &id, Seeder_t seeder)
 Register the specified function to reseed the engine id. More...
 
void freezeSeed (EngineId const &id, seed_t seed)
 Forces SeedMaster not to change the seed of a registered engine. More...
 
seed_t reseed (EngineId const &id)
 Reseeds the specified engine with a global seed (if any) More...
 
seed_t reseedEvent (EngineId const &id, EventData_t const &data)
 Reseeds the specified engine with an event seed (if any) More...
 
template<typename Stream >
void print (Stream &&) const
 Prints known (EngineId,seed) pairs. More...
 
EngineInfoIteratorBox engineIDsRange () const
 Returns an object to iterate in range-for through configured engine IDs. More...
 
void onNewEvent ()
 Prepares for a new event. More...
 
void print () const
 Prints to the framework Info logger. More...
 
seed_t getEventSeed (EventData_t const &data, std::string instanceName)
 Returns the seed value for the event with specified data. More...
 
seed_t getEventSeed (EventData_t const &data, EngineId const &id)
 Returns the seed value for the event with specified data. More...
 

Static Public Member Functions

static const std::vector< std::string > & policyNames ()
 

Static Public Attributes

static constexpr seed_t InvalidSeed = PolicyImpl_t::InvalidSeed
 An invalid seed. More...
 

Private Types

using PolicyImpl_t = details::RandomSeedPolicyBase< seed_t >
 Type of abstract class for policy implementation. More...
 
using map_type = std::map< EngineId, seed_t >
 Type for seed data base. More...
 
using EngineData_t = std::map< EngineId, EngineInfo_t >
 type of map of seeders associated with the engines More...
 

Private Member Functions

void setPolicy (std::string policyName)
 Helper function to parse the policy name. More...
 
void ensureUnique (EngineId const &id, seed_t seed, map_type const &map) const
 Throws if the seed has already been used. More...
 
void ensureUnique (EngineId const &id, seed_t seed) const
 Throws if the seed has already been used. More...
 

Static Private Member Functions

static seed_t getSeedFromMap (map_type const &seeds, EngineId const &id)
 Returns a seed from the specified map, or InvalidSeed if not present. More...
 

Private Attributes

int verbosity
 Control the level of information messages. More...
 
Policy policy
 Which of the supported policies to use? More...
 
map_type configuredSeeds
 List of seeds computed from configuration information. More...
 
map_type knownEventSeeds
 List of event seeds already computed. More...
 
map_type currentSeeds
 List of seeds already computed. More...
 
EngineData_t engineData
 list of all engine information More...
 
std::unique_ptr< PolicyImpl_tpolicy_impl
 the instance of the random policy More...
 

Detailed Description

template<typename SEED>
class rndm::SeedMaster< SEED >

A class to assist in the distribution of guaranteed unique seeds to all engine IDs.

Template Parameters
SEEDtype of random engine seed
See also
NuRandomService
Attention
direct use of this class is limited to art-less contexts; within art, use rndm::NuRandomService instead.

This class is configured from a FHiCL parameter set. The complete configuration depends on the policy chosen; the following parameters are common to all the policies:

NuRandomService : {
   policy           : "autoIncrement" // Required: Other legal value are listed in SEED_SERVICE_POLICIES
   verbosity        : 0               // Optional: default=0, no informational printout
   endOfJobSummary  : false           // Optional: print list of all managed seeds at end of job.
}

The policy parameter tells the service to which algorithm to use. If the value of the policy parameter is not one of the known policies, the code will throw an exception.

Code instanciating a SeedMaster can request a seed by making one of the following two calls:

SeedMasterInstance.getSeed("moduleLabel");
SeedMasterInstance.getSeed("moduleLabel", "instanceName");

art module code requests a seed directly to NuRandomService service.

It is the callers responsibility to use the appropriate form.

When getSeed is called with a particular module label and instance name, it computes a seed value, saves it and returns it. If there is a subsequent call to getSeed with the same module label and instance name, the class will return the saved value of the seed. The following text will use the phrase "unique calls to `getSeed`"; two calls with the same module label and instance names are not considered unique.

If the policy is defined as autoIncrement, the additional configurable items are:

NuRandomService : {
   policy           : "autoIncrement"
   // ... and all the common ones, plus:
   baseSeed         : 0     // Required: An integer >= 0.
   checkRange       : true  // Optional: legal values true (default) or false
   maxUniqueEngines : 20    // Required iff checkRange is true.
}

In this policy, the seed is set to baseSeed+offset, where on the first unique call to getSeed the offset is set to 0; on the second unique call to getSeed it is set to 1, and so on.

If the policy is defined as linearMapping, the additional configurable items are:

NuRandomService : {
   policy           : "linearMapping"
   // ... and all the common ones, plus:
   nJob             : 0     // Required: An integer >= 0.
   checkRange       : true  // Optional: legal values true (default) or false
   maxUniqueEngines : 20    // Required iff checkRange is true.
}

In this policy, the seed is set to maxUniqueEngines*nJob+offset, where on the first unique call to getSeed the offset is set to 0; on the second unique call to getSeed it is set to 1, and so on.

If the policy is defined as preDefinedOffset, the additional configurable items are:

NuRandomService : {
   policy           : "preDefinedOffset"
   // ... and all the common ones, plus:
   baseSeed         : 0     // Required: An integer >= 0.
   checkRange       : true  // Optional: legal values true (default) or false
   maxUniqueEngines : 20    // Required iff checkRange is true

   module_label1: offset1      // for each module with a nameless engine
   module_label2: {            // for each module with nemed engine instances
     instance_name1: offset21  //   ... one entry for each instance name
     instance_name2: offset22
   }
}

In this policy, when getSeed is called, the class will look into the parameter set to find a defined offset for the specified module label and instance name. The returned value of the seed will be baseSeed+offset.

If the policy is defined as preDefinedSeed, the additional configurable items are:

NuRandomService : {
   policy           : "preDefinedSeed"
   // ... and all the common ones, plus:

   module_label1: seed1      // for each module with a nameless engine
   module_label2: {          // for each module with nemed engine instances
     instance_name1: seed21  //   ... one entry for each instance name
     instance_name2: seed22
   }
}

This policy allows to specify the actual seed to be used. Note that the policy does not impose any constraint on the user-provided set of seeds. In particular, the uniqueness of the seeds is not enforced. Intended for debugging and special tests, use with care.

If the policy is defined as random, the additional configurable items are:

NuRandomService : {
   policy           : "random"
   // ... and all the common ones, plus:
   masterSeed: master_seed // optional: an integer >= 0
}

With this policy, the seed is extracted from a local random number generator. The seed used to initialize this additional random number generatot is taken from the clock, unless the masterSeed parameter is set to specify the actual seed. This policy is meant as a quick way to disentangle the code from the random seed policy used, and it's meant for special needs only and definitely not for production. You can enable this policy instead of whatever is already in your configuration by adding at the end of your configuration:

services.NuRandomService.policy: "random"

(this assumes that the configuration of the SeedMaster is read from services.NuRandomService, that is the case in the art framework).

The FHiCL grammar to specify the offsets takes two forms. If no instance name is given, the offset is given by:

moduleLabel : offset

When a module has multiple instances, the offsets are given by:

moduleLabel : {
   instanceName1 : offset1
   instanceName2 : offset2
}

SeedMaster does several additional checks, except for the preDefinedSeed policy.

If one (module label, instance name) has the same seed as another (module label, instance name), the class will throw an exception.

If the checkRange parameter is set to true, and if an offset is generated with a value outside the allowed range (typically 0<= offset < maxUniqueEngines-1) then the class will also throw an exception.

It is the responsibility of the user to ensure that the parameters (e.g. nJob and maxUniqueEngines) are chosen it a way that ensures the required level of uniqueness of seeds. The example grid jobs have a single point of maintenance to achieve this: the user must specify the starting job number for each grid submission.

Definition at line 198 of file SeedMaster.h.

Member Typedef Documentation

template<typename SEED>
using rndm::SeedMaster< SEED >::EngineData_t = std::map<EngineId, EngineInfo_t>
private

type of map of seeders associated with the engines

Definition at line 241 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::EngineId = SeedMasterHelper::EngineId

type of engine ID

Definition at line 203 of file SeedMaster.h.

An iterator to the configured engine IDs.

Definition at line 257 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::EventData_t = typename PolicyImpl_t::EventData_t

type of data used for event seeds

Definition at line 245 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::map_type = std::map<EngineId, seed_t>
private

Type for seed data base.

Definition at line 213 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::Policy = details::Policy

Enumeration of the available policies.

Definition at line 251 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::PolicyImpl_t = details::RandomSeedPolicyBase<seed_t>
private

Type of abstract class for policy implementation.

Definition at line 210 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::seed_t = SEED

type of served seeds

Definition at line 200 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::Seeder_t = std::function<void(EngineId const&, seed_t)>

type of a function setting a seed

Definition at line 206 of file SeedMaster.h.

template<typename SEED>
using rndm::SeedMaster< SEED >::SeedMaster_t = SeedMaster<SEED>

type of this class

Definition at line 201 of file SeedMaster.h.

Constructor & Destructor Documentation

template<typename SEED>
rndm::SeedMaster< SEED >::SeedMaster ( const fhicl::ParameterSet )

Definition at line 435 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::getSeed(), rndm::SeedMaster< SEED >::policy_impl, rndm::SeedMaster< SEED >::print(), and rndm::SeedMaster< SEED >::verbosity.

435  :
436  verbosity(pSet.get<int>("verbosity",0)),
437  policy(Policy::unDefined),
438  configuredSeeds(),
439  knownEventSeeds(),
440  currentSeeds(),
441  engineData()
442 {
443 
444  policy_impl = std::move(details::makeRandomSeedPolicy<seed_t>(pSet).ptr);
445 
446  if ( verbosity > 0 )
447  print(mf::LogVerbatim("SeedMaster"));
448 
449 } // SeedMaster<SEED>::SeedMaster()
void print() const
Prints to the framework Info logger.
Definition: SeedMaster.h:358
map_type knownEventSeeds
List of event seeds already computed.
Definition: SeedMaster.h:371
int verbosity
Control the level of information messages.
Definition: SeedMaster.h:362
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
Policy policy
Which of the supported policies to use?
Definition: SeedMaster.h:365
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:368
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:392
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376

Member Function Documentation

template<typename SEED>
EngineInfoIteratorBox rndm::SeedMaster< SEED >::engineIDsRange ( ) const
inline

Returns an object to iterate in range-for through configured engine IDs.

Definition at line 352 of file SeedMaster.h.

352 { return { engineData }; }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED >
void rndm::SeedMaster< SEED >::ensureUnique ( EngineId const &  id,
seed_t  seed,
map_type const &  map 
) const
private

Throws if the seed has already been used.

It does not take into account per-event seeds, but only configured ones.

Definition at line 692 of file SeedMaster.h.

References art::errors::LogicError.

Referenced by rndm::SeedMaster< seed_t >::ensureUnique(), rndm::SeedMaster< SEED >::getEventSeed(), rndm::SeedMaster< SEED >::getSeed(), and rndm::SeedMaster< SEED >::setPolicy().

693 {
694 
695  for (auto const& p: seeds) {
696 
697  // Do not compare to self
698  if ( p.first == id ) continue;
699 
700  if ( p.second == seed ){
702  << "NuRandomService::ensureUnique() seed: "<<seed
703  << " already used by module.instance: " << p.first << "\n"
704  << "May not be reused by module.instance: " << id;
705  }
706  } // for
707 } // SeedMaster<SEED>::ensureUnique()
long seed
Definition: chem4.cc:67
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:14
template<typename SEED>
void rndm::SeedMaster< SEED >::ensureUnique ( EngineId const &  id,
seed_t  seed 
) const
inlineprivate

Throws if the seed has already been used.

It does not take into account per-event seeds, but only configured ones.

Definition at line 387 of file SeedMaster.h.

388  { return ensureUnique(id, seed, configuredSeeds); }
long seed
Definition: chem4.cc:67
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:692
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:368
template<typename SEED >
void rndm::SeedMaster< SEED >::freezeSeed ( EngineId const &  id,
seed_t  seed 
)

Forces SeedMaster not to change the seed of a registered engine.

Definition at line 496 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::configuredSeeds, rndm::SeedMaster< SEED >::currentSeeds, rndm::SeedMaster< SEED >::engineData, rndm::SeedMaster< SEED >::reseed(), and seed.

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed().

496  {
497  engineData.at(id).freeze();
498  configuredSeeds[id] = seed;
499  currentSeeds[id] = seed;
500 } // SeedMaster<>::freezeSeed()
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
long seed
Definition: chem4.cc:67
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:368
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED>
seed_t rndm::SeedMaster< SEED >::getCurrentSeed ( EngineId const &  id) const
inline

Returns the last computed seed value for the specified engine ID.

Definition at line 292 of file SeedMaster.h.

293  { return getSeedFromMap(currentSeeds, id); }
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
static seed_t getSeedFromMap(map_type const &seeds, EngineId const &id)
Returns a seed from the specified map, or InvalidSeed if not present.
Definition: SeedMaster.h:396
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::getEventSeed ( EventData_t const &  data,
std::string  instanceName 
)

Returns the seed value for the event with specified data.

Definition at line 665 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::getEventSeed(), rndm::SeedMaster< SEED >::getSeed(), rndm::SeedMaster< seed_t >::hasSeeder(), and rndm::SeedMaster< SEED >::reseedEvent().

666 {
667  return getEventSeed(data, EngineId(data.moduleLabel, instanceName));
668 } // SeedMaster<SEED>::getEventSeed(string)
seed_t getEventSeed(EventData_t const &data, std::string instanceName)
Returns the seed value for the event with specified data.
Definition: SeedMaster.h:665
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:203
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::getEventSeed ( EventData_t const &  data,
EngineId const &  id 
)

Returns the seed value for the event with specified data.

Definition at line 637 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::currentSeeds, rndm::SeedMaster< SEED >::ensureUnique(), rndm::SeedMaster< SEED >::getEventSeed(), rndm::SeedMaster< SEED >::InvalidSeed, rndm::SeedMaster< SEED >::knownEventSeeds, rndm::SeedMaster< SEED >::policy_impl, and seed.

638 {
639  // Check for an already computed seed.
640  typename map_type::iterator iSeed = knownEventSeeds.find(id);
642  if (iSeed != knownEventSeeds.end()) return iSeed->second;
643 
644  // Compute the seed.
645  seed = policy_impl->getEventSeed(id, data);
646  if ((seed != InvalidSeed) && policy_impl->yieldsUniqueSeeds())
647  ensureUnique(id, seed, knownEventSeeds);
648 
649  // Save the result.
650  knownEventSeeds[id] = seed;
651 
652  // for configured-seed policies, per-event seed is invalid;
653  // in that case we don't expect to change the seed,
654  // and we should not record it as current
655  // we still store it if there is nothing (emplace does not overwrite)
656  if (seed != InvalidSeed) currentSeeds[id] = seed;
657  else currentSeeds.emplace(id, seed);
658 
659  return seed;
660 } // SeedMaster<SEED>::getEventSeed(EngineId)
intermediate_table::iterator iterator
map_type knownEventSeeds
List of event seeds already computed.
Definition: SeedMaster.h:371
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
SEED seed_t
type of served seeds
Definition: SeedMaster.h:200
long seed
Definition: chem4.cc:67
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:692
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:392
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::getSeed ( std::string  moduleLabel)
inline

Returns the seed value for this module label.

Definition at line 457 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::getSeed(), rndm::SeedMaster< seed_t >::hasSeeder(), rndm::SeedMaster< SEED >::print(), rndm::SeedMaster< SEED >::reseed(), and rndm::SeedMaster< SEED >::SeedMaster().

458 {
459  return getSeed(EngineId(moduleLabel));
460 } // SeedMaster<SEED>::getSeed(string)
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:203
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:457
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::getSeed ( std::string  moduleLabel,
std::string  instanceName 
)

Returns the seed value for this module label and instance name.

Definition at line 466 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::getSeed(), and rndm::SeedMaster< SEED >::registerSeeder().

467 {
468  return getSeed(EngineId(moduleLabel,instanceName));
469 } // SeedMaster<SEED>::getSeed(string, string)
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:203
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:457
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::getSeed ( EngineId const &  id)

Returns the seed value for the engine with the specified ID.

Definition at line 609 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::configuredSeeds, rndm::SeedMaster< SEED >::currentSeeds, rndm::SeedMaster< SEED >::ensureUnique(), rndm::SeedMaster< SEED >::getEventSeed(), rndm::SeedMaster< SEED >::InvalidSeed, rndm::SeedMaster< SEED >::policy_impl, and seed.

610 {
611  // Check for an already computed seed.
612  typename map_type::const_iterator iSeed = configuredSeeds.find(id);
614  if (iSeed != configuredSeeds.end()) return iSeed->second;
615 
616  // Compute the seed.
617  seed = policy_impl->getSeed(id);
618  if (policy_impl->yieldsUniqueSeeds()) ensureUnique(id, seed);
619 
620  // Save the result.
621  configuredSeeds[id] = seed;
622 
623  // for per-event policies, configured seed is invalid;
624  // in that case we don't expect to change the seed,
625  // and we should not record it as current; this should not matter anyway
626  // we still store it if there is nothing (emplace does not overwrite)
627  if (seed != InvalidSeed) currentSeeds[id] = seed;
628  else currentSeeds.emplace(id, seed);
629 
630  return seed;
631 } // SeedMaster<SEED>::getSeed()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
intermediate_table::const_iterator const_iterator
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
SEED seed_t
type of served seeds
Definition: SeedMaster.h:200
long seed
Definition: chem4.cc:67
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:692
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:368
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:392
template<typename SEED>
static seed_t rndm::SeedMaster< SEED >::getSeedFromMap ( map_type const &  seeds,
EngineId const &  id 
)
inlinestaticprivate

Returns a seed from the specified map, or InvalidSeed if not present.

Definition at line 396 of file SeedMaster.h.

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed(), and rndm::SeedMaster< SEED >::print().

397  {
398  auto iSeed = seeds.find(id);
399  return (iSeed == seeds.end())? InvalidSeed: iSeed->second;
400  }
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:14
template<typename SEED>
bool rndm::SeedMaster< SEED >::hasEngine ( EngineId const &  id) const
inline

Returns whether the specified engine is already registered.

Definition at line 264 of file SeedMaster.h.

Referenced by rndm::NuRandomService::hasEngine(), rndm::SeedMaster< SEED >::print(), and rndm::SeedMaster< SEED >::registerNewSeeder().

265  { return engineData.count(id) > 0; }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED>
bool rndm::SeedMaster< SEED >::hasSeeder ( EngineId const &  id) const
inline

Returns whether the specified engine has a valid seeder.

Definition at line 268 of file SeedMaster.h.

269  {
270  auto iEngineInfo = engineData.find(id);
271  return
272  (iEngineInfo != engineData.end()) && iEngineInfo->second.hasSeeder();
273  }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED >
void rndm::SeedMaster< SEED >::onNewEvent ( )
inline

Prepares for a new event.

Definition at line 674 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::knownEventSeeds.

Referenced by rndm::SeedMaster< seed_t >::engineIDsRange().

674  {
675  // forget all we know about the current event
676  knownEventSeeds.clear();
677 } // SeedMaster<SEED>::onNewEvent()
map_type knownEventSeeds
List of event seeds already computed.
Definition: SeedMaster.h:371
template<typename SEED >
std::vector< std::string > const & rndm::SeedMaster< SEED >::policyNames ( )
static

Definition at line 428 of file SeedMaster.h.

References rndm::details::policyNames().

429  { return details::policyNames(); }
std::vector< std::string > const & policyNames()
Returns a list of names of policies, in the same order as Policy enum.
Definition: PolicyNames.cxx:52
template<typename SEED >
template<typename Stream >
void rndm::SeedMaster< SEED >::print ( Stream &&  log) const

Prints known (EngineId,seed) pairs.

Definition at line 535 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::configuredSeeds, rndm::SeedMaster< SEED >::currentSeeds, rndm::SeedMaster< SEED >::engineData, rndm::SeedMaster< SEED >::getSeed(), rndm::SeedMaster< SEED >::getSeedFromMap(), rndm::SeedMaster< SEED >::hasEngine(), rndm::SeedMaster< SEED >::InvalidSeed, rndm::SeedMasterHelper::EngineId::isGlobal(), and rndm::SeedMaster< SEED >::policy_impl.

535  {
536  log << "\nSummary of seeds computed by the NuRandomService";
537 
538  // allow the policy implementation to print whatever it feels to
539  std::ostringstream sstr;
540  policy_impl->print(sstr);
541  if (!sstr.str().empty()) log << '\n' << sstr.str();
542 
543  if ( !currentSeeds.empty() ) {
544 
545  constexpr unsigned int ConfSeedWidth = 18;
546  constexpr unsigned int SepWidth1 = 2;
547  constexpr unsigned int LastSeedWidth = 18;
548  constexpr unsigned int SepWidth2 = SepWidth1 + 1;
549 
550  log << "\n "
551  << std::setw(ConfSeedWidth) << "Configured value"
552  << std::setw(SepWidth1) << ""
553  << std::setw(LastSeedWidth) << "Last value"
554  << std::setw(SepWidth2) << ""
555  << "ModuleLabel.InstanceName";
556 
557  for (auto const& p: currentSeeds) {
558  EngineId const& ID = p.first;
559  seed_t configuredSeed = getSeedFromMap(configuredSeeds, ID);
560  seed_t currentSeed = p.second;
561 
562  if (configuredSeed == InvalidSeed) {
563  if (currentSeed == InvalidSeed) {
564  log << "\n "
565  << std::setw(ConfSeedWidth) << "INVALID!!!"
566  << std::setw(SepWidth1) << ""
567  << std::setw(LastSeedWidth) << ""
568  << std::setw(SepWidth2) << ""
569  << ID;
570  }
571  else { // if seed was configured, it should be that one all the way!!
572  log << "\n "
573  << std::setw(ConfSeedWidth) << "(per event)"
574  << std::setw(SepWidth1) << ""
575  << std::setw(LastSeedWidth) << currentSeed
576  << std::setw(SepWidth2) << ""
577  << ID;
578  }
579  }
580  else {
581  if (configuredSeed == currentSeed) {
582  log << "\n "
583  << std::setw(ConfSeedWidth) << configuredSeed
584  << std::setw(SepWidth1) << ""
585  << std::setw(LastSeedWidth) << "(same)"
586  << std::setw(SepWidth2) << ""
587  << ID;
588  }
589  else { // if seed was configured, it should be that one all the way!!
590  log << "\n "
591  << std::setw(ConfSeedWidth) << configuredSeed
592  << std::setw(SepWidth1) << ""
593  << std::setw(LastSeedWidth) << currentSeed
594  << std::setw(SepWidth2) << ""
595  << ID << " [[ERROR!!!]]";
596  }
597  } // if per job
598  if (ID.isGlobal()) log << " (global)";
599  if (hasEngine(ID) && engineData.at(ID).isFrozen()) log << " [overridden]";
600  } // for all seeds
601  } // if any seed
602  log << '\n' << std::endl;
603 } // SeedMaster<SEED>::print(Stream)
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:374
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:264
static seed_t getSeedFromMap(map_type const &seeds, EngineId const &id)
Returns a seed from the specified map, or InvalidSeed if not present.
Definition: SeedMaster.h:396
SEED seed_t
type of served seeds
Definition: SeedMaster.h:200
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:203
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:368
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:392
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED>
void rndm::SeedMaster< SEED >::print ( ) const
inline

Prints to the framework Info logger.

Definition at line 358 of file SeedMaster.h.

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed(), rndm::SeedMaster< seed_t >::print(), and rndm::SeedMaster< SEED >::SeedMaster().

358 { print(mf::LogVerbatim("SEEDS")); }
void print() const
Prints to the framework Info logger.
Definition: SeedMaster.h:358
template<typename SEED >
void rndm::SeedMaster< SEED >::registerNewSeeder ( EngineId const &  id,
Seeder_t  seeder 
)

Register the specified function to reseed the engine id.

Parameters
idID of the engine to be associated to the seeder
seederfunction to be used for seeding the engine
Exceptions
art::Exception(art::errors::LogicError) if already registered
See also
registerSeeder()

This method registers a seeder for a given engine ID, just as registerSeeder() does, except that it throws an exception if a seeder has already been registered for it.

Definition at line 484 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::hasEngine(), art::errors::LogicError, and rndm::SeedMaster< SEED >::registerSeeder().

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed(), and rndm::SeedMaster< SEED >::registerSeeder().

485 {
486  if (hasEngine(id)) {
488  << "SeedMaster(): Engine with ID='" << id << "' already registered";
489  }
490  registerSeeder(id, seeder);
491 } // SeedMaster<SEED>::registerNewSeeder()
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:264
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void registerSeeder(EngineId const &id, Seeder_t seeder)
Register the specified function to reseed the engine id.
Definition: SeedMaster.h:475
template<typename SEED >
void rndm::SeedMaster< SEED >::registerSeeder ( EngineId const &  id,
Seeder_t  seeder 
)

Register the specified function to reseed the engine id.

Parameters
idID of the engine to be associated to the seeder
seederfunction to be used for seeding the engine

SeedMaster keeps a list of functions that can be used to reseed an existing engine. When reseedEvent() (or reseed()) is called, these functions are invoked to set the seed to the engine.

Definition at line 475 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::engineData, and rndm::SeedMaster< SEED >::registerNewSeeder().

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed(), rndm::SeedMaster< SEED >::getSeed(), and rndm::SeedMaster< SEED >::registerNewSeeder().

476 {
477  engineData[id].setSeeder(seeder); // creates anew and sets
478 } // SeedMaster<SEED>::registerSeeder()
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::reseed ( EngineId const &  id)

Reseeds the specified engine with a global seed (if any)

Parameters
idID of the engine to be reseeded
Returns
the seed used to reseed, InvalidSeed if no reseeding happened

Reseeding does not happen if either there is no seeder registered with that engine, or if that engine is already frozen.

Definition at line 506 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::engineData, rndm::SeedMaster< SEED >::getSeed(), rndm::SeedMaster< SEED >::InvalidSeed, rndm::SeedMaster< SEED >::reseedEvent(), and seed.

Referenced by rndm::SeedMaster< SEED >::freezeSeed(), rndm::SeedMaster< seed_t >::getCurrentSeed(), and rndm::NuRandomService::seedEngine().

507 {
508  auto const& engineInfo = engineData.at(id);
509  if (engineInfo.isFrozen()) return InvalidSeed;
510  seed_t seed = getSeed(id);
511  if (seed != InvalidSeed) { // reseed
512  engineInfo.applySeed(id, seed);
513  }
514  return seed;
515 } // SeedMaster<SEED>::reseed()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
SEED seed_t
type of served seeds
Definition: SeedMaster.h:200
long seed
Definition: chem4.cc:67
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:457
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED >
rndm::SeedMaster< SEED >::seed_t rndm::SeedMaster< SEED >::reseedEvent ( EngineId const &  id,
EventData_t const &  data 
)

Reseeds the specified engine with an event seed (if any)

Parameters
idID of the engine to be reseeded
dataevent data to extract the seed from
Returns
the seed used to reseed, InvalidSeed if no reseeding happened

Reseeding does not happen if either there is no seeder registered with that engine, or if that engine is already frozen.

Definition at line 520 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::engineData, rndm::SeedMaster< SEED >::getEventSeed(), rndm::SeedMaster< SEED >::InvalidSeed, and seed.

Referenced by rndm::SeedMaster< seed_t >::getCurrentSeed(), and rndm::SeedMaster< SEED >::reseed().

521 {
522  auto const& engineInfo = engineData.at(id);
523  if (engineInfo.isFrozen()) return InvalidSeed;
524  seed_t seed = getEventSeed(data, id);
525  if (seed != InvalidSeed) { // reseed
526  engineInfo.autoApplySeed(id, seed);
527  }
528  return seed;
529 } // SeedMaster<SEED>::reseedEvent()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:248
seed_t getEventSeed(EventData_t const &data, std::string instanceName)
Returns the seed value for the event with specified data.
Definition: SeedMaster.h:665
SEED seed_t
type of served seeds
Definition: SeedMaster.h:200
long seed
Definition: chem4.cc:67
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:376
template<typename SEED >
void rndm::SeedMaster< SEED >::setPolicy ( std::string  policyName)
private

Helper function to parse the policy name.

Definition at line 682 of file SeedMaster.h.

References rndm::SeedMaster< SEED >::ensureUnique(), rndm::SeedMaster< SEED >::policy, and rndm::details::policyFromName().

682  {
683 
685 
686 } // SeedMaster<SEED>::setPolicy()
std::string const & policyName(Policy policy)
Returns the name of the specified policy.
Definition: PolicyNames.cxx:56
Policy policy
Which of the supported policies to use?
Definition: SeedMaster.h:365
Policy policyFromName(std::string const &policyName)
Returns the policy with the specified name.
Definition: PolicyNames.cxx:67

Member Data Documentation

template<typename SEED>
map_type rndm::SeedMaster< SEED >::configuredSeeds
private

List of seeds computed from configuration information.

Definition at line 368 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::freezeSeed(), rndm::SeedMaster< SEED >::getSeed(), and rndm::SeedMaster< SEED >::print().

template<typename SEED>
constexpr seed_t rndm::SeedMaster< SEED >::InvalidSeed = PolicyImpl_t::InvalidSeed
static
template<typename SEED>
map_type rndm::SeedMaster< SEED >::knownEventSeeds
private

List of event seeds already computed.

Definition at line 371 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::getEventSeed(), and rndm::SeedMaster< SEED >::onNewEvent().

template<typename SEED>
Policy rndm::SeedMaster< SEED >::policy
private

Which of the supported policies to use?

Definition at line 365 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::setPolicy().

template<typename SEED>
std::unique_ptr<PolicyImpl_t> rndm::SeedMaster< SEED >::policy_impl
private
template<typename SEED>
int rndm::SeedMaster< SEED >::verbosity
private

Control the level of information messages.

Definition at line 362 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::SeedMaster().


The documentation for this class was generated from the following file: