mptensor  v0.3.0
Parallel Library for Tensor Network Methods
save.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_SAVE_HPP_
30 #define _MPTENSOR_SAVE_HPP_
31 
32 #include <cstdio>
33 #include <fstream>
34 
36 #include "mptensor/matrix.hpp"
37 #include "mptensor/tensor.hpp"
38 #include "mptensor/version.hpp"
39 
40 namespace mptensor {
41 
43 
52 template <template <typename> class Matrix, typename C>
53 void Tensor<Matrix, C>::save(const std::string &filename) const {
54  // Create the base file
55  if (get_comm_rank() == 0) {
56  size_t n = ndim();
57  std::ofstream fout(filename);
58  fout << "mptensor " << MPTENSOR_VERSION_STRING << "\n";
59  fout << "matrix_type= " << Matrix<C>::matrix_type_tag;
60  fout << " (" << Matrix<C>::matrix_type_name << ")\n";
61  fout << "value_type= " << value_type_tag<C>();
62  fout << " (" << value_type_name<C>() << ")\n";
63  fout << "comm_size= " << get_comm_size() << "\n";
64  fout << "ndim= " << n << "\n";
65  fout << "upper_rank= " << upper_rank << "\n";
66 
67  fout << "shape=";
68  for (size_t i = 0; i < n; ++i) fout << " " << Dim[i];
69  fout << "\n";
70 
71  fout << "axes_map=";
72  for (size_t i = 0; i < n; ++i) fout << " " << axes_map[i];
73  fout << "\n";
74 
75  fout.close();
76  }
77 
78  // Save tensor elements
79  {
80  std::ofstream fout(io_helper::binary_filename(filename, get_comm_rank()),
81  std::ofstream::binary);
82  fout.write(reinterpret_cast<const char *>(get_matrix().head()),
83  sizeof(C) * local_size());
84  fout.close();
85  }
86 
87  // Save additional information of matrix
88  {
89  std::string indexfile;
90  indexfile = io_helper::index_filename(filename, get_comm_rank());
91  get_matrix().save_index(indexfile.c_str());
92  }
93 }
94 
96 
105 template <template <typename> class Matrix, typename C>
106 void Tensor<Matrix, C>::save_ver_0_2(const char *filename) const {
107  std::ofstream fout;
108 
109  // Create the base file
110  if (get_comm_rank() == 0) {
111  fout.open(filename);
112  size_t n = ndim();
113  fout << n << "\n" << upper_rank << "\n";
114  for (size_t i = 0; i < n - 1; ++i) fout << Dim[i] << " ";
115  fout << Dim[n - 1] << "\n";
116  for (size_t i = 0; i < n - 1; ++i) fout << axes_map[i] << " ";
117  fout << axes_map[n - 1] << "\n";
118  fout.close();
119  }
120 
121  // Save tensor elements
122  {
123  char *datafile = new char[std::strlen(filename) + 16];
124  sprintf(datafile, "%s.%04d", filename, get_comm_rank());
125 
126  // save_binary(datafile,get_matrix().head(),local_size());
127  std::ofstream fout(datafile, std::ofstream::binary);
128  fout.write(reinterpret_cast<const char *>(get_matrix().head()),
129  sizeof(C) * local_size());
130  fout.close();
131 
132  delete[] datafile;
133  }
134 }
135 
136 } // namespace mptensor
137 
138 #endif // _MPTENSOR_SAVE_HPP_
Tensor class. The main object of mptensor.
Definition: tensor.hpp:54
void save(const std::string &filename) const
Save a tensor to files.
Definition: save.hpp:53
std::string filename(const std::string &prefix, int proc_size)
Definition: common.hpp:32
Header file of helper functions for file io.
List of header files for matrix classes.
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
Definition: complex.hpp:34
Tensor class.
Version of mptensor.
#define MPTENSOR_VERSION_STRING
Definition: version.hpp:35