mptensor  v0.3.0
Parallel Library for Tensor Network Methods
matrix_interface.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_INTERFACE_HPP_
29 #define _MATRIX_INTERFACE_HPP_
30 
31 #include <iostream>
32 #include <vector>
33 
34 namespace mptensor {
35 namespace matrix_interface {
36 
37 template <typename C>
38 class Matrix {
39  public:
40  typedef C value_type;
41  typedef int comm_type;
42 
43  constexpr static size_t matrix_type_tag = MATRIX_TYPE_TAG_INTERFACE;
44  constexpr static char* matrix_type_name = (char*)"Interface";
45 
46  Matrix();
47  explicit Matrix(const comm_type& comm);
48  Matrix(size_t n_row, size_t n_col);
49  Matrix(const comm_type& comm, size_t n_row, size_t n_col);
50 
51  void init(size_t n_row, size_t n_col);
52 
53  const comm_type& get_comm() const;
54  int get_comm_size() const;
55  int get_comm_rank() const;
56 
57  void print_info(std::ostream&) const;
58 
59  const C& operator[](size_t i) const;
60  C& operator[](size_t i);
61  const C* head() const;
62  C* head();
63 
64  size_t local_size() const;
65  void global_index(size_t i, size_t& g_row, size_t& g_col) const;
66  bool local_index(size_t g_row, size_t g_col, size_t& i) const;
67  void local_position(size_t g_row, size_t g_col, int& comm_rank,
68  size_t& lindex) const;
69 
70  size_t local_row_size() const;
71  size_t local_col_size() const;
72  size_t local_row_index(size_t lindex) const;
73  size_t local_col_index(size_t lindex) const;
74  size_t global_row_index(size_t lindex_row) const;
75  size_t global_col_index(size_t lindex_col) const;
76 
77  Matrix& operator+=(const Matrix& rhs);
78  Matrix& operator-=(const Matrix& rhs);
79  Matrix& operator*=(C rhs);
80  Matrix& operator/=(C rhs);
81 
82  template <typename UnaryOperation>
83  Matrix& map(UnaryOperation op);
84 
85  std::vector<C> flatten();
86 
87  void barrier() const;
88  C allreduce_sum(C value) const;
89  template <typename D>
90  void bcast(D* buffer, int count, int root) const;
91 
92  void prep_local_to_global() const;
93  void prep_global_to_local() const;
94 
95  void save_index(const std::string &filename) const;
96 };
97 
98 template <typename C>
99 void replace_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
100  const std::vector<size_t>& local_position,
101  Matrix<C>& M_new);
102 template <typename C>
103 void replace_matrix_data(const std::vector<C>& V,
104  const std::vector<int>& dest_rank,
105  const std::vector<size_t>& local_position,
106  Matrix<C>& M_new);
107 template <typename C>
108 void sum_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
109  const std::vector<size_t>& local_position,
110  Matrix<C>& M_new);
111 template <typename C>
113 template <typename C>
114 void matrix_product(const Matrix<C>& a, const Matrix<C>& b, Matrix<C>& c);
115 template <typename C>
116 int matrix_svd(Matrix<C>& a, Matrix<C>& u, std::vector<double>& s,
117  Matrix<C>& v);
118 template <typename C>
119 int matrix_svd(Matrix<C>& a, std::vector<double>& s);
120 template <typename C>
122 template <typename C>
123 int matrix_eigh(Matrix<C>& a, std::vector<double>& s, Matrix<C>& u);
124 template <typename C>
125 int matrix_eigh(Matrix<C>& a, std::vector<double>& s);
126 template <typename C>
127 int matrix_eigh(Matrix<C>& a, Matrix<C>& b, std::vector<double>& s, Matrix<C>& u);
128 template <typename C>
129 int matrix_eig(Matrix<C>& a, std::vector<complex>& s, Matrix<complex>& u);
130 template <typename C>
131 int matrix_eig(Matrix<C>& a, std::vector<complex>& s);
132 template <typename C>
134 
135 template <typename C>
136 double max(const Matrix<C>& a);
137 template <typename C>
138 double min(const Matrix<C>& a);
139 template <typename C>
140 double max_abs(const Matrix<C>& a);
141 template <typename C>
142 double min_abs(const Matrix<C>& a);
143 
144 } // namespace matrix_interface
145 } // namespace mptensor
146 
147 #include "matrix_interface_doc.hpp"
148 
149 #endif // _MATRIX_INTERFACE_HPP_
Inteface of Matrix class.
Definition: matrix_interface.hpp:38
std::vector< C > flatten()
Return the flattened vector.
size_t local_col_index(size_t lindex) const
size_t local_size() const
Return the number of elements in this process.
Matrix & operator+=(const Matrix &rhs)
Matrix()
Constructor of a size-zero matrix.
C value_type
double or complex
Definition: matrix_interface.hpp:40
Matrix(const comm_type &comm)
Constructor of a size-zero matrix.
C allreduce_sum(C value) const
Return the summation of a scalar. Every processes returns the same value.
void print_info(std::ostream &) const
void prep_global_to_local() const
Preprocess for fast conversion from local index to global one.
size_t global_row_index(size_t lindex_row) const
const C & operator[](size_t i) const
Const array subscript operator.
int comm_type
Set MPI_Comm for MPI programm.
Definition: matrix_interface.hpp:41
void local_position(size_t g_row, size_t g_col, int &comm_rank, size_t &lindex) const
bool local_index(size_t g_row, size_t g_col, size_t &i) const
size_t global_col_index(size_t lindex_col) const
void init(size_t n_row, size_t n_col)
void prep_local_to_global() const
Preprocess for fast conversion from local index to global one.
void save_index(const std::string &filename) const
C & operator[](size_t i)
Array subscript operator.
void global_index(size_t i, size_t &g_row, size_t &g_col) const
void bcast(D *buffer, int count, int root) const
Wrapper of MPI_Bcast.
int get_comm_size() const
Return the size of the MPI communicator.
const comm_type & get_comm() const
Return the MPI communicator.
int get_comm_rank() const
Return the MPI rank.
size_t local_row_index(size_t lindex) const
Matrix(size_t n_row, size_t n_col)
Constructor of a (n_row, n_col) matrix.
Matrix & operator-=(const Matrix &rhs)
constexpr static size_t matrix_type_tag
Definition: matrix_interface.hpp:43
Matrix & map(UnaryOperation op)
constexpr static char * matrix_type_name
Definition: matrix_interface.hpp:44
void barrier() const
Wrapper of MPI_Barrier.
Matrix(const comm_type &comm, size_t n_row, size_t n_col)
Constructor of a (n_row, n_col) matrix.
std::string filename(const std::string &prefix, int proc_size)
Definition: common.hpp:32
constexpr size_t MATRIX_TYPE_TAG_INTERFACE
Definition: matrix.hpp:33
Documentation of Matrix class interface.
int matrix_eig(Matrix< C > &a, std::vector< complex > &s, Matrix< complex > &u)
double min(const Matrix< C > &a)
Return the minimum element.
int matrix_qr(Matrix< C > &a, Matrix< C > &r)
int matrix_eigh(Matrix< C > &a, std::vector< double > &s, Matrix< C > &u)
double max_abs(const Matrix< C > &a)
Return the maximum of the absolute value of elements.
C matrix_trace(const Matrix< C > &a)
double max(const Matrix< C > &a)
Return the maximum element.
double min_abs(const Matrix< C > &a)
Return the minimum of the absolute value of elements.
int matrix_solve(Matrix< C > &a, Matrix< C > &b)
Solve linear equation .
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)
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)
void matrix_product(const Matrix< C > &a, const Matrix< C > &b, Matrix< C > &c)
int matrix_svd(Matrix< C > &a, Matrix< C > &u, std::vector< double > &s, Matrix< C > &v)
Definition: complex.hpp:34