www.mooseframework.org
Sampler.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 #include "Shuffle.h"
12 #include "DenseMatrix.h"
13 #include "MooseObject.h"
14 #include "MooseRandom.h"
15 #include "SetupInterface.h"
16 #include "DistributionInterface.h"
17 #include "PerfGraphInterface.h"
18 #include "SamplerInterface.h"
19 #include "MultiApp.h"
21 #include "ReporterInterface.h"
22 
43 class Sampler : public MooseObject,
44  public SetupInterface,
45  public DistributionInterface,
46  public PerfGraphInterface,
47  public SamplerInterface,
49  public ReporterInterface
50 {
51 public:
52  enum class SampleMode
53  {
54  GLOBAL = 0,
55  LOCAL = 1
56  };
57 
60 
61  // The public members define the API that is exposed to application developers that are using
62  // Sampler objects to perform calculations, so be very careful when adding items here since
63  // they are exposed to any other object via the SamplerInterface.
64  //
65  // It is also important to point out that when Sampler objects, when used, are not const. This is
66  // due to the fact that calling the various get methods below must store various pieces of data as
67  // well as control the state of the random number generators.
68 
70 
79 
97  std::vector<Real> getNextLocalRow();
98 
107 
109 
113  dof_id_type getLocalRowEnd() const;
115 
120  const LocalRankConfig & getRankConfig(bool batch_mode) const
121  {
122  return batch_mode ? _rank_config.second : _rank_config.first;
123  }
124 
128  virtual bool isAdaptiveSamplingCompleted() const
129  {
130  mooseError("This method should be overridden in adaptive sampling classes.");
131  return false;
132  }
133 
138 
139 protected:
150  {
151  NONE = 0,
152  LOCAL = 1,
154  };
155 
156  // The following methods are the basic methods that should be utilized my most application
157  // developers that are creating a custom Sampler.
158 
160 
164  void setNumberOfRows(dof_id_type n_rows);
165  void setNumberOfCols(dof_id_type n_cols);
167 
174  void setNumberOfRandomSeeds(std::size_t number);
175 
183  Real getRand(unsigned int index = 0);
184 
194  uint32_t getRandl(unsigned int index, uint32_t lower, uint32_t upper);
195 
202  virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) = 0;
203 
205 
211  virtual void sampleSetUp(const SampleMode /*mode*/) {}
212  virtual void sampleTearDown(const SampleMode /*mode*/) {}
214 
215  // The following methods are advanced methods that should not be needed by application developers,
216  // but exist for special cases.
217 
219 
226  virtual void computeSampleMatrix(DenseMatrix<Real> & matrix);
227  virtual void computeLocalSampleMatrix(DenseMatrix<Real> & matrix);
229 
231 
239  virtual void computeSampleRow(dof_id_type i, std::vector<Real> & data);
240 
247  virtual void advanceGenerators(const dof_id_type count);
248  virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count);
249  void setAutoAdvanceGenerators(const bool state);
250 
256  template <typename T>
257  void shuffle(std::vector<T> & data,
258  const std::size_t seed_index = 0,
259  const CommMethod method = CommMethod::LOCAL);
260 
262 
269  virtual void executeSetUp() {}
270  virtual void executeTearDown() {}
272 
274 
280 
285  virtual LocalRankConfig constructRankConfig(bool batch_mode) const;
286 
291 
294 
295 private:
297 
306  void init(); // sets up MooseRandom
307  void reinit(); // partitions sampler output
309  friend void FEProblemBase::addSampler(const std::string & type,
310  const std::string & name,
319  void execute();
320  friend void FEProblemBase::objectExecuteHelper<Sampler>(const std::vector<Sampler *> & objects);
321 
325  void checkReinitStatus() const;
326 
330  void advanceGeneratorsInternal(const dof_id_type count);
331 
334 
337 
340 
343 
346 
349 
351  std::size_t _n_seeds;
352 
355 
358 
361 
364 
367 
370 
373 
376 
379  std::pair<LocalRankConfig, LocalRankConfig> _rank_config;
380 
383 };
384 
385 template <typename T>
386 void
387 Sampler::shuffle(std::vector<T> & data, const std::size_t seed_index, const CommMethod method)
388 {
389  if (method == CommMethod::NONE)
390  MooseUtils::shuffle<T>(data, _generator, seed_index, nullptr);
391  else if (method == CommMethod::LOCAL)
392  MooseUtils::shuffle<T>(data, _generator, seed_index, &_communicator);
393  else if (method == CommMethod::SEMI_LOCAL)
394  MooseUtils::shuffle<T>(data, _generator, seed_index, &_local_comm);
395 }
void setNumberOfRows(dof_id_type n_rows)
These methods must be called within the constructor of child classes to define the size of the matrix...
Definition: Sampler.C:147
void init()
Functions called by MOOSE to setup the Sampler for use.
Definition: Sampler.C:89
virtual void addSampler(const std::string &type, const std::string &name, InputParameters &parameters)
The following functions will enable MOOSE to have the capability to import Samplers.
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
Definition: Sampler.C:347
DenseMatrix< Real > getLocalSamples()
Definition: Sampler.C:222
dof_id_type _local_row_begin
Global row index for start of data for this processor.
Definition: Sampler.h:339
void shuffle(std::vector< T > &data, const std::size_t seed_index=0, const CommMethod method=CommMethod::LOCAL)
Helper for shuffling a vector of data in-place; the default assumes data is distributed.
Definition: Sampler.h:387
uint32_t getRandl(unsigned int index, uint32_t lower, uint32_t upper)
Get the next random integer from the generator within the specified range [lower, upper) ...
Definition: Sampler.C:367
MooseRandom _generator
Random number generator, don&#39;t give users access. Control it via the interface from this class...
Definition: Sampler.h:333
virtual Real computeSample(dof_id_type row_index, dof_id_type col_index)=0
Base class must override this method to supply the sample distribution data.
static InputParameters validParams()
Definition: Sampler.C:17
virtual void advanceGenerators(const dof_id_type count)
Method for advancing the random number generator(s) by the supplied number or calls to rand()...
Definition: Sampler.C:332
This is the base class for Samplers as used within the Stochastic Tools module.
Definition: Sampler.h:43
dof_id_type _n_local_rows
Number of rows for this processor.
Definition: Sampler.h:336
virtual void executeSetUp()
Callbacks for before and after execute.
Definition: Sampler.h:269
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:140
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
Definition: Sampler.C:198
Interface for objects that need to use samplers.
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
Definition: Sampler.C:340
std::vector< Real > getNextLocalRow()
Return the "next" local row.
Definition: Sampler.C:249
virtual void sampleTearDown(const SampleMode)
Definition: Sampler.h:212
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
Definition: Sampler.C:395
void checkReinitStatus() const
Helper function for reinit() errors.
Definition: Sampler.C:409
const Parallel::Communicator & _communicator
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
Definition: Sampler.C:302
const dof_id_type _max_procs_per_row
The maximum number of processors that are associated with a set of rows.
Definition: Sampler.h:290
void restoreGeneratorState()
Definition: Sampler.h:278
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
Definition: Sampler.C:286
const dof_id_type _limit_get_next_local_row
Max number of entries for matrix returned by getNextLocalRow.
Definition: Sampler.h:375
dof_id_type getNumberOfLocalRows() const
Definition: Sampler.C:388
Real getRand(unsigned int index=0)
Get the next random number from the generator.
Definition: Sampler.C:360
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
Sampler(const InputParameters &parameters)
Definition: Sampler.C:62
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data)
Method to populate a complete row of sample data.
Definition: Sampler.C:321
virtual bool isAdaptiveSamplingCompleted() const
Returns true if the adaptive sampling is completed.
Definition: Sampler.h:128
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
Definition: Sampler.h:363
libMesh::Parallel::Communicator & getLocalComm()
Return the parallel communicator.
Definition: Sampler.h:137
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
Definition: Sampler.h:369
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
Definition: Sampler.h:372
Interface for objects that need to use distributions.
Interface to allow object to consume Reporter values.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
void saveGeneratorState()
Here we save/restore generator states.
Definition: Sampler.h:277
std::pair< LocalRankConfig, LocalRankConfig > _rank_config
The partitioning of the sampler matrix, built in reinit() first is for normal mode and send is for ba...
Definition: Sampler.h:379
std::size_t _n_seeds
Number of seeds.
Definition: Sampler.h:351
const LocalRankConfig & getRankConfig(bool batch_mode) const
Reference to rank configuration defining the partitioning of the sampler matrix This is primarily use...
Definition: Sampler.h:120
void reinit()
Definition: Sampler.C:116
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:152
bool _has_executed
Flag for initial execute to allow the first set of random numbers to be always be the same...
Definition: Sampler.h:366
Interface for objects interacting with the PerfGraph.
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
Definition: Sampler.C:140
dof_id_type getLocalRowEnd() const
Definition: Sampler.C:402
const dof_id_type _min_procs_per_row
The minimum number of processors that are associated with a set of rows.
Definition: Sampler.h:288
libMesh::Parallel::Communicator _local_comm
Communicator that was split based on samples that have rows.
Definition: Sampler.h:293
dof_id_type _local_row_end
Global row index for end of data for this processor.
Definition: Sampler.h:342
dof_id_type getNumberOfRows() const
Return the number of samples.
Definition: Sampler.C:374
virtual void sampleSetUp(const SampleMode)
Setup method called prior and after looping through distributions.
Definition: Sampler.h:211
void setAutoAdvanceGenerators(const bool state)
Definition: Sampler.C:354
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void setNumberOfCols(dof_id_type n_cols)
Definition: Sampler.C:157
virtual void executeTearDown()
Definition: Sampler.h:270
bool _initialized
Flag to indicate if the init method for this class was called.
Definition: Sampler.h:360
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & parameters() const
Get the parameters of the object.
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
Definition: Sampler.h:382
bool _next_local_row_requires_state_restore
Flag for restoring state during getNextLocalRow iteration.
Definition: Sampler.h:357
Holds app partitioning information relevant to the a particular rank for a multiapp scenario...
Definition: MultiApp.h:46
dof_id_type _n_rows
Total number of rows in the sample matrix.
Definition: Sampler.h:345
SampleMode
Definition: Sampler.h:52
void execute()
Store the state of the MooseRandom generator so that new calls to getGlobalSamples/getLocalSamples me...
Definition: Sampler.C:181
dof_id_type _n_cols
Total number of columns in the sample matrix.
Definition: Sampler.h:348
CommMethod
Enum describing the type of parallel communication to perform.
Definition: Sampler.h:149
dof_id_type getNumberOfCols() const
Definition: Sampler.C:381
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
Definition: MooseRandom.h:36
uint8_t dof_id_type
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
Definition: Sampler.C:167
dof_id_type _next_local_row
Iterator index for getNextLocalRow method.
Definition: Sampler.h:354