2014-05-20 21:10:19 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* 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
|
2014-06-08 21:47:32 +02:00
|
|
|
#include "./base.hpp"
|
2014-05-20 21:10:19 +02:00
|
|
|
|
|
|
|
//#define CHECK_OR_THROW(Cond, Mess)
|
|
|
|
#define CHECK_OR_THROW(Cond, Mess) \
|
2014-06-11 15:47:29 +02:00
|
|
|
if (!(Cond)) TRIQS_RUNTIME_ERROR << "Error in h5 (de)serialization " << Mess;
|
2014-05-20 21:10:19 +02:00
|
|
|
|
|
|
|
namespace triqs {
|
|
|
|
namespace h5 {
|
|
|
|
|
|
|
|
template <typename T> std::string serialize(T const &x) {
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
|
|
|
|
CHECK_OR_THROW((fapl >= 0), "creating fapl");
|
2014-05-20 21:10:19 +02:00
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
auto err = H5Pset_fapl_core(fapl, (size_t)(64 * 1024), false);
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW((err >= 0), "setting core file driver in fapl.");
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
h5::file f = H5Fcreate("serialization", 0, H5P_DEFAULT, fapl);
|
|
|
|
CHECK_OR_THROW((f.is_valid()), "created core file");
|
2014-05-20 21:10:19 +02:00
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
auto gr = triqs::h5::group(f);
|
2014-05-20 21:10:19 +02:00
|
|
|
h5_write(gr, "object", x);
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
err = H5Fflush(f, H5F_SCOPE_GLOBAL);
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW((err >= 0), "flushed core file.");
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
ssize_t image_len = H5Fget_file_image(f, NULL, (size_t)0);
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW((image_len > 0), "got image file size");
|
|
|
|
|
|
|
|
std::string buf(image_len, 0);
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
ssize_t bytes_read = H5Fget_file_image(f, (void *)&buf[0], (size_t)image_len);
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW(bytes_read == image_len, "wrote file into image buffer");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
// -----------------------------
|
|
|
|
|
2014-05-20 21:10:19 +02:00
|
|
|
template <typename T> T deserialize(std::string const &buf) {
|
|
|
|
T res;
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
|
|
|
|
CHECK_OR_THROW((fapl >= 0), "creating fapl");
|
2014-05-20 21:10:19 +02:00
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
auto err = H5Pset_fapl_core(fapl, (size_t)(64 * 1024), false);
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW((err >= 0), "setting core file driver in fapl.");
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
err = H5Pset_file_image(fapl, (void *)&buf[0], buf.size());
|
2014-05-20 21:10:19 +02:00
|
|
|
CHECK_OR_THROW((err >= 0), "set file image in fapl.");
|
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
h5::file f = H5Fopen("serialization", H5F_ACC_RDONLY, fapl);
|
|
|
|
CHECK_OR_THROW((f.is_valid()), "opened received file image file");
|
2014-05-20 21:10:19 +02:00
|
|
|
|
2014-06-08 21:47:32 +02:00
|
|
|
auto gr = triqs::h5::group(f);
|
2014-05-20 21:10:19 +02:00
|
|
|
h5_read(gr, "object", res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHECK_OR_THROW
|
|
|
|
}
|
|
|
|
|