ButterflyMatrix.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 STRUMPACK_BUTTERFLY_MATRIX_HPP
33 #define STRUMPACK_BUTTERFLY_MATRIX_HPP
34 
35 #include "HODLRMatrix.hpp"
36 
37 namespace strumpack {
38 
43  namespace HODLR {
44 
62  template<typename scalar_t> class ButterflyMatrix
63  : public structured::StructuredMatrix<scalar_t> {
69  using VecVec_t = std::vector<std::vector<std::size_t>>;
70  using F2Cptr = void*;
71 
72  public:
73  using mult_t = typename std::function
74  <void(Trans,scalar_t,const DenseM_t&,scalar_t,DenseM_t&)>;
75  using delem_blocks_t = typename HODLRMatrix<scalar_t>::delem_blocks_t;
76  using elem_blocks_t = typename HODLRMatrix<scalar_t>::elem_blocks_t;
77 
78  ButterflyMatrix() {}
79 
80  ButterflyMatrix(const MPIComm& comm,
81  const structured::ClusterTree& row_tree,
82  const structured::ClusterTree& col_tree,
83  const opts_t& opts);
84 
90  const HODLRMatrix<scalar_t>& B);
91 
93  const HODLRMatrix<scalar_t>& B,
94  DenseMatrix<int>& neighbors_rows,
95  DenseMatrix<int>& neighbors_cols,
96  const opts_t& opts);
97 
98  ButterflyMatrix(const ButterflyMatrix<scalar_t>& h) = delete;
99  ButterflyMatrix(ButterflyMatrix<scalar_t>&& h) { *this = std::move(h); }
100  virtual ~ButterflyMatrix();
101  ButterflyMatrix<scalar_t>& operator=(const ButterflyMatrix<scalar_t>& h) = delete;
103 
104  std::size_t rows() const override { return rows_; }
105  std::size_t cols() const override { return cols_; }
106  std::size_t lrows() const { return lrows_; }
107  std::size_t local_rows() const override { return lrows_; }
108  std::size_t lcols() const { return lcols_; }
109  std::size_t begin_row() const override { return rdist_[c_->rank()]; }
110  std::size_t end_row() const override { return rdist_[c_->rank()+1]; }
111  const std::vector<int>& rdist() const override { return rdist_; }
112  std::size_t begin_col() const { return cdist_[c_->rank()]; }
113  std::size_t end_col() const { return cdist_[c_->rank()+1]; }
114  const std::vector<int>& cdist() const override { return cdist_; }
115  const MPIComm& Comm() const { return *c_; }
116 
117  std::size_t memory() const override { return get_stat("Mem_Fill") * 1e6; }
118  std::size_t nonzeros() const override { return memory() / sizeof(scalar_t); }
119  std::size_t rank() const override { return get_stat("Rank_max"); }
120 
121  void compress(const mult_t& Amult);
122  void compress(const mult_t& Amult, int rank_guess);
123  void compress(const delem_blocks_t& Aelem);
124  void compress(const elem_blocks_t& Aelem);
125 
126  void mult(Trans op, const DenseM_t& X, DenseM_t& Y) const override;
127 
141  void mult(Trans op, const DistM_t& X, DistM_t& Y) const override;
142 
143  void extract_add_elements(const VecVec_t& I, const VecVec_t& J,
144  std::vector<DenseMW_t>& B);
145  void extract_add_elements(ExtractionMeta& e, std::vector<DistMW_t>& B);
146  void extract_add_elements(ExtractionMeta& e, std::vector<DenseMW_t>& B);
147 
148  double get_stat(const std::string& name) const;
149 
150  void print_stats();
151 
152  void set_sampling_parameter(double sample_param);
153  void set_BACA_block(int bsize);
154 
155  DistM_t dense(const BLACSGrid* g) const;
156 
157  DenseM_t redistribute_2D_to_1D(const DistM_t& R2D,
158  const std::vector<int>& dist) const;
159  void redistribute_2D_to_1D(scalar_t a, const DistM_t& R2D,
160  scalar_t b, DenseM_t& R1D,
161  const std::vector<int>& dist) const;
162  void redistribute_1D_to_2D(const DenseM_t& S1D, DistM_t& S2D,
163  const std::vector<int>& dist) const;
164 
165  private:
166  F2Cptr lr_bf_ = nullptr; // Butterfly handle returned by Fortran code
167  F2Cptr options_ = nullptr; // options structure returned by Fortran code
168  F2Cptr stats_ = nullptr; // statistics structure returned by Fortran code
169  F2Cptr msh_ = nullptr; // mesh structure returned by Fortran code
170  F2Cptr kerquant_ = nullptr; // kernel quantities structure returned by Fortran code
171  F2Cptr ptree_ = nullptr; // process tree returned by Fortran code
172  MPI_Fint Fcomm_; // the fortran MPI communicator
173  const MPIComm* c_;
174  int rows_ = 0, cols_ = 0, lrows_ = 0, lcols_ = 0;
175  std::vector<int> rdist_, cdist_; // begin rows/cols of each rank
176 
177  void set_dist();
178 
179  void options_init(const opts_t& opts);
180 
181  void set_extraction_meta_1grid(const VecVec_t& I, const VecVec_t& J,
182  ExtractionMeta& e,
183  int Nalldat_loc, int* pmaps) const;
184  };
185 
186  template<typename integer_t> DenseMatrix<int>
187  get_odiag_neighbors(int knn, const CSRGraph<integer_t>& gAB,
188  const CSRGraph<integer_t>& gA,
189  const CSRGraph<integer_t>& gB);
190 
191  } // end namespace HODLR
192 } // end namespace strumpack
193 
194 #endif // STRUMPACK_BUTTERFLY_MATRIX_HPP
Class wrapping around Yang Liu's HODLR code.
This is a small wrapper class around a BLACS grid and a BLACS context.
Definition: BLACSGrid.hpp:66
Definition: CompressedSparseMatrix.hpp:49
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:733
2D block cyclicly distributed matrix, as used by ScaLAPACK.
Definition: DistributedMatrix.hpp:84
Butterfly matrix representation, this includes low-rank matrix representation as a special case.
Definition: ButterflyMatrix.hpp:63
std::size_t rows() const override
Definition: ButterflyMatrix.hpp:104
std::size_t end_row() const override
Definition: ButterflyMatrix.hpp:110
std::size_t local_rows() const override
Definition: ButterflyMatrix.hpp:107
void mult(Trans op, const DenseM_t &X, DenseM_t &Y) const override
std::size_t memory() const override
Definition: ButterflyMatrix.hpp:117
std::size_t nonzeros() const override
Definition: ButterflyMatrix.hpp:118
std::size_t cols() const override
Definition: ButterflyMatrix.hpp:105
const std::vector< int > & rdist() const override
Definition: ButterflyMatrix.hpp:111
void mult(Trans op, const DistM_t &X, DistM_t &Y) const override
ButterflyMatrix(const HODLRMatrix< scalar_t > &A, const HODLRMatrix< scalar_t > &B)
std::size_t begin_row() const override
Definition: ButterflyMatrix.hpp:109
std::size_t rank() const override
Definition: ButterflyMatrix.hpp:119
const std::vector< int > & cdist() const override
Definition: ButterflyMatrix.hpp:114
Hierarchically low-rank matrix representation.
Definition: HODLRMatrix.hpp:79
Class containing several options for the HODLR code and data-structures.
Definition: HODLROptions.hpp:117
Wrapper class around an MPI_Comm object.
Definition: MPIWrapper.hpp:194
int rank() const
Definition: MPIWrapper.hpp:271
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 > & dist() const
Definition: StructuredMatrix.hpp:299
Definition: StrumpackOptions.hpp:43
Trans
Definition: DenseMatrix.hpp:51
Definition: HODLRMatrix.hpp:54