LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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

enum  Policy { SEED_SERVICE_POLICIES }
 
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 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 208 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 251 of file SeedMaster.h.

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

type of engine ID

Definition at line 213 of file SeedMaster.h.

An iterator to the configured engine IDs.

Definition at line 270 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 255 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 223 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 220 of file SeedMaster.h.

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

type of served seeds

Definition at line 210 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 216 of file SeedMaster.h.

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

type of this class

Definition at line 211 of file SeedMaster.h.

Member Enumeration Documentation

template<typename SEED>
enum rndm::SeedMaster::Policy
Enumerator
SEED_SERVICE_POLICIES 

Definition at line 260 of file SeedMaster.h.

260  {
261 #define SEED_SERVICE_POLICY(x) x,
263 #undef SEED_SERVICE_POLICY
264  };

Constructor & Destructor Documentation

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

Definition at line 460 of file SeedMaster.h.

References fhicl::ParameterSet::get(), rndm::SeedMaster< SEED >::getSeed(), art::errors::LogicError, rndm::SeedMaster< SEED >::policy, rndm::SeedMaster< SEED >::policy_impl, rndm::SeedMaster< SEED >::print(), rndm::SeedMaster< SEED >::setPolicy(), and rndm::SeedMaster< SEED >::verbosity.

460  :
461  verbosity(pSet.get<int>("verbosity",0)),
462  policy(unDefined),
463  configuredSeeds(),
464  knownEventSeeds(),
465  currentSeeds(),
466  engineData()
467 {
468 
469  // Throw if policy is not recognized.
470  const std::string strPolicy = pSet.get<std::string>("policy");
471  setPolicy(strPolicy);
472 
473  // Finish parsing the parameter set, as required by the selected policy
474  switch(policy) {
475  case autoIncrement:
476  policy_impl.reset(new details::AutoIncrementPolicy<seed_t>(pSet));
477  break;
478  case linearMapping:
479  policy_impl.reset(new details::LinearMappingPolicy<seed_t>(pSet));
480  break;
481  case preDefinedOffset:
482  policy_impl.reset(new details::PredefinedOffsetPolicy<seed_t>(pSet));
483  break;
484  case preDefinedSeed:
485  policy_impl.reset(new details::PredefinedSeedPolicy<seed_t>(pSet));
486  break;
487  case random:
488  policy_impl.reset(new details::RandomPolicy<seed_t>(pSet));
489  break;
490  case perEvent:
491  policy_impl.reset(new details::PerEventPolicy<seed_t>(pSet));
492  break;
493  default:
495  << "SeedMaster(): Internal error: unknown policy_ value";
496  } // switch
497 
498  if ( verbosity > 0 )
499  print(mf::LogVerbatim("SeedMaster"));
500 
501 } // SeedMaster<SEED>::SeedMaster()
void setPolicy(std::string policyName)
Helper function to parse the policy name.
Definition: SeedMaster.h:734
void print() const
Prints to the framework Info logger.
Definition: SeedMaster.h:371
map_type knownEventSeeds
List of event seeds already computed.
Definition: SeedMaster.h:384
int verbosity
Control the level of information messages.
Definition: SeedMaster.h:375
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Policy policy
Which of the supported policies to use?
Definition: SeedMaster.h:378
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:381
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:405
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389

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 365 of file SeedMaster.h.

Referenced by rndm::NuRandomService::reseedGlobal(), and rndm::NuRandomService::reseedModule().

365 { return { engineData }; }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
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 759 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().

760 {
761 
762  for (auto const& p: seeds) {
763 
764  // Do not compare to self
765  if ( p.first == id ) continue;
766 
767  if ( p.second == seed ){
769  << "NuRandomService::ensureUnique() seed: "<<seed
770  << " already used by module.instance: " << p.first << "\n"
771  << "May not be reused by module.instance: " << id;
772  }
773  } // for
774 } // SeedMaster<SEED>::ensureUnique()
long seed
Definition: chem4.cc:68
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:11
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 400 of file SeedMaster.h.

401  { return ensureUnique(id, seed, configuredSeeds); }
long seed
Definition: chem4.cc:68
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:759
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:381
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 548 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::NuRandomService::freezeSeed(), and rndm::SeedMaster< seed_t >::getCurrentSeed().

548  {
549  engineData.at(id).freeze();
550  configuredSeeds[id] = seed;
551  currentSeeds[id] = seed;
552 } // SeedMaster<>::freezeSeed()
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
long seed
Definition: chem4.cc:68
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:381
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
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 305 of file SeedMaster.h.

306  { return getSeedFromMap(currentSeeds, id); }
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
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:409
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 717 of file SeedMaster.h.

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

718 {
719  return getEventSeed(data, EngineId(data.moduleLabel, instanceName));
720 } // 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:717
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:213
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 689 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.

690 {
691  // Check for an already computed seed.
692  typename map_type::iterator iSeed = knownEventSeeds.find(id);
694  if (iSeed != knownEventSeeds.end()) return iSeed->second;
695 
696  // Compute the seed.
697  seed = policy_impl->getEventSeed(id, data);
698  if ((seed != InvalidSeed) && policy_impl->yieldsUniqueSeeds())
699  ensureUnique(id, seed, knownEventSeeds);
700 
701  // Save the result.
702  knownEventSeeds[id] = seed;
703 
704  // for configured-seed policies, per-event seed is invalid;
705  // in that case we don't expect to change the seed,
706  // and we should not record it as current
707  // we still store it if there is nothing (emplace does not overwrite)
708  if (seed != InvalidSeed) currentSeeds[id] = seed;
709  else currentSeeds.emplace(id, seed);
710 
711  return seed;
712 } // SeedMaster<SEED>::getEventSeed(EngineId)
intermediate_table::iterator iterator
map_type knownEventSeeds
List of event seeds already computed.
Definition: SeedMaster.h:384
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
SEED seed_t
type of served seeds
Definition: SeedMaster.h:210
long seed
Definition: chem4.cc:68
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:759
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:405
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 509 of file SeedMaster.h.

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

510 {
511  return getSeed(EngineId(moduleLabel));
512 } // SeedMaster<SEED>::getSeed(string)
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:213
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:509
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 518 of file SeedMaster.h.

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

519 {
520  return getSeed(EngineId(moduleLabel,instanceName));
521 } // SeedMaster<SEED>::getSeed(string, string)
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:213
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:509
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 661 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.

662 {
663  // Check for an already computed seed.
664  typename map_type::const_iterator iSeed = configuredSeeds.find(id);
666  if (iSeed != configuredSeeds.end()) return iSeed->second;
667 
668  // Compute the seed.
669  seed = policy_impl->getSeed(id);
670  if (policy_impl->yieldsUniqueSeeds()) ensureUnique(id, seed);
671 
672  // Save the result.
673  configuredSeeds[id] = seed;
674 
675  // for per-event policies, configured seed is invalid;
676  // in that case we don't expect to change the seed,
677  // and we should not record it as current; this should not matter anyway
678  // we still store it if there is nothing (emplace does not overwrite)
679  if (seed != InvalidSeed) currentSeeds[id] = seed;
680  else currentSeeds.emplace(id, seed);
681 
682  return seed;
683 } // SeedMaster<SEED>::getSeed()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
SEED seed_t
type of served seeds
Definition: SeedMaster.h:210
intermediate_table::const_iterator const_iterator
long seed
Definition: chem4.cc:68
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
Definition: SeedMaster.h:759
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:381
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:405
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 409 of file SeedMaster.h.

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

410  {
411  auto iSeed = seeds.find(id);
412  return (iSeed == seeds.end())? InvalidSeed: iSeed->second;
413  }
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:11
template<typename SEED>
bool rndm::SeedMaster< SEED >::hasEngine ( EngineId const &  id) const
inline

Returns whether the specified engine is already registered.

Definition at line 277 of file SeedMaster.h.

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

278  { return engineData.count(id) > 0; }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
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 281 of file SeedMaster.h.

Referenced by rndm::NuRandomService::defineEngineID().

282  {
283  auto iEngineInfo = engineData.find(id);
284  return
285  (iEngineInfo != engineData.end()) && iEngineInfo->second.hasSeeder();
286  }
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
template<typename SEED >
void rndm::SeedMaster< SEED >::onNewEvent ( )
inline

Prepares for a new event.

Definition at line 726 of file SeedMaster.h.

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

Referenced by rndm::SeedMaster< seed_t >::engineIDsRange(), and rndm::NuRandomService::preProcessEvent().

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

Definition at line 441 of file SeedMaster.h.

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

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

441  {
442  static std::vector<std::string> names;
443  if(names.empty()) {
444  const char *cnames[] = {
445 #define SEED_SERVICE_POLICY(x) #x,
447 #undef SEED_SERVICE_POLICY
448  };
449  names = std::vector<std::string>
450  (cnames, cnames + sizeof(cnames)/sizeof(cnames[0]));
451  }
452 
453  return names;
454 } // SeedMaster<SEED>::policyNames()
template<typename SEED >
template<typename Stream >
void rndm::SeedMaster< SEED >::print ( Stream &&  log) const

Prints known (EngineId,seed) pairs.

Definition at line 587 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.

587  {
588  log << "\nSummary of seeds computed by the NuRandomService";
589 
590  // allow the policy implementation to print whatever it feels to
591  std::ostringstream sstr;
592  policy_impl->print(sstr);
593  if (!sstr.str().empty()) log << '\n' << sstr.str();
594 
595  if ( !currentSeeds.empty() ) {
596 
597  constexpr unsigned int ConfSeedWidth = 18;
598  constexpr unsigned int SepWidth1 = 2;
599  constexpr unsigned int LastSeedWidth = 18;
600  constexpr unsigned int SepWidth2 = SepWidth1 + 1;
601 
602  log << "\n "
603  << std::setw(ConfSeedWidth) << "Configured value"
604  << std::setw(SepWidth1) << ""
605  << std::setw(LastSeedWidth) << "Last value"
606  << std::setw(SepWidth2) << ""
607  << "ModuleLabel.InstanceName";
608 
609  for (auto const& p: currentSeeds) {
610  EngineId const& ID = p.first;
611  seed_t configuredSeed = getSeedFromMap(configuredSeeds, ID);
612  seed_t currentSeed = p.second;
613 
614  if (configuredSeed == InvalidSeed) {
615  if (currentSeed == InvalidSeed) {
616  log << "\n "
617  << std::setw(ConfSeedWidth) << "INVALID!!!"
618  << std::setw(SepWidth1) << ""
619  << std::setw(LastSeedWidth) << ""
620  << std::setw(SepWidth2) << ""
621  << ID;
622  }
623  else { // if seed was configured, it should be that one all the way!!
624  log << "\n "
625  << std::setw(ConfSeedWidth) << "(per event)"
626  << std::setw(SepWidth1) << ""
627  << std::setw(LastSeedWidth) << currentSeed
628  << std::setw(SepWidth2) << ""
629  << ID;
630  }
631  }
632  else {
633  if (configuredSeed == currentSeed) {
634  log << "\n "
635  << std::setw(ConfSeedWidth) << configuredSeed
636  << std::setw(SepWidth1) << ""
637  << std::setw(LastSeedWidth) << "(same)"
638  << std::setw(SepWidth2) << ""
639  << ID;
640  }
641  else { // if seed was configured, it should be that one all the way!!
642  log << "\n "
643  << std::setw(ConfSeedWidth) << configuredSeed
644  << std::setw(SepWidth1) << ""
645  << std::setw(LastSeedWidth) << currentSeed
646  << std::setw(SepWidth2) << ""
647  << ID << " [[ERROR!!!]]";
648  }
649  } // if per job
650  if (ID.isGlobal()) log << " (global)";
651  if (hasEngine(ID) && engineData.at(ID).isFrozen()) log << " [overridden]";
652  } // for all seeds
653  } // if any seed
654  log << '\n' << std::endl;
655 } // SeedMaster<SEED>::print(Stream)
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
map_type currentSeeds
List of seeds already computed.
Definition: SeedMaster.h:387
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:277
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:409
SEED seed_t
type of served seeds
Definition: SeedMaster.h:210
SeedMasterHelper::EngineId EngineId
type of engine ID
Definition: SeedMaster.h:213
map_type configuredSeeds
List of seeds computed from configuration information.
Definition: SeedMaster.h:381
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
Definition: SeedMaster.h:405
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
template<typename SEED>
void rndm::SeedMaster< SEED >::print ( ) const
inline

Prints to the framework Info logger.

Definition at line 371 of file SeedMaster.h.

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

371 { print(mf::LogVerbatim("SEEDS")); }
void print() const
Prints to the framework Info logger.
Definition: SeedMaster.h:371
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 536 of file SeedMaster.h.

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

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

537 {
538  if (hasEngine(id)) {
540  << "SeedMaster(): Engine with ID='" << id << "' already registered";
541  }
542  registerSeeder(id, seeder);
543 } // SeedMaster<SEED>::registerNewSeeder()
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:277
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:527
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 527 of file SeedMaster.h.

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

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

528 {
529  engineData[id].setSeeder(seeder); // creates anew and sets
530 } // SeedMaster<SEED>::registerSeeder()
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
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 558 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().

559 {
560  auto const& engineInfo = engineData.at(id);
561  if (engineInfo.isFrozen()) return InvalidSeed;
562  seed_t seed = getSeed(id);
563  if (seed != InvalidSeed) { // reseed
564  engineInfo.applySeed(id, seed);
565  }
566  return seed;
567 } // SeedMaster<SEED>::reseed()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
SEED seed_t
type of served seeds
Definition: SeedMaster.h:210
long seed
Definition: chem4.cc:68
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:509
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
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 572 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(), rndm::SeedMaster< SEED >::reseed(), and rndm::NuRandomService::reseedInstance().

573 {
574  auto const& engineInfo = engineData.at(id);
575  if (engineInfo.isFrozen()) return InvalidSeed;
576  seed_t seed = getEventSeed(data, id);
577  if (seed != InvalidSeed) { // reseed
578  engineInfo.autoApplySeed(id, seed);
579  }
580  return seed;
581 } // SeedMaster<SEED>::reseedEvent()
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: SeedMaster.h:258
seed_t getEventSeed(EventData_t const &data, std::string instanceName)
Returns the seed value for the event with specified data.
Definition: SeedMaster.h:717
SEED seed_t
type of served seeds
Definition: SeedMaster.h:210
long seed
Definition: chem4.cc:68
EngineData_t engineData
list of all engine information
Definition: SeedMaster.h:389
template<typename SEED >
void rndm::SeedMaster< SEED >::setPolicy ( std::string  policyName)
private

Helper function to parse the policy name.

Definition at line 734 of file SeedMaster.h.

References evd::details::begin(), art::errors::Configuration, evd::details::end(), rndm::SeedMaster< SEED >::ensureUnique(), rndm::SeedMaster< SEED >::policy, and rndm::SeedMaster< SEED >::policyNames().

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

734  {
735 
737  = std::find(policyNames().begin(), policyNames().end(), policyName);
738  if (iter != policyNames().end()) {
739  policy = Policy(std::distance(policyNames().begin(), iter));
740  }
741 
742  if (policy == unDefined) {
743  std::ostringstream os;
744  os<< "NuRandomService::setPolicy(): Unrecognized policy: "
745  << policyName
746  << "\n Known policies are: ";
747 
748  std::copy(policyNames().begin(), policyNames().end(),
749  std::ostream_iterator<std::string>(os, ", "));
750 
751  throw art::Exception(art::errors::Configuration) << os.str();
752  }
753 } // SeedMaster<SEED>::setPolicy()
intermediate_table::const_iterator const_iterator
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
static const std::vector< std::string > & policyNames()
Definition: SeedMaster.h:441
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Policy policy
Which of the supported policies to use?
Definition: SeedMaster.h:378
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)

Member Data Documentation

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

List of seeds computed from configuration information.

Definition at line 381 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 384 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 378 of file SeedMaster.h.

Referenced by rndm::SeedMaster< SEED >::SeedMaster(), and 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 375 of file SeedMaster.h.

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


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