suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
BandSymmMat.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
29// ReSharper disable CppCStyleCast
30#ifndef BANDSYMMMAT_HPP
31#define BANDSYMMMAT_HPP
32
33#include "../DenseMat.hpp"
34
35template<sp_d T> class BandSymmMat final : public DenseMat<T> {
36 static constexpr auto UPLO = 'L';
37
38 static T bin;
39
40 const uword band;
41 const uword m_rows; // memory block layout
42
43 int solve_trs(Mat<T>&, Mat<T>&&);
44
45protected:
47
48 int direct_solve(Mat<T>&, Mat<T>&&) override;
49
50public:
51 BandSymmMat(const uword in_size, const uword in_bandwidth)
52 : DenseMat<T>(in_size, in_size, (in_bandwidth + 1) * in_size)
53 , band(in_bandwidth)
54 , m_rows(in_bandwidth + 1) {}
55
56 unique_ptr<MetaMat<T>> unique_copy() override { return std::make_unique<BandSymmMat>(*this); }
57
58 void nullify(const uword K) override {
59 this->factored = false;
60 suanpan::for_each(std::max(K, band) - band, K, [&](const uword I) { this->memory[K - I + I * m_rows] = T(0); });
61 const auto t_factor = K * m_rows - K;
62 suanpan::for_each(K, std::min(this->n_rows, K + band + 1), [&](const uword I) { this->memory[I + t_factor] = T(0); });
63 }
64
65 T operator()(const uword in_row, const uword in_col) const override {
66 if(in_row > band + in_col || in_col > in_row + band) [[unlikely]]
67 return bin = T(0);
68 return this->memory[in_row > in_col ? in_row - in_col + in_col * m_rows : in_col - in_row + in_row * m_rows];
69 }
70
71 T& unsafe_at(const uword in_row, const uword in_col) override {
72 this->factored = false;
73 return this->memory[in_row - in_col + in_col * m_rows];
74 }
75
76 T& at(const uword in_row, const uword in_col) override {
77 if(in_row > band + in_col || in_row < in_col) [[unlikely]]
78 return bin = T(0);
79 return this->unsafe_at(in_row, in_col);
80 }
81
82 Mat<T> operator*(const Mat<T>&) const override;
83
84 [[nodiscard]] int sign_det() const override { return 1; }
85};
86
87template<sp_d T> T BandSymmMat<T>::bin = T(0);
88
89template<sp_d T> Mat<T> BandSymmMat<T>::operator*(const Mat<T>& X) const {
90 Mat<T> Y(arma::size(X));
91
92 const auto N = static_cast<blas_int>(this->n_cols);
93 const auto K = static_cast<blas_int>(band);
94 const auto LDA = static_cast<blas_int>(m_rows);
95 static constexpr blas_int INC = 1;
96 static constexpr T ALPHA{1}, BETA{0};
97
98 if constexpr(std::is_same_v<T, float>) suanpan::for_each(X.n_cols, [&](const uword I) { arma_fortran(arma_ssbmv)(&UPLO, &N, &K, &ALPHA, this->memptr(), &LDA, X.colptr(I), &INC, &BETA, Y.colptr(I), &INC); });
99 else suanpan::for_each(X.n_cols, [&](const uword I) { arma_fortran(arma_dsbmv)(&UPLO, &N, &K, &ALPHA, this->memptr(), &LDA, X.colptr(I), &INC, &BETA, Y.colptr(I), &INC); });
100 return Y;
101}
102
103template<sp_d T> int BandSymmMat<T>::direct_solve(Mat<T>& X, Mat<T>&& B) {
104 if(this->factored) return this->solve_trs(X, std::move(B));
105
106 suanpan_assert([&] { if(this->n_rows != this->n_cols) throw std::invalid_argument("requires a square matrix"); });
107
108 blas_int INFO = 0;
109
110 const auto N = static_cast<blas_int>(this->n_rows);
111 const auto KD = static_cast<blas_int>(band);
112 const auto NRHS = static_cast<blas_int>(B.n_cols);
113 const auto LDAB = static_cast<blas_int>(m_rows);
114 const auto LDB = static_cast<blas_int>(B.n_rows);
115 this->factored = true;
116
117 if constexpr(std::is_same_v<T, float>) {
118 arma_fortran(arma_spbsv)(&UPLO, &N, &KD, &NRHS, this->memptr(), &LDAB, B.memptr(), &LDB, &INFO);
119 X = std::move(B);
120 }
121 else if(Precision::FULL == this->setting.precision) {
122 arma_fortran(arma_dpbsv)(&UPLO, &N, &KD, &NRHS, this->memptr(), &LDAB, B.memptr(), &LDB, &INFO);
123 X = std::move(B);
124 }
125 else {
126 this->s_memory = this->to_float();
127 arma_fortran(arma_spbtrf)(&UPLO, &N, &KD, this->s_memory.memptr(), &LDAB, &INFO);
128 if(0 == INFO) INFO = this->solve_trs(X, std::move(B));
129 }
130
131 if(0 != INFO)
132 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
133
134 return INFO;
135}
136
137template<sp_d T> int BandSymmMat<T>::solve_trs(Mat<T>& X, Mat<T>&& B) {
138 blas_int INFO = 0;
139
140 const auto N = static_cast<blas_int>(this->n_rows);
141 const auto KD = static_cast<blas_int>(band);
142 const auto NRHS = static_cast<blas_int>(B.n_cols);
143 const auto LDAB = static_cast<blas_int>(m_rows);
144 const auto LDB = static_cast<blas_int>(B.n_rows);
145
146 if constexpr(std::is_same_v<T, float>) {
147 arma_fortran(arma_spbtrs)(&UPLO, &N, &KD, &NRHS, this->memptr(), &LDAB, B.memptr(), &LDB, &INFO);
148 X = std::move(B);
149 }
150 else if(Precision::FULL == this->setting.precision) {
151 arma_fortran(arma_dpbtrs)(&UPLO, &N, &KD, &NRHS, this->memptr(), &LDAB, B.memptr(), &LDB, &INFO);
152 X = std::move(B);
153 }
154 else
155 this->mixed_trs(X, std::move(B), [&](fmat& residual) {
156 arma_fortran(arma_spbtrs)(&UPLO, &N, &KD, &NRHS, this->s_memory.memptr(), &LDAB, residual.memptr(), &LDB, &INFO);
157 return INFO;
158 });
159
160 if(0 != INFO)
161 suanpan_error("Error code {} received, the matrix is probably singular.\n", INFO);
162
163 return INFO;
164}
165
166#endif
167
A BandSymmMat class that holds matrices.
Definition BandSymmMat.hpp:35
unique_ptr< MetaMat< T > > unique_copy() override
Definition BandSymmMat.hpp:56
T operator()(const uword in_row, const uword in_col) const override
Access element (read-only), returns zero if out-of-bound.
Definition BandSymmMat.hpp:65
BandSymmMat(const uword in_size, const uword in_bandwidth)
Definition BandSymmMat.hpp:51
T & at(const uword in_row, const uword in_col) override
Access element with bound check.
Definition BandSymmMat.hpp:76
int sign_det() const override
Definition BandSymmMat.hpp:84
void nullify(const uword K) override
Definition BandSymmMat.hpp:58
T & unsafe_at(const uword in_row, const uword in_col) override
Access element without bound check.
Definition BandSymmMat.hpp:71
A DenseMat class that holds matrices.
Definition DenseMat.hpp:39
std::unique_ptr< T[]> memory
Definition DenseMat.hpp:48
const uword n_rows
Definition MetaMat.hpp:115
bool factored
Definition MetaMat.hpp:76
Mat< T > operator*(const Mat< T > &) const override
Definition BandSymmMat.hpp:89
int direct_solve(Mat< T > &, Mat< T > &&) override
Definition BandSymmMat.hpp:103
void for_each(const IT start, const IT end, F &&FN)
Definition utility.h:31
auto suanpan_assert(F &&handler)
Definition suanPan.h:339
#define suanpan_error(...)
Definition suanPan.h:349