Skip to content

Commit

Permalink
Merge pull request #26531 from lynnmunday/fixReporterVectorMath
Browse files Browse the repository at this point in the history
Testing and fixing parallel issue with reporters that operate on data collected from samplers
  • Loading branch information
loganharbour authored Jan 15, 2024
2 parents d494f0e + 5ee5e40 commit a4b4331
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
type = InputMatrix
#omega;
matrix = '2; 3; 5'
min_procs_per_row = 1
execute_on = 'PRE_MULTIAPP_SETUP'
[]
[]
Expand All @@ -18,8 +17,7 @@
input_files = forward.i
sampler = omega_sampler
ignore_solve_not_converge = true
mode = normal #This is the only mode that works. batch-reset will only transfer data to first sample
min_procs_per_app = 1
mode = normal #This is the only mode that works. batch-reset will only transfer data to first sample
[]
[]

Expand Down Expand Up @@ -52,17 +50,16 @@
[grad_sum]
type = ParsedVectorVectorRealReductionReporter
name = row_sum
reporter_name= "storage/fromForward:grad_f:grad_f"
reporter_name = "storage/fromForward:grad_f:grad_f"
initial_value = 0
expression = 'reduction_value+indexed_value'
[]
[obj_sum]
type = ParsedVectorRealReductionReporter
name = value
reporter_name= "storage/fromForward:obj_pp:value"
reporter_name = "storage/fromForward:obj_pp:value"
initial_value = 0
expression = 'reduction_value+indexed_value'
outputs=none
[]
[]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[Tests]
issues = '#21885'
issues = '#21885 #26530'
design = 'VectorOfVectorRowSum.md VectorDotProduct.md VectorSum.md'
[sampler_opt]
requirement = "The system shall be able to find an optimal value to a linear combination of forward problems run using the sampler system."
requirement = "The system shall be able to find an optimal value to a linear combination of parallel forward problems run using the sampler system."
type = CSVDiff
input = main.i
csvdiff = main_out.csv
min_parallel = 6
max_parallel = 6
max_threads = 1
# skip recover for steady solve
recover = false
# skip test if test is being run out-of-tree. Issue Ref: #25279
installation_type = in_tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ class ParsedVectorVectorRealReductionReporter : public ParsedReporterBase

/// output containing reduction of vector of vector into a vector
std::vector<Real> & _output_reporter;

/// Vector being operated on
const std::vector<std::vector<Real>> & _reporter_data;
};
5 changes: 2 additions & 3 deletions modules/optimization/src/reporters/ParsedScalarReporter.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ ParsedScalarReporter::validParams()

ParsedScalarReporter::ParsedScalarReporter(const InputParameters & parameters)
: ParsedReporterBase(parameters),
_output_reporter(
declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_REPLICATED))
_output_reporter(declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_ROOT))
{
// get reporters to operate on
const std::vector<ReporterName> reporter_names(
Expand All @@ -39,7 +38,7 @@ ParsedScalarReporter::ParsedScalarReporter(const InputParameters & parameters)
_reporter_data.resize(reporter_names.size());
for (const auto rep_index : index_range(_reporter_data))
_reporter_data[rep_index] =
&getReporterValueByName<Real>(reporter_names[rep_index], REPORTER_MODE_REPLICATED);
&getReporterValueByName<Real>(reporter_names[rep_index], REPORTER_MODE_ROOT);
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ ParsedVectorRealReductionReporter::ParsedVectorRealReductionReporter(
_initial_value(getParam<Real>("initial_value")),
_reporter_data(
getReporterValueByName<std::vector<Real>>(getParam<ReporterName>("reporter_name"))),
_output_reporter(
declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_REPLICATED))
_output_reporter(declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_ROOT))
{
// parse function
std::string function = getParam<std::string>("expression");
Expand Down
8 changes: 4 additions & 4 deletions modules/optimization/src/reporters/ParsedVectorReporter.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ ParsedVectorReporter::validParams()

ParsedVectorReporter::ParsedVectorReporter(const InputParameters & parameters)
: ParsedReporterBase(parameters),
_output_reporter(declareValueByName<std::vector<Real>>(getParam<std::string>("name"),
REPORTER_MODE_REPLICATED))
_output_reporter(
declareValueByName<std::vector<Real>>(getParam<std::string>("name"), REPORTER_MODE_ROOT))
{
// get reporters to operate on
const std::vector<ReporterName> reporter_names(
Expand All @@ -38,8 +38,8 @@ ParsedVectorReporter::ParsedVectorReporter(const InputParameters & parameters)

_reporter_data.resize(reporter_names.size());
for (const auto rep_index : index_range(_reporter_data))
_reporter_data[rep_index] = &getReporterValueByName<std::vector<Real>>(
reporter_names[rep_index], REPORTER_MODE_REPLICATED);
_reporter_data[rep_index] =
&getReporterValueByName<std::vector<Real>>(reporter_names[rep_index], REPORTER_MODE_ROOT);
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ParsedVectorVectorRealReductionReporter.h"
#include <cstddef>

registerMooseObject("OptimizationApp", ParsedVectorVectorRealReductionReporter);

Expand Down Expand Up @@ -37,8 +38,10 @@ ParsedVectorVectorRealReductionReporter::ParsedVectorVectorRealReductionReporter
: ParsedReporterBase(parameters),
_initial_value(getParam<Real>("initial_value")),
_vec_of_vec_name(getParam<ReporterName>("reporter_name")),
_output_reporter(declareValueByName<std::vector<Real>>(getParam<std::string>("name"),
REPORTER_MODE_REPLICATED))
_output_reporter(
declareValueByName<std::vector<Real>>(getParam<std::string>("name"), REPORTER_MODE_ROOT)),
_reporter_data(getReporterValueByName<std::vector<std::vector<Real>>>(
getParam<ReporterName>("reporter_name")))
{
// parse function
std::string function = getParam<std::string>("expression");
Expand All @@ -53,15 +56,12 @@ ParsedVectorVectorRealReductionReporter::ParsedVectorVectorRealReductionReporter
void
ParsedVectorVectorRealReductionReporter::finalize()
{
// Reporter is gotten in finalize instead of intial_setup because it needs to work for
// StochasticReporter which is cloned into by SamplerReporterTransfer
const std::vector<std::vector<Real>> * vector_vector =
&getReporterValueByName<std::vector<std::vector<Real>>>(_vec_of_vec_name,
REPORTER_MODE_REPLICATED);
std::size_t nrows(vector_vector->at(0).size());
std::size_t ncols(vector_vector->size());
std::size_t ncols = _reporter_data.size();
std::size_t nrows = 0;
if (!_reporter_data.empty())
nrows = _reporter_data[0].size();

for (auto & reporter_vector : *vector_vector)
for (auto & reporter_vector : _reporter_data)
{
if (reporter_vector.size() != nrows)
mooseError("Every vector in 'reporter_name=",
Expand All @@ -81,7 +81,7 @@ ParsedVectorVectorRealReductionReporter::finalize()
for (const auto j_col : make_range(ncols))
{
_func_params[0] = reduction;
_func_params[1] = vector_vector->at(j_col)[i_row];
_func_params[1] = _reporter_data[j_col][i_row];

if (_use_t)
_func_params[2] = _t;
Expand Down

0 comments on commit a4b4331

Please sign in to comment.