mptensor v0.5.1
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
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 "mptensor/complex.hpp"
36
37namespace mptensor {
38inline namespace matrix {
39namespace lapack {
40
43
44template <typename C>
45class Matrix {
46 public:
47 using value_type = C;
48 using comm_type = int;
49
50 template <typename D>
52
53 constexpr static size_t matrix_type_tag = MATRIX_TYPE_TAG_LAPACK;
54 constexpr static char* matrix_type_name = (char*)"LAPACK";
55
56 Matrix();
57 explicit Matrix(const comm_type& comm_dummy);
58 Matrix(size_t n_row, size_t n_col);
59 Matrix(const comm_type& comm_dummy, size_t n_row, size_t n_col);
60
61 void init(size_t n_row, size_t n_col);
62
63 const comm_type& get_comm() const;
64 int get_comm_size() const;
65 int get_comm_rank() const;
66
67 void print_info(std::ostream&) const;
68
69 const C& operator[](size_t i) const;
70 C& operator[](size_t i);
71 const C* head() const;
72 C* head();
73
74 size_t local_size() const;
75 void global_index(size_t i, size_t& g_row, size_t& g_col) const;
76 bool local_index(size_t g_row, size_t g_col, size_t& i) const;
77 void local_position(size_t g_row, size_t g_col, int& comm_rank,
78 size_t& lindex) const;
79
80 size_t local_row_size() const;
81 size_t local_col_size() const;
82 size_t local_row_index(size_t lindex) const;
83 size_t local_col_index(size_t lindex) const;
84 size_t global_row_index(size_t lindex_row) const;
85 size_t global_col_index(size_t lindex_col) const;
86
87 Matrix& operator+=(const Matrix& rhs);
88 Matrix& operator-=(const Matrix& rhs);
91
92 template <typename UnaryOperation>
94
95 std::vector<C> flatten();
96
97 void barrier() const;
98 C allreduce_sum(C value) const;
99 template <typename D>
100 void bcast(D* buffer, int count, int root) const;
101
102 void prep_local_to_global() const;
103 void prep_global_to_local() const;
104
105 int n_row() const;
106 int n_col() const;
107
108 const Matrix transpose();
109 void save_index(const std::string &filename) const;
110
111 private:
112 std::vector<C> V;
113 int comm_;
114 int n_row_;
115 int n_col_;
116};
117
120template <typename C>
121void replace_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
122 const std::vector<size_t>& local_position,
124template <typename C>
125void replace_matrix_data(const std::vector<C>& V,
126 const std::vector<int>& dest_rank,
127 const std::vector<size_t>& local_position,
129template <typename C>
130void sum_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
131 const std::vector<size_t>& local_position,
133
134template <typename C>
135C matrix_trace(const Matrix<C>& a);
136
137template <typename C>
138double max_abs(const Matrix<C>& a);
139template <typename C>
140double min_abs(const Matrix<C>& a);
141
142// The following functions are defined in matrix_lapack.cc.
143template <typename C>
145
146template <typename C>
147int matrix_svd(Matrix<C>& a, Matrix<C>& u, std::vector<double>& s,
148 Matrix<C>& v);
149
150template <typename C>
151int matrix_svd(Matrix<C>& a, std::vector<double>& s);
152
153template <typename C>
155
156template <typename C>
157int matrix_eigh(Matrix<C>& a, std::vector<double>& s, Matrix<C>& u);
158
159template <typename C>
160int matrix_eigh(Matrix<C>& a, std::vector<double>& s);
161
162template <typename C>
163int matrix_eigh(Matrix<C>& a, Matrix<C>& b, std::vector<double>& s,
164 Matrix<C>& u);
165
166template <typename C>
167int matrix_eig(Matrix<C>& a, std::vector<complex>& s, Matrix<complex>& u);
168
169template <typename C>
170int matrix_eig(Matrix<C>& a, std::vector<complex>& s);
171
172template <typename C>
174
175template <typename C>
176double max(const Matrix<C>& a);
177template <typename C>
178double min(const Matrix<C>& a);
180
182
183} // namespace lapack
184} // inline namespace matrix
185} // namespace mptensor
186
188
189#endif // _MATRIX_LAPACK_HPP_
Non-distributed matrix using LAPACK.
Definition matrix_lapack.hpp:45
void init(size_t n_row, size_t n_col)
Definition matrix_lapack_impl.hpp:206
void print_info(std::ostream &) const
Definition matrix_lapack_impl.hpp:214
void barrier() const
Definition matrix_lapack_impl.hpp:273
std::vector< C > flatten()
Definition matrix_lapack_impl.hpp:268
C value_type
Definition matrix_lapack.hpp:47
size_t global_col_index(size_t lindex_col) const
Definition matrix_lapack_impl.hpp:201
void prep_local_to_global() const
Do nothing.
Definition matrix_lapack_impl.hpp:290
size_t local_size() const
Definition matrix_lapack_impl.hpp:115
size_t local_col_size() const
Definition matrix_lapack_impl.hpp:181
static constexpr char * matrix_type_name
Definition matrix_lapack.hpp:54
static constexpr size_t matrix_type_tag
Definition matrix_lapack.hpp:53
bool local_index(size_t g_row, size_t g_col, size_t &i) const
Definition matrix_lapack_impl.hpp:155
C allreduce_sum(C value) const
Definition matrix_lapack_impl.hpp:278
int get_comm_size() const
Always returns 1.
Definition matrix_lapack_impl.hpp:127
void bcast(D *buffer, int count, int root) const
Definition matrix_lapack_impl.hpp:284
Matrix & operator+=(const Matrix &rhs)
Definition matrix_lapack_impl.hpp:219
const Matrix transpose()
Definition matrix_lapack_impl.hpp:252
Matrix< D > rebind
Definition matrix_lapack.hpp:51
const C * head() const
Definition matrix_lapack_impl.hpp:105
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:169
Matrix & map(UnaryOperation op)
Matrix & operator/=(C rhs)
Definition matrix_lapack_impl.hpp:239
int get_comm_rank() const
Always returns 0.
Definition matrix_lapack_impl.hpp:133
Matrix & operator*=(C rhs)
Definition matrix_lapack_impl.hpp:233
int n_row() const
Definition matrix_lapack_impl.hpp:138
size_t local_row_size() const
Definition matrix_lapack_impl.hpp:176
const C & operator[](size_t i) const
Definition matrix_lapack_impl.hpp:95
int n_col() const
Definition matrix_lapack_impl.hpp:143
size_t global_row_index(size_t lindex_row) const
Definition matrix_lapack_impl.hpp:196
size_t local_row_index(size_t lindex) const
Definition matrix_lapack_impl.hpp:186
int comm_type
Definition matrix_lapack.hpp:48
void global_index(size_t i, size_t &g_row, size_t &g_col) const
Definition matrix_lapack_impl.hpp:148
Matrix()
Default constructor.
Definition matrix_lapack_impl.hpp:59
size_t local_col_index(size_t lindex) const
Definition matrix_lapack_impl.hpp:191
Matrix & operator-=(const Matrix &rhs)
Definition matrix_lapack_impl.hpp:226
void save_index(const std::string &filename) const
Definition matrix_lapack_impl.hpp:301
const comm_type & get_comm() const
Always returns 0.
Definition matrix_lapack_impl.hpp:121
void prep_global_to_local() const
Do nothing.
Definition matrix_lapack_impl.hpp:296
std::string filename(const std::string &prefix, int proc_size)
Definition common.hpp:32
Define the type of a complex number.
std::complex< double > complex
Definition complex.hpp:38
constexpr size_t MATRIX_TYPE_TAG_LAPACK
Definition matrix.hpp:34
Implemation of mptensor::lapack::Matrix.
int matrix_solve(Matrix< C > &a, Matrix< C > &b)
int matrix_eig(Matrix< C > &a, std::vector< complex > &s, Matrix< complex > &u)
C matrix_trace(const Matrix< C > &a)
Definition matrix_lapack_impl.hpp:375
int matrix_eigh(Matrix< C > &a, std::vector< double > &s, Matrix< C > &u)
int matrix_svd(Matrix< C > &a, Matrix< C > &u, std::vector< double > &s, Matrix< C > &v)
int matrix_qr(Matrix< C > &a, Matrix< C > &r)
double max_abs(const Matrix< C > &a)
Definition matrix_lapack_impl.hpp:389
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:324
double min(const Matrix< C > &a)
double max(const Matrix< C > &a)
void matrix_product(const Matrix< C > &a, const Matrix< C > &b, Matrix< C > &c)
double min_abs(const Matrix< C > &a)
Definition matrix_lapack_impl.hpp:399
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:358
Definition complex.hpp:34