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

Merged
merged 55 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 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
009f6ae
handling merge
K20shores Jan 8, 2025
543d57a
removing more template parameters
K20shores Jan 8, 2025
316d005
removing template parameters
K20shores Jan 8, 2025
a450599
removing template parameters
K20shores Jan 8, 2025
d00baca
removing another
K20shores Jan 8, 2025
ae93072
adding typename
K20shores Jan 8, 2025
8f83390
fixing merge conflict in cuda files
K20shores Jan 8, 2025
b63e8c7
trying to get cuda to compile
K20shores Jan 9, 2025
c6e89ab
getting cuda to compile
K20shores Jan 9, 2025
eceb9a8
merging main
K20shores Jan 9, 2025
10b8ab0
merging again
K20shores Jan 9, 2025
7fb1560
removing variable
K20shores Jan 9, 2025
d2770cf
removing getter
K20shores Jan 9, 2025
69fbc05
Merge branch 'allowSolversToModify_621' of https://github.com/NCAR/mi…
K20shores Jan 9, 2025
f34dfca
merging main
K20shores Jan 13, 2025
bc1ea2f
addressing PR comments
K20shores Jan 13, 2025
8aef106
adding a more explicit test
K20shores Jan 13, 2025
bb6d0b5
correcting cuda build
K20shores Jan 13, 2025
63615bf
correcting timing difference between main and this branch
K20shores Jan 13, 2025
37a8870
initializing relative tolerance
K20shores Jan 14, 2025
757e613
removing std::move
K20shores Jan 14, 2025
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
21 changes: 16 additions & 5 deletions include/micm/configure/solver_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,28 @@ 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),
processes_(processes),
parameters_(parameters)
parameters_(parameters),
relative_tolerance_(0)
{
}

SolverParameters(System&& system, std::vector<Process>&& processes, RosenbrockSolverParameters&& parameters)
: system_(system),
processes_(processes),
parameters_(parameters)
parameters_(parameters),
relative_tolerance_(0)
{
}
SolverParameters(System&& system, std::vector<Process>&& processes, RosenbrockSolverParameters&& parameters, double relative_tolerance)
: system_(system),
processes_(processes),
parameters_(parameters),
relative_tolerance_(relative_tolerance)
sjsprecious marked this conversation as resolved.
Show resolved Hide resolved
{
}
};
Expand All @@ -138,6 +148,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 +284,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 +437,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 +970,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_), 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
25 changes: 13 additions & 12 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 @@ -66,24 +65,22 @@ namespace micm
/// @param rates Rates calculator
/// @param jacobian Jacobian matrix
CudaRosenbrockSolver(
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,12 +112,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)
const
requires(CudaMatrix<DenseMatrixPolicy> && VectorizableDense<DenseMatrixPolicy>)
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.relative_tolerance_,
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, LuDecompositionPolicy>(parameters){};
: State<DenseMatrixPolicy, SparseMatrixPolicy, LuDecompositionPolicy>(parameters)
{
auto& atol = this->absolute_tolerance_;

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, LuDecompositionPolicy>(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, LuDecompositionPolicy>::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, LuDecompositionPolicy>::SetAbsoluteTolerances(absoluteTolerance);
CHECK_CUDA_ERROR(micm::cuda::CopyToDevice<double>(absolute_tolerance_param_, absoluteTolerance), "cudaMemcpyHostToDevice");
}

/// @brief Copy input variables to the device
void SyncInputsToDevice()
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 @@ -41,7 +41,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 @@ -90,8 +90,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
8 changes: 3 additions & 5 deletions include/micm/jit/solver/jit_rosenbrock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,18 @@ namespace micm
}

/// @brief Builds a Rosenbrock solver for the given system and solver parameters
/// @param parameters Solver parameters
/// @param linear_solver Linear solver
/// @param rates Rates calculator
/// @param jacobian Jacobian matrix
JitRosenbrockSolver(
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
26 changes: 10 additions & 16 deletions include/micm/solver/backward_euler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace micm
class BackwardEuler
{
public:
BackwardEulerSolverParameters parameters_;
LinearSolverPolicy linear_solver_;
RatesPolicy rates_;
std::vector<std::size_t> jacobian_diagonal_elements_;
Expand All @@ -41,17 +40,15 @@ namespace micm
using ParametersType = BackwardEulerSolverParameters;

/// @brief Default constructor
/// @param parameters Solver parameters
/// @param linear_solver Linear solver
/// @param rates Rates calculator
/// @param jacobian Jacobian matrix
BackwardEuler(
const BackwardEulerSolverParameters& parameters,
LinearSolverPolicy&& linear_solver,
RatesPolicy&& rates,
auto& jacobian)
: parameters_(parameters),
linear_solver_(std::move(linear_solver)),
auto& jacobian,
const size_t number_of_species)
: linear_solver_(std::move(linear_solver)),
rates_(std::move(rates)),
jacobian_diagonal_elements_(jacobian.DiagonalIndices(0))
{
Expand All @@ -68,26 +65,23 @@ namespace micm
/// @param time_step Time [s] to advance the state by
/// @param state The state to advance
/// @return result of the solver (success or failure, and statistics)
SolverResult Solve(double time_step, auto& state) const;
SolverResult Solve(double time_step, auto& state, const BackwardEulerSolverParameters& parameters) const;

/// @brief Determines whether the residual is small enough to stop the
/// internal solver iteration
/// @param parameters Solver parameters
/// @param residual The residual to check
/// @param state The current state being solved for
/// @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
Loading
Loading