mptensor v0.5.1
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
matrix_scalapack.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_SCALAPACK_HPP_
29#define _MATRIX_SCALAPACK_HPP_
30#ifndef _NO_MPI
31
32#include <iostream>
33#include <vector>
34#include <string>
35
36#include <mpi.h>
37
38#include "mptensor/complex.hpp"
40
41namespace mptensor {
42inline namespace matrix {
43namespace scalapack {
44
47
48template <typename C>
49class Matrix {
50 public:
51 using value_type = C;
53
54 template <typename D>
56
57 constexpr static size_t matrix_type_tag = MATRIX_TYPE_TAG_SCALAPACK;
58 constexpr static char* matrix_type_name = (char*)"ScaLAPACK";
59
60 Matrix();
61 explicit Matrix(const MPI_Comm& comm);
62 Matrix(size_t n_row, size_t n_col);
63 Matrix(const MPI_Comm& comm, size_t n_row, size_t n_col);
64
65 void init(size_t n_row, size_t n_col);
66
67 const MPI_Comm& get_comm() const;
68 int get_comm_size() const;
69 int get_comm_rank() const;
70
71 void print_info(std::ostream&) const;
72
73 const C& operator[](size_t i) const;
74 C& operator[](size_t i);
75 const C* head() const;
76 C* head();
77
78 size_t local_size() const;
79 void global_index(size_t i, size_t& g_row, size_t& g_col) const;
80 bool local_index(size_t g_row, size_t g_col, size_t& i) const;
81 void local_position(size_t g_row, size_t g_col, int& comm_rank,
82 size_t& lindex) const;
83
84 size_t local_row_size() const;
85 size_t local_col_size() const;
86 size_t local_row_index(size_t lindex) const;
87 size_t local_col_index(size_t lindex) const;
88 size_t global_row_index(size_t lindex_row) const;
89 size_t global_col_index(size_t lindex_col) const;
90
91 Matrix& operator+=(const Matrix& rhs);
92 Matrix& operator-=(const Matrix& rhs);
95
96 template <typename UnaryOperation>
98
99 std::vector<C> flatten();
100
101 void barrier() const;
102 C allreduce_sum(C value) const;
103 template <typename D>
104 void bcast(D* buffer, int count, int root) const;
105
106 void prep_local_to_global() const;
107 void prep_global_to_local() const;
108
109 int n_row() const;
110 int n_col() const;
111 const int* descriptor() const;
112
113 const Matrix transpose();
114 void save_index(const std::string &filename) const;
115
116 private:
117 BlacsGrid grid;
118 std::vector<C> V; // local strage
119 std::vector<int> desc; // descriptor
120 static const size_t BLOCK_SIZE;
121 size_t local_row_size_;
122 size_t local_col_size_;
123
124 int get_lld() const;
125 // void set(C (*element)(size_t g_row, size_t g_col));
126 // const BlacsGrid &get_grid() const;
127 // int blacs_context() const;
128 // int nb_row() const;
129 // int nb_col() const;
130
131 mutable bool has_local_to_global;
132 mutable bool has_global_to_local;
133 mutable std::vector<size_t>
134 global_row;
135 mutable std::vector<size_t>
136 global_col;
137 mutable std::vector<size_t>
138 local_row;
139 mutable std::vector<size_t>
140 local_col;
141 mutable std::vector<int>
142 proc_row;
143 mutable std::vector<int>
144 proc_col;
145 mutable std::vector<int> lld_list;
147};
148
151template <typename C>
152void replace_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
153 const std::vector<size_t>& local_position,
155template <typename C>
156void replace_matrix_data(const std::vector<C>& V, const std::vector<int>& dest_rank,
157 const std::vector<size_t>& local_position,
159template <typename C>
160void sum_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
161 const std::vector<size_t>& local_position,
163
164template <typename C>
165double max_abs(const Matrix<C>& a);
166template <typename C>
167double min_abs(const Matrix<C>& a);
168template <typename C>
169C matrix_trace(const Matrix<C>& a);
170
171// defined in matrix_scalapack.cc
172template <typename C>
174template <typename C>
175int matrix_svd(Matrix<C>& a, Matrix<C>& u, std::vector<double>& s,
176 Matrix<C>& v);
177template <typename C>
178int matrix_svd(Matrix<C>& a, std::vector<double>& s);
179template <typename C>
181template <typename C>
182int matrix_eigh(Matrix<C>& a, std::vector<double>& s, Matrix<C>& u);
183template <typename C>
184int matrix_eigh(Matrix<C>& a, std::vector<double>& s);
185template <typename C>
186int matrix_eigh(Matrix<C>& a, Matrix<C>& b, std::vector<double>& s,
187 Matrix<C>& u);
188// template <typename C>
189// int matrix_eig(Matrix<C>& a, std::vector<complex>& s, Matrix<complex>& u);
190// template <typename C>
191// int matrix_eig(Matrix<C>& a, std::vector<complex>& s);
192template <typename C>
194template <typename C>
195double max(const Matrix<C>& a);
196template <typename C>
197double min(const Matrix<C>& a);
199
201
202} // namespace scalapack
203} // inline namespace matrix
204} // namespace mptensor
205
207
208#endif // _NO_MPI
209#endif // _MATRIX_SCALAPACK_HPP_
BlacsGrid class.
Definition blacsgrid.hpp:40
Distributed matrix using ScaLAPACK.
Definition matrix_scalapack.hpp:49
size_t global_row_index(size_t lindex_row) const
Definition matrix_scalapack_impl.hpp:227
Matrix & operator*=(C rhs)
Definition matrix_scalapack_impl.hpp:372
const C * head() const
Definition matrix_scalapack_impl.hpp:103
void global_index(size_t i, size_t &g_row, size_t &g_col) const
Definition matrix_scalapack_impl.hpp:165
Matrix & operator/=(C rhs)
Definition matrix_scalapack_impl.hpp:378
static constexpr char * matrix_type_name
Definition matrix_scalapack.hpp:58
C value_type
Definition matrix_scalapack.hpp:51
int n_col() const
Definition matrix_scalapack_impl.hpp:149
size_t local_size() const
Definition matrix_scalapack_impl.hpp:113
Matrix()
Definition matrix_scalapack_impl.hpp:70
int get_comm_rank() const
Definition matrix_scalapack_impl.hpp:131
const int * descriptor() const
Definition matrix_scalapack_impl.hpp:136
size_t global_col_index(size_t lindex_col) const
Definition matrix_scalapack_impl.hpp:233
Matrix & operator+=(const Matrix &rhs)
Definition matrix_scalapack_impl.hpp:358
size_t local_row_size() const
Definition matrix_scalapack_impl.hpp:207
const Matrix transpose()
Definition matrix_scalapack_impl.hpp:391
void print_info(std::ostream &) const
Definition matrix_scalapack_impl.hpp:351
void local_position(size_t g_row, size_t g_col, int &comm_rank, size_t &lindex) const
Definition matrix_scalapack_impl.hpp:197
Matrix & map(UnaryOperation op)
size_t local_row_index(size_t lindex) const
Definition matrix_scalapack_impl.hpp:217
void prep_local_to_global() const
Preprocess for fast conversion from local index to global one.
Definition matrix_scalapack_impl.hpp:240
int n_row() const
Definition matrix_scalapack_impl.hpp:144
int get_comm_size() const
Definition matrix_scalapack_impl.hpp:126
Matrix< D > rebind
Definition matrix_scalapack.hpp:55
MPI_Comm comm_type
Definition matrix_scalapack.hpp:52
void init(size_t n_row, size_t n_col)
Definition matrix_scalapack_impl.hpp:321
void prep_global_to_local() const
Preprocess for fast conversion from global index to local one.
Definition matrix_scalapack_impl.hpp:271
const MPI_Comm & get_comm() const
Definition matrix_scalapack_impl.hpp:121
void save_index(const std::string &filename) const
Definition matrix_scalapack_impl.hpp:446
std::vector< C > flatten()
Definition matrix_scalapack_impl.hpp:417
size_t local_col_size() const
Definition matrix_scalapack_impl.hpp:212
size_t local_col_index(size_t lindex) const
Definition matrix_scalapack_impl.hpp:222
C allreduce_sum(C value) const
Definition matrix_scalapack_impl.hpp:435
bool local_index(size_t g_row, size_t g_col, size_t &i) const
Definition matrix_scalapack_impl.hpp:176
void bcast(D *buffer, int count, int root) const
Definition matrix_scalapack_impl.hpp:441
static constexpr size_t matrix_type_tag
Definition matrix_scalapack.hpp:57
Matrix & operator-=(const Matrix &rhs)
Definition matrix_scalapack_impl.hpp:365
void barrier() const
Definition matrix_scalapack_impl.hpp:430
const C & operator[](size_t i) const
Definition matrix_scalapack_impl.hpp:93
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_SCALAPACK
Definition matrix.hpp:35
Implementation of scalapack::Matrix class.
C matrix_trace(const Matrix< C > &a)
Definition matrix_scalapack_impl.hpp:694
double max(const Matrix< C > &a)
void matrix_product(const Matrix< C > &a, const Matrix< C > &b, Matrix< C > &c)
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_scalapack_impl.hpp:604
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_scalapack_impl.hpp:472
double max_abs(const Matrix< C > &a)
Definition matrix_scalapack_impl.hpp:670
double min(const Matrix< C > &a)
int matrix_qr(Matrix< C > &a, Matrix< C > &r)
int matrix_solve(Matrix< C > &a, Matrix< C > &b)
int matrix_eigh(Matrix< C > &a, std::vector< double > &s, Matrix< C > &u)
double min_abs(const Matrix< C > &a)
Definition matrix_scalapack_impl.hpp:682
int matrix_svd(Matrix< C > &a, Matrix< C > &u, std::vector< double > &s, Matrix< C > &v)
Definition complex.hpp:34