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 {
80 constexpr size_t sites_per_write = WRITE_BUFFER_SIZE /
sizeof(T);
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);
90 for (
size_t i = 0; i < lattice.volume(); i += sites_per_write) {
91 size_t sites = std::min(sites_per_write, lattice.volume() - i);
92 for (
size_t j = 0; j < sites; j++)
95 if (sites < sites_per_write)
96 coord_list.resize(sites);
98 fs->gather_elements(buffer, coord_list);
101 outputfile.write((
char *)buffer, sites *
sizeof(T));
103 for (
size_t j = 0; j < sites; j++) {
104 outputfile << buffer[j] <<
'\n';
116 std::ofstream outputfile;
117 hila::open_output_file(filename, outputfile, binary);
118 write(outputfile, binary, precision);
119 hila::close_file(filename, outputfile);
124static void write_fields(std::ofstream &outputfile,
Field<T> &last) {
125 last.
write(outputfile);
128template <
typename T,
typename... fieldtypes>
129static void write_fields(std::ofstream &outputfile,
Field<T> &next, fieldtypes &...fields) {
130 next.
write(outputfile);
131 write_fields(outputfile, fields...);
135template <
typename... fieldtypes>
136static void write_fields(
const std::string &filename, fieldtypes &...fields) {
137 std::ofstream outputfile;
138 hila::open_output_file(filename, outputfile);
139 write_fields(outputfile, fields...);
140 hila::close_file(filename, outputfile);
148 constexpr size_t sites_per_read = WRITE_BUFFER_SIZE /
sizeof(T);
149 constexpr size_t read_size = sites_per_read *
sizeof(T);
151 if (!this->is_allocated())
156 std::vector<CoordinateVector> coord_list(sites_per_read);
157 T *buffer = (T *)memalloc(read_size);
160 for (
size_t i = 0; i < lattice.volume(); i += sites_per_read) {
161 size_t sites = std::min(sites_per_read, lattice.volume() - i);
162 for (
size_t j = 0; j < sites; j++)
165 if (sites < sites_per_read)
166 coord_list.resize(sites);
169 inputfile.read((
char *)buffer, sites *
sizeof(T));
171 fs->scatter_elements(buffer, coord_list);
180 std::ifstream inputfile;
181 hila::open_input_file(filename, inputfile);
183 hila::close_file(filename, inputfile);
188static void read_fields(std::ifstream &inputfile,
Field<T> &last) {
189 last.read_from_stream(inputfile);
192template <
typename T,
typename... fieldtypes>
193static void read_fields(std::ifstream &inputfile,
Field<T> &next, fieldtypes &...fields) {
194 next.read_from_stream(inputfile);
195 read_fields(inputfile, fields...);
199template <
typename... fieldtypes>
200static void read_fields(
const std::string &filename, fieldtypes &...fields) {
201 std::ifstream inputfile;
202 hila::open_input_file(filename, inputfile);
203 read_fields(inputfile, fields...);
204 hila::close_file(filename, inputfile);
218 constexpr size_t sites_per_write = WRITE_BUFFER_SIZE /
sizeof(T);
223 assert(cmin[d] >= 0 && cmax[d] >= cmin[d] && cmax[d] < lattice.size(d) &&
224 "subvolume size mismatch");
225 sites *= cmax[d] - cmin[d] + 1;
231 size_t n_write = std::min(sites_per_write, sites);
233 std::vector<CoordinateVector> coord_list(n_write);
234 T *buffer = (T *)memalloc(n_write *
sizeof(T));
241 outputfile.precision(precision);
244 forcoordinaterange(c, cmin, cmax) {
248 if (i == n_write || j == sites) {
250 coord_list.resize(i);
252 fs->gather_elements(buffer, coord_list);
255 for (
size_t k = 0; k < i; k++) {
256 for (
int l = 0; l <
sizeof(T) /
sizeof(hila::arithmetic_type<T>); l++) {
273 hila::open_output_file(filename, out,
false);
274 write_subvolume(out, cmin, cmax, precision);
275 hila::close_file(filename, out);
284template <
typename outf_type>
287 static_assert(std::is_same<outf_type, std::string>::value ||
288 std::is_same<outf_type, std::ofstream>::value,
289 "file name / output stream argument in write_slice()?");
295 cmax[d] = lattice.size(d) - 1;
297 cmin[d] = cmax[d] = slice[d];
300 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.
CoordinateVector global_coordinates(size_t index) const
#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)