Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove solver_.parameters_ #668

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
263af07
draft of 621
montythind Sep 24, 2024
da7cd22
second draft
montythind Sep 30, 2024
32a16e2
third draft
montythind Oct 1, 2024
f125b16
darft fourth
montythind Oct 1, 2024
c580d79
Merge branch 'main' into allowSolversToModify_621
montythind Oct 1, 2024
03a8105
test fixed
montythind Oct 2, 2024
0904e97
fix cuda test
montythind Oct 2, 2024
f4cf73a
fix cuda tests
montythind Oct 2, 2024
d67add6
fix again
montythind Oct 2, 2024
ecb857a
cuda test fix
montythind Oct 3, 2024
fefab05
removed params
montythind Oct 14, 2024
c481a0b
added tolerance back
montythind Oct 14, 2024
fdb1fb6
fix cuda tests
montythind Oct 16, 2024
1bc0df6
Merge branch 'main' into allowSolversToModify_621
montythind Oct 17, 2024
9f4b7b3
trying to copy tolerances from state
K20shores Oct 30, 2024
4c84691
updating usage of absolute tolerance
K20shores Nov 6, 2024
1b67f79
removed compile errors
K20shores Nov 7, 2024
3e0d09f
moving relative tolerance
K20shores Nov 7, 2024
a5796b1
fixed some backward euler stuff
K20shores Nov 7, 2024
752519e
removing debug print statements
K20shores Nov 8, 2024
601b79e
correctly setting oregonator tolerances
K20shores Nov 8, 2024
aee4f4f
correctly using tolerances
K20shores Nov 8, 2024
f42bfca
updating jit constructor
K20shores Nov 8, 2024
96ddc42
passing the number of species to the constructor
K20shores Nov 8, 2024
96fbfd0
correcting jit tests
K20shores Nov 11, 2024
09c4df2
uncommenting tests
K20shores Nov 11, 2024
f3c71f7
removing comment
K20shores Nov 11, 2024
59460c6
using same calling convention for analytical tests
K20shores Nov 11, 2024
4b81426
simplifying cuda analytical test interface
K20shores Nov 11, 2024
6112c2c
added tests
montythind Nov 13, 2024
bd1829e
move the copy of absolute tolerance into the cuda state constructor
sjsprecious Nov 15, 2024
130b83c
Merge branch 'allowSolversToModify_621' of github.com:NCAR/micm into …
sjsprecious Nov 15, 2024
5a612d6
fix broken CI test
sjsprecious Nov 15, 2024
bacb558
uncomment a GPU test
sjsprecious Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/profile_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ int Run(const char* filepath, const char* initial_conditions, const std::string&
auto reactions = solver_params.processes_;

auto params = RosenbrockSolverParameters::ThreeStageRosenbrockParameters();
params.relative_tolerance_ = 0.1;


auto solver = VectorBuilder<L>(params)
.SetSystem(chemical_system)
.SetReactions(reactions)
.Build();
auto state = solver.GetState();

state.SetRelativeTolerance(0.1);

state.conditions_[0].temperature_ = dataMap.environments["temperature"]; // K
state.conditions_[0].pressure_ = dataMap.environments["pressure"]; // Pa
state.conditions_[0].air_density_ = dataMap.environments["air_density"]; // mol m-3
Expand Down
15 changes: 12 additions & 3 deletions include/micm/configure/solver_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace micm
System system_;
std::vector<Process> processes_;
RosenbrockSolverParameters parameters_;
double relative_tolerance_;

SolverParameters(const System& system, std::vector<Process>&& processes, const RosenbrockSolverParameters&& parameters)
: system_(system),
Expand All @@ -116,6 +117,13 @@ namespace micm
parameters_(parameters)
{
}
SolverParameters(System&& system, std::vector<Process>&& processes, RosenbrockSolverParameters&& parameters, double relative_tolerance)
: system_(system),
processes_(processes),
parameters_(parameters),
relative_tolerance_(relative_tolerance)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@montythind @K20shores since we move relative and absolute tolerances into the State class now, why we still need the relative_tolerance_ here for the solver parameters?

{
}
};

class ConfigReaderPolicy
Expand All @@ -138,6 +146,7 @@ namespace micm
std::unordered_map<std::string, Phase> phases_;
std::vector<Process> processes_;
RosenbrockSolverParameters parameters_;
double relative_tolerance_;

// Common YAML
inline static const std::string DEFAULT_CONFIG_FILE_JSON = "config.json";
Expand Down Expand Up @@ -273,7 +282,7 @@ namespace micm
processes_.clear();

// Parse species object array
ParseSpeciesArray(species_objects);
ParseSpeciesArray(species_objects);

// Assign the parsed 'Species' to 'Phase'
std::vector<Species> species_arr;
Expand Down Expand Up @@ -426,7 +435,7 @@ namespace micm
void ParseRelativeTolerance(const objectType& object)
{
ValidateSchema(object, { "value", "type" }, {});
this->parameters_.relative_tolerance_ = object["value"].as<double>();
this->relative_tolerance_ = object["value"].as<double>();
}

void ParseMechanism(const objectType& object)
Expand Down Expand Up @@ -959,7 +968,7 @@ namespace micm
SolverParameters GetSolverParams()
{
return SolverParameters(
std::move(System(this->gas_phase_, this->phases_)), std::move(this->processes_), std::move(this->parameters_));
std::move(System(this->gas_phase_, this->phases_)), std::move(this->processes_), std::move(this->parameters_), std::move(this->relative_tolerance_));
}
};
} // namespace micm
3 changes: 2 additions & 1 deletion include/micm/cuda/solver/cuda_rosenbrock.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ namespace micm
const CudaMatrixParam& y_old_param,
const CudaMatrixParam& y_new_param,
const CudaMatrixParam& errors_param,
const RosenbrockSolverParameters& ros_param,
const CudaMatrixParam& absolute_tolerance_param,
const double relative_tolerance,
CudaRosenbrockSolverParam devstruct);
} // namespace cuda
} // namespace micm
20 changes: 12 additions & 8 deletions include/micm/cuda/solver/cuda_rosenbrock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace micm
{
other.devstruct_.errors_input_ = nullptr;
other.devstruct_.errors_output_ = nullptr;
other.devstruct_.absolute_tolerance_ = nullptr;
other.devstruct_.jacobian_diagonal_elements_ = nullptr;
};

Expand Down Expand Up @@ -69,21 +68,21 @@ namespace micm
RosenbrockSolverParameters parameters,
LinearSolverPolicy&& linear_solver,
RatesPolicy&& rates,
auto& jacobian)
auto& jacobian,
const size_t number_of_species)
: AbstractRosenbrockSolver<RatesPolicy, LinearSolverPolicy, CudaRosenbrockSolver<RatesPolicy, LinearSolverPolicy>>(
parameters,
std::move(linear_solver),
std::move(rates),
jacobian)
jacobian,
number_of_species)
{
CudaRosenbrockSolverParam hoststruct;
// jacobian.GroupVectorSize() is the same as the number of grid cells for the CUDA implementation
// the absolute tolerance size is the same as the number of solved variables in one grid cell
hoststruct.errors_size_ = jacobian.GroupVectorSize() * this->parameters_.absolute_tolerance_.size();
hoststruct.errors_size_ = jacobian.GroupVectorSize() * number_of_species;
hoststruct.jacobian_diagonal_elements_ = this->jacobian_diagonal_elements_.data();
hoststruct.jacobian_diagonal_elements_size_ = this->jacobian_diagonal_elements_.size();
hoststruct.absolute_tolerance_ = this->parameters_.absolute_tolerance_.data();
hoststruct.absolute_tolerance_size_ = this->parameters_.absolute_tolerance_.size();
// Copy the data from host struct to device struct
this->devstruct_ = micm::cuda::CopyConstData(hoststruct);
};
Expand Down Expand Up @@ -115,11 +114,16 @@ namespace micm
/// @param errors The computed errors
/// @return The scaled norm of the errors
template<class DenseMatrixPolicy>
double NormalizedError(const DenseMatrixPolicy& y_old, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors)
double NormalizedError(const DenseMatrixPolicy& y_old, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors, auto& state)
const requires(CudaMatrix<DenseMatrixPolicy>&& VectorizableDense<DenseMatrixPolicy>)
{
return micm::cuda::NormalizedErrorDriver(
y_old.AsDeviceParam(), y_new.AsDeviceParam(), errors.AsDeviceParam(), this->parameters_, this->devstruct_);
y_old.AsDeviceParam(),
y_new.AsDeviceParam(),
errors.AsDeviceParam(),
state.absolute_tolerance_param_,
state.GetRelativeTolerance(),
this->devstruct_);
}
}; // end CudaRosenbrockSolver
} // namespace micm
46 changes: 43 additions & 3 deletions include/micm/cuda/solver/cuda_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,53 @@ namespace micm
public:
CudaState(const CudaState&) = delete;
CudaState& operator=(const CudaState&) = delete;
CudaState(CudaState&&) = default;
CudaState& operator=(CudaState&&) = default;

CudaMatrixParam absolute_tolerance_param_;

~CudaState()
{
CHECK_CUDA_ERROR(micm::cuda::FreeVector(absolute_tolerance_param_), "cudaFree");
}

/// @brief Constructor which takes the state dimension information as input
/// @param parameters State dimension information
CudaState(const StateParameters& parameters)
: State<DenseMatrixPolicy, SparseMatrixPolicy>(parameters){};
: State<DenseMatrixPolicy, SparseMatrixPolicy>(parameters)
{
const auto& atol = this->GetAbsoluteTolerances();

absolute_tolerance_param_.number_of_elements_ = atol.size();
absolute_tolerance_param_.number_of_grid_cells_ = 1;

CHECK_CUDA_ERROR(micm::cuda::MallocVector<double>(absolute_tolerance_param_, absolute_tolerance_param_.number_of_elements_), "cudaMalloc");
CHECK_CUDA_ERROR(micm::cuda::CopyToDevice<double>(absolute_tolerance_param_, atol), "cudaMemcpyHostToDevice");
};

/// @brief Move constructor
CudaState(CudaState&& other)
: State<DenseMatrixPolicy, SparseMatrixPolicy>(std::move(other))
{
absolute_tolerance_param_ = other.absolute_tolerance_param_;
other.absolute_tolerance_param_.d_data_ = nullptr;
}

/// @brief Move assignment operator
CudaState& operator=(CudaState&& other)
{
if (this != &other)
{
State<DenseMatrixPolicy, SparseMatrixPolicy>::operator=(std::move(other));
absolute_tolerance_param_ = other.absolute_tolerance_param_;
other.absolute_tolerance_param_.d_data_ = nullptr;
}
return *this;
}

void SetAbsoluteTolerances(const std::vector<double>& absoluteTolerance) override
{
State<DenseMatrixPolicy, SparseMatrixPolicy>::SetAbsoluteTolerances(absoluteTolerance);
CHECK_CUDA_ERROR(micm::cuda::CopyToDevice<double>(absolute_tolerance_param_, absoluteTolerance), "cudaMemcpyHostToDevice");
}

/// @brief Copy input variables to the device
void SyncInputsToDevice() requires(CudaMatrix<DenseMatrixPolicy>&& VectorizableDense<DenseMatrixPolicy>)
Expand Down
2 changes: 1 addition & 1 deletion include/micm/cuda/util/cuda_matrix.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace micm
/// @param h_data Host data to copy from
/// @returns Error code from copying to device from the host, if any
template<typename T>
cudaError_t CopyToDevice(CudaMatrixParam& vectorMatrix, std::vector<T>& h_data);
cudaError_t CopyToDevice(CudaMatrixParam& vectorMatrix, const std::vector<T>& h_data);

/// @brief Copies data from the device to the host
/// @param vectorMatrix Struct containing allocated device memory
Expand Down
2 changes: 0 additions & 2 deletions include/micm/cuda/util/cuda_param.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ struct CudaRosenbrockSolverParam
// for NormalizedError function
double* errors_input_ = nullptr;
double* errors_output_ = nullptr;
double* absolute_tolerance_ = nullptr;
std::size_t absolute_tolerance_size_;
std::size_t errors_size_;
// for AlphaMinusJacobian function
std::size_t* jacobian_diagonal_elements_ = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions include/micm/jit/solver/jit_rosenbrock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ namespace micm
RosenbrockSolverParameters parameters,
LinearSolverPolicy linear_solver,
RatesPolicy rates,
auto& jacobian)
auto& jacobian,
const size_t number_of_species)
: AbstractRosenbrockSolver<RatesPolicy, LinearSolverPolicy, JitRosenbrockSolver<RatesPolicy, LinearSolverPolicy>>(
parameters,
std::move(linear_solver),
std::move(rates),
jacobian)
jacobian, number_of_species)
{
this->GenerateAlphaMinusJacobian(jacobian);
}
Expand Down
15 changes: 8 additions & 7 deletions include/micm/solver/backward_euler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ namespace micm
const BackwardEulerSolverParameters& parameters,
LinearSolverPolicy&& linear_solver,
RatesPolicy&& rates,
auto& jacobian)
auto& jacobian,
const size_t number_of_species)
: parameters_(parameters),
linear_solver_(std::move(linear_solver)),
rates_(std::move(rates)),
Expand Down Expand Up @@ -78,14 +79,14 @@ namespace micm
/// @return true if the residual is small enough to stop the iteration
template<class DenseMatrixPolicy>
static bool IsConverged(
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& state) requires(!VectorizableDense<DenseMatrixPolicy>);
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& Yn1, const std::vector<double>& absolute_tolerance, double relative_tolerance) requires(!VectorizableDense<DenseMatrixPolicy>);
template<class DenseMatrixPolicy>
static bool IsConverged(
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& state) requires(VectorizableDense<DenseMatrixPolicy>);
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& Yn1, const std::vector<double>& absolute_tolerance, double relative_tolerance) requires(VectorizableDense<DenseMatrixPolicy>);
};

} // namespace micm
Expand Down
29 changes: 14 additions & 15 deletions include/micm/solver/backward_euler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace micm
continue;

// check for convergence
converged = IsConverged(parameters_, forcing, Yn1);
converged = IsConverged(parameters_, forcing, Yn1, state.GetAbsoluteTolerances(), state.GetRelativeTolerance());
} while (!converged && iterations < max_iter);

if (!converged)
Expand Down Expand Up @@ -180,23 +180,23 @@ namespace micm
inline bool BackwardEuler<RatesPolicy, LinearSolverPolicy>::IsConverged(
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& state) requires(!VectorizableDense<DenseMatrixPolicy>)
const DenseMatrixPolicy& Yn1, const std::vector<double>& absolute_tolerance, double relative_tolerance) requires(!VectorizableDense<DenseMatrixPolicy>)
{
double small = parameters.small_;
double rel_tol = parameters.relative_tolerance_;
auto& abs_tol = parameters.absolute_tolerance_;
double rel_tol = relative_tolerance;
auto& abs_tol = absolute_tolerance;
auto residual_iter = residual.AsVector().begin();
auto state_iter = state.AsVector().begin();
auto Yn1_iter = Yn1.AsVector().begin();
const std::size_t n_elem = residual.NumRows() * residual.NumColumns();
const std::size_t n_vars = abs_tol.size();
for (std::size_t i = 0; i < n_elem; ++i)
{
if (std::abs(*residual_iter) > small && std::abs(*residual_iter) > abs_tol[i % n_vars] &&
std::abs(*residual_iter) > rel_tol * std::abs(*state_iter))
std::abs(*residual_iter) > rel_tol * std::abs(*Yn1_iter))
{
return false;
}
++residual_iter, ++state_iter;
++residual_iter, ++Yn1_iter;
}
return true;
}
Expand All @@ -206,27 +206,26 @@ namespace micm
inline bool BackwardEuler<RatesPolicy, LinearSolverPolicy>::IsConverged(
const BackwardEulerSolverParameters& parameters,
const DenseMatrixPolicy& residual,
const DenseMatrixPolicy& state) requires(VectorizableDense<DenseMatrixPolicy>)
const DenseMatrixPolicy& Yn1, const std::vector<double>& absolute_tolerance, double relative_tolerance) requires(VectorizableDense<DenseMatrixPolicy>)
{
double small = parameters.small_;
double rel_tol = parameters.relative_tolerance_;
auto& abs_tol = parameters.absolute_tolerance_;
double rel_tol = relative_tolerance;
auto& abs_tol = absolute_tolerance;
auto residual_iter = residual.AsVector().begin();
auto state_iter = state.AsVector().begin();
auto Yn1_iter = Yn1.AsVector().begin();
const std::size_t n_elem = residual.NumRows() * residual.NumColumns();
const std::size_t L = residual.GroupVectorSize();
const std::size_t n_vars = abs_tol.size();
const std::size_t whole_blocks = std::floor(residual.NumRows() / L) * residual.GroupSize();

// evaluate the rows that fit exactly into the vectorizable dimension (L)
for (std::size_t i = 0; i < whole_blocks; ++i)
{
if (std::abs(*residual_iter) > small && std::abs(*residual_iter) > abs_tol[(i / L) % n_vars] &&
std::abs(*residual_iter) > rel_tol * std::abs(*state_iter))
std::abs(*residual_iter) > rel_tol * std::abs(*Yn1_iter))
{
return false;
}
++residual_iter, ++state_iter;
++residual_iter, ++Yn1_iter;
}

// evaluate the remaining rows
Expand All @@ -239,7 +238,7 @@ namespace micm
for (std::size_t i = offset; i < offset + remaining_rows; ++i)
{
if (std::abs(residual_iter[i]) > small && std::abs(residual_iter[i]) > abs_tol[y] &&
std::abs(residual_iter[i]) > rel_tol * std::abs(state_iter[i]))
std::abs(residual_iter[i]) > rel_tol * std::abs(Yn1_iter[i]))
{
return false;
}
Expand Down
2 changes: 0 additions & 2 deletions include/micm/solver/backward_euler_solver_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace micm
template<class RatesPolicy, class LinearSolverPolicy>
using SolverType = BackwardEuler<RatesPolicy, LinearSolverPolicy>;

std::vector<double> absolute_tolerance_;
double relative_tolerance_{ 1.0e-8 };
double small_{ 1.0e-40 };
size_t max_number_of_steps_{ 11 };
// The time step reductions are used to determine the time step after a failed solve
Expand Down
13 changes: 8 additions & 5 deletions include/micm/solver/rosenbrock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ namespace micm
const RosenbrockSolverParameters& parameters,
LinearSolverPolicy&& linear_solver,
RatesPolicy&& rates,
auto& jacobian)
auto& jacobian,
const size_t number_of_species)
: parameters_(parameters),
linear_solver_(std::move(linear_solver)),
rates_(std::move(rates)),
Expand Down Expand Up @@ -116,10 +117,10 @@ namespace micm
/// @param errors The computed errors
/// @return
template<class DenseMatrixPolicy>
double NormalizedError(const DenseMatrixPolicy& y, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors) const
double NormalizedError(const DenseMatrixPolicy& y, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors, auto& state) const
requires(!VectorizableDense<DenseMatrixPolicy>);
template<class DenseMatrixPolicy>
double NormalizedError(const DenseMatrixPolicy& y, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors) const
double NormalizedError(const DenseMatrixPolicy& y, const DenseMatrixPolicy& y_new, const DenseMatrixPolicy& errors, auto& state) const
requires(VectorizableDense<DenseMatrixPolicy>);
}; // end of Abstract Rosenbrock Solver

Expand All @@ -139,12 +140,14 @@ namespace micm
const RosenbrockSolverParameters& parameters,
LinearSolverPolicy&& linear_solver,
RatesPolicy&& rates,
auto& jacobian)
auto& jacobian,
const size_t number_of_species)
: AbstractRosenbrockSolver<RatesPolicy, LinearSolverPolicy, RosenbrockSolver<RatesPolicy, LinearSolverPolicy>>(
parameters,
std::move(linear_solver),
std::move(rates),
jacobian)
jacobian,
number_of_species)
{
}
RosenbrockSolver(const RosenbrockSolver&) = delete;
Expand Down
Loading
Loading