StructuredMatrix.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 STRUCTURED_MATRIX_HPP
33#define STRUCTURED_MATRIX_HPP
34
35#include <vector>
36#include <cassert>
37#include <memory>
38#include <functional>
39#include <algorithm>
40
41#include "StructuredOptions.hpp"
42#include "ClusterTree.hpp"
43#include "dense/DenseMatrix.hpp"
44#if defined(STRUMPACK_USE_MPI)
46#endif
47
48namespace strumpack {
49
55 namespace structured {
56
57
66 template<typename scalar_t>
67 using extract_t = std::function
68 <scalar_t(std::size_t i, std::size_t j)>;
69
79 template<typename scalar_t>
80 using extract_block_t = std::function
81 <void(const std::vector<std::size_t>& I,
82 const std::vector<std::size_t>& J,
84
97 template<typename scalar_t>
98 using mult_t = std::function
99 <void(Trans op,
100 const DenseMatrix<scalar_t>& R,
102
103#if defined(STRUMPACK_USE_MPI)
115 template<typename scalar_t>
116 using extract_dist_block_t = std::function
117 <void(const std::vector<std::size_t>& I,
118 const std::vector<std::size_t>& J,
120
133 template<typename scalar_t>
134 using mult_2d_t = std::function
135 <void(Trans op,
138
157 template<typename scalar_t>
158 using mult_1d_t = std::function
159 <void(Trans op,
160 const DenseMatrix<scalar_t>& R,
162 const std::vector<int>& rdist,
163 const std::vector<int>& cdist)>;
164#endif
165
170
171
172
209 template<typename scalar_t> class StructuredMatrix {
210 using real_t = typename RealType<scalar_t>::value_type;
211
212 public:
213
217 virtual ~StructuredMatrix() = default;
218
222 virtual std::size_t rows() const = 0;
223
227 virtual std::size_t cols() const = 0;
228
236 virtual std::size_t memory() const = 0;
237
244 virtual std::size_t nonzeros() const = 0;
245
252 virtual std::size_t rank() const = 0;
253
262 virtual std::size_t local_rows() const {
263 throw std::invalid_argument
264 ("1d block row distribution not supported for this format.");
265 }
266
274 virtual std::size_t begin_row() const {
275 throw std::invalid_argument
276 ("1d block row distribution not supported for this format.");
277 }
278
286 virtual std::size_t end_row() const {
287 throw std::invalid_argument
288 ("1d block row distribution not supported for this format.");
289 }
290
299 virtual const std::vector<int>& dist() const {
300 static std::vector<int> d = {0, int(rows())};
301 return d;
302 };
310 virtual const std::vector<int>& rdist() const {
311 return dist();
312 };
320 virtual const std::vector<int>& cdist() const {
321 return dist();
322 };
323
332 virtual void mult(Trans op, const DenseMatrix<scalar_t>& x,
333 DenseMatrix<scalar_t>& y) const;
334
347 void mult(Trans op, int m, const scalar_t* x, int ldx,
348 scalar_t* y, int ldy) const;
349
350#if defined(STRUMPACK_USE_MPI)
359 virtual void mult(Trans op, const DistributedMatrix<scalar_t>& x,
361#endif
362
369 virtual void factor();
370
378 virtual void solve(DenseMatrix<scalar_t>& b) const;
379
389 virtual void solve(int nrhs, scalar_t* b, int ldb) const {
390 int lr = rows();
391 try { lr = local_rows(); }
392 catch(...) {}
393 DenseMatrixWrapper<scalar_t> B(lr, nrhs, b, ldb);
394 solve(B);
395 }
396
397#if defined(STRUMPACK_USE_MPI)
405 virtual void solve(DistributedMatrix<scalar_t>& b) const;
406#endif
407
417 virtual void shift(scalar_t s);
418
419 };
420
421
459 template<typename scalar_t>
460 std::unique_ptr<StructuredMatrix<scalar_t>>
462 const StructuredOptions<scalar_t>& opts,
463 const structured::ClusterTree* row_tree=nullptr,
464 const structured::ClusterTree* col_tree=nullptr);
465
506 template<typename scalar_t>
507 std::unique_ptr<StructuredMatrix<scalar_t>>
508 construct_from_dense(int rows, int cols, const scalar_t* A, int ldA,
509 const StructuredOptions<scalar_t>& opts,
510 const structured::ClusterTree* row_tree=nullptr,
511 const structured::ClusterTree* col_tree=nullptr);
512
545 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
546 construct_from_elements(int rows, int cols,
547 const extract_t<scalar_t>& A,
548 const StructuredOptions<scalar_t>& opts,
549 const structured::ClusterTree* row_tree=nullptr,
550 const structured::ClusterTree* col_tree=nullptr);
551
552
585 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
586 construct_from_elements(int rows, int cols,
588 const StructuredOptions<scalar_t>& opts,
589 const structured::ClusterTree* row_tree=nullptr,
590 const structured::ClusterTree* col_tree=nullptr);
591
624 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
625 construct_matrix_free(int rows, int cols,
626 const mult_t<scalar_t>& Amult,
627 const StructuredOptions<scalar_t>& opts,
628 const structured::ClusterTree* row_tree=nullptr,
629 const structured::ClusterTree* col_tree=nullptr);
630
665 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
667 const mult_t<scalar_t>& Amult,
668 const extract_block_t<scalar_t>& Aelem,
669 const StructuredOptions<scalar_t>& opts,
670 const structured::ClusterTree* row_tree=nullptr,
671 const structured::ClusterTree* col_tree=nullptr);
672
707 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
709 const mult_t<scalar_t>& Amult,
710 const extract_t<scalar_t>& Aelem,
711 const StructuredOptions<scalar_t>& opts,
712 const structured::ClusterTree* row_tree=nullptr,
713 const structured::ClusterTree* col_tree=nullptr);
714
715
716#if defined(STRUMPACK_USE_MPI)
747 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
749 const StructuredOptions<scalar_t>& opts,
750 const structured::ClusterTree* row_tree=nullptr,
751 const structured::ClusterTree* col_tree=nullptr);
752
787 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
788 construct_from_elements(const MPIComm& comm, const BLACSGrid* grid,
789 int rows, int cols,
790 const extract_t<scalar_t>& A,
791 const StructuredOptions<scalar_t>& opts,
792 const structured::ClusterTree* row_tree=nullptr,
793 const structured::ClusterTree* col_tree=nullptr);
794
829 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
830 construct_from_elements(const MPIComm& comm, const BLACSGrid* grid,
831 int rows, int cols,
833 const StructuredOptions<scalar_t>& opts,
834 const structured::ClusterTree* row_tree=nullptr,
835 const structured::ClusterTree* col_tree=nullptr);
836
867 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
869 int rows, int cols,
870 const mult_2d_t<scalar_t>& Amult,
871 const StructuredOptions<scalar_t>& opts,
872 const structured::ClusterTree* row_tree=nullptr,
873 const structured::ClusterTree* col_tree=nullptr);
874
905 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
906 construct_matrix_free(const MPIComm& comm, int rows, int cols,
907 const mult_1d_t<scalar_t>& Amult,
908 const StructuredOptions<scalar_t>& opts,
909 const structured::ClusterTree* row_tree=nullptr,
910 const structured::ClusterTree* col_tree=nullptr);
911
912
949 template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
950 construct_partially_matrix_free(const BLACSGrid* grid, int rows, int cols,
951 const mult_2d_t<scalar_t>& Amult,
953 const StructuredOptions<scalar_t>& opts,
954 const structured::ClusterTree* row_tree=nullptr,
955 const structured::ClusterTree* col_tree=nullptr);
956
957#endif
958
959 } // end namespace structured
960} // end namespace strumpack
961
962#endif // STRUCTURED_MATRIX_HPP
This file contains the ClusterTree class definition.
Contains the DenseMatrix and DenseMatrixWrapper classes, simple wrappers around BLAS/LAPACK style den...
Contains the DistributedMatrix and DistributedMatrixWrapper classes, wrappers around ScaLAPACK/PBLAS ...
Contains the class definition for StructuredOptions, as well as some routines to get default options,...
This is a small wrapper class around a BLACS grid and a BLACS context.
Definition: BLACSGrid.hpp:66
Like DenseMatrix, this class represents a matrix, stored in column major format, to allow direct use ...
Definition: DenseMatrix.hpp:1015
This class represents a matrix, stored in column major format, to allow direct use of BLAS/LAPACK rou...
Definition: DenseMatrix.hpp:138
2D block cyclicly distributed matrix, as used by ScaLAPACK.
Definition: DistributedMatrix.hpp:84
Wrapper class around an MPI_Comm object.
Definition: MPIWrapper.hpp:194
The cluster tree, or partition tree that represents the partitioning of the rows or columns of a hier...
Definition: ClusterTree.hpp:67
Class to represent a structured matrix. This is the abstract base class for several types of structur...
Definition: StructuredMatrix.hpp:209
virtual const std::vector< int > & rdist() const
Definition: StructuredMatrix.hpp:310
virtual const std::vector< int > & dist() const
Definition: StructuredMatrix.hpp:299
virtual const std::vector< int > & cdist() const
Definition: StructuredMatrix.hpp:320
virtual void mult(Trans op, const DistributedMatrix< scalar_t > &x, DistributedMatrix< scalar_t > &y) const
virtual std::size_t begin_row() const
Definition: StructuredMatrix.hpp:274
void mult(Trans op, int m, const scalar_t *x, int ldx, scalar_t *y, int ldy) const
virtual std::size_t rows() const =0
virtual std::size_t memory() const =0
virtual std::size_t cols() const =0
virtual void solve(int nrhs, scalar_t *b, int ldb) const
Definition: StructuredMatrix.hpp:389
virtual std::size_t nonzeros() const =0
virtual std::size_t end_row() const
Definition: StructuredMatrix.hpp:286
virtual void solve(DistributedMatrix< scalar_t > &b) const
virtual std::size_t rank() const =0
virtual void solve(DenseMatrix< scalar_t > &b) const
virtual std::size_t local_rows() const
Definition: StructuredMatrix.hpp:262
virtual void mult(Trans op, const DenseMatrix< scalar_t > &x, DenseMatrix< scalar_t > &y) const
Class containing several options for the StructuredMatrix code and data-structures.
Definition: StructuredOptions.hpp:106
std::unique_ptr< StructuredMatrix< scalar_t > > construct_from_dense(const DenseMatrix< scalar_t > &A, const StructuredOptions< scalar_t > &opts, const structured::ClusterTree *row_tree=nullptr, const structured::ClusterTree *col_tree=nullptr)
std::function< scalar_t(std::size_t i, std::size_t j)> extract_t
Definition: StructuredMatrix.hpp:68
std::function< void(const std::vector< std::size_t > &I, const std::vector< std::size_t > &J, DenseMatrix< scalar_t > &B)> extract_block_t
Definition: StructuredMatrix.hpp:83
std::function< void(Trans op, const DistributedMatrix< scalar_t > &R, DistributedMatrix< scalar_t > &S)> mult_2d_t
Definition: StructuredMatrix.hpp:137
std::unique_ptr< StructuredMatrix< scalar_t > > construct_matrix_free(int rows, int cols, const mult_t< scalar_t > &Amult, const StructuredOptions< scalar_t > &opts, const structured::ClusterTree *row_tree=nullptr, const structured::ClusterTree *col_tree=nullptr)
std::unique_ptr< StructuredMatrix< scalar_t > > construct_from_elements(int rows, int cols, const extract_t< scalar_t > &A, const StructuredOptions< scalar_t > &opts, const structured::ClusterTree *row_tree=nullptr, const structured::ClusterTree *col_tree=nullptr)
std::function< void(Trans op, const DenseMatrix< scalar_t > &R, DenseMatrix< scalar_t > &S, const std::vector< int > &rdist, const std::vector< int > &cdist)> mult_1d_t
Definition: StructuredMatrix.hpp:163
std::unique_ptr< StructuredMatrix< scalar_t > > construct_partially_matrix_free(int rows, int cols, const mult_t< scalar_t > &Amult, const extract_block_t< scalar_t > &Aelem, const StructuredOptions< scalar_t > &opts, const structured::ClusterTree *row_tree=nullptr, const structured::ClusterTree *col_tree=nullptr)
std::function< void(const std::vector< std::size_t > &I, const std::vector< std::size_t > &J, DistributedMatrix< scalar_t > &B)> extract_dist_block_t
Definition: StructuredMatrix.hpp:119
std::function< void(Trans op, const DenseMatrix< scalar_t > &R, DenseMatrix< scalar_t > &S)> mult_t
Definition: StructuredMatrix.hpp:101
Definition: StrumpackOptions.hpp:43
Trans
Definition: DenseMatrix.hpp:51