mirror of
https://github.com/triqs/dft_tools
synced 2024-12-25 05:43:40 +01:00
4af1afbdaf
- For users : only change is : H5::H5File in apps. to be replaced by triqs::h5::file, same API. - using only the C API because : - it is cleaner, better documented, more examples. - it is the native hdf5 interface. - simplify the installation e.g. on mac. Indeed, hdf5 is usually installed without C++ interface, which is optional. E.g. EPD et al., brew by default. Also the infamous mpi+ hdf5_cpp bug, for which we have no clean solution. - clean the notion of parent of a group. Not needed, better iterate function in C LT API. - modified doc : no need for C++ bindings any more. - modified cmake to avoid requiring CPP bindings.
86 lines
2.8 KiB
C++
86 lines
2.8 KiB
C++
/*******************************************************************************
|
|
*
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
*
|
|
* Copyright (C) 2012 by M. Ferrero, O. Parcollet
|
|
*
|
|
* TRIQS is free software: you can redistribute it and/or modify it under the
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
******************************************************************************/
|
|
#pragma once
|
|
#include "./base.hpp"
|
|
|
|
//#define CHECK_OR_THROW(Cond, Mess)
|
|
#define CHECK_OR_THROW(Cond, Mess) \
|
|
if (!(Cond)) TRIQS_RUNTIME_ERROR << "Error in h5 (de)serialization " << Mess;
|
|
|
|
namespace triqs {
|
|
namespace h5 {
|
|
|
|
template <typename T> std::string serialize(T const &x) {
|
|
|
|
proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
|
|
CHECK_OR_THROW((fapl >= 0), "creating fapl");
|
|
|
|
auto err = H5Pset_fapl_core(fapl, (size_t)(64 * 1024), false);
|
|
CHECK_OR_THROW((err >= 0), "setting core file driver in fapl.");
|
|
|
|
h5::file f = H5Fcreate("serialization", 0, H5P_DEFAULT, fapl);
|
|
CHECK_OR_THROW((f.is_valid()), "created core file");
|
|
|
|
auto gr = triqs::h5::group(f);
|
|
h5_write(gr, "object", x);
|
|
|
|
err = H5Fflush(f, H5F_SCOPE_GLOBAL);
|
|
CHECK_OR_THROW((err >= 0), "flushed core file.");
|
|
|
|
ssize_t image_len = H5Fget_file_image(f, NULL, (size_t)0);
|
|
CHECK_OR_THROW((image_len > 0), "got image file size");
|
|
|
|
std::string buf(image_len, 0);
|
|
|
|
ssize_t bytes_read = H5Fget_file_image(f, (void *)&buf[0], (size_t)image_len);
|
|
CHECK_OR_THROW(bytes_read == image_len, "wrote file into image buffer");
|
|
|
|
return buf;
|
|
}
|
|
|
|
// -----------------------------
|
|
|
|
template <typename T> T deserialize(std::string const &buf) {
|
|
T res;
|
|
|
|
proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
|
|
CHECK_OR_THROW((fapl >= 0), "creating fapl");
|
|
|
|
auto err = H5Pset_fapl_core(fapl, (size_t)(64 * 1024), false);
|
|
CHECK_OR_THROW((err >= 0), "setting core file driver in fapl.");
|
|
|
|
err = H5Pset_file_image(fapl, (void *)&buf[0], buf.size());
|
|
CHECK_OR_THROW((err >= 0), "set file image in fapl.");
|
|
|
|
h5::file f = H5Fopen("serialization", H5F_ACC_RDONLY, fapl);
|
|
CHECK_OR_THROW((f.is_valid()), "opened received file image file");
|
|
|
|
auto gr = triqs::h5::group(f);
|
|
h5_read(gr, "object", res);
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
#undef CHECK_OR_THROW
|
|
}
|
|
|