mptensor v0.5.1
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
matrix_lapack_impl.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_IMPL_HPP_
29#define _MATRIX_LAPACK_IMPL_HPP_
30
31#include <algorithm>
32#include <cassert>
33#include <fstream>
34#include <iostream>
35#include <vector>
36#include <string>
37
39
40namespace mptensor {
41inline namespace matrix {
42
44namespace lapack {
45
47
52/* ---------- constructors ---------- */
53
55
58template <typename C>
60 init(0, 0);
61};
62
64
67template <typename C>
69 init(0, 0);
70};
71
73
77template <typename C>
78Matrix<C>::Matrix(size_t n_row, size_t n_col) {
79 init(n_row, n_col);
80};
81
83
88template <typename C>
89Matrix<C>::Matrix(const comm_type& comm_dummy, size_t n_row, size_t n_col) {
90 init(n_row, n_col);
91};
92
93/* ---------- member functions ---------- */
94template <typename C>
95inline const C& Matrix<C>::operator[](size_t i) const {
96 return V[i];
97}
98
99template <typename C>
100inline C& Matrix<C>::operator[](size_t i) {
101 return V[i];
102}
103
104template <typename C>
105inline const C* Matrix<C>::head() const {
106 return V.data();
107}
108
109template <typename C>
111 return V.data();
112}
113
114template <typename C>
115inline size_t Matrix<C>::local_size() const {
116 return V.size();
117}
118
120template <typename C>
121inline const typename Matrix<C>::comm_type& Matrix<C>::get_comm() const {
122 return comm_;
123}
124
126template <typename C>
127inline int Matrix<C>::get_comm_size() const {
128 return 1;
129}
130
132template <typename C>
133inline int Matrix<C>::get_comm_rank() const {
134 return 0;
135}
136
137template <typename C>
138inline int Matrix<C>::n_row() const {
139 return n_row_;
140}
141
142template <typename C>
143inline int Matrix<C>::n_col() const {
144 return n_col_;
145}
146
147template <typename C>
148inline void Matrix<C>::global_index(size_t i, size_t& g_row,
149 size_t& g_col) const {
150 g_row = i % n_row_;
151 g_col = i / n_row_;
152};
153
154template <typename C>
155inline bool Matrix<C>::local_index(size_t g_row, size_t g_col,
156 size_t& i) const {
157 i = g_row + g_col * n_row_; // column major
158 return true;
159};
160
162
168template <typename C>
169inline void Matrix<C>::local_position(size_t g_row, size_t g_col,
170 int& comm_rank, size_t& lindex) const {
171 comm_rank = 0;
172 lindex = g_row + g_col * n_row_;
173}
174
175template <typename C>
176inline size_t Matrix<C>::local_row_size() const {
177 return n_row_;
178}
179
180template <typename C>
181inline size_t Matrix<C>::local_col_size() const {
182 return n_col_;
183}
184
185template <typename C>
186inline size_t Matrix<C>::local_row_index(size_t lindex) const {
187 return lindex % n_row_;
188}
189
190template <typename C>
191inline size_t Matrix<C>::local_col_index(size_t lindex) const {
192 return lindex / n_row_;
193}
194
195template <typename C>
196inline size_t Matrix<C>::global_row_index(size_t lindex_row) const {
197 return lindex_row;
198}
199
200template <typename C>
201inline size_t Matrix<C>::global_col_index(size_t lindex_col) const {
202 return lindex_col;
203}
204
205template <typename C>
206inline void Matrix<C>::init(size_t n_row, size_t n_col) {
207 comm_ = 0;
208 n_row_ = n_row;
209 n_col_ = n_col;
210 V.resize(n_row * n_col);
211};
212
213template <typename C>
214inline void Matrix<C>::print_info(std::ostream& out) const {
215 out << "Matrix: local_size= " << local_size() << "\n";
216};
217
218template <typename C>
220 assert(V.size() == rhs.local_size());
221 for (size_t i = 0; i < V.size(); ++i) V[i] += rhs[i];
222 return *this;
223};
224
225template <typename C>
227 assert(V.size() == rhs.local_size());
228 for (size_t i = 0; i < V.size(); ++i) V[i] -= rhs[i];
229 return *this;
230};
231
232template <typename C>
234 for (size_t i = 0; i < V.size(); ++i) V[i] *= rhs;
235 return *this;
236};
237
238template <typename C>
240 for (size_t i = 0; i < V.size(); ++i) V[i] /= rhs;
241 return *this;
242};
243
244template <typename C>
245template <typename UnaryOperation>
247 std::transform(V.begin(), V.end(), V.begin(), op);
248 return *this;
249};
250
251template <typename C>
253 /* new matrix */
254 Matrix<C> M_new(get_comm(), n_col(), n_row());
255
256 size_t g_col, g_row, i_new;
257 for (size_t i = 0; i < local_size(); i++) {
258 global_index(i, g_row, g_col);
259 M_new.local_index(g_col, g_row, i_new);
260
261 M_new[i_new] = V[i];
262 }
263
264 return M_new;
265}
266
267template <typename C>
268inline std::vector<C> Matrix<C>::flatten() {
269 return V;
270};
271
272template <typename C>
273inline void Matrix<C>::barrier() const {
274 return;
275}
276
277template <typename C>
279 return value;
280}
281
282template <typename C>
283template <typename D>
284inline void Matrix<C>::bcast(D* buffer, int count, int root) const {
285 return;
286}
287
289template <typename C>
291 return;
292}
293
295template <typename C>
297 return;
298}
299
300template <typename C>
301void Matrix<C>::save_index(const std::string &filename) const {
302 std::ofstream fout(filename);
303 fout << "local_size= " << local_size() << "\n";
304 fout << "local_n_row= " << n_row_ << "\n";
305 fout << "local_n_col= " << n_col_ << "\n";
306 fout << "[global_row]\n";
307 for (size_t i = 0; i < n_row_; ++i) {
308 fout << " " << i;
309 if (i % 10 == 9) fout << "\n";
310 }
311 fout << "\n";
312 fout << "[global_col]\n";
313 for (size_t i = 0; i < n_col_; ++i) {
314 fout << " " << i;
315 if (i % 10 == 9) fout << "\n";
316 }
317 fout << "\n";
318 fout.close();
319}
320
321/* ---------- non-member functions ---------- */
322
323template <typename C>
324void replace_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
325 const std::vector<size_t>& local_position,
326 Matrix<C>& M_new) {
327 assert(dest_rank.size() == M.local_size());
328 assert(local_position.size() == M.local_size());
329
330 const C* mat = M.head();
331 C* mat_new = M_new.head();
332
333 for (size_t i = 0; i < M.local_size(); i++) {
334 if (dest_rank[i] == 0) {
335 mat_new[local_position[i]] = mat[i];
336 }
337 }
338}
339
340template <typename C>
341void replace_matrix_data(const std::vector<C>& V, const std::vector<int>& dest_rank,
342 const std::vector<size_t>& local_position,
343 Matrix<C>& M_new) {
344 assert(dest_rank.size() == V.size());
345 assert(local_position.size() == V.size());
346
347 const C* mat = V.data();
348 C* mat_new = M_new.head();
349
350 for (size_t i = 0; i < V.size(); i++) {
351 if (dest_rank[i] == 0) {
352 mat_new[local_position[i]] = mat[i];
353 }
354 }
355}
356
357template <typename C>
358void sum_matrix_data(const Matrix<C>& M, const std::vector<int>& dest_rank,
359 const std::vector<size_t>& local_position,
360 Matrix<C>& M_new) {
361 assert(dest_rank.size() == M.local_size());
362 assert(local_position.size() == M.local_size());
363
364 const C* mat = M.head();
365 C* mat_new = M_new.head();
366
367 for (size_t i = 0; i < M.local_size(); i++) {
368 if (dest_rank[i] == 0) {
369 mat_new[local_position[i]] += mat[i];
370 }
371 }
372}
373
374template <typename C>
376 const size_t n = A.local_size();
377 size_t g_row, g_col;
378 C val(0.0);
379
380 for (size_t i = 0; i < n; ++i) {
381 A.global_index(i, g_row, g_col);
382 if (g_row == g_col) val += A[i];
383 }
384
385 return val;
386};
387
388template <typename C>
389double max_abs(const Matrix<C>& a) {
390 const size_t n = a.local_size();
391 double val = 0.0;
392 for (size_t i = 0; i < n; ++i) {
393 val = std::max(val, std::abs(a[i]));
394 }
395 return val;
396};
397
398template <typename C>
399double min_abs(const Matrix<C>& a) {
400 const size_t n = a.local_size();
401 if (n == 0) return 0.0;
402 double val = std::abs(a[0]);
403 for (size_t i = 0; i < n; ++i) {
404 val = std::min(val, std::abs(a[i]));
405 }
406 return val;
407};
408
409} // namespace lapack
410} // inline namespace matrix
411} // namespace mptensor
412
413#endif // _MATRIX_LAPACK_IMPL_HPP_
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
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
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
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
std::complex< double > complex
Definition complex.hpp:38
Header file for mptensor::lapack::Matrix.
C matrix_trace(const Matrix< C > &a)
Definition matrix_lapack_impl.hpp:375
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_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