mptensor  v0.3.0
Parallel Library for Tensor Network Methods
io_helper.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 
29 #ifndef _MPTENSOR_LOAD_HELPER_HPP_
30 #define _MPTENSOR_LOAD_HELPER_HPP_
31 
32 #include <fstream>
33 #include <iomanip>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 #include "mptensor/matrix.hpp"
39 
40 namespace mptensor {
41 namespace io_helper {
42 
43 #if defined(NDEBUG)
44 constexpr bool debug = false;
45 #else
46 constexpr bool debug = true;
47 #endif
48 
49 inline std::string binary_filename(const std::string& prefix, int comm_rank) {
50  std::ostringstream ss;
51  ss << prefix << ".";
52  ss << std::setw(4) << std::setfill('0') << comm_rank;
53  ss << ".bin";
54  return ss.str();
55 }
56 
57 inline std::string index_filename(const std::string& prefix, int comm_rank) {
58  std::ostringstream ss;
59  ss << prefix << ".";
60  ss << std::setw(4) << std::setfill('0') << comm_rank;
61  ss << ".idx";
62  return ss.str();
63 }
64 
65 template <typename C>
66 void load_binary(const std::string& prefix, int comm_rank, C* data_head,
67  std::size_t local_size) {
68  std::string filename;
69  filename = io_helper::binary_filename(prefix, comm_rank);
70  std::ifstream fin(filename, std::ofstream::binary);
71  assert(fin.is_open());
72  fin.read(reinterpret_cast<char*>(data_head), sizeof(C) * local_size);
73  fin.close();
74 }
75 
76 template <template <typename> class Matrix, typename C>
77 void load_local_files(const std::string& prefix, int comm_rank,
78  size_t& local_size, std::vector<int>& dest_rank,
79  std::vector<size_t>& local_idx, std::vector<C>& data,
80  const Matrix<C>& mat) {
81  std::vector<size_t> g_row;
82  std::vector<size_t> g_col;
83  size_t local_n_row;
84  size_t local_n_col;
85 
86  {
87  std::string dummy;
88  std::string filename;
89  filename = io_helper::index_filename(prefix, comm_rank);
90  std::ifstream fin(filename);
91  assert(fin.is_open());
92 
93  fin >> dummy >> local_size;
94  fin >> dummy >> local_n_row;
95  fin >> dummy >> local_n_col;
96  assert(local_size == local_n_row * local_n_col);
97 
98  g_row.resize(local_n_row);
99  g_col.resize(local_n_col);
100 
101  fin >> dummy;
102  for (size_t i = 0; i < local_n_row; ++i) {
103  fin >> g_row[i];
104  }
105  fin >> dummy;
106  for (size_t i = 0; i < local_n_col; ++i) {
107  fin >> g_col[i];
108  }
109  fin.close();
110  }
111 
112  dest_rank.resize(local_size);
113  local_idx.resize(local_size);
114  for (size_t i = 0; i < local_size; ++i) {
115  size_t i_row = i % local_n_row;
116  size_t i_col = i / local_n_row;
117  mat.local_position(g_row[i_row], g_col[i_col], dest_rank[i], local_idx[i]);
118  }
119 
120  data.resize(local_size);
121  load_binary(prefix, comm_rank, &(data[0]), local_size);
122 }
123 
124 template <template <typename> class Matrix, typename C>
125 void load_scalapack(const std::string& prefix, int loaded_comm_size,
126  Matrix<C>& mat) {
127  const size_t this_comm_size = mat.get_comm_size();
128  const size_t this_comm_rank = mat.get_comm_rank();
129  size_t local_size;
130  std::vector<int> dest_rank;
131  std::vector<size_t> local_idx;
132  std::vector<C> data;
133  int n = loaded_comm_size / this_comm_size;
134  if (loaded_comm_size % this_comm_size > 0) n += 1;
135  for (int i = 0; i < n; ++i) {
136  int comm_rank = this_comm_rank + i * this_comm_size;
137  if (comm_rank < loaded_comm_size) {
138  load_local_files(prefix, comm_rank, local_size, dest_rank, local_idx,
139  data, mat);
140  } else {
141  local_size = 0;
142  local_idx.resize(local_size);
143  dest_rank.resize(local_size);
144  data.resize(local_size);
145  }
146  replace_matrix_data(data, dest_rank, local_idx, mat);
147  }
148 }
149 
150 } // namespace io_helper
151 } // namespace mptensor
152 #endif // _MPTENSOR_IO_HELPER_HPP_
std::string filename(const std::string &prefix, int proc_size)
Definition: common.hpp:32
List of header files for matrix classes.
void load_local_files(const std::string &prefix, int comm_rank, size_t &local_size, std::vector< int > &dest_rank, std::vector< size_t > &local_idx, std::vector< C > &data, const Matrix< C > &mat)
Definition: io_helper.hpp:77
constexpr bool debug
Definition: io_helper.hpp:46
void load_binary(const std::string &prefix, int comm_rank, C *data_head, std::size_t local_size)
Definition: io_helper.hpp:66
std::string index_filename(const std::string &prefix, int comm_rank)
Definition: io_helper.hpp:57
std::string binary_filename(const std::string &prefix, int comm_rank)
Definition: io_helper.hpp:49
void load_scalapack(const std::string &prefix, int loaded_comm_size, Matrix< C > &mat)
Definition: io_helper.hpp:125
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
Definition: complex.hpp:34