Loading include/dca/io/hdf5/hdf5_reader.hpp +198 −188 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ public: typedef H5::H5File file_type; // In: verbose. If true, the reader outputs a short log whenever it is executed. HDF5Reader(bool verbose = true) : my_file(NULL), my_paths(0), verbose_(verbose) {} HDF5Reader(bool verbose = true) : verbose_(verbose) {} ~HDF5Reader(); Loading @@ -49,10 +49,10 @@ public: void close_file(); void open_group(std::string name) { my_paths.push_back(name); paths_.push_back(name); } void close_group() { my_paths.pop_back(); paths_.pop_back(); } std::string get_path(); Loading @@ -62,18 +62,24 @@ public: // `execute` returns true if the object is read correctly. template <typename scalartype> bool execute(std::string name, scalartype& value); template <typename Scalartype> bool execute(const std::string& name, Scalartype& value); template <typename scalar_type> bool execute(std::string name, std::vector<scalar_type>& value); template <typename Scalar> bool execute(const std::string& name, std::vector<Scalar>& value); template <typename scalar_type> bool execute(std::string name, std::vector<std::complex<scalar_type>>& value); template <typename Scalar> bool execute(const std::string& name, std::vector<std::complex<Scalar>>& value); bool execute(std::string name, std::string& value); template <typename Scalar> bool execute(const std::string& name, std::vector<std::vector<Scalar>>& value); bool execute(std::string name, std::vector<std::string>& value); template <typename Scalar, std::size_t n> bool execute(const std::string& name, std::vector<std::array<Scalar, n>>& value); bool execute(const std::string& name, std::string& value); bool execute(const std::string& name, std::vector<std::string>& value); // TODO: Remove? (only thing that depends on domains.hpp) template <typename domain_type> Loading @@ -81,36 +87,41 @@ public: return false; } template <typename scalartype, typename domain_type> bool execute(func::function<scalartype, domain_type>& f); template <typename Scalartype, typename domain_type> bool execute(func::function<Scalartype, domain_type>& f); template <typename scalartype, typename domain_type> bool execute(std::string name, func::function<scalartype, domain_type>& f); template <typename Scalartype, typename domain_type> bool execute(const std::string& name, func::function<Scalartype, domain_type>& f); template <typename scalar_type> bool execute(std::string name, dca::linalg::Vector<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Vector<Scalar, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Vector<std::complex<Scalar>, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Matrix<std::complex<Scalar>, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A); bool execute(std::string name, io::Buffer& buff) { bool execute(const std::string& name, io::Buffer& buff) { return execute(name, static_cast<io::Buffer::Container&>(buff)); } private: bool fexists(const char* filename); bool exists(const std::string& name) const; void read(const std::string& name, H5::PredType type, void* data) const; std::vector<hsize_t> readSize(const std::string& name) const; H5::H5File* my_file; std::vector<std::string> my_paths; std::unique_ptr<H5::H5File> file_; std::vector<std::string> paths_; bool verbose_; }; Loading @@ -123,229 +134,228 @@ void HDF5Reader::from_file(arbitrary_struct_t& arbitrary_struct, std::string fil reader_obj.close_file(); } template <typename scalar_type> bool HDF5Reader::execute(std::string name, scalar_type& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, Scalar& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; if (!exists(full_name)) { return false; } read(full_name, HDF5_TYPE<Scalar>::get_PredType(), &value); return true; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, std::vector<scalar_type>& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<Scalar>& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); value.resize(dataset.getInMemDataSize() / sizeof(scalar_type)); if (!exists(full_name)) { return false; } H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 1); value.resize(dims.at(0)); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } } template <typename scalar_type> bool HDF5Reader::execute(std::string name, std::vector<std::complex<scalar_type>>& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<std::complex<Scalar>>& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); value.resize(dataset.getInMemDataSize() / sizeof(std::complex<scalar_type>)); if (!exists(full_name)) { return false; } H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); value.resize(dims.at(0)); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } } template <typename scalartype, typename domain_type> bool HDF5Reader::execute(func::function<scalartype, domain_type>& f) { return execute(f.get_name(), f); template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<std::vector<Scalar>>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } template <typename scalartype, typename domain_type> bool HDF5Reader::execute(std::string name, func::function<scalartype, domain_type>& f) { std::cout << "\n\tstart reading function : " << name; open_group(name); bool success = true; const bool equal_size = !exists(full_name + "/data"); try { std::string full_name = get_path() + "/data"; if (equal_size) { auto dims = readSize(full_name); assert(dims.size() == 2); std::vector<Scalar> linearized(dims[0] * dims[1]); H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); value.resize(dims[0]); const Scalar* read_location = linearized.data(); for (auto& v : value) { v.resize(dims[1]); std::copy_n(read_location, dims[1], v.data()); read_location += dims[1]; } } else { open_group(name); H5::DataSpace dataspace = dataset.getSpace(); int size = -1; execute("size", size); value.resize(size); H5Dread(dataset.getId(), HDF5_TYPE<scalartype>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &f(0)); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; open_group("data"); for (int i = 0; i < value.size(); ++i) { execute("row_" + std::to_string(i), value[i]); } close_group(); close_group(); return success; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Vector<scalar_type, dca::linalg::CPU>& V) { bool success = true; return true; } try { template <typename Scalar, std::size_t n> bool HDF5Reader::execute(const std::string& name, std::vector<std::array<Scalar, n>>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); V.resize(dataset.getInMemDataSize() / sizeof(scalar_type)); H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); if (dims.at(1) != n) { throw(std::length_error("Wrong array size")); } H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &V[0]); value.resize(dims[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); V.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; return true; } return success; template <typename Scalartype, typename domain_type> bool HDF5Reader::execute(func::function<Scalartype, domain_type>& f) { return execute(f.get_name(), f); } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& V) { bool success = true; try { template <typename Scalartype, typename domain_type> bool HDF5Reader::execute(const std::string& name, func::function<Scalartype, domain_type>& f) { std::string full_name = get_path() + "/" + name; H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); if (!exists(full_name)) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } V.resize(dataset.getInMemDataSize() / sizeof(std::complex<scalar_type>)); std::cout << "\n\tstart reading function : " << name; H5::DataSpace dataspace = dataset.getSpace(); // Check sizes. std::vector<hsize_t> dims; execute(name + "/" + "domain-sizes", dims); if (dims.size() != f.signature()) throw(std::length_error("The number of domains is different")); for (int i = 0; i < f.signature(); ++i) { if (dims[i] != f[i]) throw(std::length_error("The size of domain " + std::to_string(i) + " is different")); } H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &V[0]); read(full_name + "/data", HDF5_TYPE<Scalartype>::get_PredType(), f.values()); V.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; return true; } return success; template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Vector<Scalar, dca::linalg::CPU>& V) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) { bool success = true; auto dims = readSize(full_name); assert(dims.size() == 1); V.resize(dims.at(0)); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), V.ptr()); return true; } try { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Vector<std::complex<Scalar>, dca::linalg::CPU>& V) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); V.resize(dims.at(0)); std::array<hsize_t, 2> dims; dataspace.getSimpleExtentDims(dims.data(), nullptr); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), V.ptr()); std::vector<scalar_type> linearized(dims[0] * dims[1]); dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace); return true; } // HDF5 is column major, while Matrix is not major. template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } auto dims = readSize(full_name); assert(dims.size() == 2); std::vector<Scalar> linearized(dims[0] * dims[1]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); // HDF5 is column major, while Matrix is row major. A.resizeNoCopy(std::make_pair(dims[0], dims[1])); unsigned linindex = 0; for (int i = 0; i < A.nrRows(); ++i) { for (int i = 0, linindex = 0; i < A.nrRows(); ++i) { for (int j = 0; j < A.nrCols(); ++j) A(i, j) = linearized[linindex++]; } A.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; } return success; return true; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A) { bool success = true; try { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Matrix<std::complex<Scalar>, dca::linalg::CPU>& A) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); std::array<hsize_t, 3> dims; dataspace.getSimpleExtentDims(dims.data(), nullptr); A.resizeNoCopy(std::make_pair(dims[1], dims[2])); std::vector<std::complex<scalar_type>> linearized(A.nrRows() * A.nrCols()); auto dims = readSize(full_name); assert(dims.size() == 3); dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace); std::vector<std::complex<Scalar>> linearized(dims[0] * dims[1]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); // HDF5 is column major, while Matrix is not major. unsigned linindex = 0; for (int i = 0; i < A.nrRows(); ++i) for (int j = 0; j < A.nrCols(); ++j) { // HDF5 is column major, while Matrix is row major. A.resizeNoCopy(std::make_pair(dims[0], dims[1])); for (int i = 0, linindex = 0; i < A.nrRows(); ++i) { for (int j = 0; j < A.nrCols(); ++j) A(i, j) = linearized[linindex++]; } A.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; } return success; return true; } template <typename scalar_type> bool HDF5Reader::execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) { template <typename Scalar> bool HDF5Reader::execute(dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A) { return execute(A.get_name(), A); } Loading include/dca/io/hdf5/hdf5_writer.hpp +94 −114 File changed.Preview size limit exceeded, changes collapsed. Show changes include/dca/phys/dca_loop/dca_loop.hpp +25 −22 Original line number Diff line number Diff line Loading @@ -109,7 +109,7 @@ private: update_chemical_potential_type update_chemical_potential_obj; std::string file_name_; io::HDF5Writer output_file_tmp_; io::HDF5Writer output_file_; unsigned dca_iteration_ = 0; Loading @@ -135,12 +135,12 @@ DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::DcaLoop(ParametersType& update_chemical_potential_obj(parameters, MOMS, cluster_mapping_obj), output_file_tmp_(false), output_file_(false), monte_carlo_integrator_(parameters_ref, MOMS_ref) { if (concurrency.id() == concurrency.first()) { file_name_ = parameters.get_directory() + parameters.get_filename_dca(); dca::util::SignalHandler::registerFile(output_file_tmp_); dca::util::SignalHandler::registerFile(output_file_); std::cout << "\n\n\t" << __FUNCTION__ << " has started \t" << dca::util::print_time() << "\n\n"; } Loading @@ -150,18 +150,21 @@ template <typename ParametersType, typename DcaDataType, typename MCIntegratorTy void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::write() { if (concurrency.id() == concurrency.first()) { std::cout << "\n\n\t\t start writing " << file_name_ << "\t" << dca::util::print_time() << "\n\n"; io::HDF5Writer output_file; output_file.open_file(file_name_); parameters.write(output_file); MOMS.write(output_file); monte_carlo_integrator_.write(output_file); DCA_info_struct.write(output_file); output_file_.set_verbose(true); output_file.close_file(); parameters.write(output_file_); MOMS.write(output_file_); monte_carlo_integrator_.write(output_file_); DCA_info_struct.write(output_file_); output_file_.close_file(); std::error_code code; std::filesystem::remove(file_name_ + ".tmp", code); std::filesystem::rename(file_name_ + ".tmp", file_name_, code); if (code) { std::cerr << "Failed to rename file." << std::endl; } } } Loading @@ -187,7 +190,7 @@ void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::initialize() { } if (concurrency.id() == concurrency.first()) { output_file_tmp_.open_file(file_name_ + ".tmp", false); output_file_.open_file(file_name_ + ".tmp", parameters.autoresume() ? false : true); } } Loading Loading @@ -372,18 +375,18 @@ template <typename ParametersType, typename DcaDataType, typename MCIntegratorTy void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::logSelfEnergy(int i) { DCA_info_struct.last_completed_iteration = i; if (output_file_tmp_) { output_file_tmp_.open_group("functions"); output_file_tmp_.execute(MOMS.Sigma); output_file_tmp_.close_group(); if (output_file_) { output_file_.open_group("functions"); output_file_.execute(MOMS.Sigma); output_file_.close_group(); output_file_tmp_.open_group("parameters"); output_file_tmp_.open_group("physics"); output_file_tmp_.execute("chemical-potential", parameters.get_chemical_potential()); output_file_tmp_.close_group(); output_file_tmp_.close_group(); output_file_.open_group("parameters"); output_file_.open_group("physics"); output_file_.execute("chemical-potential", parameters.get_chemical_potential()); output_file_.close_group(); output_file_.close_group(); DCA_info_struct.write(output_file_tmp_); DCA_info_struct.write(output_file_); } } Loading src/io/hdf5/hdf5_reader.cpp +50 −42 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ // See CITATION.md for citation guidelines, if DCA++ is used for scientific publications. // // Author: Peter Staar (taa@zurich.ibm.com) // Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch) // // This file implements hdf5_reader.hpp. Loading @@ -19,7 +20,7 @@ namespace io { // dca::io:: HDF5Reader::~HDF5Reader() { if (my_file != NULL) if (file_) close_file(); } Loading @@ -35,56 +36,50 @@ void HDF5Reader::open_file(std::string file_name) { } } my_file = new H5::H5File(file_name.c_str(), H5F_ACC_RDONLY); file_ = std::make_unique<H5::H5File>(file_name.c_str(), H5F_ACC_RDONLY); } void HDF5Reader::close_file() { my_file->close(); delete my_file; my_file = NULL; file_->close(); file_.release(); } std::string HDF5Reader::get_path() { std::string path = "/"; for (size_t i = 0; i < my_paths.size(); i++) { path = path + my_paths[i]; for (size_t i = 0; i < paths_.size(); i++) { path = path + paths_[i]; if (i < my_paths.size() - 1) if (i < paths_.size() - 1) path = path + "/"; } return path; } bool HDF5Reader::execute(std::string name, std::string& value) //, H5File& file, std::string path) { bool HDF5Reader::execute(const std::string& name, std::string& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); if (!exists(full_name)) { return false; } value.resize(dataset.getInMemDataSize(), 'a'); auto dims = readSize(full_name); assert(dims.size() == 1); value.resize(dims.at(0)); H5::DataSpace dataspace = dataset.getSpace(); H5Dread(dataset.getId(), HDF5_TYPE<char>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<char>::get_PredType(), value.data()); return true; } catch (const H5::FileIException& err) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; bool HDF5Reader::execute(const std::string& name, std::vector<std::string>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } } bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) { open_group(name); bool success = true; try { int size = -1; execute("size", size); Loading @@ -93,21 +88,34 @@ bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) { open_group("data"); for (size_t l = 0; l < value.size(); l++) { open_group(std::to_string(l)); execute(std::to_string(l), value[l]); } close_group(); close_group(); return true; } void HDF5Reader::read(const std::string& name, H5::PredType type, void* data) const { H5::DataSet dataset = file_->openDataSet(name.c_str()); dataset.read(data, type); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; bool HDF5Reader::exists(const std::string& name) const { auto code = H5Gget_objinfo(file_->getId(), name.c_str(), 0, NULL); return code == 0; } close_group(); close_group(); return success; std::vector<hsize_t> HDF5Reader::readSize(const std::string& name) const { H5::DataSet dataset = file_->openDataSet(name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); int n_dims = dataspace.getSimpleExtentNdims(); std::vector<hsize_t> dims(n_dims); dataspace.getSimpleExtentDims(dims.data(), nullptr); return dims; } } // namespace io Loading src/io/hdf5/hdf5_writer.cpp +2 −4 Original line number Diff line number Diff line Loading @@ -98,9 +98,7 @@ void HDF5Writer::execute(const std::string& name, open_group("data"); for (int i = 0; i < value.size(); ++i) { open_group(std::to_string(i)); execute(std::to_string(i), value[i]); close_group(); } close_group(); Loading @@ -118,7 +116,7 @@ void HDF5Writer::write(const std::string& name, const std::vector<hsize_t>& dims dataspace.getSimpleExtentDims(size_check_.data(), nullptr); if (size_check_ != dims) { throw(std::out_of_range("Object size different than HDF5 dataset.")); throw(std::length_error("Object size different than HDF5 dataset.")); } dataset.write(data, type, dataspace, H5P_DEFAULT); Loading @@ -131,7 +129,7 @@ void HDF5Writer::write(const std::string& name, const std::vector<hsize_t>& dims } } bool HDF5Writer::exists(const std::string& name) { bool HDF5Writer::exists(const std::string& name) const { auto code = H5Gget_objinfo(file_id_, name.c_str(), 0, NULL); return code == 0; } Loading Loading
include/dca/io/hdf5/hdf5_reader.hpp +198 −188 Original line number Diff line number Diff line Loading @@ -34,7 +34,7 @@ public: typedef H5::H5File file_type; // In: verbose. If true, the reader outputs a short log whenever it is executed. HDF5Reader(bool verbose = true) : my_file(NULL), my_paths(0), verbose_(verbose) {} HDF5Reader(bool verbose = true) : verbose_(verbose) {} ~HDF5Reader(); Loading @@ -49,10 +49,10 @@ public: void close_file(); void open_group(std::string name) { my_paths.push_back(name); paths_.push_back(name); } void close_group() { my_paths.pop_back(); paths_.pop_back(); } std::string get_path(); Loading @@ -62,18 +62,24 @@ public: // `execute` returns true if the object is read correctly. template <typename scalartype> bool execute(std::string name, scalartype& value); template <typename Scalartype> bool execute(const std::string& name, Scalartype& value); template <typename scalar_type> bool execute(std::string name, std::vector<scalar_type>& value); template <typename Scalar> bool execute(const std::string& name, std::vector<Scalar>& value); template <typename scalar_type> bool execute(std::string name, std::vector<std::complex<scalar_type>>& value); template <typename Scalar> bool execute(const std::string& name, std::vector<std::complex<Scalar>>& value); bool execute(std::string name, std::string& value); template <typename Scalar> bool execute(const std::string& name, std::vector<std::vector<Scalar>>& value); bool execute(std::string name, std::vector<std::string>& value); template <typename Scalar, std::size_t n> bool execute(const std::string& name, std::vector<std::array<Scalar, n>>& value); bool execute(const std::string& name, std::string& value); bool execute(const std::string& name, std::vector<std::string>& value); // TODO: Remove? (only thing that depends on domains.hpp) template <typename domain_type> Loading @@ -81,36 +87,41 @@ public: return false; } template <typename scalartype, typename domain_type> bool execute(func::function<scalartype, domain_type>& f); template <typename Scalartype, typename domain_type> bool execute(func::function<Scalartype, domain_type>& f); template <typename scalartype, typename domain_type> bool execute(std::string name, func::function<scalartype, domain_type>& f); template <typename Scalartype, typename domain_type> bool execute(const std::string& name, func::function<Scalartype, domain_type>& f); template <typename scalar_type> bool execute(std::string name, dca::linalg::Vector<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Vector<Scalar, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Vector<std::complex<Scalar>, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(std::string name, dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A); template <typename Scalar> bool execute(const std::string& name, dca::linalg::Matrix<std::complex<Scalar>, dca::linalg::CPU>& A); template <typename scalar_type> bool execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A); template <typename Scalar> bool execute(dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A); bool execute(std::string name, io::Buffer& buff) { bool execute(const std::string& name, io::Buffer& buff) { return execute(name, static_cast<io::Buffer::Container&>(buff)); } private: bool fexists(const char* filename); bool exists(const std::string& name) const; void read(const std::string& name, H5::PredType type, void* data) const; std::vector<hsize_t> readSize(const std::string& name) const; H5::H5File* my_file; std::vector<std::string> my_paths; std::unique_ptr<H5::H5File> file_; std::vector<std::string> paths_; bool verbose_; }; Loading @@ -123,229 +134,228 @@ void HDF5Reader::from_file(arbitrary_struct_t& arbitrary_struct, std::string fil reader_obj.close_file(); } template <typename scalar_type> bool HDF5Reader::execute(std::string name, scalar_type& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, Scalar& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; if (!exists(full_name)) { return false; } read(full_name, HDF5_TYPE<Scalar>::get_PredType(), &value); return true; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, std::vector<scalar_type>& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<Scalar>& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); value.resize(dataset.getInMemDataSize() / sizeof(scalar_type)); if (!exists(full_name)) { return false; } H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 1); value.resize(dims.at(0)); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } } template <typename scalar_type> bool HDF5Reader::execute(std::string name, std::vector<std::complex<scalar_type>>& value) { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<std::complex<Scalar>>& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); value.resize(dataset.getInMemDataSize() / sizeof(std::complex<scalar_type>)); if (!exists(full_name)) { return false; } H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); value.resize(dims.at(0)); H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); return true; } catch (...) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } } template <typename scalartype, typename domain_type> bool HDF5Reader::execute(func::function<scalartype, domain_type>& f) { return execute(f.get_name(), f); template <typename Scalar> bool HDF5Reader::execute(const std::string& name, std::vector<std::vector<Scalar>>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } template <typename scalartype, typename domain_type> bool HDF5Reader::execute(std::string name, func::function<scalartype, domain_type>& f) { std::cout << "\n\tstart reading function : " << name; open_group(name); bool success = true; const bool equal_size = !exists(full_name + "/data"); try { std::string full_name = get_path() + "/data"; if (equal_size) { auto dims = readSize(full_name); assert(dims.size() == 2); std::vector<Scalar> linearized(dims[0] * dims[1]); H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); value.resize(dims[0]); const Scalar* read_location = linearized.data(); for (auto& v : value) { v.resize(dims[1]); std::copy_n(read_location, dims[1], v.data()); read_location += dims[1]; } } else { open_group(name); H5::DataSpace dataspace = dataset.getSpace(); int size = -1; execute("size", size); value.resize(size); H5Dread(dataset.getId(), HDF5_TYPE<scalartype>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &f(0)); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; open_group("data"); for (int i = 0; i < value.size(); ++i) { execute("row_" + std::to_string(i), value[i]); } close_group(); close_group(); return success; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Vector<scalar_type, dca::linalg::CPU>& V) { bool success = true; return true; } try { template <typename Scalar, std::size_t n> bool HDF5Reader::execute(const std::string& name, std::vector<std::array<Scalar, n>>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); V.resize(dataset.getInMemDataSize() / sizeof(scalar_type)); H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); if (dims.at(1) != n) { throw(std::length_error("Wrong array size")); } H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &V[0]); value.resize(dims[0]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), value.data()); V.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; return true; } return success; template <typename Scalartype, typename domain_type> bool HDF5Reader::execute(func::function<Scalartype, domain_type>& f) { return execute(f.get_name(), f); } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Vector<std::complex<scalar_type>, dca::linalg::CPU>& V) { bool success = true; try { template <typename Scalartype, typename domain_type> bool HDF5Reader::execute(const std::string& name, func::function<Scalartype, domain_type>& f) { std::string full_name = get_path() + "/" + name; H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); if (!exists(full_name)) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; return false; } V.resize(dataset.getInMemDataSize() / sizeof(std::complex<scalar_type>)); std::cout << "\n\tstart reading function : " << name; H5::DataSpace dataspace = dataset.getSpace(); // Check sizes. std::vector<hsize_t> dims; execute(name + "/" + "domain-sizes", dims); if (dims.size() != f.signature()) throw(std::length_error("The number of domains is different")); for (int i = 0; i < f.signature(); ++i) { if (dims[i] != f[i]) throw(std::length_error("The size of domain " + std::to_string(i) + " is different")); } H5Dread(dataset.getId(), HDF5_TYPE<scalar_type>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &V[0]); read(full_name + "/data", HDF5_TYPE<Scalartype>::get_PredType(), f.values()); V.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the vector (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; return true; } return success; template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Vector<Scalar, dca::linalg::CPU>& V) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) { bool success = true; auto dims = readSize(full_name); assert(dims.size() == 1); V.resize(dims.at(0)); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), V.ptr()); return true; } try { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Vector<std::complex<Scalar>, dca::linalg::CPU>& V) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); auto dims = readSize(full_name); assert(dims.size() == 2); V.resize(dims.at(0)); std::array<hsize_t, 2> dims; dataspace.getSimpleExtentDims(dims.data(), nullptr); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), V.ptr()); std::vector<scalar_type> linearized(dims[0] * dims[1]); dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace); return true; } // HDF5 is column major, while Matrix is not major. template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } auto dims = readSize(full_name); assert(dims.size() == 2); std::vector<Scalar> linearized(dims[0] * dims[1]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); // HDF5 is column major, while Matrix is row major. A.resizeNoCopy(std::make_pair(dims[0], dims[1])); unsigned linindex = 0; for (int i = 0; i < A.nrRows(); ++i) { for (int i = 0, linindex = 0; i < A.nrRows(); ++i) { for (int j = 0; j < A.nrCols(); ++j) A(i, j) = linearized[linindex++]; } A.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; } return success; return true; } template <typename scalar_type> bool HDF5Reader::execute(std::string name, dca::linalg::Matrix<std::complex<scalar_type>, dca::linalg::CPU>& A) { bool success = true; try { template <typename Scalar> bool HDF5Reader::execute(const std::string& name, dca::linalg::Matrix<std::complex<Scalar>, dca::linalg::CPU>& A) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); std::array<hsize_t, 3> dims; dataspace.getSimpleExtentDims(dims.data(), nullptr); A.resizeNoCopy(std::make_pair(dims[1], dims[2])); std::vector<std::complex<scalar_type>> linearized(A.nrRows() * A.nrCols()); auto dims = readSize(full_name); assert(dims.size() == 3); dataset.read(linearized.data(), HDF5_TYPE<scalar_type>::get_PredType(), dataspace); std::vector<std::complex<Scalar>> linearized(dims[0] * dims[1]); read(full_name, HDF5_TYPE<Scalar>::get_PredType(), linearized.data()); // HDF5 is column major, while Matrix is not major. unsigned linindex = 0; for (int i = 0; i < A.nrRows(); ++i) for (int j = 0; j < A.nrCols(); ++j) { // HDF5 is column major, while Matrix is row major. A.resizeNoCopy(std::make_pair(dims[0], dims[1])); for (int i = 0, linindex = 0; i < A.nrRows(); ++i) { for (int j = 0; j < A.nrCols(); ++j) A(i, j) = linearized[linindex++]; } A.set_name(name); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the function (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; } return success; return true; } template <typename scalar_type> bool HDF5Reader::execute(dca::linalg::Matrix<scalar_type, dca::linalg::CPU>& A) { template <typename Scalar> bool HDF5Reader::execute(dca::linalg::Matrix<Scalar, dca::linalg::CPU>& A) { return execute(A.get_name(), A); } Loading
include/dca/io/hdf5/hdf5_writer.hpp +94 −114 File changed.Preview size limit exceeded, changes collapsed. Show changes
include/dca/phys/dca_loop/dca_loop.hpp +25 −22 Original line number Diff line number Diff line Loading @@ -109,7 +109,7 @@ private: update_chemical_potential_type update_chemical_potential_obj; std::string file_name_; io::HDF5Writer output_file_tmp_; io::HDF5Writer output_file_; unsigned dca_iteration_ = 0; Loading @@ -135,12 +135,12 @@ DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::DcaLoop(ParametersType& update_chemical_potential_obj(parameters, MOMS, cluster_mapping_obj), output_file_tmp_(false), output_file_(false), monte_carlo_integrator_(parameters_ref, MOMS_ref) { if (concurrency.id() == concurrency.first()) { file_name_ = parameters.get_directory() + parameters.get_filename_dca(); dca::util::SignalHandler::registerFile(output_file_tmp_); dca::util::SignalHandler::registerFile(output_file_); std::cout << "\n\n\t" << __FUNCTION__ << " has started \t" << dca::util::print_time() << "\n\n"; } Loading @@ -150,18 +150,21 @@ template <typename ParametersType, typename DcaDataType, typename MCIntegratorTy void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::write() { if (concurrency.id() == concurrency.first()) { std::cout << "\n\n\t\t start writing " << file_name_ << "\t" << dca::util::print_time() << "\n\n"; io::HDF5Writer output_file; output_file.open_file(file_name_); parameters.write(output_file); MOMS.write(output_file); monte_carlo_integrator_.write(output_file); DCA_info_struct.write(output_file); output_file_.set_verbose(true); output_file.close_file(); parameters.write(output_file_); MOMS.write(output_file_); monte_carlo_integrator_.write(output_file_); DCA_info_struct.write(output_file_); output_file_.close_file(); std::error_code code; std::filesystem::remove(file_name_ + ".tmp", code); std::filesystem::rename(file_name_ + ".tmp", file_name_, code); if (code) { std::cerr << "Failed to rename file." << std::endl; } } } Loading @@ -187,7 +190,7 @@ void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::initialize() { } if (concurrency.id() == concurrency.first()) { output_file_tmp_.open_file(file_name_ + ".tmp", false); output_file_.open_file(file_name_ + ".tmp", parameters.autoresume() ? false : true); } } Loading Loading @@ -372,18 +375,18 @@ template <typename ParametersType, typename DcaDataType, typename MCIntegratorTy void DcaLoop<ParametersType, DcaDataType, MCIntegratorType>::logSelfEnergy(int i) { DCA_info_struct.last_completed_iteration = i; if (output_file_tmp_) { output_file_tmp_.open_group("functions"); output_file_tmp_.execute(MOMS.Sigma); output_file_tmp_.close_group(); if (output_file_) { output_file_.open_group("functions"); output_file_.execute(MOMS.Sigma); output_file_.close_group(); output_file_tmp_.open_group("parameters"); output_file_tmp_.open_group("physics"); output_file_tmp_.execute("chemical-potential", parameters.get_chemical_potential()); output_file_tmp_.close_group(); output_file_tmp_.close_group(); output_file_.open_group("parameters"); output_file_.open_group("physics"); output_file_.execute("chemical-potential", parameters.get_chemical_potential()); output_file_.close_group(); output_file_.close_group(); DCA_info_struct.write(output_file_tmp_); DCA_info_struct.write(output_file_); } } Loading
src/io/hdf5/hdf5_reader.cpp +50 −42 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ // See CITATION.md for citation guidelines, if DCA++ is used for scientific publications. // // Author: Peter Staar (taa@zurich.ibm.com) // Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch) // // This file implements hdf5_reader.hpp. Loading @@ -19,7 +20,7 @@ namespace io { // dca::io:: HDF5Reader::~HDF5Reader() { if (my_file != NULL) if (file_) close_file(); } Loading @@ -35,56 +36,50 @@ void HDF5Reader::open_file(std::string file_name) { } } my_file = new H5::H5File(file_name.c_str(), H5F_ACC_RDONLY); file_ = std::make_unique<H5::H5File>(file_name.c_str(), H5F_ACC_RDONLY); } void HDF5Reader::close_file() { my_file->close(); delete my_file; my_file = NULL; file_->close(); file_.release(); } std::string HDF5Reader::get_path() { std::string path = "/"; for (size_t i = 0; i < my_paths.size(); i++) { path = path + my_paths[i]; for (size_t i = 0; i < paths_.size(); i++) { path = path + paths_[i]; if (i < my_paths.size() - 1) if (i < paths_.size() - 1) path = path + "/"; } return path; } bool HDF5Reader::execute(std::string name, std::string& value) //, H5File& file, std::string path) { bool HDF5Reader::execute(const std::string& name, std::string& value) { std::string full_name = get_path() + "/" + name; try { H5::DataSet dataset = my_file->openDataSet(full_name.c_str()); if (!exists(full_name)) { return false; } value.resize(dataset.getInMemDataSize(), 'a'); auto dims = readSize(full_name); assert(dims.size() == 1); value.resize(dims.at(0)); H5::DataSpace dataspace = dataset.getSpace(); H5Dread(dataset.getId(), HDF5_TYPE<char>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT, &value[0]); read(full_name, HDF5_TYPE<char>::get_PredType(), value.data()); return true; } catch (const H5::FileIException& err) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; bool HDF5Reader::execute(const std::string& name, std::vector<std::string>& value) { std::string full_name = get_path() + "/" + name; if (!exists(full_name)) { return false; } } bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) { open_group(name); bool success = true; try { int size = -1; execute("size", size); Loading @@ -93,21 +88,34 @@ bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) { open_group("data"); for (size_t l = 0; l < value.size(); l++) { open_group(std::to_string(l)); execute(std::to_string(l), value[l]); } close_group(); close_group(); return true; } void HDF5Reader::read(const std::string& name, H5::PredType type, void* data) const { H5::DataSet dataset = file_->openDataSet(name.c_str()); dataset.read(data, type); } catch (const H5::FileIException& err) { std::cout << "\n\n\t the variable (" + name + ") does not exist in path : " + get_path() + "\n\n"; success = false; bool HDF5Reader::exists(const std::string& name) const { auto code = H5Gget_objinfo(file_->getId(), name.c_str(), 0, NULL); return code == 0; } close_group(); close_group(); return success; std::vector<hsize_t> HDF5Reader::readSize(const std::string& name) const { H5::DataSet dataset = file_->openDataSet(name.c_str()); H5::DataSpace dataspace = dataset.getSpace(); int n_dims = dataspace.getSimpleExtentNdims(); std::vector<hsize_t> dims(n_dims); dataspace.getSimpleExtentDims(dims.data(), nullptr); return dims; } } // namespace io Loading
src/io/hdf5/hdf5_writer.cpp +2 −4 Original line number Diff line number Diff line Loading @@ -98,9 +98,7 @@ void HDF5Writer::execute(const std::string& name, open_group("data"); for (int i = 0; i < value.size(); ++i) { open_group(std::to_string(i)); execute(std::to_string(i), value[i]); close_group(); } close_group(); Loading @@ -118,7 +116,7 @@ void HDF5Writer::write(const std::string& name, const std::vector<hsize_t>& dims dataspace.getSimpleExtentDims(size_check_.data(), nullptr); if (size_check_ != dims) { throw(std::out_of_range("Object size different than HDF5 dataset.")); throw(std::length_error("Object size different than HDF5 dataset.")); } dataset.write(data, type, dataspace, H5P_DEFAULT); Loading @@ -131,7 +129,7 @@ void HDF5Writer::write(const std::string& name, const std::vector<hsize_t>& dims } } bool HDF5Writer::exists(const std::string& name) { bool HDF5Writer::exists(const std::string& name) const { auto code = H5Gget_objinfo(file_id_, name.c_str(), 0, NULL); return code == 0; } Loading