LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
StandardPolicies.h
Go to the documentation of this file.
1 
13 #ifndef NURANDOM_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H
14 #define NURANDOM_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H 1
15 
16 // C/C++ standard libraries
17 #include <string>
18 #include <ostream> // std::endl
19 
20 // From art and its tool chain
22 #include "fhiclcpp/ParameterSet.h"
23 
24 // Some helper classes
27 
28 
29 namespace rndm {
30 
31  namespace details {
32 
39  template <typename SEED>
40  class AutoIncrementPolicy: public CheckedRangePolicy<SEED> {
41  public:
44  using seed_t = typename base_t::seed_t;
45 
49  base_t("autoIncrement")
50  { this_t::configure(pset); }
51 
52 
64  virtual void configure(fhicl::ParameterSet const& pset) override
65  {
67  ("maxUniqueEngines", "", "checkRange");
68  base_t::configure(pset);
69  static_configure(pset);
70  } // configure()
71 
73  virtual void print(std::ostream& out) const override;
74 
75  protected:
78 
80  virtual seed_t createSeed(SeedMasterHelper::EngineId const&) override
81  { return next_seed++; }
82 
84  }; // class AutoIncrementPolicy<>
85 
86 
87  template <typename SEED>
89  (fhicl::ParameterSet const& pset)
90  {
91  first_seed = pset.get<seed_t>("baseSeed");
95  } // AutoIncrementPolicy<SEED>::configure()
96 
97 
98  template <typename SEED>
99  void AutoIncrementPolicy<SEED>::print(std::ostream& out) const {
100  base_t::print(out);
101  out << "\n first seed: " << first_seed;
102  } // AutoIncrementPolicy<SEED>::print()
103 
104 
111  template <typename SEED>
112  class LinearMappingPolicy: public CheckedRangePolicy<SEED> {
113  public:
116  using seed_t = typename base_t::seed_t;
117 
121  base_t("linearMapping")
122  { this_t::configure(pset); }
123 
124 
136  virtual void configure(fhicl::ParameterSet const& pset) override
137  {
138  base_t::range_check.SetConfigLabels("", "", "checkRange");
139  base_t::configure(pset);
140  static_configure(pset);
141  }
142 
144  virtual void print(std::ostream& out) const override;
145 
146  protected:
148  seed_t next_seed;
149  unsigned int nSeedsPerJob;
150 
153  { return next_seed++; }
154 
155  void static_configure(fhicl::ParameterSet const& pset);
156 
157  }; // class LinearMappingPolicy<>
158 
159 
160  template <typename SEED>
162  (fhicl::ParameterSet const& pset)
163  {
164  // this code is for legacy support, and it could disappear in the future
165  if (!pset.get_if_present<seed_t>("nJob", first_seed)) {
166  if (!pset.get_if_present<seed_t>("baseSeed", first_seed)) {
167  // this is going to fail; I am doing this just to get
168  // the more appropriate error message possible
169  first_seed = pset.get<seed_t>("nJob");
170  }
171  else {
172  mf::LogWarning("SeedMaster") <<
173  std::string(80, '*') <<
174  "\nDEPRECATION WARNING: 'baseSeed' parameter has been deprecated"
175  " for linearMapping policy, in favour of 'nJob'."
176  "\nPlease update your configuration accordingly."
177  << "\n" << std::string(80, '*');
178  }
179  }
180  // first_seed = pset.get<seed_t>("nJob");
181  nSeedsPerJob = pset.get<seed_t>("maxUniqueEngines");
182  first_seed *= nSeedsPerJob;
183  ++first_seed; // we don't want 0 as a seed
186  base_t::range_check.SetNSeeds(nSeedsPerJob);
188  } // LinearMappingPolicy<SEED>::configure()
189 
190 
191  template <typename SEED>
192  void LinearMappingPolicy<SEED>::print(std::ostream& out) const {
193  base_t::print(out);
194  out
195  << "\n first seed: " << first_seed
196  << "\n seeds per job: " << nSeedsPerJob;
197  } // LinearMappingPolicy<SEED>::print()
198 
199 
200 
205  template <typename SEED>
206  class PredefinedSeedPolicy: public PerInstancePolicy<SEED> {
207  public:
210  using seed_t = typename base_t::seed_t;
211 
215  base_t("preDefinedSeed")
216  { this_t::configure(pset); }
217 
218 
237  virtual void configure(fhicl::ParameterSet const& pset) override
238  { base_t::configure(pset); static_configure(pset); }
239 
241  virtual void print(std::ostream& out) const override;
242 
243 
245  virtual bool yieldsUniqueSeeds() const override { return false; }
246 
247  protected:
248 
250  virtual seed_t createSeed(SeedMasterHelper::EngineId const& id) override
251  { return base_t::getInstanceSeed(id); }
252 
254  { base_t::range_check.SetCheck(false); }
255 
256  }; // class PredefinedSeedPolicy<>
257 
258 
259  template <typename SEED>
260  void PredefinedSeedPolicy<SEED>::print(std::ostream& out) const {
261  base_t::print(out);
262  out << "\n seeds directly from the configuration";
263  } // PredefinedSeedPolicy<SEED>::print()
264 
265 
266 
273  template <typename SEED>
274  class PredefinedOffsetPolicy: public PerInstancePolicy<SEED> {
275  public:
278  using seed_t = typename base_t::seed_t;
279 
283  base_t("preDefinedOffset")
284  { this_t::configure(pset); }
285 
286 
311  virtual void configure(fhicl::ParameterSet const& pset) override
312  {
314  ("maxUniqueEngines", "", "checkRange");
315  base_t::configure(pset);
316  static_configure(pset);
317  }
318 
320  virtual void print(std::ostream& out) const override;
321 
322  protected:
323  seed_t base_seed;
324 
326  virtual seed_t createSeed(SeedMasterHelper::EngineId const& id) override
327  { return base_seed + base_t::getInstanceSeed(id); }
328 
330 
331  }; // class PredefinedOffsetPolicy<>
332 
333 
334  template <typename SEED>
336  (fhicl::ParameterSet const& pset)
337  {
338  base_seed = pset.get<seed_t>("baseSeed");
339  base_t::range_check.SetBaseSeed(base_seed);
341  } // PredefinedOffsetPolicy<SEED>::configure()
342 
343 
344  template <typename SEED>
345  void PredefinedOffsetPolicy<SEED>::print(std::ostream& out) const {
346  base_t::print(out);
347  out << "\n base seed: " << base_seed;
348  } // PredefinedOffsetPolicy<SEED>::print()
349 
350 
351  } // namespace details
352 
353 } // namespace rndm
354 
355 
356 #endif // NURANDOM_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H
Base class for policies reacting at engine instance level.
Definition: BasePolicies.h:510
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
SEED seed_t
type of the random seed
Definition: BasePolicies.h:45
virtual bool yieldsUniqueSeeds() const override
Returns whether the returned seed should be unique: for us it "no".
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:679
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:764
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
void static_configure(fhicl::ParameterSet const &pset)
Definition: BasePolicies.h:465
Implementation of the "preDefinedOffset" policy.
Definition: BasePolicies.h:693
typename base_t::seed_t seed_t
Definition: BasePolicies.h:258
void static_configure(fhicl::ParameterSet const &)
void SetCheck(bool doCheck=true)
Sets whether to perform the check or not.
Definition: BasePolicies.h:124
virtual void print(std::ostream &out) const override
Prints information on the configuration of this policy.
Definition: BasePolicies.h:289
void SetNSeeds(seed_t nSeeds)
Sets the number of seeds directly.
Definition: BasePolicies.h:132
Interface for a policy implementation.
Definition: BasePolicies.h:43
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:367
Implementation of the "autoIncrement" policy.
Definition: BasePolicies.h:343
T get(std::string const &key) const
Definition: ParameterSet.h:314
PredefinedOffsetPolicy(fhicl::ParameterSet const &pset)
Implementation of the "preDefinedSeed" policy.
Definition: BasePolicies.h:625
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:277
RangeCheckHelper< seed_t > range_check
Definition: BasePolicies.h:296
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
Defines an interface for random seed assignment policies.
seed_t next_seed
next seed delivered
Definition: BasePolicies.h:380
Implementation of the "linearMapping" policy.
Definition: BasePolicies.h:415
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:267
PredefinedSeedPolicy(fhicl::ParameterSet const &pset)
Range-checked policy (abstract)
Definition: BasePolicies.h:254
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
LinearMappingPolicy(fhicl::ParameterSet const &pset)
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:495
void static_configure(fhicl::ParameterSet const &)
Definition: BasePolicies.h:392
void SetConfigLabels(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
Definition: BasePolicies.h:177
void SetBaseSeed(seed_t base_seed)
Sets the base seed directly.
Definition: BasePolicies.h:128
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:402
void CheckRangeConfiguration() const
Check that the configuration is complete.
Definition: BasePolicies.h:323
An identifier for random engines.
AutoIncrementPolicy(fhicl::ParameterSet const &pset)
void static_configure(fhicl::ParameterSet const &)
Definition: BasePolicies.h:755