mptensor v0.5.1
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
load.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_HPP_
30#define _MPTENSOR_LOAD_HPP_
31
32#include <cassert>
33#include <cstdio>
34#include <fstream>
35#include <string>
36
38#include "mptensor/tensor.hpp"
39
40namespace mptensor {
41
43
52template <typename MatrixType>
53void Tensor<MatrixType>::load(const std::string &filename) {
54 const bool comm_root = (get_comm_rank() == 0);
55 std::ifstream fin;
56 std::string dummy;
57 std::string version;
58 size_t ibuf[8] = {0};
59 // size_t& loaded_version_major = ibuf[0];
60 // size_t& loaded_version_minor = ibuf[1];
61 // size_t& loaded_version_patch = ibuf[2];
62 size_t& loaded_matrix_type = ibuf[3];
63 size_t& loaded_value_type = ibuf[4];
64 size_t& loaded_comm_size = ibuf[5];
65 size_t& loaded_ndim = ibuf[6];
66 size_t& loaded_urank = ibuf[7];
67 const size_t this_matrix_type = MatrixType::matrix_type_tag;
69 const size_t this_comm_size = get_comm_size();
72
73 // Read the base file
74 {
75 if (comm_root) {
76 fin.open(filename);
77 fin >> dummy >> version;
78
79 if (dummy != "mptensor") {
80 ibuf[0] = 0;
81 ibuf[1] = 2;
82 ibuf[2] = 0;
83 fin.close();
84 } else {
85 sscanf(version.c_str(), "%lu.%lu.%lu", &(ibuf[0]), &(ibuf[1]),
86 &(ibuf[2]));
87 fin >> dummy >> ibuf[3] >> dummy; // matrix_type
88 fin >> dummy >> ibuf[4] >> dummy; // value_type
89 fin >> dummy >> ibuf[5]; // comm_size
90 fin >> dummy >> ibuf[6]; // ndim
91 fin >> dummy >> ibuf[7]; // upper_rank
92 }
93 }
94 Mat.bcast(ibuf, 8, 0);
95
96 if (ibuf[0] == 0 && ibuf[1] <= 2) {
97 load_ver_0_2(filename.c_str());
98 return;
99 }
100
101 const size_t count = 2 * loaded_ndim;
102 std::vector<size_t> buffer(count);
104 if (comm_root) {
105 size_t k = 0;
106 fin >> dummy;
107 for (size_t i = 0; i < loaded_ndim; ++i) fin >> buffer[k++];
108 fin >> dummy;
109 for (size_t i = 0; i < loaded_ndim; ++i) fin >> buffer[k++];
110 fin.close();
111 }
112 Mat.bcast(buffer.data(), count, 0);
113
114 loaded_shape.assign(loaded_ndim, buffer.data());
116 }
117
119
120 // Initialize tensor shape
122
123 // Read tensor elements
127 std::clog << "Info: Load a tensor directly." << std::endl;
128 }
129 io_helper::load_binary(filename, get_comm_rank(), get_matrix().head(),
130 local_size());
131 return;
134 std::clog << "Info: Load a non-distributed tensor." << std::endl;
135 }
137 return;
140 std::clog
141 << "Info: Load a tensor distributed on different-size communicator."
142 << std::endl;
143 }
145 return;
146 }
147}
148
150
159template <typename MatrixType>
160void Tensor<MatrixType>::load_ver_0_2(const char* filename) {
161 std::ifstream fin;
162 size_t n;
163 size_t urank;
164 Shape shape;
165 Axes map;
166
167 if (get_comm_rank() == 0) {
168 std::clog << "Warning: \"" << filename
169 << "\" will be loaded using v0.2 interface." << std::endl;
170 }
171
172 // Read the base file
173 {
174 if (get_comm_rank() == 0) {
175 fin.open(filename);
176 fin >> n;
177 }
178 Mat.bcast(&n, 1, 0);
179
180 const size_t count = 2 * n + 1;
181 std::vector<size_t> buffer(count);
182
183 if (get_comm_rank() == 0) {
184 for (size_t i = 0; i < count; ++i) fin >> buffer[i];
185 fin.close();
186 }
187 Mat.bcast(buffer.data(), count, 0);
188
189 urank = buffer[0];
190 shape.assign(n, &(buffer[1]));
191 map.assign(n, &(buffer[n + 1]));
192 }
193
194 // Initialize tensor shape
195 init(shape, urank, map);
196
197 // Read tensor elements
198 {
199 std::vector<char> datafile(std::strlen(filename) + 16);
200 std::sprintf(datafile.data(), "%s.%04d", filename, get_comm_rank());
201
202 // load_binary(datafile,get_matrix().head(),local_size());
203 fin.open(datafile.data(), std::ofstream::binary);
204 fin.read(reinterpret_cast<char*>(get_matrix().head()),
205 sizeof(value_type) * local_size());
206 fin.close();
207 }
208}
209
210} // namespace mptensor
211
212#endif // _MPTENSOR_LOAD_HPP_
Definition index.hpp:39
void load(const std::string &filename)
Load a tensor from files.
Definition load.hpp:53
std::string filename(const std::string &prefix, int proc_size)
Definition common.hpp:32
std::complex< double > complex
Definition complex.hpp:38
Header file of helper functions for file io.
constexpr size_t MATRIX_TYPE_TAG_LAPACK
Definition matrix.hpp:34
constexpr size_t MATRIX_TYPE_TAG_SCALAPACK
Definition matrix.hpp:35
mptensor::complex complex
Definition matrix_lapack.cc:36
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
void load_scalapack(const std::string &prefix, int loaded_comm_size, Matrix< C > &mat)
Definition io_helper.hpp:125
Definition complex.hpp:34
Index Shape
Definition tensor.hpp:46
Index Axes
Definition tensor.hpp:45
Tensor class.