28 params.
addParam<
unsigned int>(
"seed", 0,
"Random number generator initial seed");
37 params.
addParam<dof_id_type>(
"limit_get_global_samples",
38 0.1 * std::numeric_limits<unsigned int>::max(),
39 "The maximum allowed number of items in the DenseMatrix returned by "
40 "getGlobalSamples method.");
42 "limit_get_local_samples",
43 0.1 * std::numeric_limits<unsigned int>::max(),
44 "The maximum allowed number of items in the DenseMatrix returned by getLocalSamples method.");
46 "limit_get_next_local_row",
47 0.1 * std::numeric_limits<unsigned int>::max(),
48 "The maximum allowed number of items in the std::vector returned by getNextLocalRow method.");
53 "This will ensure that the sampler is partitioned properly when "
54 "'MultiApp/*/min_procs_per_app' is specified. It is not recommended to use otherwise.");
57 std::numeric_limits<unsigned int>::max(),
58 "This will ensure that the sampler is partitioned properly when "
59 "'MultiApp/*/max_procs_per_app' is specified. It is not recommended to use otherwise.");
71 _min_procs_per_row(getParam<unsigned
int>(
"min_procs_per_row") > n_processors()
73 : getParam<unsigned
int>(
"min_procs_per_row")),
74 _max_procs_per_row(getParam<unsigned
int>(
"max_procs_per_row")),
78 _next_local_row_requires_state_restore(true),
82 _limit_get_global_samples(getParam<dof_id_type>(
"limit_get_global_samples")),
83 _limit_get_local_samples(getParam<dof_id_type>(
"limit_get_local_samples")),
84 _limit_get_next_local_row(getParam<dof_id_type>(
"limit_get_next_local_row")),
85 _auto_advance_generators(true)
95 mooseError(
"The Sampler::init() method is called automatically and should not be called.");
101 const unsigned int seed = getParam<unsigned int>(
"seed");
103 seed_generator.
seed(0, seed);
107 for (std::size_t i = 0; i <
_n_seeds; ++i)
109 const auto gseed = seed_generator.
randl(0);
110 _generators[i] = std::make_unique<MooseRandomStateless>(gseed);
125 mooseError(
"Sampler has inconsistent partitionings for normal and batch mode.");
152 mooseError(
"The number of rows cannot be zero.");
162 mooseError(
"The number of columns cannot be zero.");
172 mooseError(
"The 'setNumberOfRandomSeeds()' method can not be called after the Sampler has been "
174 "this method should be called in the constructor of the Sampler object.");
177 mooseError(
"The number of seeds must be larger than zero.");
199 TIME_SECTION(
"getGlobalSamples", 1,
"Retrieving Global Samples");
205 "The number of entries in the DenseMatrix (",
207 ") exceeds the allowed limit of ",
220 TIME_SECTION(
"getLocalSamples", 1,
"Retrieving Local Samples");
226 "The number of entries in the DenseMatrix (",
228 ") exceeds the allowed limit of ",
252 "The number of entries in the std::vector (",
254 ") exceeds the allowed limit of ",
259 std::vector<Real> output(
_n_cols);
261 mooseAssert(output.size() ==
_n_cols,
"The row of sample data is not sized correctly.");
276 TIME_SECTION(
"computeSampleMatrix", 2,
"Computing Sample Matrix");
278 for (dof_id_type i = 0; i <
_n_rows; ++i)
280 std::vector<Real> row(
_n_cols, 0);
282 mooseAssert(row.size() ==
_n_cols,
"The row of sample data is not sized correctly.");
284 "Changing the size of the sample must not occur during matrix access.");
285 std::copy(row.begin(), row.end(), matrix.get_values().begin() + i *
_n_cols);
292 TIME_SECTION(
"computeLocalSampleMatrix", 2,
"Computing Local Sample Matrix");
296 std::vector<Real> row(
_n_cols, 0);
298 mooseAssert(row.size() ==
_n_cols,
"The row of sample data is not sized correctly.");
300 "Changing the size of the sample must not occur during matrix access.");
309 for (dof_id_type j = 0; j <
_n_cols; ++j)
313 "Changing the size of the sample must not occur during matrix access.");
320 TIME_SECTION(
"advanceGenerators", 2,
"Advancing Generators");
322 for (std::size_t j = 0; j <
_generators.size(); ++j)
328 mooseAssert(seed_index <
_generators.size(),
"The seed number index does not exists.");
348 mooseAssert(index <
_generators.size(),
"The seed number index does not exists.");
353Sampler::getRandl(std::size_t n,
unsigned int lower,
unsigned int upper,
unsigned int index)
const
355 mooseAssert(index <
_generators.size(),
"The seed number index does not exists.");
398 mooseError(
"A call to 'setNumberOfRows()/Columns()' was made after initialization, as such the "
399 "expected Sampler output has changed and a new sample must be created. However, a "
400 "call to Sampler::reinit() was not performed. The renit() method is automatically "
401 "called during Sampler execution, which occurs according to the 'execute_on' "
402 "settings of the Sampler object. An adjustment to this parameter may be required. "
403 "It is recommended that calls to 'setNumberOfRows()/Columns() occur within the "
404 "Sampler::executeSetUp() method; this will ensure that the reinitialize is handled "
405 "correctly. Have a nice day.");
const ExecFlagType EXEC_PRE_MULTIAPP_SETUP
const ExecFlagType EXEC_INITIAL
LocalRankConfig rankConfig(processor_id_type rank, processor_id_type nprocs, dof_id_type napps, processor_id_type min_app_procs, processor_id_type max_app_procs, bool batch_mode=false)
Returns app partitioning information relevant to the given rank for a multiapp scenario with the give...
void ErrorVector unsigned int
Interface for objects that need to use distributions.
static InputParameters validParams()
A MultiMooseEnum object to hold "execute_on" flags.
void addAvailableFlags(const ExecFlagType &flag, Args... flags)
Add additional execute_on flags to the list of possible flags.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Every object that can be built by the factory should be derived from this class.
static InputParameters validParams()
This class encapsulates a useful, consistent, cross-platform random number generator with multiple ut...
static uint32_t randl()
This method returns the next random number (long format) from the generator.
static void seed(unsigned int seed)
The method seeds the random number generator.
Interface for objects interacting with the PerfGraph.
Interface to allow object to consume Reporter values.
Interface for objects that need to use samplers.
virtual void advanceGenerator(const unsigned int seed_index, const dof_id_type count)
void checkReinitStatus() const
Helper function for reinit() errors.
void setNumberOfCols(dof_id_type n_cols)
const dof_id_type _limit_get_local_samples
Max number of entries for matrix returned by getLocalSamples.
dof_id_type _next_local_row
Iterator index for getNextLocalRow method.
dof_id_type _local_row_begin
Global row index for start of data for this processor.
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...
std::vector< Real > getNextLocalRow()
Return the "next" local row.
Real getRand(std::size_t n, unsigned int index=0) const
Get nth random number from the generator.
const dof_id_type _limit_get_global_samples
Max number of entries for matrix returned by getGlobalSamples.
std::vector< std::unique_ptr< MooseRandomStateless > > _generators
Random number generators, don't give users access. Control it via the interface from this class.
void advanceGeneratorsInternal(const dof_id_type count)
Advance method for internal use that considers the auto advance flag.
bool _initialized
Flag to indicate if the init method for this class was called.
DenseMatrix< Real > getLocalSamples()
dof_id_type getNumberOfLocalRows() const
const dof_id_type _max_procs_per_row
The maximum number of processors that are associated with a set of rows.
void execute()
Advance MooseRandomStateless generators so that new calls to sample methods will create new numbers.
bool _has_executed
Flag for initial execute to allow the first set of random numbers to be always be the same.
dof_id_type _n_rows
Total number of rows in the sample matrix.
unsigned int getRandl(std::size_t n, unsigned int lower, unsigned int upper, unsigned int index=0) const
Get nth random integer from the generator within the specified range [lower, upper)
dof_id_type _n_cols
Total number of columns in the sample matrix.
dof_id_type _n_local_rows
Number of rows for this processor.
dof_id_type getLocalRowEnd() const
const dof_id_type _limit_get_next_local_row
Max number of entries for matrix returned by getNextLocalRow.
void init()
Functions called by MOOSE to setup the Sampler for use.
dof_id_type getLocalRowBegin() const
Return the beginning/end local row index for this processor.
bool _auto_advance_generators
Flag for disabling automatic generator advancing.
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.
libMesh::Parallel::Communicator _local_comm
Communicator that was split based on samples that have rows.
virtual void executeSetUp()
Callbacks for before and after execute.
virtual LocalRankConfig constructRankConfig(bool batch_mode) const
This is where the sampler partitioning is defined.
dof_id_type getNumberOfRows() const
Return the number of samples.
virtual void advanceGenerators(const dof_id_type count)
Method for advancing the random number generator(s) by the supplied number or calls to rand().
virtual void executeTearDown()
virtual void computeSampleMatrix(DenseMatrix< Real > &matrix)
Methods to populate the global or local sample matrix.
void setAutoAdvanceGenerators(const bool state)
bool _needs_reinit
Flag to indicate if the reinit method should be called during execute.
static InputParameters validParams()
virtual void computeLocalSampleMatrix(DenseMatrix< Real > &matrix)
std::size_t _n_seeds
Number of seeds.
dof_id_type getNumberOfCols() const
dof_id_type _local_row_end
Global row index for end of data for this processor.
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...
bool _next_local_row_requires_state_restore
Flag for restoring state during getNextLocalRow iteration.
const dof_id_type _min_procs_per_row
The minimum number of processors that are associated with a set of rows.
virtual void computeSampleRow(dof_id_type i, std::vector< Real > &data)
Method to populate a complete row of sample data.
Sampler(const InputParameters ¶meters)
void setNumberOfRandomSeeds(std::size_t number)
Set the number of seeds required by the sampler.
DenseMatrix< Real > getGlobalSamples()
Return the sampled complete or distributed sample data.
static InputParameters validParams()
void split(int color, int key, Communicator &target) const
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
processor_id_type n_processors() const
Holds app partitioning information relevant to the a particular rank for a multiapp scenario.