mptensor  v0.3.0
Parallel Library for Tensor Network Methods
matrix_lapack.hpp
Go to the documentation of this file.
1 /*
2  mptensor - Parallel Library for Tensor Network Methods
3 
4  Copyright 2016 Satoshi Morita
5 
6  mptensor is free software: you can redistribute it and/or modify it
7  under the terms of the GNU Lesser General Public License as
8  published by the Free Software Foundation, either version 3 of the
9  License, or (at your option) any later version.
10 
11  mptensor is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with mptensor. If not, see
18  <https://www.gnu.org/licenses/>.
19 */
20 
28 #ifndef _MATRIX_LAPACK_HPP_
29 #define _MATRIX_LAPACK_HPP_
30 
31 #include <iostream>
32 #include <vector>
33 #include <string>
34 
35 #include "../complex.hpp"
36 
37 namespace mptensor {
38 namespace lapack {
39 
42 
43 template <typename C>
44 class Matrix {
45  public:
46  typedef C value_type;
47  typedef int comm_type;
48 
49  constexpr static size_t matrix_type_tag = MATRIX_TYPE_TAG_LAPACK;
50  constexpr static char* matrix_type_name = (char*)"LAPACK";
51 
52  Matrix();
53  explicit Matrix(const comm_type& comm_dummy);
54  Matrix(size_t n_row, size_t n_col);
55  Matrix(const comm_type& comm_dummy, size_t n_row, size_t n_col);
56 
57  void init(size_t n_row, size_t n_col);
58 
59  const comm_type& get_comm() const;
60  int get_comm_size() const;
61  int get_comm_rank() const;
62 
63  void print_info(std::ostream&) const;
64 
65  const C& operator[](size_t i) const;
66  C& operator[](size_t i);
67  const C* head() const;
68  C* head();
69 
70  size_t local_size() const;
71  void global_index(size_t i, size_t& g_row, size_t& g_col) const;
72  bool local_index(size_t g_row, size_t g_col, size_t& i) const;
73  void local_position(size_t g_row, size_t g_col, int& comm_rank,
74  size_t& lindex) const;
75 
76  size_t local_row_size() const;
77  size_t local_col_size() const;
78  size_t local_row_index(size_t lindex) const;
79  size_t local_col_index(size_t lindex) const;
80  size_t global_row_index(size_t lindex_row) const;
81  size_t global_col_index(size_t lindex_col) const;
82 
83  Matrix& operator+=(const Matrix& rhs);
84  Matrix& operator-=(const Matrix& rhs);
85  Matrix& operator*=(C rhs);
86  Matrix& operator/=(C rhs);
87 
88  template <typename UnaryOperation>
89  Matrix& map(UnaryOperation op);
90 
91  std::vector<C> flatten();
92 
93  void barrier() const;
94  C allreduce_sum(C value) const;
95  template <typename D>
96  void bcast(D* buffer, int count, int root) const;
97 
98  void prep_local_to_global() const;
99  void prep_global_to_local() const;
100 
101  int n_row() const;
102  int n_col() const;
103 
104  const Matrix transpose();
105  void save_index(const std::string &filename) const;
106 
107  private:
108  std::vector<C> V;
109  int comm_;
110  int n_row_;
111  int n_col_;
112 };
113 
116 template <typename C>
117 void replace_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
118  const std::vector<size_t>& local_position,
119  Matrix<C>& M_new);
120 template <typename C>
121 void replace_matrix_data(const std::vector<C>& V,
122  const std::vector<int>& dest_rank,
123  const std::vector<size_t>& local_position,
124  Matrix<C>& M_new);
125 template <typename C>
126 void sum_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
127  const std::vector<size_t>& local_position,
128  Matrix<C>& M_new);
129 
130 template <typename C>
131 C matrix_trace(const Matrix<C>& a);
132 
133 template <typename C>
134 double max_abs(const Matrix<C>& a);
135 template <typename C>
136 double min_abs(const Matrix<C>& a);
137 
138 // The following functions are defined in matrix_lapack.cc.
139 template <typename C>
140 void matrix_product(const Matrix<C>& a, const Matrix<C>& b, Matrix<C>& c);
141 
142 template <typename C>
143 int matrix_svd(Matrix<C>& a, Matrix<C>& u, std::vector<double>& s,
144  Matrix<C>& v);
145 
146 template <typename C>
147 int matrix_svd(Matrix<C>& a, std::vector<double>& s);
148 
149 template <typename C>
151 
152 template <typename C>
153 int matrix_eigh(Matrix<C>& a, std::vector<double>& s, Matrix<C>& u);
154 
155 template <typename C>
156 int matrix_eigh(Matrix<C>& a, std::vector<double>& s);
157 
158 template <typename C>
159 int matrix_eigh(Matrix<C>& a, Matrix<C>& b, std::vector<double>& s,
160  Matrix<C>& u);
161 
162 template <typename C>
163 int matrix_eig(Matrix<C>& a, std::vector<complex>& s, Matrix<complex>& u);
164 
165 template <typename C>
166 int matrix_eig(Matrix<C>& a, std::vector<complex>& s);
167 
168 template <typename C>
170 
171 template <typename C>
172 double max(const Matrix<C>& a);
173 template <typename C>
174 double min(const Matrix<C>& a);
176 
178 
179 } // namespace lapack
180 } // namespace mptensor
181 
182 #include "matrix_lapack_impl.hpp"
183 
184 #endif // _MATRIX_LAPACK_HPP_
Non-distributed matrix using LAPACK.
Definition: matrix_lapack.hpp:44
void print_info(std::ostream &) const
Definition: matrix_lapack_impl.hpp:213
size_t global_col_index(size_t lindex_col) const
Definition: matrix_lapack_impl.hpp:200
int n_row() const
Definition: matrix_lapack_impl.hpp:137
int get_comm_size() const
Always returns 1.
Definition: matrix_lapack_impl.hpp:126
Matrix & operator/=(C rhs)
Definition: matrix_lapack_impl.hpp:238
size_t local_row_index(size_t lindex) const
Definition: matrix_lapack_impl.hpp:185
C value_type
Definition: matrix_lapack.hpp:46
Matrix & operator*=(C rhs)
Definition: matrix_lapack_impl.hpp:232
void save_index(const std::string &filename) const
Definition: matrix_lapack_impl.hpp:300
Matrix & operator+=(const Matrix &rhs)
Definition: matrix_lapack_impl.hpp:218
std::vector< C > flatten()
Definition: matrix_lapack_impl.hpp:267
constexpr static char * matrix_type_name
Definition: matrix_lapack.hpp:50
size_t local_size() const
Definition: matrix_lapack_impl.hpp:114
int get_comm_rank() const
Always returns 0.
Definition: matrix_lapack_impl.hpp:132
const comm_type & get_comm() const
Always returns 0.
Definition: matrix_lapack_impl.hpp:120
const Matrix transpose()
Definition: matrix_lapack_impl.hpp:251
Matrix & operator-=(const Matrix &rhs)
Definition: matrix_lapack_impl.hpp:225
Matrix()
Default constructor.
Definition: matrix_lapack_impl.hpp:58
void prep_local_to_global() const
Do nothing.
Definition: matrix_lapack_impl.hpp:289
size_t global_row_index(size_t lindex_row) const
Definition: matrix_lapack_impl.hpp:195
void global_index(size_t i, size_t &g_row, size_t &g_col) const
Definition: matrix_lapack_impl.hpp:147
const C & operator[](size_t i) const
Definition: matrix_lapack_impl.hpp:94
void init(size_t n_row, size_t n_col)
Definition: matrix_lapack_impl.hpp:205
size_t local_row_size() const
Definition: matrix_lapack_impl.hpp:175
size_t local_col_index(size_t lindex) const
Definition: matrix_lapack_impl.hpp:190
int comm_type
Definition: matrix_lapack.hpp:47
size_t local_col_size() const
Definition: matrix_lapack_impl.hpp:180
constexpr static size_t matrix_type_tag
Definition: matrix_lapack.hpp:49
void prep_global_to_local() const
Do nothing.
Definition: matrix_lapack_impl.hpp:295
void local_position(size_t g_row, size_t g_col, int &comm_rank, size_t &lindex) const
Convert a global index to an index of local storage.
Definition: matrix_lapack_impl.hpp:168
void bcast(D *buffer, int count, int root) const
Definition: matrix_lapack_impl.hpp:283
const C * head() const
Definition: matrix_lapack_impl.hpp:104
C allreduce_sum(C value) const
Definition: matrix_lapack_impl.hpp:277
bool local_index(size_t g_row, size_t g_col, size_t &i) const
Definition: matrix_lapack_impl.hpp:154
void barrier() const
Definition: matrix_lapack_impl.hpp:272
int n_col() const
Definition: matrix_lapack_impl.hpp:142
Matrix & map(UnaryOperation op)
std::string filename(const std::string &prefix, int proc_size)
Definition: common.hpp:32
constexpr size_t MATRIX_TYPE_TAG_LAPACK
Definition: matrix.hpp:34
Implemation of mptensor::lapack::Matrix.
int matrix_eig(Matrix< C > &a, std::vector< complex > &s, Matrix< complex > &u)
void matrix_product(const Matrix< C > &a, const Matrix< C > &b, Matrix< C > &c)
double min(const Matrix< C > &a)
double max(const Matrix< C > &a)
C matrix_trace(const Matrix< C > &a)
Definition: matrix_lapack_impl.hpp:374
int matrix_solve(Matrix< C > &a, Matrix< C > &b)
int matrix_eigh(Matrix< C > &a, std::vector< double > &s, Matrix< C > &u)
void replace_matrix_data(const Matrix< C > &M, const std::vector< int > &dest_rank, const std::vector< size_t > &local_position, Matrix< C > &M_new)
Definition: matrix_lapack_impl.hpp:323
double max_abs(const Matrix< C > &a)
Definition: matrix_lapack_impl.hpp:388
int matrix_svd(Matrix< C > &a, Matrix< C > &u, std::vector< double > &s, Matrix< C > &v)
double min_abs(const Matrix< C > &a)
Definition: matrix_lapack_impl.hpp:398
int matrix_qr(Matrix< C > &a, Matrix< C > &r)
void sum_matrix_data(const Matrix< C > &M, const std::vector< int > &dest_rank, const std::vector< size_t > &local_position, Matrix< C > &M_new)
Definition: matrix_lapack_impl.hpp:357
Definition: complex.hpp:34