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)
45 #include "dense/DistributedMatrix.hpp"
46 #endif
47 
48 namespace 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,
360  DistributedMatrix<scalar_t>& y) const;
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,
587  const extract_block_t<scalar_t>& A,
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 
786  template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
787  construct_from_elements(const MPIComm& comm, int rows, int cols,
788  const extract_t<scalar_t>& A,
789  const StructuredOptions<scalar_t>& opts,
790  const structured::ClusterTree* row_tree=nullptr,
791  const structured::ClusterTree* col_tree=nullptr);
792 
826  template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
827  construct_from_elements(const MPIComm& comm, int rows, int cols,
829  const StructuredOptions<scalar_t>& opts,
830  const structured::ClusterTree* row_tree=nullptr,
831  const structured::ClusterTree* col_tree=nullptr);
832 
863  template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
864  construct_matrix_free(const MPIComm& comm, const BLACSGrid* g,
865  int rows, int cols,
866  const mult_2d_t<scalar_t>& Amult,
867  const StructuredOptions<scalar_t>& opts,
868  const structured::ClusterTree* row_tree=nullptr,
869  const structured::ClusterTree* col_tree=nullptr);
870 
901  template<typename scalar_t> std::unique_ptr<StructuredMatrix<scalar_t>>
902  construct_matrix_free(const MPIComm& comm, int rows, int cols,
903  const mult_1d_t<scalar_t>& Amult,
904  const StructuredOptions<scalar_t>& opts,
905  const structured::ClusterTree* row_tree=nullptr,
906  const structured::ClusterTree* col_tree=nullptr);
907 
908 #endif
909 
910  } // end namespace structured
911 } // end namespace strumpack
912 
913 #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 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
Definition: DistributedMatrix.hpp:52
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:62
Class to represent a structured matrix. This is the abstract base class for several types of structur...
Definition: StructuredMatrix.hpp:209
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 const std::vector< int > & cdist() const
Definition: StructuredMatrix.hpp:320
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 const std::vector< int > & rdist() const
Definition: StructuredMatrix.hpp:310
virtual const std::vector< int > & dist() const
Definition: StructuredMatrix.hpp:299
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_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::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< 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::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::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::function< void(Trans op, const DistributedMatrix< scalar_t > &R, DistributedMatrix< scalar_t > &S)> mult_2d_t
Definition: StructuredMatrix.hpp:137
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::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:42
Trans
Definition: DenseMatrix.hpp:51