1#ifndef HILA_FIELD_IO_H_
2#define HILA_FIELD_IO_H_
12template <typename IS, std::enable_if_t<std::is_same<IS, std::ifstream>::value,
int> = 0>
13bool open_input_file(
const std::string &filename, IS &inputfile) {
16 inputfile.open(filename, std::ios::in | std::ios::binary);
17 if (inputfile.fail()) {
18 hila::out0 <<
"ERROR in opening file " << filename <<
'\n';
29template <typename OS, std::enable_if_t<std::is_same<OS, std::ofstream>::value,
int> = 0>
30bool open_output_file(
const std::string &filename, OS &outputfile,
bool binary =
true,
34 std::ios::openmode mode = std::ios::out | std::ios::trunc;
36 mode |= std::ios::binary;
38 outputfile.open(filename, mode);
39 outputfile.precision(precision);
40 if (outputfile.fail()) {
41 hila::out0 <<
"ERROR in opening file " << filename <<
'\n';
53template <typename S, std::enable_if_t<std::is_same<S, std::ofstream>::value ||
54 std::is_same<S, std::ifstream>::value,
56bool close_file(
const std::string &filename, S &fstream) {
61 hila::out0 <<
"ERROR in reading/writing file " << filename <<
'\n';
79void Field<T>::write(std::ofstream &outputfile,
bool binary,
int precision)
const {
81 constexpr size_t write_size = sites_per_write *
sizeof(T);
84 outputfile.precision(precision);
86 std::vector<CoordinateVector> coord_list(sites_per_write);
87 T *buffer = (T *)memalloc(write_size);
88 auto mylat = fs->mylattice;
91 for (
size_t i = 0; i < mylat.volume(); i += sites_per_write) {
92 size_t sites = std::min(sites_per_write, mylat.volume() - i);
93 for (
size_t j = 0; j < sites; j++)
94 coord_list[j] = mylat->global_coordinates(i + j);
96 if (sites < sites_per_write)
97 coord_list.resize(sites);
99 fs->gather_elements(buffer, coord_list);
102 outputfile.write((
char *)buffer, sites *
sizeof(T));
104 for (
size_t j = 0; j < sites; j++) {
105 outputfile << buffer[j] <<
'\n';
117 std::ofstream outputfile;
118 hila::open_output_file(filename, outputfile, binary);
119 write(outputfile, binary, precision);
120 hila::close_file(filename, outputfile);
125static void write_fields(std::ofstream &outputfile,
Field<T> &last) {
126 last.
write(outputfile);
129template <
typename T,
typename... fieldtypes>
130static void write_fields(std::ofstream &outputfile,
Field<T> &next, fieldtypes &...fields) {
131 next.
write(outputfile);
132 write_fields(outputfile, fields...);
136template <
typename... fieldtypes>
137static void write_fields(
const std::string &filename, fieldtypes &...fields) {
138 std::ofstream outputfile;
139 hila::open_output_file(filename, outputfile);
140 write_fields(outputfile, fields...);
141 hila::close_file(filename, outputfile);
150 constexpr size_t read_size = sites_per_read *
sizeof(T);
152 if (!this->is_allocated())
157 std::vector<CoordinateVector> coord_list(sites_per_read);
158 T *buffer = (T *)memalloc(read_size);
159 auto mylat = fs->mylattice;
162 for (
size_t i = 0; i < mylat.volume(); i += sites_per_read) {
163 size_t sites = std::min(sites_per_read, mylat.volume() - i);
164 for (
size_t j = 0; j < sites; j++)
165 coord_list[j] = mylat->global_coordinates(i + j);
167 if (sites < sites_per_read)
168 coord_list.resize(sites);
171 inputfile.read((
char *)buffer, sites *
sizeof(T));
173 fs->scatter_elements(buffer, coord_list);
183 constexpr size_t read_size = sites_per_read *
sizeof(T);
185 if (!this->is_allocated())
190 std::vector<CoordinateVector> coord_list(sites_per_read);
191 T *buffer = (T *)memalloc(read_size);
198 scalef[d] = lsize[d] / insize[d];
203 for (
size_t i = 0; i < tvol; i += sites_per_read) {
204 size_t sites = std::min(sites_per_read, tvol - i);
206 inputfile.read((
char *)buffer, sites *
sizeof(T));
209 for (
size_t j = 0; j < sites; j++) {
212 coord_list[j][dir] = ind % insize[dir];
217 if (sites < sites_per_read) {
218 coord_list.resize(sites);
221 fs->scatter_elements(buffer, coord_list);
225 for (
size_t sci = 1; sci < scvol; ++sci) {
226 std::vector<CoordinateVector> tcoord_list(coord_list.size());
229 int tsf = ind % scalef[dir];
231 for (
size_t j = 0; j < sites; j++) {
232 tcoord_list[j][dir] = (coord_list[j][dir] + tsf * insize[dir]) % lsize[dir];
235 fs->scatter_elements(buffer, tcoord_list);
245 std::ifstream inputfile;
246 hila::open_input_file(filename, inputfile);
248 hila::close_file(filename, inputfile);
253static void read_fields(std::ifstream &inputfile,
Field<T> &last) {
254 last.read_from_stream(inputfile);
257template <
typename T,
typename... fieldtypes>
258static void read_fields(std::ifstream &inputfile,
Field<T> &next, fieldtypes &...fields) {
259 next.read_from_stream(inputfile);
260 read_fields(inputfile, fields...);
264template <
typename... fieldtypes>
265static void read_fields(
const std::string &filename, fieldtypes &...fields) {
266 std::ifstream inputfile;
267 hila::open_input_file(filename, inputfile);
268 read_fields(inputfile, fields...);
269 hila::close_file(filename, inputfile);
288 assert(cmin[d] >= 0 && cmax[d] >= cmin[d] && cmax[d] < fs->mylattice.
size(d) &&
289 "subvolume size mismatch");
290 sites *= cmax[d] - cmin[d] + 1;
296 size_t n_write = std::min(sites_per_write, sites);
298 std::vector<CoordinateVector> coord_list(n_write);
299 T *buffer = (T *)memalloc(n_write *
sizeof(T));
306 outputfile.precision(precision);
309 forcoordinaterange(c, cmin, cmax) {
313 if (i == n_write || j == sites) {
315 coord_list.resize(i);
317 fs->gather_elements(buffer, coord_list);
320 for (
size_t k = 0; k < i; k++) {
321 for (
int l = 0; l <
sizeof(T) /
sizeof(hila::arithmetic_type<T>); l++) {
338 hila::open_output_file(filename, out,
false);
339 write_subvolume(out, cmin, cmax, precision);
340 hila::close_file(filename, out);
349template <
typename outf_type>
352 static_assert(std::is_same<outf_type, std::string>::value ||
353 std::is_same<outf_type, std::ofstream>::value,
354 "file name / output stream argument in write_slice()?");
360 cmax[d] = fs->mylattice.
size(d) - 1;
362 cmin[d] = cmax[d] = slice[d];
365 write_subvolume(outf, cmin, cmax, precision);
The field class implements the standard methods for accessing Fields. Hilapp replaces the parity acce...
void write_subvolume(std::ofstream &outputfile, const CoordinateVector &cmin, const CoordinateVector &cmax, int precision=6) const
void write(std::ofstream &outputfile, bool binary=true, int precision=8) const
Write the field to a file stream.
void read(std::ifstream &inputfile)
Read the Field from a stream.
int size(Direction d) const
lattice.size() -> CoordinateVector or lattice.size(d) -> int returns the dimensions of the lattice,...
static constexpr int size()
Returns size of Vector or square Matrix.
#define foralldir(d)
Macro to loop over (all) Direction(s)
constexpr Parity ALL
bit pattern: 011
This files containts definitions for the Field class and the classes required to define it such as fi...
Implement hila::swap for gauge fields.
int myrank()
rank of this node
std::ostream out0
This writes output only from main process (node 0)
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
void terminate(int status)
hila::arithmetic_type< T > get_number_in_var(const T &var, int i)
#define WRITE_BUFFER_SIZE