32 #ifndef VSMC_UTILITY_HDF5_HPP    33 #define VSMC_UTILITY_HDF5_HPP    39 #pragma warning(disable : 2282)    48 #define VSMC_DEFINE_HDF5TYPE(CPPName, CName)                                  \    49     class HDF5##CPPName : public HDF5ID<HDF5##CPPName>                        \    52         HDF5##CPPName(::hid_t id) : HDF5ID<HDF5##CPPName>(id) {}              \    54         static void close(::hid_t id) { ::H5##CName##close(id); }             \    63 template <
typename Derived>
    77     bool good()
 const { 
return id_ >= 0; }
    81     explicit operator bool()
 const { 
return good(); }
    97     template <
typename InputIter>
   100         set(n, first, std::is_convertible<InputIter, const T *>());
   103     const T *
get() 
const { 
return ptr_ == 
nullptr ? data_.data() : ptr_; }
   109     template <
typename InputIter>
   110     void set(std::size_t, InputIter first, std::true_type)
   112         ptr_ = 
static_cast<const T *
>(first);
   115     template <
typename InputIter>
   116     void set(std::size_t n, InputIter first, std::false_type)
   119         std::copy_n(first, n, data_.begin());
   124     MatrixLayout layout, std::size_t nrow, std::size_t ncol, ::hsize_t *dim)
   136 template <
typename IntType>
   139     if (
sizeof(
int) > 
sizeof(IntType))
   143     for (std::size_t i = 0; i != n; ++i) {
   144         if (r[i] > std::numeric_limits<int>::max()) {
   153 template <
typename IntType>
   156     if (
sizeof(
int) > 
sizeof(IntType))
   160     for (std::size_t i = 0; i != n; ++i) {
   161         if (r[i] < std::numeric_limits<int>::min()) {
   165         if (r[i] > std::numeric_limits<int>::max()) {
   175     const std::string &filename, 
bool append, 
bool read_only)
   179             filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
   183         flag = H5F_ACC_RDONLY;
   187     return ::H5Fopen(filename.c_str(), flag, H5P_DEFAULT);
   205     return ::H5Tcopy(H5T_NATIVE_CHAR);
   213     return ::H5Tcopy(H5T_NATIVE_SCHAR);
   221     return ::H5Tcopy(H5T_NATIVE_UCHAR);
   229     return ::H5Tcopy(H5T_NATIVE_SHORT);
   237     return ::H5Tcopy(H5T_NATIVE_UCHAR);
   245     return ::H5Tcopy(H5T_NATIVE_INT);
   253     return ::H5Tcopy(H5T_NATIVE_UINT);
   261     return ::H5Tcopy(H5T_NATIVE_LONG);
   269     return ::H5Tcopy(H5T_NATIVE_ULONG);
   277     return ::H5Tcopy(H5T_NATIVE_LLONG);
   285     return ::H5Tcopy(H5T_NATIVE_ULLONG);
   293     return ::H5Tcopy(H5T_NATIVE_FLOAT);
   301     return ::H5Tcopy(H5T_NATIVE_DOUBLE);
   309     return ::H5Tcopy(H5T_NATIVE_LDOUBLE);
   315     const std::string &filename, 
const std::string &dataname)
   322         ::H5Dopen(datafile.
id(), dataname.c_str(), H5P_DEFAULT));
   330     ::hssize_t n = ::H5Sget_simple_extent_npoints(dataspace.
id());
   334     return static_cast<std::size_t
>(n);
   339 template <
typename OutputIter>
   341     const std::string &filename, 
const std::string &dataname, OutputIter first)
   348         ::H5Dopen(datafile.
id(), dataname.c_str(), H5P_DEFAULT));
   356     ::hssize_t n = ::H5Sget_simple_extent_npoints(dataspace.
id());
   360     using T = 
typename std::iterator_traits<OutputIter>::value_type;
   361     std::size_t N = 
static_cast<std::size_t
>(n);
   372     ::htri_t is_eq = ::H5Tequal(src_datatype.
id(), dst_datatype.
id());
   376     std::size_t src_size = ::H5Tget_size(src_datatype.
id());
   377     std::size_t dst_size = ::H5Tget_size(dst_datatype.
id());
   378     std::size_t buf_size = std::max(src_size, dst_size);
   379     std::size_t buf_rate =
   380         buf_size / dst_size + (buf_size % dst_size == 0 ? 0 : 1);
   382     err = ::H5Dread(dataset.
id(), src_datatype.
id(), H5S_ALL, H5S_ALL,
   383         H5P_DEFAULT, buf.data());
   388         err = ::H5Tconvert(src_datatype.
id(), dst_datatype.
id(), N, buf.data(),
   389             nullptr, H5P_DEFAULT);
   394     return std::copy_n(buf.begin(), N, first);
   398 template <
typename T>
   400     const std::string &filename, 
const std::string &dataname)
   403     hdf5load(filename, dataname, vector.data());
   417     const std::string &filename, 
const std::string dataname, 
bool append)
   423             dataname.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
   429 template <
typename InputIter>
   431     const std::string &filename, 
const std::string &dataname, 
bool append)
   433     using T = 
typename std::iterator_traits<InputIter>::value_type;
   447     ::hsize_t dim[1] = {N};
   453         datatype.
id(), dataspace.
id(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
   458     ::H5Dwrite(dataset.
id(), datatype.
id(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
   464 template <
typename T, 
typename Alloc>
   465 inline void hdf5store(
const std::vector<T, Alloc> &vector,
   466     const std::string &filename, 
const std::string &dataname, 
bool append)
   468     hdf5store(vector.size(), vector.data(), filename, dataname, append);
   513 template <
typename InputIter>
   515     InputIter first, 
const std::string &filename, 
const std::string &dataname,
   518     using T = 
typename std::iterator_traits<InputIter>::value_type;
   520     if (nrow == 0 || ncol == 0)
   539         datatype.
id(), dataspace.
id(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
   544     ::H5Dwrite(dataset.
id(), datatype.
id(), H5S_ALL, H5S_ALL, H5P_DEFAULT,
   550 template <MatrixLayout Layout, std::
size_t Dim, 
typename T>
   552     const std::string &filename, 
const std::string &dataname, 
bool append)
   554     hdf5store(Layout, state_matrix.size(), state_matrix.dim(),
   555         state_matrix.data(), filename, dataname, append);
   560 template <
typename T>
   562     const std::string &dataname, 
bool append)
   567         particle.
weight().data(), filename, dataname + 
"/Weight", 
true);
   572 template <
typename T>
   574     const std::string &dataname, 
bool append)
   576     std::map<std::string, Vector<double>> df = monitor.
summary();
   578     for (
const auto &v : df)
   579         hdf5store(v.second, filename, dataname + 
"/" + v.first, append);
   584 template <
typename T>
   586     const std::string &dataname, 
bool append)
   588     std::map<std::string, Vector<double>> df = sampler.
summary();
   590     for (
const auto &v : df)
   591         hdf5store(v.second, filename, dataname + 
"/" + v.first, append);
   596 #endif // VSMC_UTILITY_HDF5_HPP #define VSMC_DEFINE_HDF5TYPE(CPPName, CName)                                                                
inline::hid_t hdf5_datatype< long long >()
HDF5 data type specialization for long long. 
Particle class representing the whole particle set. 
state_type & state()
Read and write access to the state collection object. 
MatrixLayout
Matrix layout. 
std::size_t hdf5load_size(const std::string &filename, const std::string &dataname)
The number of elements in HDF5 data. 
inline::hid_t hdf5_datatype< long double >()
HDF5 data type specialization for long double. 
weight_type & weight()
Read and write access to the weight collection object. 
std::map< std::string, Vector< double > > summary() const 
Summary of monitor history. 
inline::hid_t hdf5_datatype< unsigned short >()
HDF5 data type specialization for unsigned short. 
void hdf5store(const std::string &filename)
Create a new HDF5 file. 
inline::hid_t hdf5_datatype< signed char >()
HDF5 data type specialization for signed char. 
inline::hid_t hdf5_datatype< unsigned int >()
HDF5 data type specialization for unsigned int. 
inline::hid_t hdf5_datatype< float >()
HDF5 data type specialization for float. 
inline::hid_t hdf5_datatype< short >()
HDF5 data type specialization for short. 
HDF5StoreDataPtr(std::size_t n, InputIter first)
inline::hid_t hdf5_datatype< char >()
HDF5 data type specialization for char. 
bool hdf5_use_int(std::size_t n, IntType *r, std::false_type)
inline::hid_t hdf5_datatype< double >()
HDF5 data type specialization for double. 
std::map< std::string, Vector< double > > summary() const 
Summary of sampler history. 
void hdf5_dim(MatrixLayout layout, std::size_t nrow, std::size_t ncol,::hsize_t *dim)
inline::hid_t hdf5_datatype< unsigned long long >()
HDF5 data type specialization for unsigned long. 
Monitor for Monte Carlo integration. 
inline::hid_t hdf5_datafile(const std::string &filename, bool append, bool read_only)
inline::hid_t hdf5_datatype< int >()
HDF5 data type specialization for int. 
inline::hid_t hdf5_datatype< long >()
HDF5 data type specialization for long. 
inline::hid_t hdf5_datatype< unsigned char >()
HDF5 data type specialization for unsigned char. 
inline::hid_t hdf5_datatype()
HDF5 data type. 
inline::hid_t hdf5_datatype< unsigned long >()
HDF5 data type specialization for unsigned long. 
OutputIter hdf5load(const std::string &filename, const std::string &dataname, OutputIter first)
Load HDF5 data.