32 #ifndef VSMC_UTILITY_HDF5IO_HPP 
   33 #define VSMC_UTILITY_HDF5IO_HPP 
   38 #if VSMC_HAS_CXX11LIB_TUPLE 
   46 template <MatrixOrder>
 
   70     template <
typename OutputIter>
 
   71     void set (std::size_t n, OutputIter) {data_.resize(n);}
 
   73     void set (std::size_t, T *ptr) {ptr_ = ptr;}
 
   90     template <
typename InputIter>
 
   91     InputIter 
set (std::size_t n, InputIter first)
 
   95         for (std::size_t i = 0; i != n; ++i, ++first)
 
  101     T *
set (std::size_t n, T *ptr) {ptr_ = ptr; 
return ptr + n;}
 
  103     const T *
set (std::size_t n, 
const T *ptr) {ptr_ = ptr; 
return ptr + n;}
 
  107     const T *
get () 
const {
return ptr_ == 
VSMC_NULLPTR ? &data_[0] : ptr_;}
 
  112     std::vector<T> data_;
 
  125 {return ::H5Tcopy(H5T_NATIVE_CHAR);}
 
  131 {return ::H5Tcopy(H5T_NATIVE_SCHAR);}
 
  137 {return ::H5Tcopy(H5T_NATIVE_UCHAR);}
 
  143 {return ::H5Tcopy(H5T_NATIVE_SHORT);}
 
  149 {return ::H5Tcopy(H5T_NATIVE_UCHAR);}
 
  155 {return ::H5Tcopy(H5T_NATIVE_INT);}
 
  161 {return ::H5Tcopy(H5T_NATIVE_UINT);}
 
  167 {return ::H5Tcopy(H5T_NATIVE_LONG);}
 
  173 {return ::H5Tcopy(H5T_NATIVE_ULONG);}
 
  175 #if VSMC_HAS_CXX11_LONG_LONG 
  181 {return ::H5Tcopy(H5T_NATIVE_LLONG);}
 
  187 {return ::H5Tcopy(H5T_NATIVE_ULLONG);}
 
  189 #endif // VSMC_HAS_CXX11_LONG_LONG 
  195 {return ::H5Tcopy(H5T_NATIVE_FLOAT);}
 
  201 {return ::H5Tcopy(H5T_NATIVE_DOUBLE);}
 
  207 {return ::H5Tcopy(H5T_NATIVE_LDOUBLE);}
 
  211 template <
typename T>
 
  212 inline ::hsize_t 
hdf5size (
const std::string &file_name,
 
  213         const std::string &data_name)
 
  215     ::hid_t datafile = ::H5Fopen(file_name.c_str(),
 
  216             H5F_ACC_RDONLY, H5P_DEFAULT);
 
  217     ::hid_t dataset = ::H5Dopen(datafile, data_name.c_str(), H5P_DEFAULT);
 
  218     ::hid_t dataspace = ::H5Dget_space(dataset);
 
  219     ::hid_t datatype = ::H5Dget_type(dataset);
 
  220     std::size_t n = 
static_cast<std::size_t
>(H5Sget_simple_extent_npoints(
 
  222     std::size_t b = H5Tget_size(datatype);
 
  223     std::size_t bytes = n * b;
 
  225     ::H5Tclose(datatype);
 
  226     ::H5Sclose(dataspace);
 
  228     ::H5Fclose(datafile);
 
  230     return bytes / 
sizeof(T);
 
  235 template <
typename T, 
typename OutputIter>
 
  236 inline OutputIter 
hdf5load (
const std::string &file_name,
 
  237         const std::string &data_name, OutputIter first)
 
  239     std::size_t n = hdf5size<T>(file_name, data_name);
 
  241     data_ptr.
set(n, first);
 
  242     T *data = data_ptr.get();
 
  244     ::hid_t datafile = ::H5Fopen(file_name.c_str(),
 
  245             H5F_ACC_RDONLY, H5P_DEFAULT);
 
  246     ::hid_t dataset = ::H5Dopen(datafile, data_name.c_str(), H5P_DEFAULT);
 
  247     ::hid_t datatype = ::H5Dget_type(dataset);
 
  248     ::H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
 
  249     if (data_ptr.set_ptr()) {
 
  252         for (std::size_t i = 0; i != n; ++i, ++first)
 
  256     ::H5Tclose(datatype);
 
  258     ::H5Fclose(datafile);
 
  312 template <MatrixOrder Order, 
typename T, 
typename InputIter>
 
  314         const std::string &file_name, 
const std::string &data_name,
 
  315         InputIter first, 
bool append = 
false)
 
  317     if (nrow == 0 || ncol == 0)
 
  320     std::string dataset_name(
"/" + data_name);
 
  322     internal::hdf5io_matrix_dim<Order>(nrow, ncol, dim);
 
  324     InputIter last = data_ptr.
set(nrow * ncol, first);
 
  325     const T *data = data_ptr.get();
 
  329         datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
 
  331         datafile = ::H5Fcreate(file_name.c_str(),
 
  332                 H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
 
  334     ::hid_t dataspace = ::H5Screate_simple(2, dim, 
VSMC_NULLPTR);
 
  335     ::hid_t datatype = hdf5io_datatype<T>();
 
  336     ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(),
 
  337             datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
 
  338     ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
 
  341     ::H5Tclose(datatype);
 
  342     ::H5Sclose(dataspace);
 
  343     ::H5Fclose(datafile);
 
  356         const std::string &data_name, 
bool append)
 
  358     std::string group_name(
"/" + data_name);
 
  362         datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
 
  364         datafile = ::H5Fcreate(file_name.c_str(),
 
  365                 H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
 
  367     ::hid_t datagroup = ::H5Gcreate(datafile, group_name.c_str(),
 
  368             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
 
  369     ::H5Gclose(datagroup);
 
  370     ::H5Fclose(datafile);
 
  398 template <
typename T, 
typename InputIterIter, 
typename SInputIter>
 
  400         const std::string &file_name, 
const std::string &data_name,
 
  401         InputIterIter first, SInputIter sfirst, 
bool append = 
false)
 
  403     std::string group_name(
"/" + data_name);
 
  404     ::hsize_t dim[1] = {nrow};
 
  408         datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
 
  410         datafile = ::H5Fcreate(file_name.c_str(),
 
  411                 H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
 
  413     ::hid_t datagroup = ::H5Gcreate(datafile, group_name.c_str(),
 
  414             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
 
  416     if (nrow != 0 && ncol != 0) {
 
  417         ::hid_t dataspace = ::H5Screate_simple(1, dim, 
VSMC_NULLPTR);
 
  418         ::hid_t datatype = hdf5io_datatype<T>();
 
  420         for (std::size_t j = 0; j != ncol; ++j, ++first, ++sfirst) {
 
  421             data_ptr.
set(nrow, *first);
 
  422             const T *data = data_ptr.get();
 
  423             std::string dataset_name(group_name + 
"/" + (*sfirst));
 
  424             ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(),
 
  426                     H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
 
  427             ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
 
  430         ::H5Tclose(datatype);
 
  431         ::H5Sclose(dataspace);
 
  434     ::H5Gclose(datagroup);
 
  435     ::H5Fclose(datafile);
 
  447 template <
typename T, 
typename InputIter>
 
  449         const std::string &file_name, 
const std::string &data_name,
 
  450         InputIter first, 
const std::string &vname)
 
  455     std::string dataset_name(
"/" + data_name + 
"/" + vname);
 
  456     ::hsize_t dim[1] = {N};
 
  458     ::hid_t datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
 
  459     ::hid_t dataspace = ::H5Screate_simple(1, dim, 
VSMC_NULLPTR);
 
  460     ::hid_t datatype = hdf5io_datatype<T>();
 
  462     data_ptr.
set(N, first);
 
  463     const T *data = data_ptr.get();
 
  464     ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(),
 
  465             datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
 
  466     ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
 
  469     ::H5Tclose(datatype);
 
  470     ::H5Sclose(dataspace);
 
  471     ::H5Fclose(datafile);
 
  474 #if VSMC_HAS_CXX11LIB_TUPLE 
  478 template <
typename TupleVectorType>
 
  481 {std::get<0>(vec).resize(n);}
 
  483 template <
typename TupleVectorType, std::
size_t Pos>
 
  487     std::get<Pos>(vec).resize(n);
 
  491 template <
typename TupleVectorType, 
typename InputIter>
 
  495     typedef typename std::iterator_traits<InputIter>::value_type tuple_type;
 
  496     typedef typename std::tuple_element<0, tuple_type>::type value_type;
 
  497     value_type *dst = &std::get<0>(vec)[0];
 
  498     InputIter ffirst = first;
 
  499     for (std::size_t i = 0; i != n; ++i, ++ffirst)
 
  500         dst[i] = std::get<0>(*ffirst);
 
  503 template <
typename TupleVectorType, 
typename InputIter, std::
size_t Pos>
 
  507     typedef typename std::iterator_traits<InputIter>::value_type tuple_type;
 
  508     typedef typename std::tuple_element<Pos, tuple_type>::type value_type;
 
  509     value_type *dst = &std::get<Pos>(vec)[0];
 
  510     InputIter ffirst = first;
 
  511     for (std::size_t i = 0; i != n; ++i, ++ffirst)
 
  512         dst[i] = std::get<Pos>(*ffirst);
 
  516 template <
typename TupleVectorType, 
typename TuplePtrType>
 
  518         const TupleVectorType &vec, TuplePtrType &ptr, 
Position<0>)
 
  519 {std::get<0>(ptr) = &std::get<0>(vec)[0];}
 
  521 template <
typename TupleVectorType, 
typename TuplePtrType, std::
size_t Pos>
 
  523         const TupleVectorType &vec, TuplePtrType &ptr, 
Position<Pos>)
 
  525     std::get<Pos>(ptr) = &std::get<Pos>(vec)[0];
 
  529 template <
typename InputIter, 
typename... InputIters>
 
  531         const std::string &file_name, 
const std::string &data_name,
 
  532         const std::tuple<InputIter, InputIters...> &first,
 
  535     typedef typename std::tuple_element<
 
  536         0, std::tuple<InputIter, InputIters...> >
::type iter_type;
 
  537     typedef typename std::iterator_traits<iter_type>::value_type dtype;
 
  539     data_ptr.
set(nrow, std::get<0>(first));
 
  540     const dtype *data = data_ptr.
get();
 
  541     hdf5store_list_insert<dtype>(nrow, file_name, data_name, data, *sptr);
 
  544 template <
typename InputIter, 
typename... InputIters, std::size_t Pos>
 
  546         const std::string &file_name, 
const std::string &data_name,
 
  547         const std::tuple<InputIter, InputIters...> &first,
 
  550     typedef typename std::tuple_element<
 
  551         Pos, std::tuple<InputIter, InputIters...> >
::type iter_type;
 
  552     typedef typename std::iterator_traits<iter_type>::value_type dtype;
 
  554     data_ptr.
set(nrow, std::get<Pos>(first));
 
  555     const dtype *data = data_ptr.
get();
 
  556     hdf5store_list_insert<dtype>(nrow, file_name, data_name, data, *sptr);
 
  580 template <
typename SInputIter, 
typename InputIter, 
typename... InputIters>
 
  582         const std::string &file_name, 
const std::string &data_name,
 
  583         const std::tuple<InputIter, InputIters...> &first,
 
  584         SInputIter sfirst, 
bool append = 
false)
 
  586     static VSMC_CONSTEXPR const std::size_t dim = 
sizeof...(InputIters) + 1;
 
  588     vnames.
set(dim, sfirst);
 
  589     const std::string *sptr = vnames.
get() + dim;
 
  590     hdf5store_list<int>(0, 0, file_name, data_name,
 
  597 #endif // VSMC_HAS_CXX11LIB_TUPLE 
  601 template <
typename T>
 
  603         const std::string &file_name, 
const std::string &data_name,
 
  609     std::vector<std::string> header(ncol);
 
  611     std::vector<double> data(nrow * ncol);
 
  612     sampler.template summary_data<ColMajor>(data.begin());
 
  613     std::vector<const double *> data_ptr(ncol);
 
  614     for (std::size_t j = 0; j != ncol; ++j)
 
  615         data_ptr[j] = &data[0] + j * nrow;
 
  616     hdf5store_list<double>(nrow, ncol, file_name, data_name,
 
  617             data_ptr.begin(), header.begin(), append);
 
  619     std::vector<int> resampled(nrow);
 
  621     hdf5store_list_insert<int>(nrow, file_name, data_name,
 
  622             &resampled[0], 
"Resampled");
 
  627 template <MatrixOrder Order, std::
size_t Dim, 
typename T>
 
  629         const std::string &file_name, 
const std::string &data_name,
 
  632     hdf5store_matrix<Order, T>(state.size(), state.dim(), file_name, data_name,
 
  633             state.data(), append);
 
  636 #if VSMC_HAS_CXX11LIB_TUPLE 
  640 template <
typename T, 
typename... Types>
 
  642         const std::string &file_name, 
const std::string &data_name,
 
  645     static VSMC_CONSTEXPR const std::size_t dim = 
sizeof...(Types) + 1;
 
  646     std::vector<std::string> vnames;
 
  648     for (std::size_t i = 0; i != dim; ++i) {
 
  649         std::stringstream ss;
 
  651         vnames.push_back(ss.str());
 
  654     std::tuple<std::vector<T>, std::vector<Types>...> data_vec;
 
  660     typename std::tuple<
const T *, 
const Types *...> data_ptr;
 
  669 template <
typename T, 
typename... Types>
 
  671         const std::string &file_name, 
const std::string &data_name,
 
  674     static VSMC_CONSTEXPR const std::size_t dim = 
sizeof...(Types) + 1;
 
  675     std::vector<std::string> vnames;
 
  677     for (std::size_t i = 0; i != dim; ++i) {
 
  678         std::stringstream ss;
 
  680         vnames.push_back(ss.str());
 
  687 #endif // VSMC_HAS_CXX11LIB_TUPLE 
  692          std::size_t StateSize, 
typename FPType, 
typename ID>
 
  694         const std::string &file_name, 
const std::string &data_name,
 
  697     std::size_t nrow = state.
size();
 
  698     std::size_t ncol = state.
state_size() / 
sizeof(T);
 
  699     std::size_t N = nrow * ncol;
 
  700     std::vector<T> data(N);
 
  703     hdf5store_matrix<Order, T>(nrow, ncol, file_name, data_name,
 
  709 #endif // VSMC_UTILITY_HDF5IO_HPP 
Particle::value_type subtype. 
void set(std::size_t, T *ptr)
inline::hid_t hdf5io_datatype< signed char >()
HDF5 data type specialization for signed char. 
const CLBuffer< char, ID > & state_buffer() const 
The OpenCL buffer that stores the state values. 
state_tuple_base_type::state_tuple_type * data()
void hdf5store_list_empty(const std::string &file_name, const std::string &data_name, bool append)
Create an empty list. 
inline::hid_t hdf5io_datatype< double >()
HDF5 data type specialization for double. 
#define VSMC_CONSTEXPR
constexpr 
std::size_t summary_header_size() const 
The size of Sampler summary header. 
void set(std::size_t n, OutputIter)
T * set(std::size_t n, T *ptr)
void hdf5store_list_insert(std::size_t N, const std::string &file_name, const std::string &data_name, InputIter first, const std::string &vname)
Insert a variable into an existing list saved in HDF5 format. 
inline::hid_t hdf5io_datatype< unsigned char >()
HDF5 data type specialization for unsigned char. 
std::size_t iter_size() const 
Number of iterations (including initialization) 
InputIter hdf5store_matrix(std::size_t nrow, std::size_t ncol, const std::string &file_name, const std::string &data_name, InputIter first, bool append=false)
Store a matrix in the HDF5 format from an input iterator. 
void summary_header(OutputIter first) const 
Sampler summary header. 
const T * set(std::size_t n, const T *ptr)
void hdf5io_matrix_dim< RowMajor >(std::size_t nrow, std::size_t ncol,::hsize_t *dim)
inline::hid_t hdf5io_datatype< char >()
HDF5 data type specialization for char. 
Function template argument used for position. 
inline::hid_t hdf5io_datatype< unsigned int >()
HDF5 data type specialization for unsigned int. 
inline::hsize_t hdf5size(const std::string &file_name, const std::string &data_name)
Get the number of bytes of the data in the HDF5 format. 
InputIter set(std::size_t n, InputIter first)
void hdf5io_matrix_dim(std::size_t, std::size_t,::hsize_t *)
inline::hid_t hdf5io_datatype< unsigned short >()
HDF5 data type specialization for unsigned short. 
void hdf5io_matrix_dim< ColMajor >(std::size_t nrow, std::size_t ncol,::hsize_t *dim)
inline::hid_t hdf5io_datatype< int >()
HDF5 data type specialization for int. 
const ::cl::Buffer & data() const 
Read only access to the raw cl::Buffer object. 
void read_resampled_history(OutputIter first) const 
Read resampling indicator history through an output iterator. 
void hdf5store_list(std::size_t nrow, std::size_t ncol, const std::string &file_name, const std::string &data_name, InputIterIter first, SInputIter sfirst, bool append=false)
Store a list in the HDF5 format from an iterator to iterators. 
inline::hid_t hdf5io_datatype< long >()
HDF5 data type specialization for long. 
void hdf5store_list_insert_tuple(std::size_t nrow, const std::string &file_name, const std::string &data_name, const std::tuple< InputIter, InputIters...> &first, const std::string *sptr, Position< 0 >)
#define VSMC_NULLPTR
nullptr 
state_tuple_base_type::template state_type< Pos >::type * data(Position< Pos >)
Particle::value_type subtype. 
inline::hid_t hdf5io_datatype< unsigned long >()
HDF5 data type specialization for unsigned long. 
void hdf5_tuple_vector_copy(std::size_t n, TupleVectorType &vec, InputIter first, Position< 0 >)
void hdf5_tuple_vector_ptr(const TupleVectorType &vec, TuplePtrType &ptr, Position< 0 >)
OutputIter hdf5load(const std::string &file_name, const std::string &data_name, OutputIter first)
Load raw data in the HDF5 format. 
inline::hid_t hdf5io_datatype< short >()
HDF5 data type specialization for short. 
inline::hid_t hdf5io_datatype< float >()
HDF5 data type specialization for float. 
inline::hid_t hdf5io_datatype< long double >()
HDF5 data type specialization for long double. 
Particle::value_type subtype using OpenCL. 
void hdf5_tuple_vector_resize(std::size_t n, TupleVectorType &vec, Position< 0 >)
::hid_t hdf5io_datatype()
HDF5 data type. 
inline::hid_t hdf5io_datatype< unsigned long long >()
HDF5 data type specialization for unsigned long. 
inline::hid_t hdf5io_datatype< long long >()
HDF5 data type specialization for long long. 
static manager_type & manager()
The instance of the CLManager signleton associated with this value collcection. 
std::size_t state_size() const 
void hdf5store(const Sampler< T > &sampler, const std::string &file_name, const std::string &data_name, bool append=false)
Store a Sampler in the HDF5 format.