ezp
Loading...
Searching...
No Matches
pgbsv.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2025 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 ******************************************************************************/
60#ifndef PGBSV_HPP
61#define PGBSV_HPP
62
63#include "abstract/band_solver.hpp"
64
65namespace ezp {
66 template<data_t DT, index_t IT> class pgbsv final : public detail::band_solver<DT, IT, band_mat<DT, IT>> {
68
69 struct band_system {
70 IT n{-1}, kl{-1}, ku{-1}, lead{-1}, block{-1}, lines{-1};
71 desc<IT> desc1d_a;
72 std::vector<DT> a, b, work;
73 std::vector<IT> ipiv;
74 };
75
76 band_system loc;
77
78 auto init_storage(const IT n, const IT kl, const IT ku) {
79 loc.n = n;
80 loc.kl = kl;
81 loc.ku = ku;
82 loc.lead = 2 * (loc.kl + loc.ku) + 1;
83 loc.block = std::max(loc.n / std::max(IT{1}, this->ctx.n_rows - 1) + 1, std::max(loc.kl + loc.ku + 1, this->ctx.row_block(loc.n)));
84 loc.block = std::min(loc.block, loc.n);
85 loc.lines = this->ctx.rows(loc.n, loc.block);
86 loc.desc1d_a = {501, this->trans_ctx.context, loc.n, loc.block, 0, loc.lead, 0, 0, 0};
87
88 // see: https://github.com/Reference-ScaLAPACK/scalapack/issues/117
89 loc.a.resize(loc.lead * loc.lines + loc.ku);
90 loc.ipiv.resize(std::min(loc.n, loc.lines + loc.kl + loc.ku), -987654);
91 }
92
93 using base_t::to_band;
94 using base_t::to_full;
95
96 public:
97 explicit pgbsv(const IT rows = get_env<IT>().size())
98 : base_t(rows) {}
99
100 class indexer {
101 IT n, kl, ku;
102
103 public:
104 explicit indexer(const band_mat<DT, IT>& A)
105 : n(A.n)
106 , kl(A.kl)
107 , ku(A.ku) {}
108
109 indexer(const IT N, const IT KL, const IT KU)
110 : n(N)
111 , kl(KL)
112 , ku(KU) {}
113
114 auto operator()(const IT i, const IT j) const {
115 if(i < 0 || i >= n || j < 0 || j >= n) return IT{-1};
116 if(i - j > kl || j - i > ku) return IT{-1};
117 return 2 * ku + kl + i + 2 * j * (kl + ku);
118 }
119 };
120
121 template<band_container_t AT, full_container_t BT> IT solve(AT&& A, BT&& B) { return solve(to_band(A), to_full(B)); }
122
123 IT solve(band_mat<DT, IT>&& A, full_mat<DT, IT>&& B) {
124 if(!this->ctx.is_valid() || !this->trans_ctx.is_valid()) return 0;
125
126 if(A.n_rows != A.n_cols || A.n_rows != B.n_rows) return -1;
127
128 init_storage(A.n_cols, A.kl, A.ku);
129
130 // pretend that A is a full matrix of size (2*(kl+ku)+1) x n
131 // redistribute A to the process grid
132 this->trans_ctx.scatter(
133 full_mat<DT, IT>(loc.lead, loc.n, A.data, A.distributed),
134 this->trans_ctx.desc_g(loc.lead, loc.n),
135 loc.a,
136 this->trans_ctx.desc_l(loc.lead, loc.n, loc.lead, loc.block, loc.lead)
137 );
138
139 const IT laf = (loc.block + 6 * loc.kl + 13 * loc.ku) * (loc.kl + loc.ku);
140 const IT lwork = std::max(B.n_cols * (loc.block + 2 * loc.kl + 4 * loc.ku), IT{1});
141 loc.work.resize(laf + lwork);
142
143 IT info{-1};
144 // ReSharper disable CppCStyleCast
145 if constexpr(std::is_same_v<DT, double>) {
146 using E = double;
147 pdgbtrf(&loc.n, &loc.kl, &loc.ku, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
148 }
149 else if constexpr(std::is_same_v<DT, float>) {
150 using E = float;
151 psgbtrf(&loc.n, &loc.kl, &loc.ku, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
152 }
153 else if constexpr(std::is_same_v<DT, complex16>) {
154 using E = complex16;
155 pzgbtrf(&loc.n, &loc.kl, &loc.ku, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
156 }
157 else if constexpr(std::is_same_v<DT, complex8>) {
158 using E = complex8;
159 pcgbtrf(&loc.n, &loc.kl, &loc.ku, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
160 }
161 // ReSharper restore CppCStyleCast
162
163 if((info = this->trans_ctx.amx(info)) != 0) return info;
164
165 return solve(std::move(B));
166 }
167
168 IT solve(full_mat<DT, IT>&& B) {
169 static constexpr char TRANS = 'N';
170
171 if(B.n_rows != loc.n) return -1;
172
173 if(!this->ctx.is_valid() || !this->trans_ctx.is_valid()) return 0;
174
175 const auto lead_b = std::max(loc.block, loc.lines);
176
177 loc.b.resize(lead_b * B.n_cols);
178
179 const auto full_desc_b = this->ctx.desc_g(B.n_rows, B.n_cols);
180 const auto local_desc_b = this->ctx.desc_l(B.n_rows, B.n_cols, loc.block, B.n_cols, lead_b);
181
182 this->ctx.scatter(B, full_desc_b, loc.b, local_desc_b);
183
184 const IT laf = (loc.block + 6 * loc.kl + 13 * loc.ku) * (loc.kl + loc.ku);
185 const IT lwork = std::max(B.n_cols * (loc.block + 2 * loc.kl + 4 * loc.ku), IT{1});
186 loc.work.resize(laf + lwork);
187
188 desc<IT> desc1d_b{502, this->trans_ctx.context, loc.n, loc.block, 0, lead_b, 0, 0, 0};
189
190 IT info{-1};
191 // ReSharper disable CppCStyleCast
192 if constexpr(std::is_same_v<DT, double>) {
193 using E = double;
194 pdgbtrs(&TRANS, &loc.n, &loc.kl, &loc.ku, &B.n_cols, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.b.data(), &this->ONE, desc1d_b.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
195 }
196 else if constexpr(std::is_same_v<DT, float>) {
197 using E = float;
198 psgbtrs(&TRANS, &loc.n, &loc.kl, &loc.ku, &B.n_cols, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.b.data(), &this->ONE, desc1d_b.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
199 }
200 else if constexpr(std::is_same_v<DT, complex16>) {
201 using E = complex16;
202 pzgbtrs(&TRANS, &loc.n, &loc.kl, &loc.ku, &B.n_cols, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.b.data(), &this->ONE, desc1d_b.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
203 }
204 else if constexpr(std::is_same_v<DT, complex8>) {
205 using E = complex8;
206 pcgbtrs(&TRANS, &loc.n, &loc.kl, &loc.ku, &B.n_cols, (E*)loc.a.data(), &this->ONE, loc.desc1d_a.data(), loc.ipiv.data(), (E*)loc.b.data(), &this->ONE, desc1d_b.data(), (E*)loc.work.data(), &laf, (E*)(loc.work.data() + laf), &lwork, &info);
207 }
208 // ReSharper restore CppCStyleCast
209
210 if((info = this->trans_ctx.amx(info)) == 0) this->ctx.gather(loc.b, local_desc_b, B, full_desc_b);
211
212 return info;
213 }
214 };
215
216 template<index_t IT> using par_dgbsv = pgbsv<double, IT>;
217 template<index_t IT> using par_sgbsv = pgbsv<float, IT>;
218 template<index_t IT> using par_zgbsv = pgbsv<complex16, IT>;
219 template<index_t IT> using par_cgbsv = pgbsv<complex8, IT>;
220} // namespace ezp
221
222#endif // PGBSV_HPP
223
Definition band_solver.hpp:24
Definition pgbsv.hpp:100
Definition pgbsv.hpp:66
Solver for general band matrices.
Definition abstract_solver.hpp:72
Definition abstract_solver.hpp:68