Loading...
Searching...
No Matches
BLRMatrix.hpp
Go to the documentation of this file.
1/*
2 * STRUMPACK -- STRUctured Matrices PACKage, Copyright (c) 2014, The
3 * Regents of the University of California, through Lawrence Berkeley
4 * National Laboratory (subject to receipt of any required approvals
5 * from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * If you have questions about your rights to use or distribute this
8 * software, please contact Berkeley Lab's Technology Transfer
9 * Department at TTD@lbl.gov.
10 *
11 * NOTICE. This software is owned by the U.S. Department of Energy. As
12 * such, the U.S. Government has been granted for itself and others
13 * acting on its behalf a paid-up, nonexclusive, irrevocable,
14 * worldwide license in the Software to reproduce, prepare derivative
15 * works, and perform publicly and display publicly. Beginning five
16 * (5) years after the date permission to assert copyright is obtained
17 * from the U.S. Department of Energy, and subject to any subsequent
18 * five (5) year renewals, the U.S. Government is granted for itself
19 * and others acting on its behalf a paid-up, nonexclusive,
20 * irrevocable, worldwide license in the Software to reproduce,
21 * prepare derivative works, distribute copies to the public, perform
22 * publicly and display publicly, and to permit others to do so.
23 *
24 * Developers: Pieter Ghysels, Francois-Henry Rouet, Xiaoye S. Li.
25 * (Lawrence Berkeley National Lab, Computational Research
26 * Division).
27 *
28 */
32#ifndef BLR_MATRIX_HPP
33#define BLR_MATRIX_HPP
34
35#include <cassert>
36#include <memory>
37#include <functional>
38#include <algorithm>
39
40#include "BLROptions.hpp"
41#include "BLRTileBLAS.hpp" // TODO remove
43#include "misc/Tools.hpp"
44#include "dense/GPUWrapper.hpp"
45
46namespace strumpack {
47 namespace BLR {
48
49 // forward declarations
50 template<typename scalar> class BLRTile;
51 template<typename scalar_t,typename integer_t> class BLRExtendAdd;
52
53
68 template<typename scalar_t> class BLRMatrix
69 : public structured::StructuredMatrix<scalar_t> {
74 using real_t = typename RealType<scalar_t>::value_type;
75 using extract_t =
76 std::function<void(const std::vector<std::size_t>&,
77 const std::vector<std::size_t>&, DenseMatrix<scalar_t>&)>;
79
80 public:
81 BLRMatrix() = default;
82
83 BLRMatrix(std::size_t m, const std::vector<std::size_t>& rowtiles,
84 std::size_t n, const std::vector<std::size_t>& coltiles);
85
86 std::size_t rows() const override { return m_; }
87 std::size_t cols() const override { return n_; }
88
89 std::size_t memory() const override;
90 std::size_t nonzeros() const override;
91 std::size_t rank() const override;
92
96 std::size_t subnormals() const;
97 std::size_t zeros() const;
98
99 DenseM_t dense() const;
100 void dense(DenseM_t& A) const;
101
102 void compress(const DenseM_t& A, const adm_t& admissible,
103 const Opts_t& opts);
104 void compress(const extract_t& Aelem, const adm_t& admissible,
105 const Opts_t& opts);
106
107 void compress_and_factor(const DenseM_t& A, const adm_t& admissible,
108 const Opts_t& opts);
109 void compress_and_factor(const extract_t& Aelem, const adm_t& admissible,
110 const Opts_t& opts);
111
112 void draw(std::ostream& of, std::size_t roff, std::size_t coff) const;
113
114 void print(const std::string& name) const;
115
116 void clear();
117
118 void solve(DenseM_t& x) const override {
119 x.laswp(piv_, true);
120 trsm(Side::L, UpLo::L, Trans::N, Diag::U, scalar_t(1.), *this, x, 0);
121 trsm(Side::L, UpLo::U, Trans::N, Diag::N, scalar_t(1.), *this, x, 0);
122 }
123
124 const std::vector<int>& piv() const { return piv_; }
125
137 void mult(Trans op, const DenseM_t& x, DenseM_t& y) const override;
138
139 std::size_t rg2t(std::size_t i) const;
140 std::size_t cg2t(std::size_t j) const;
141
142 scalar_t operator()(std::size_t i, std::size_t j) const;
143 scalar_t& operator()(std::size_t i, std::size_t j);
144 DenseM_t extract(const std::vector<std::size_t>& I,
145 const std::vector<std::size_t>& J) const;
146
147 void decompress();
148 void decompress_local_columns(int c_min, int c_max);
149 void remove_tiles_before_local_column(int c_min, int c_max);
150
151 std::size_t rowblocks() const { return nbrows_; }
152 std::size_t colblocks() const { return nbcols_; }
153 std::size_t tilerows(std::size_t i) const {
154 assert(i < rowblocks());
155 return roff_[i+1] - roff_[i];
156 }
157 std::size_t tilecols(std::size_t j) const {
158 assert(j < colblocks());
159 return coff_[j+1] - coff_[j];
160 }
161 std::size_t tileroff(std::size_t i) const {
162 assert(i <= rowblocks());
163 return roff_[i];
164 }
165 std::size_t tilecoff(std::size_t j) const {
166 assert(j <= colblocks());
167 return coff_[j];
168 }
169 std::size_t maxtilerows() const;
170 std::size_t maxtilecols() const;
171
172 BLRTile<scalar_t>& tile(std::size_t i, std::size_t j);
173 const BLRTile<scalar_t>& tile(std::size_t i, std::size_t j) const;
174 std::unique_ptr<BLRTile<scalar_t>>& block(std::size_t i, std::size_t j);
175 DenseMW_t tile(DenseM_t& A, std::size_t i, std::size_t j) const;
176 DenseTile<scalar_t>& tile_dense(std::size_t i, std::size_t j);
177 const DenseTile<scalar_t>& tile_dense(std::size_t i, std::size_t j) const;
178
179 void compress_tile(std::size_t i, std::size_t j, const Opts_t& opts);
180 void fill(scalar_t v);
181 void fill_col(scalar_t v, std::size_t k, std::size_t CP);
182
183 static void
184 construct_and_partial_factor(DenseM_t& A11, DenseM_t& A12,
185 DenseM_t& A21, DenseM_t& A22,
186 BLRM_t& B11, BLRM_t& B12, BLRM_t& B21,
187 const std::vector<std::size_t>& tiles1,
188 const std::vector<std::size_t>& tiles2,
189 const adm_t& admissible,
190 const Opts_t& opts);
191
192#if defined(STRUMPACK_USE_GPU)
193 static void
194 construct_and_partial_factor_gpu(DenseM_t& A11, DenseM_t& A12,
195 DenseM_t& A21, DenseM_t& A22,
196 BLRM_t& B11, BLRM_t& B12, BLRM_t& B21,
197 const std::vector<std::size_t>& tiles1,
198 const std::vector<std::size_t>& tiles2,
199 const adm_t& admissible,
200 VectorPool<scalar_t>& workspace,
201 const Opts_t& opts);
202#endif
203
204 static void
205 construct_and_partial_factor(BLRM_t& B11, BLRM_t& B12,
206 BLRM_t& B21, BLRM_t& B22,
207 const std::vector<std::size_t>& tiles1,
208 const std::vector<std::size_t>& tiles2,
209 const adm_t& admissible,
210 const Opts_t& opts);
211
212 static void
213 construct_and_partial_factor_col(BLRM_t& B11, BLRM_t& B12,
214 BLRM_t& B21, BLRM_t& B22,
215 const std::vector<std::size_t>& tiles1,
216 const std::vector<std::size_t>& tiles2,
217 const adm_t& admissible,
218 const Opts_t& opts,
219 const std::function<void
220 (int, bool, std::size_t)>& blockcol);
221
222 static void
223 construct_and_partial_factor(std::size_t n1, std::size_t n2,
224 const extract_t& A11, const extract_t& A12,
225 const extract_t& A21, const extract_t& A22,
226 BLRM_t& B11, BLRM_t& B12,
227 BLRM_t& B21, BLRM_t& B22,
228 const std::vector<std::size_t>& tiles1,
229 const std::vector<std::size_t>& tiles2,
230 const adm_t& admissible,
231 const BLROptions<scalar_t>& opts);
232
233 static void
234 trsmLNU_gemm(const BLRM_t& F1, const BLRM_t& F2,
235 DenseM_t& B1, DenseM_t& B2, int task_depth);
236
237 static void
238 gemm_trsmUNN(const BLRM_t& F1, const BLRM_t& F2,
239 DenseM_t& B1, DenseM_t& B2, int task_depth);
240
241 private:
242 std::size_t m_ = 0, n_ = 0, nbrows_ = 0, nbcols_ = 0;
243 std::vector<std::size_t> roff_, coff_, cl2l_, rl2l_;
244 std::vector<std::unique_ptr<BLRTile<scalar_t>>> blocks_;
245 std::vector<int> piv_;
246
247 void create_dense_tile(std::size_t i, std::size_t j, DenseM_t& A);
248 void create_dense_tile(std::size_t i, std::size_t j,
249 const extract_t& Aelem);
250 void create_dense_tile_left_looking(std::size_t i, std::size_t j,
251 const extract_t& Aelem);
252 void create_dense_tile_left_looking(std::size_t i, std::size_t j,
253 std::size_t k,
254 const extract_t& Aelem,
255 const BLRM_t& B21,
256 const BLRM_t& B12);
257 void create_LR_tile(std::size_t i, std::size_t j,
258 DenseM_t& A, const Opts_t& opts);
259 void create_LR_tile(std::size_t i, std::size_t j,
260 const extract_t& A, const Opts_t& opts);
261
262#if defined(STRUMPACK_USE_GPU)
263 void create_from_column_major_gpu(DenseM_t& A, scalar_t* work);
264#endif
265
266 void create_LR_tile_left_looking(std::size_t i, std::size_t j,
267 const extract_t& Aelem,
268 const Opts_t& opts);
269
270 void create_LR_tile_left_looking(std::size_t i, std::size_t j,
271 std::size_t k,
272 const extract_t& Aelem,
273 const BLRM_t& B21, const BLRM_t& B12,
274 const Opts_t& opts);
275 void LUAR_B11(std::size_t i, std::size_t j, std::size_t kmax,
276 DenseM_t& A11, const Opts_t& opts, int* B);
277 void LUAR_B12(std::size_t i, std::size_t j, std::size_t kmax,
278 BLRM_t& B11, DenseM_t& A12,
279 const Opts_t& opts, int* B);
280 void LUAR_B21(std::size_t i, std::size_t j, std::size_t kmax,
281 BLRM_t& B11, DenseM_t& A21,
282 const Opts_t& opts, int* B);
283
284 template<typename T> friend
285 void draw(const BLRMatrix<T>& H, const std::string& name);
286 template<typename T,typename I> friend class BLRExtendAdd;
287
288 // suppress warnings
289 using structured::StructuredMatrix<scalar_t>::mult;
290 using structured::StructuredMatrix<scalar_t>::solve;
291 };
292
293 template<typename scalar_t> void
294 LUAR(const std::vector<BLRTile<scalar_t>*>& Ti,
295 const std::vector<BLRTile<scalar_t>*>& Tj,
296 DenseMatrixWrapper<scalar_t>& tij,
297 const BLROptions<scalar_t>& opts, int* B);
298
299 template<typename scalar_t> void
300 LUAR_B22(std::size_t i, std::size_t j, std::size_t kmax,
301 BLRMatrix<scalar_t>& B12, BLRMatrix<scalar_t>& B21,
302 DenseMatrix<scalar_t>& A22,
303 const BLROptions<scalar_t>& opts, int* B);
304
305 template<typename scalar_t> void
306 trsm(Side s, UpLo ul, Trans ta, Diag d, scalar_t alpha,
307 const BLRMatrix<scalar_t>& a, DenseMatrix<scalar_t>& b,
308 int task_depth);
309
310 template<typename scalar_t> void
311 trsv(UpLo ul, Trans ta, Diag d, const BLRMatrix<scalar_t>& a,
312 DenseMatrix<scalar_t>& b, int task_depth);
313
314 template<typename scalar_t> void
315 gemv(Trans ta, scalar_t alpha, const BLRMatrix<scalar_t>& a,
316 const DenseMatrix<scalar_t>& x, scalar_t beta,
317 DenseMatrix<scalar_t>& y, int task_depth);
318
319 template<typename scalar_t> void
320 gemm(Trans ta, Trans tb, scalar_t alpha, const BLRMatrix<scalar_t>& a,
321 const BLRMatrix<scalar_t>& b, scalar_t beta,
322 DenseMatrix<scalar_t>& c, int task_depth);
323
324 template<typename scalar_t> void
325 gemm(Trans ta, Trans tb, scalar_t alpha, const BLRMatrix<scalar_t>& A,
326 const DenseMatrix<scalar_t>& B, scalar_t beta,
327 DenseMatrix<scalar_t>& C, int task_depth);
328
329 template<typename scalar_t>
330 void draw(const BLRMatrix<scalar_t>& B, const std::string& name) {
331 std::ofstream of("plot" + name + ".gnuplot");
332 of << "set terminal pdf enhanced color size 5,4" << std::endl;
333 of << "set output '" << name << ".pdf'" << std::endl;
334 B.draw(of, 0, 0);
335 of << "set xrange [0:" << B.cols() << "]" << std::endl;
336 of << "set yrange [" << B.rows() << ":0]" << std::endl;
337 of << "plot x lt -1 notitle" << std::endl;
338 of.close();
339 }
340
341 } // end namespace BLR
342} // end namespace strumpack
343
344#endif // BLR_MATRIX_HPP
Contains class holding BLROptions.
Contains the structured matrix interfaces.
Definition BLRMatrix.hpp:51
Class to represent a block low-rank matrix.
Definition BLRMatrix.hpp:69
std::size_t rows() const override
Definition BLRMatrix.hpp:86
std::size_t cols() const override
Definition BLRMatrix.hpp:87
std::size_t nonzeros() const override
std::size_t memory() const override
std::size_t rank() const override
void solve(DenseM_t &x) const override
Definition BLRMatrix.hpp:118
void mult(Trans op, const DenseM_t &x, DenseM_t &y) const override
std::size_t subnormals() const
Class containing several options for the BLR code and data-structures.
Definition BLROptions.hpp:82
Definition BLRMatrix.hpp:50
Like DenseMatrix, this class represents a matrix, stored in column major format, to allow direct use ...
Definition DenseMatrix.hpp:1018
This class represents a matrix, stored in column major format, to allow direct use of BLAS/LAPACK rou...
Definition DenseMatrix.hpp:139
void laswp(const std::vector< int > &P, bool fwd)
Class to represent a structured matrix. This is the abstract base class for several types of structur...
Definition StructuredMatrix.hpp:209
Definition StrumpackOptions.hpp:44
UpLo
Definition DenseMatrix.hpp:83
void trsv(UpLo ul, Trans ta, Diag d, const DenseMatrix< scalar_t > &a, DenseMatrix< scalar_t > &b, int depth=0)
void gemv(Trans ta, scalar_t alpha, const DenseMatrix< scalar_t > &a, const DenseMatrix< scalar_t > &x, scalar_t beta, DenseMatrix< scalar_t > &y, int depth=0)
Trans
Definition DenseMatrix.hpp:51
void trsm(Side s, UpLo ul, Trans ta, Diag d, scalar_t alpha, const DenseMatrix< scalar_t > &a, DenseMatrix< scalar_t > &b, int depth=0)
Side
Definition DenseMatrix.hpp:74
void gemm(Trans ta, Trans tb, scalar_t alpha, const DenseMatrix< scalar_t > &a, const DenseMatrix< scalar_t > &b, scalar_t beta, DenseMatrix< scalar_t > &c, int depth=0)
Diag
Definition DenseMatrix.hpp:93