/* * (C) Crown Copyright 2025 Met Office * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. */ #pragma once #include #include #include #include #include #include #include "atlas/linalg/sparse/SparseMatrixStorage.h" #include "atlas/linalg/sparse/SparseMatrixView.h" namespace atlas::linalg { /// @brief A triplet which represents a non-zero entry in a sparse matrix. template class Triplet { static_assert(std::is_integral_v, "Index must be an integral type"); public: Triplet() = default; Triplet(Index row, Index column, Value value): row_{row}, column_{column}, value_{value} {} Index row() const { return row_; } Index col() const { return column_; } Value value() const { return value_; } /// @brief Less-than operator for sorting triplets. bool operator<(const Triplet& other) const { return std::tie(row_, column_) < std::tie(other.row_, other.column_); } private: Index row_{}; Index column_{}; Value value_{}; }; namespace detail { // SFINAE types/variables in place of C++20 concepts. template struct is_triplet : std::false_type {}; template struct is_triplet> : std::true_type {}; template constexpr bool is_triplet_iterator = is_triplet::value_type>::value; template constexpr bool is_random_access_iterator = std::is_base_of_v::iterator_category, std::random_access_iterator_tag>; template constexpr bool is_mutable_iterator = !std::is_const_v::reference>>; template constexpr bool is_sortable_iterator = is_random_access_iterator && is_mutable_iterator; } // namespace detail /// @brief Construct a SparseMatrixStorage from a range of triplets. template std::enable_if_t, SparseMatrixStorage> make_sparse_matrix_storage_from_triplets( std::size_t n_rows, std::size_t n_cols, Iter triplets_begin, Iter triplets_end) { using TripletType = typename std::iterator_traits::value_type; using Index = decltype(std::declval().row()); using Value = decltype(std::declval().value()); const std::size_t n_non_zero = std::distance(triplets_begin, triplets_end); auto outer_array = std::unique_ptr(array::Array::create(n_rows + 1)); auto inner_array = std::unique_ptr(array::Array::create(n_non_zero)); auto values_array = std::unique_ptr(array::Array::create(n_non_zero)); auto outer_view = array::make_view(*outer_array); auto inner_view = array::make_view(*inner_array); auto values_view = array::make_view(*values_array); std::size_t index = 0; Iter triplet_iter = triplets_begin; TripletType previous_triplet = *triplet_iter; for (std::size_t row = 0; row < n_rows; ++row) { outer_view(row) = index; for (; triplet_iter != triplets_end && triplet_iter->row() == static_cast(row); ++index, ++triplet_iter) { ATLAS_ASSERT(!(triplet_iter->row() < previous_triplet.row()), "Triplets must be sorted per row."); ATLAS_ASSERT(static_cast(triplet_iter->col()) < n_cols, "Triplet column index out of bounds."); inner_view(index) = triplet_iter->col(); values_view(index) = triplet_iter->value(); previous_triplet = *triplet_iter; } } ATLAS_ASSERT(index == n_non_zero, "Triplet row index out of bounds."); outer_view(n_rows) = n_non_zero; return SparseMatrixStorage::make(n_rows, n_cols, n_non_zero, std::move(values_array), std::move(inner_array), std::move(outer_array), std::any()); } /// @brief Construct a SparseMatrixStorage from a random-access range of triplets, sorting if necessary. template std::enable_if_t && detail::is_sortable_iterator, SparseMatrixStorage> make_sparse_matrix_storage_from_triplets(std::size_t n_rows, std::size_t n_cols, std::size_t n_non_zero, Iter triplets_begin, bool is_sorted = false) { if (!is_sorted) { std::sort(triplets_begin, triplets_begin + n_non_zero); } return make_sparse_matrix_storage_from_triplets(n_rows, n_cols, triplets_begin, triplets_begin + n_non_zero); } /// @brief Construct a SparseMatrixStorage from a vector of triplets, sorting if necessary. template SparseMatrixStorage make_sparse_matrix_storage_from_triplets(std::size_t n_rows, std::size_t n_cols, std::vector>& triplets, bool is_sorted = false) { return make_sparse_matrix_storage_from_triplets(n_rows, n_cols, triplets.size(), triplets.begin(), is_sorted); } /// @brief For-each iteration over all non-zero triplets in row. template std::enable_if_t> sparse_matrix_for_each_row( std::size_t row, const SparseMatrixView& matrix, Functor&& functor) { const Index* outer = matrix.outer(); const Index* inner = matrix.inner(); const Value* values = matrix.value(); for (Index index = outer[row]; index < outer[row + 1]; ++index) { const Index column = inner[index]; const Value value = values[index]; functor(static_cast(row), column, value); } } /// @brief For-each iteration over all non-zero triplets in matrix. template std::enable_if_t> sparse_matrix_for_each( const SparseMatrixView& matrix, Functor&& functor) { for (std::size_t row = 0; row < matrix.rows(); ++row) { sparse_matrix_for_each_row(row, matrix, functor); } } } // namespace atlas::linalg