HODLRMatrix.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  */
33 #ifndef STRUMPACK_HODLR_MATRIX_HPP
34 #define STRUMPACK_HODLR_MATRIX_HPP
35 
36 #include <cassert>
37 #include <functional>
38 
39 #include "kernel/Kernel.hpp"
40 #include "dense/DistributedMatrix.hpp"
41 #include "HODLROptions.hpp"
42 #include "sparse/CSRGraph.hpp"
44 
45 namespace strumpack {
46 
52  namespace HODLR {
53 
54  struct ExtractionMeta {
55  std::unique_ptr<int[]> iwork;
56  int Ninter, Nallrows, Nallcols, Nalldat_loc,
57  *allrows, *allcols, *rowids, *colids, *pgids, Npmap, *pmaps;
58  };
59 
78  template<typename scalar_t> class HODLRMatrix
79  : public structured::StructuredMatrix<scalar_t> {
85  using Vec_t = std::vector<std::size_t>;
86  using VecVec_t = std::vector<std::vector<std::size_t>>;
87  using F2Cptr = void*;
88 
89  public:
90  using real_t = typename RealType<scalar_t>::value_type;
91  using mult_t = typename std::function
92  <void(Trans, const DenseM_t&, DenseM_t&)>;
93  using delem_blocks_t = typename std::function
94  <void(VecVec_t& I, VecVec_t& J, std::vector<DistMW_t>& B,
95  ExtractionMeta&)>;
96  using elem_blocks_t = typename std::function
97  <void(VecVec_t& I, VecVec_t& J, std::vector<DenseMW_t>& B,
98  ExtractionMeta&)>;
99 
104 
117  const opts_t& opts);
118 
137  const std::function<scalar_t(int i, int j)>& Aelem,
138  const opts_t& opts);
139 
140 
162  const elem_blocks_t& Aelem, const opts_t& opts);
163 
164 
183  const std::function<
184  void(Trans op, const DenseM_t& R, DenseM_t& S)>& Amult,
185  const opts_t& opts);
186 
200  const opts_t& opts);
201 
215  template<typename integer_t>
217  const CSRGraph<integer_t>& graph, const opts_t& opts);
218 
222  HODLRMatrix(const HODLRMatrix<scalar_t>& h) = delete;
223 
228  HODLRMatrix(HODLRMatrix<scalar_t>&& h) { *this = std::move(h); }
229 
233  virtual ~HODLRMatrix();
234 
239 
245 
250  std::size_t rows() const override { return rows_; }
255  std::size_t cols() const override { return cols_; }
260  std::size_t lrows() const { return lrows_; }
265  std::size_t local_rows() const override { return lrows(); }
270  std::size_t begin_row() const override { return dist_[c_->rank()]; }
275  std::size_t end_row() const override { return dist_[c_->rank()+1]; }
282  const std::vector<int>& dist() const override { return dist_; }
283 
287  const MPIComm& Comm() const { return *c_; }
288 
293  std::size_t memory() const override {
294  return get_stat("Mem_Fill") * 1024 * 1024;
295  }
296 
297  std::size_t nonzeros() const override {
298  return memory() / sizeof(scalar_t);
299  }
300 
305  std::size_t total_memory() const {
306  return c_->all_reduce(memory(), MPI_SUM);
307  }
308 
315  std::size_t factor_memory() const {
316  return get_stat("Mem_Factor") * 1.e6;
317  }
318 
326  std::size_t total_factor_memory() const {
327  return c_->all_reduce(factor_memory(), MPI_SUM);
328  }
329 
333  std::size_t rank() const override { return get_stat("Rank_max"); }
334 
340  std::size_t max_rank() const {
341  return c_->all_reduce(rank(), MPI_MAX);
342  }
343 
344 
353  double get_stat(const std::string& name) const;
354 
355  void print_stats();
356 
368  void compress(const std::function
369  <void(Trans op, const DenseM_t& R,
370  DenseM_t& S)>& Amult);
371 
384  void compress(const std::function
385  <void(Trans op, const DenseM_t& R,
386  DenseM_t& S)>& Amult,
387  int rank_guess);
388 
396  void compress(const delem_blocks_t& Aelem);
397 
405  void compress(const elem_blocks_t& Aelem);
406 
421  void mult(Trans op, const DenseM_t& X, DenseM_t& Y) const override;
422 
435  void mult(Trans op, const DistM_t& X, DistM_t& Y) const override;
436 
443  void factor() override;
444 
457  long long int solve(const DenseM_t& B, DenseM_t& X) const;
458 
471  void solve(DenseM_t& B) const override;
472 
486  long long int solve(const DistM_t& B, DistM_t& X) const;
487 
500  void solve(DistM_t& B) const override;
501 
514  long long int inv_mult(Trans op, const DenseM_t& B, DenseM_t& X) const;
515 
516  void extract_elements(const VecVec_t& I, const VecVec_t& J,
517  std::vector<DistM_t>& B);
518  void extract_elements(const VecVec_t& I, const VecVec_t& J,
519  std::vector<DenseM_t>& B);
520 
533  void extract_elements(const Vec_t& I, const Vec_t& J, DenseM_t& B);
534 
543  DistM_t dense(const BLACSGrid* g) const;
544 
545  void set_sampling_parameter(double sample_param);
546  void set_BACA_block(int bsize);
547 
548  DenseM_t redistribute_2D_to_1D(const DistM_t& R) const;
549  void redistribute_2D_to_1D(const DistM_t& R2D, DenseM_t& R1D) const;
550  void redistribute_1D_to_2D(const DenseM_t& S1D, DistM_t& S2D) const;
551 
552  DenseM_t gather_from_1D(const DenseM_t& A) const;
553  DenseM_t all_gather_from_1D(const DenseM_t& A) const;
554  DenseM_t scatter_to_1D(const DenseM_t& A) const;
555 
560  const std::vector<int>& perm() const { return perm_; }
565  const std::vector<int>& iperm() const { return iperm_; }
566 
567  private:
568  F2Cptr ho_bf_ = nullptr; // HODLR handle returned by Fortran code
569  F2Cptr options_ = nullptr; // options structure returned by Fortran code
570  F2Cptr stats_ = nullptr; // statistics structure returned by Fortran code
571  F2Cptr msh_ = nullptr; // mesh structure returned by Fortran code
572  F2Cptr kerquant_ = nullptr; // kernel quantities structure returned by Fortran code
573  F2Cptr ptree_ = nullptr; // process tree returned by Fortran code
574  MPI_Fint Fcomm_; // the fortran MPI communicator
575  const MPIComm* c_;
576  int rows_ = 0, cols_ = 0, lrows_ = 0, lvls_ = 0;
577  std::vector<int> perm_, iperm_; // permutation used by the HODLR code
578  std::vector<int> dist_; // begin rows of each rank
579  std::vector<int> leafs_; // leaf sizes of the tree
580 
581  void options_init(const opts_t& opts);
582  void perm_init();
583  void dist_init();
584 
585  template<typename S> friend class ButterflyMatrix;
586  };
587 
588 
589  template<typename scalar_t> struct AelemCommPtrs {
590  const typename HODLRMatrix<scalar_t>::delem_blocks_t* Aelem;
591  const MPIComm* c;
592  const BLACSGrid* gl;
593  const BLACSGrid* g0;
594  };
595 
596  template<typename scalar_t> void HODLR_block_evaluation
597  (int* Ninter, int* Nallrows, int* Nallcols, int* Nalldat_loc,
598  int* allrows, int* allcols, scalar_t* alldat_loc,
599  int* rowids, int* colids, int* pgids, int* Npmap, int* pmaps,
600  void* AC);
601 
602  template<typename scalar_t> void HODLR_block_evaluation_seq
603  (int* Ninter, int* Nallrows, int* Nallcols, int* Nalldat_loc,
604  int* allrows, int* allcols, scalar_t* alldat_loc,
605  int* rowids, int* colids, int* pgids, int* Npmap, int* pmaps,
606  void* f);
607 
608  } // end namespace HODLR
609 } // end namespace strumpack
610 
611 #endif // STRUMPACK_HODLR_MATRIX_HPP
Contains the class holding HODLR matrix options.
Definitions of several kernel functions, and helper routines. Also provides driver routines for kerne...
Contains the structured matrix interfaces.
This is a small wrapper class around a BLACS grid and a BLACS context.
Definition: BLACSGrid.hpp:66
Definition: CompressedSparseMatrix.hpp:56
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:289
Definition: DistributedMatrix.hpp:52
Butterfly matrix representation, this includes low-rank matrix representation as a special case.
Definition: ButterflyMatrix.hpp:63
Hierarchically low-rank matrix representation.
Definition: HODLRMatrix.hpp:79
long long int solve(const DistM_t &B, DistM_t &X) const
void extract_elements(const Vec_t &I, const Vec_t &J, DenseM_t &B)
HODLRMatrix(const MPIComm &c, const structured::ClusterTree &tree, const opts_t &opts)
void compress(const std::function< void(Trans op, const DenseM_t &R, DenseM_t &S)> &Amult)
DistM_t dense(const BLACSGrid *g) const
void mult(Trans op, const DistM_t &X, DistM_t &Y) const override
std::size_t total_memory() const
Definition: HODLRMatrix.hpp:305
long long int inv_mult(Trans op, const DenseM_t &B, DenseM_t &X) const
const std::vector< int > & dist() const override
Definition: HODLRMatrix.hpp:282
void compress(const std::function< void(Trans op, const DenseM_t &R, DenseM_t &S)> &Amult, int rank_guess)
std::size_t end_row() const override
Definition: HODLRMatrix.hpp:275
long long int solve(const DenseM_t &B, DenseM_t &X) const
const MPIComm & Comm() const
Definition: HODLRMatrix.hpp:287
double get_stat(const std::string &name) const
HODLRMatrix(HODLRMatrix< scalar_t > &&h)
Definition: HODLRMatrix.hpp:228
std::size_t rows() const override
Definition: HODLRMatrix.hpp:250
void solve(DistM_t &B) const override
const std::vector< int > & perm() const
Definition: HODLRMatrix.hpp:560
HODLRMatrix()
Definition: HODLRMatrix.hpp:103
HODLRMatrix< scalar_t > & operator=(const HODLRMatrix< scalar_t > &h)=delete
HODLRMatrix(const MPIComm &c, const structured::ClusterTree &tree, const std::function< scalar_t(int i, int j)> &Aelem, const opts_t &opts)
std::size_t begin_row() const override
Definition: HODLRMatrix.hpp:270
std::size_t lrows() const
Definition: HODLRMatrix.hpp:260
HODLRMatrix(const MPIComm &c, const structured::ClusterTree &tree, const CSRGraph< integer_t > &graph, const opts_t &opts)
std::size_t factor_memory() const
Definition: HODLRMatrix.hpp:315
HODLRMatrix(const HODLRMatrix< scalar_t > &h)=delete
HODLRMatrix(const MPIComm &c, const structured::ClusterTree &tree, const std::function< void(Trans op, const DenseM_t &R, DenseM_t &S)> &Amult, const opts_t &opts)
HODLRMatrix< scalar_t > & operator=(HODLRMatrix< scalar_t > &&h)
HODLRMatrix(const MPIComm &c, kernel::Kernel< real_t > &K, const opts_t &opts)
std::size_t cols() const override
Definition: HODLRMatrix.hpp:255
std::size_t total_factor_memory() const
Definition: HODLRMatrix.hpp:326
void mult(Trans op, const DenseM_t &X, DenseM_t &Y) const override
std::size_t local_rows() const override
Definition: HODLRMatrix.hpp:265
HODLRMatrix(const MPIComm &c, const structured::ClusterTree &tree, const elem_blocks_t &Aelem, const opts_t &opts)
std::size_t rank() const override
Definition: HODLRMatrix.hpp:333
void compress(const delem_blocks_t &Aelem)
std::size_t max_rank() const
Definition: HODLRMatrix.hpp:340
void compress(const elem_blocks_t &Aelem)
std::size_t memory() const override
Definition: HODLRMatrix.hpp:293
void solve(DenseM_t &B) const override
const std::vector< int > & iperm() const
Definition: HODLRMatrix.hpp:565
std::size_t nonzeros() const override
Definition: HODLRMatrix.hpp:297
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:190
T all_reduce(T t, MPI_Op op) const
Definition: MPIWrapper.hpp:606
int rank() const
Definition: MPIWrapper.hpp:267
Representation of a kernel matrix.
Definition: Kernel.hpp:73
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
Definition: StrumpackOptions.hpp:42
Trans
Definition: DenseMatrix.hpp:51
Definition: HODLRMatrix.hpp:589
Definition: HODLRMatrix.hpp:54