Skip to content

Commit

Permalink
Auto-format code changes (#702)
Browse files Browse the repository at this point in the history
Auto-format code using Clang-Format

Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
github-actions[bot] and actions-user authored Jan 9, 2025
1 parent 706a34a commit 25dee9b
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 104 deletions.
25 changes: 13 additions & 12 deletions include/micm/solver/linear_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#pragma once

#include <micm/profiler/instrumentation.hpp>
#include <micm/solver/lu_decomposition.hpp>
#include <micm/solver/linear_solver_in_place.hpp>
#include <micm/solver/lu_decomposition.hpp>
#include <micm/util/matrix.hpp>
#include <micm/util/sparse_matrix.hpp>
#include <micm/util/sparse_matrix_vector_ordering.hpp>
Expand All @@ -17,20 +17,21 @@ namespace micm

/// @brief Concept for in-place linear solver algorithms
template<class T, class DenseMatrixPolicy, class SparseMatrixPolicy>
concept LinearSolverInPlaceConcept = requires(T t)
{
concept LinearSolverInPlaceConcept = requires(T t) {
{ t.Factor(std::declval<SparseMatrixPolicy&>()) };
{ t.Solve(std::declval<DenseMatrixPolicy&>(), SparseMatrixPolicy{}) };
};
static_assert(LinearSolverInPlaceConcept<LinearSolverInPlace<StandardSparseMatrix>, StandardDenseMatrix, StandardSparseMatrix>, "LinearSolverInPlace does not meet the LinearSolverInPlaceConcept requirements");
static_assert(LinearSolverInPlaceConcept<
LinearSolverInPlace<
SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>,
LuDecompositionMozartInPlace
>,
VectorMatrix<double, 1>,
SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>
>, "LinearSolverInPlace for vector matrices does not meet the LinearSolverInPlaceConcept requirements");
static_assert(
LinearSolverInPlaceConcept<LinearSolverInPlace<StandardSparseMatrix>, StandardDenseMatrix, StandardSparseMatrix>,
"LinearSolverInPlace does not meet the LinearSolverInPlaceConcept requirements");
static_assert(
LinearSolverInPlaceConcept<
LinearSolverInPlace<
SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>,
LuDecompositionMozartInPlace>,
VectorMatrix<double, 1>,
SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>>,
"LinearSolverInPlace for vector matrices does not meet the LinearSolverInPlaceConcept requirements");

/// @brief Reorders a set of state variables using Diagonal Markowitz algorithm
/// @param matrix Original matrix non-zero elements
Expand Down
10 changes: 4 additions & 6 deletions include/micm/solver/linear_solver_in_place.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ namespace micm
/// @param x The solution vector
/// @param LU The LU decomposition of the matrix as a square sparse matrix
template<class MatrixPolicy>
requires(!VectorizableDense<MatrixPolicy> || !VectorizableSparse<SparseMatrixPolicy>) void Solve(
MatrixPolicy& x,
const SparseMatrixPolicy& lu_matrix) const;
requires(!VectorizableDense<MatrixPolicy> || !VectorizableSparse<SparseMatrixPolicy>)
void Solve(MatrixPolicy& x, const SparseMatrixPolicy& lu_matrix) const;
template<class MatrixPolicy>
requires(VectorizableDense<MatrixPolicy>&& VectorizableSparse<SparseMatrixPolicy>) void Solve(
MatrixPolicy& x,
const SparseMatrixPolicy& lu_matrix) const;
requires(VectorizableDense<MatrixPolicy> && VectorizableSparse<SparseMatrixPolicy>)
void Solve(MatrixPolicy& x, const SparseMatrixPolicy& lu_matrix) const;
};

} // namespace micm
Expand Down
21 changes: 10 additions & 11 deletions include/micm/solver/linear_solver_in_place.inl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ namespace micm
};

template<class SparseMatrixPolicy, class LuDecompositionPolicy>
inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::Factor(
SparseMatrixPolicy& matrix) const
inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::Factor(SparseMatrixPolicy& matrix) const
{
MICM_PROFILE_FUNCTION();

Expand All @@ -66,10 +65,10 @@ namespace micm

template<class SparseMatrixPolicy, class LuDecompositionPolicy>
template<class MatrixPolicy>
requires(
!VectorizableDense<MatrixPolicy> ||
!VectorizableSparse<SparseMatrixPolicy>) inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::
Solve(MatrixPolicy& x, const SparseMatrixPolicy& lu_matrix) const
requires(!VectorizableDense<MatrixPolicy> || !VectorizableSparse<SparseMatrixPolicy>)
inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::Solve(
MatrixPolicy& x,
const SparseMatrixPolicy& lu_matrix) const
{
MICM_PROFILE_FUNCTION();

Expand Down Expand Up @@ -120,18 +119,18 @@ namespace micm

template<class SparseMatrixPolicy, class LuDecompositionPolicy>
template<class MatrixPolicy>
requires(VectorizableDense<MatrixPolicy>&&
VectorizableSparse<SparseMatrixPolicy>) inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::
Solve(MatrixPolicy& x, const SparseMatrixPolicy& lu_matrix) const
requires(VectorizableDense<MatrixPolicy> && VectorizableSparse<SparseMatrixPolicy>)
inline void LinearSolverInPlace<SparseMatrixPolicy, LuDecompositionPolicy>::Solve(
MatrixPolicy& x,
const SparseMatrixPolicy& lu_matrix) const
{
MICM_PROFILE_FUNCTION();
constexpr std::size_t n_cells = MatrixPolicy::GroupVectorSize();
// Loop over groups of blocks
for (std::size_t i_group = 0; i_group < x.NumberOfGroups(); ++i_group)
{
auto x_group = std::next(x.AsVector().begin(), i_group * x.GroupSize());
auto LU_group =
std::next(lu_matrix.AsVector().begin(), i_group * lu_matrix.GroupSize());
auto LU_group = std::next(lu_matrix.AsVector().begin(), i_group * lu_matrix.GroupSize());
// Forward Substitution
{
auto y_elem = x_group;
Expand Down
14 changes: 10 additions & 4 deletions include/micm/solver/lu_decomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "lu_decomposition_doolittle.hpp"
#include "lu_decomposition_mozart.hpp"
#include "lu_decomposition_mozart_in_place.hpp"

#include <micm/util/sparse_matrix.hpp>
#include <micm/util/sparse_matrix_vector_ordering.hpp>

Expand All @@ -13,13 +14,18 @@ namespace micm

/// @brief Concept for in-place LU decomposition algorithms
template<class T, class SparseMatrixPolicy>
concept LuDecompositionInPlaceConcept = requires(T t)
{
concept LuDecompositionInPlaceConcept = requires(T t) {
{ t.GetLUMatrix(SparseMatrixPolicy{}, 0.0) };
{ t.Decompose(std::declval<SparseMatrixPolicy&>()) };
};
static_assert(LuDecompositionInPlaceConcept<LuDecompositionMozartInPlace, StandardSparseMatrix>, "LuDecompositionMozartInPlace does not meet the LuDecompositionInPlaceConcept requirements");
static_assert(LuDecompositionInPlaceConcept<LuDecompositionMozartInPlace, SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>>, "LuDecompositionMozartInPlace for vector matrices does not meet the LuDecompositionInPlaceConcept requirements");
static_assert(
LuDecompositionInPlaceConcept<LuDecompositionMozartInPlace, StandardSparseMatrix>,
"LuDecompositionMozartInPlace does not meet the LuDecompositionInPlaceConcept requirements");
static_assert(
LuDecompositionInPlaceConcept<
LuDecompositionMozartInPlace,
SparseMatrix<double, SparseMatrixVectorOrderingCompressedSparseRow<1>>>,
"LuDecompositionMozartInPlace for vector matrices does not meet the LuDecompositionInPlaceConcept requirements");

/// @brief Alias for the default LU decomposition algorithm
using LuDecomposition = LuDecompositionDoolittle;
Expand Down
20 changes: 12 additions & 8 deletions include/micm/solver/lu_decomposition_mozart_in_place.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,42 @@ namespace micm
/// @brief Construct an LU decomposition algorithm for a given sparse matrix
/// @param matrix Sparse matrix
template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) LuDecompositionMozartInPlace(const SparseMatrixPolicy& matrix);
requires(SparseMatrixConcept<SparseMatrixPolicy>)
LuDecompositionMozartInPlace(const SparseMatrixPolicy& matrix);

~LuDecompositionMozartInPlace() = default;

/// @brief Create an LU decomposition algorithm for a given sparse matrix policy
/// @param matrix Sparse matrix
template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) static LuDecompositionMozartInPlace Create(const SparseMatrixPolicy& matrix);
requires(SparseMatrixConcept<SparseMatrixPolicy>)
static LuDecompositionMozartInPlace Create(const SparseMatrixPolicy& matrix);

/// @brief Create a combined sparse L and U matrix for a given A matrix
/// @param A Sparse matrix that will be decomposed
/// @return combined L and U Sparse matrices
template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) static SparseMatrixPolicy GetLUMatrix(
requires(SparseMatrixConcept<SparseMatrixPolicy>)
static SparseMatrixPolicy GetLUMatrix(
const SparseMatrixPolicy& A,
typename SparseMatrixPolicy::value_type initial_value);

/// @brief Perform an LU decomposition on a given A matrix.
/// All elements of L and U that are zero in A should be set to zero before calling this function.
/// @param ALU Sparse matrix to decompose (will be overwritten with L and U matrices)
template<class SparseMatrixPolicy>
requires(!VectorizableSparse<SparseMatrixPolicy>) void Decompose(
SparseMatrixPolicy& ALU) const;
requires(!VectorizableSparse<SparseMatrixPolicy>)
void Decompose(SparseMatrixPolicy& ALU) const;
template<class SparseMatrixPolicy>
requires(VectorizableSparse<SparseMatrixPolicy>) void Decompose(
SparseMatrixPolicy& ALU) const;
requires(VectorizableSparse<SparseMatrixPolicy>)
void Decompose(SparseMatrixPolicy& ALU) const;

private:
/// @brief Initialize arrays for the LU decomposition
/// @param A Sparse matrix to decompose
template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) void Initialize(const SparseMatrixPolicy& matrix, auto initial_value);
requires(SparseMatrixConcept<SparseMatrixPolicy>)
void Initialize(const SparseMatrixPolicy& matrix, auto initial_value);
};

} // namespace micm
Expand Down
32 changes: 17 additions & 15 deletions include/micm/solver/lu_decomposition_mozart_in_place.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ namespace micm
}

template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) inline LuDecompositionMozartInPlace::LuDecompositionMozartInPlace(const SparseMatrixPolicy& matrix)
requires(SparseMatrixConcept<SparseMatrixPolicy>)
inline LuDecompositionMozartInPlace::LuDecompositionMozartInPlace(const SparseMatrixPolicy& matrix)
{
Initialize<SparseMatrixPolicy>(matrix, typename SparseMatrixPolicy::value_type());
}

template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) inline LuDecompositionMozartInPlace LuDecompositionMozartInPlace::Create(
const SparseMatrixPolicy& matrix)
requires(SparseMatrixConcept<SparseMatrixPolicy>)
inline LuDecompositionMozartInPlace LuDecompositionMozartInPlace::Create(const SparseMatrixPolicy& matrix)
{
LuDecompositionMozartInPlace lu_decomp{};
lu_decomp.Initialize<SparseMatrixPolicy>(matrix, typename SparseMatrixPolicy::value_type());
return lu_decomp;
}

template<class SparseMatrixPolicy>
requires(SparseMatrixConcept<SparseMatrixPolicy>) inline void LuDecompositionMozartInPlace::Initialize(
const SparseMatrixPolicy& matrix,
auto initial_value)
requires(SparseMatrixConcept<SparseMatrixPolicy>)
inline void LuDecompositionMozartInPlace::Initialize(const SparseMatrixPolicy& matrix, auto initial_value)
{
MICM_PROFILE_FUNCTION();

std::size_t n = matrix.NumRows();
auto ALU = GetLUMatrix<SparseMatrixPolicy>(matrix, initial_value);
for (std::size_t i = 0; i < n; ++i)
{
if (ALU.IsZero(i, i)) {
if (ALU.IsZero(i, i))
{
throw std::runtime_error("Diagonal element is zero in LU decomposition");
}
std::tuple<std::size_t, std::size_t, std::size_t> aii_nji_nki(ALU.VectorIndex(0, i, i), 0, 0);
Expand Down Expand Up @@ -66,9 +67,10 @@ namespace micm
}

template<class SparseMatrixPolicy>
requires(
SparseMatrixConcept<SparseMatrixPolicy>) inline SparseMatrixPolicy LuDecompositionMozartInPlace::
GetLUMatrix(const SparseMatrixPolicy& A, typename SparseMatrixPolicy::value_type initial_value)
requires(SparseMatrixConcept<SparseMatrixPolicy>)
inline SparseMatrixPolicy LuDecompositionMozartInPlace::GetLUMatrix(
const SparseMatrixPolicy& A,
typename SparseMatrixPolicy::value_type initial_value)
{
MICM_PROFILE_FUNCTION();

Expand Down Expand Up @@ -100,8 +102,8 @@ namespace micm
}

template<class SparseMatrixPolicy>
requires(!VectorizableSparse<SparseMatrixPolicy>) inline void LuDecompositionMozartInPlace::Decompose(
SparseMatrixPolicy& ALU) const
requires(!VectorizableSparse<SparseMatrixPolicy>)
inline void LuDecompositionMozartInPlace::Decompose(SparseMatrixPolicy& ALU) const
{
MICM_PROFILE_FUNCTION();
const std::size_t n = ALU.NumRows();
Expand All @@ -113,7 +115,7 @@ namespace micm
auto aji = aji_.begin();
auto aik_njk = aik_njk_.begin();
auto ajk_aji = ajk_aji_.begin();

for (const auto& aii_nji_nki : aii_nji_nki_)
{
const typename SparseMatrixPolicy::value_type Aii_inverse = 1.0 / ALU_vector[std::get<0>(aii_nji_nki)];
Expand All @@ -137,8 +139,8 @@ namespace micm
}

template<class SparseMatrixPolicy>
requires(VectorizableSparse<SparseMatrixPolicy>) inline void LuDecompositionMozartInPlace::Decompose(
SparseMatrixPolicy& ALU) const
requires(VectorizableSparse<SparseMatrixPolicy>)
inline void LuDecompositionMozartInPlace::Decompose(SparseMatrixPolicy& ALU) const
{
MICM_PROFILE_FUNCTION();

Expand Down
5 changes: 3 additions & 2 deletions include/micm/solver/state.inl
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,16 @@ namespace micm

jacobian_ = BuildJacobian<SparseMatrixPolicy>(
parameters.nonzero_jacobian_elements_, parameters.number_of_grid_cells_, state_size_);

if constexpr (LuDecompositionInPlaceConcept<LuDecompositionPolicy, SparseMatrixPolicy>)
{
auto lu = LuDecompositionPolicy::template GetLUMatrix<SparseMatrixPolicy>(jacobian_, 0);
jacobian_ = std::move(lu);
}
else
{
auto lu = LuDecompositionPolicy::template GetLUMatrices<SparseMatrixPolicy, LMatrixPolicy, UMatrixPolicy>(jacobian_, 0);
auto lu =
LuDecompositionPolicy::template GetLUMatrices<SparseMatrixPolicy, LMatrixPolicy, UMatrixPolicy>(jacobian_, 0);
auto lower_matrix = std::move(lu.first);
auto upper_matrix = std::move(lu.second);
lower_matrix_ = lower_matrix;
Expand Down
20 changes: 12 additions & 8 deletions test/integration/test_analytical_backward_euler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ using VectorBackwardEulerMozartInPlace = micm::SolverBuilder<
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::ProcessSet,
micm::LuDecompositionMozartInPlace,
micm::LinearSolverInPlace<micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>, micm::LuDecompositionMozartInPlace>,
micm::LinearSolverInPlace<
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>,
micm::State<
micm::VectorMatrix<double, L>,
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>>;
micm::VectorMatrix<double, L>,
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>>;

template<std::size_t L>
using VectorStateTypeMozartInPlace = micm::State<
Expand All @@ -96,11 +98,13 @@ using VectorBackwardEulerMozartInPlace = micm::SolverBuilder<
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::ProcessSet,
micm::LuDecompositionMozartInPlace,
micm::LinearSolverInPlace<micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>, micm::LuDecompositionMozartInPlace>,
micm::LinearSolverInPlace<
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>,
micm::State<
micm::VectorMatrix<double, L>,
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>>;
micm::VectorMatrix<double, L>,
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>,
micm::LuDecompositionMozartInPlace>>;

template<std::size_t L>
using VectorStateTypeMozartInPlace = micm::State<
Expand Down
27 changes: 18 additions & 9 deletions test/unit/solver/test_linear_solver_in_place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ TEST(LinearSolverInPlace, StandardOrderingAgnosticToInitialValue)
{
double initial_values[5] = { -INFINITY, -1.0, 0.0, 1.0, INFINITY };
for (auto initial_value : initial_values)
testExtremeInitialValue<DenseMatrixTest, SparseMatrixTest, micm::LinearSolverInPlace<SparseMatrixTest>>(5, initial_value);
testExtremeInitialValue<DenseMatrixTest, SparseMatrixTest, micm::LinearSolverInPlace<SparseMatrixTest>>(
5, initial_value);
}

using Group1VectorMatrix = micm::VectorMatrix<FloatingPointType, 1>;
Expand Down Expand Up @@ -73,14 +74,22 @@ TEST(LinearSolverInPlace, VectorOrderingAgnosticToInitialValue)
double initial_values[5] = { -INFINITY, -1.0, 0.0, 1.0, INFINITY };
for (auto initial_value : initial_values)
{
testExtremeInitialValue<Group1VectorMatrix, Group1SparseVectorMatrix, micm::LinearSolverInPlace<Group1SparseVectorMatrix>>(
1, initial_value);
testExtremeInitialValue<Group2VectorMatrix, Group2SparseVectorMatrix, micm::LinearSolverInPlace<Group2SparseVectorMatrix>>(
2, initial_value);
testExtremeInitialValue<Group3VectorMatrix, Group3SparseVectorMatrix, micm::LinearSolverInPlace<Group3SparseVectorMatrix>>(
5, initial_value);
testExtremeInitialValue<Group4VectorMatrix, Group4SparseVectorMatrix, micm::LinearSolverInPlace<Group4SparseVectorMatrix>>(
5, initial_value);
testExtremeInitialValue<
Group1VectorMatrix,
Group1SparseVectorMatrix,
micm::LinearSolverInPlace<Group1SparseVectorMatrix>>(1, initial_value);
testExtremeInitialValue<
Group2VectorMatrix,
Group2SparseVectorMatrix,
micm::LinearSolverInPlace<Group2SparseVectorMatrix>>(2, initial_value);
testExtremeInitialValue<
Group3VectorMatrix,
Group3SparseVectorMatrix,
micm::LinearSolverInPlace<Group3SparseVectorMatrix>>(5, initial_value);
testExtremeInitialValue<
Group4VectorMatrix,
Group4SparseVectorMatrix,
micm::LinearSolverInPlace<Group4SparseVectorMatrix>>(5, initial_value);
}
}

Expand Down
Loading

0 comments on commit 25dee9b

Please sign in to comment.