32 #ifndef VSMC_UTILITY_HDF5IO_HPP 33 #define VSMC_UTILITY_HDF5IO_HPP 44 template <MatrixOrder>
49 std::size_t nrow, std::size_t ncol, ::hsize_t *dim)
57 std::size_t nrow, std::size_t ncol, ::hsize_t *dim)
69 template <
typename OutputIter>
70 void set(std::size_t n, OutputIter)
75 void set(std::size_t, T *ptr) { ptr_ = ptr; }
77 T *
get() {
return ptr_ ==
nullptr ? data_.data() : ptr_; }
92 template <
typename InputIter>
93 InputIter
set(std::size_t n, InputIter first)
96 T *dst = data_.data();
97 for (std::size_t i = 0; i != n; ++i, ++first)
103 T *
set(std::size_t n, T *ptr)
109 const T *
set(std::size_t n,
const T *ptr)
115 const T *
get()
const {
return ptr_ ==
nullptr ? data_.data() : ptr_; }
139 return ::H5Tcopy(H5T_NATIVE_CHAR);
147 return ::H5Tcopy(H5T_NATIVE_SCHAR);
155 return ::H5Tcopy(H5T_NATIVE_UCHAR);
163 return ::H5Tcopy(H5T_NATIVE_SHORT);
171 return ::H5Tcopy(H5T_NATIVE_UCHAR);
179 return ::H5Tcopy(H5T_NATIVE_INT);
187 return ::H5Tcopy(H5T_NATIVE_UINT);
195 return ::H5Tcopy(H5T_NATIVE_LONG);
203 return ::H5Tcopy(H5T_NATIVE_ULONG);
211 return ::H5Tcopy(H5T_NATIVE_LLONG);
219 return ::H5Tcopy(H5T_NATIVE_ULLONG);
227 return ::H5Tcopy(H5T_NATIVE_FLOAT);
235 return ::H5Tcopy(H5T_NATIVE_DOUBLE);
243 return ::H5Tcopy(H5T_NATIVE_LDOUBLE);
249 const std::string &file_name,
const std::string &data_name)
252 ::H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
253 ::hid_t dataset = ::H5Dopen(datafile, data_name.c_str(), H5P_DEFAULT);
254 ::hid_t dataspace = ::H5Dget_space(dataset);
255 ::hid_t datatype = ::H5Dget_type(dataset);
257 static_cast<std::size_t
>(H5Sget_simple_extent_npoints(dataspace));
258 std::size_t b = H5Tget_size(datatype);
259 std::size_t bytes = n * b;
261 ::H5Tclose(datatype);
262 ::H5Sclose(dataspace);
264 ::H5Fclose(datafile);
271 template <
typename T>
273 const std::string &file_name,
const std::string &data_name)
275 return hdf5size(file_name, data_name) /
sizeof(T);
280 template <
typename T,
typename OutputIter>
281 inline OutputIter
hdf5load(
const std::string &file_name,
282 const std::string &data_name, OutputIter first)
284 std::size_t n =
hdf5size(file_name, data_name) /
sizeof(T);
286 data_ptr.
set(n, first);
287 T *data = data_ptr.
get();
290 ::H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
291 ::hid_t dataset = ::H5Dopen(datafile, data_name.c_str(), H5P_DEFAULT);
292 ::hid_t datatype = ::H5Dget_type(dataset);
293 ::H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
297 for (std::size_t i = 0; i != n; ++i, ++first)
301 ::H5Tclose(datatype);
303 ::H5Fclose(datafile);
312 ::hid_t datafile = ::H5Fcreate(
313 file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
314 ::H5Fclose(datafile);
368 template <MatrixOrder Order,
typename T,
typename InputIter>
370 const std::string &file_name,
const std::string &data_name,
371 InputIter first,
bool append =
false)
373 if (nrow == 0 || ncol == 0)
376 std::string dataset_name(
"/" + data_name);
378 internal::hdf5io_matrix_dim<Order>(nrow, ncol, dim);
380 InputIter last = data_ptr.
set(nrow * ncol, first);
381 const T *data = data_ptr.get();
385 datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
387 datafile = ::H5Fcreate(
388 file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
390 ::hid_t dataspace = ::H5Screate_simple(2, dim,
nullptr);
391 ::hid_t datatype = hdf5io_datatype<T>();
392 ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(), datatype,
393 dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
394 ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
397 ::H5Tclose(datatype);
398 ::H5Sclose(dataspace);
399 ::H5Fclose(datafile);
413 const std::string &data_name,
bool append =
false)
415 std::string group_name(
"/" + data_name);
419 datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
421 datafile = ::H5Fcreate(
422 file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
424 ::hid_t datagroup = ::H5Gcreate(
425 datafile, group_name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
426 ::H5Gclose(datagroup);
427 ::H5Fclose(datafile);
457 template <
typename T,
typename InputIterIter,
typename SInputIter>
459 const std::string &file_name,
const std::string &data_name,
460 InputIterIter first, SInputIter sfirst,
bool append =
false)
462 std::string group_name(
"/" + data_name);
463 ::hsize_t dim[1] = {nrow};
467 datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
469 datafile = ::H5Fcreate(
470 file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
472 ::hid_t datagroup = ::H5Gcreate(
473 datafile, group_name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
475 if (nrow != 0 && ncol != 0) {
476 ::hid_t dataspace = ::H5Screate_simple(1, dim,
nullptr);
477 ::hid_t datatype = hdf5io_datatype<T>();
479 for (std::size_t j = 0; j != ncol; ++j, ++first, ++sfirst) {
480 data_ptr.
set(nrow, *first);
481 const T *data = data_ptr.get();
482 std::string dataset_name(group_name +
"/" + (*sfirst));
483 ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(),
484 datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
485 ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
488 ::H5Tclose(datatype);
489 ::H5Sclose(dataspace);
492 ::H5Gclose(datagroup);
493 ::H5Fclose(datafile);
505 template <
typename T,
typename InputIter>
507 const std::string &data_name, InputIter first,
const std::string &vname)
512 std::string dataset_name(
"/" + data_name +
"/" + vname);
513 ::hsize_t dim[1] = {N};
515 ::hid_t datafile = ::H5Fopen(file_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
516 ::hid_t dataspace = ::H5Screate_simple(1, dim,
nullptr);
517 ::hid_t datatype = hdf5io_datatype<T>();
519 data_ptr.
set(N, first);
520 const T *data = data_ptr.get();
521 ::hid_t dataset = ::H5Dcreate(datafile, dataset_name.c_str(), datatype,
522 dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
523 ::H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
526 ::H5Tclose(datatype);
527 ::H5Sclose(dataspace);
528 ::H5Fclose(datafile);
534 template <
typename InputIter,
typename... InputIters>
536 const std::string &file_name,
const std::string &data_name,
537 const std::tuple<InputIter, InputIters...> &first,
const std::string *sptr,
538 std::integral_constant<std::size_t, 0>)
541 typename std::iterator_traits<
typename std::tuple_element<0,
542 std::tuple<InputIter, InputIters...>>::type>::value_type;
544 data_ptr.
set(nrow, std::get<0>(first));
545 const value_type *data = data_ptr.
get();
546 hdf5store_list_insert<value_type>(nrow, file_name, data_name, data, *sptr);
549 template <
typename InputIter,
typename... InputIters, std::size_t Pos>
551 const std::string &file_name,
const std::string &data_name,
552 const std::tuple<InputIter, InputIters...> &first,
const std::string *sptr,
553 std::integral_constant<std::size_t, Pos>)
556 typename std::iterator_traits<
typename std::tuple_element<Pos,
557 std::tuple<InputIter, InputIters...>>::type>::value_type;
559 data_ptr.
set(nrow, std::get<Pos>(first));
560 const value_type *data = data_ptr.
get();
561 hdf5store_list_insert<value_type>(nrow, file_name, data_name, data, *sptr);
563 std::integral_constant<std::size_t, Pos - 1>());
587 template <
typename SInputIter,
typename InputIter,
typename... InputIters>
589 const std::string &data_name,
590 const std::tuple<InputIter, InputIters...> &first, SInputIter sfirst,
593 static constexpr std::size_t dim =
sizeof...(InputIters) + 1;
595 vnames.
set(dim, sfirst);
596 const std::string *sptr = vnames.
get() + dim;
599 --sptr, std::integral_constant<std::size_t, dim - 1>());
605 template <
typename IntType>
608 if (
sizeof(
int) >
sizeof(IntType))
612 for (std::size_t i = 0; i != n; ++i) {
613 if (r[i] > std::numeric_limits<int>::max
VSMC_MNE()) {
622 template <
typename IntType>
625 if (
sizeof(
int) >
sizeof(IntType))
629 for (std::size_t i = 0; i != n; ++i) {
630 if (r[i] < std::numeric_limits<int>::min
VSMC_MNE()) {
634 if (r[i] > std::numeric_limits<int>::max
VSMC_MNE()) {
647 template <
typename T>
649 const std::string &data_name,
bool append =
false)
664 sampler.template summary_data_int<ColMajor>(data_int.begin());
666 data_int.size(), data_int.data(), std::is_signed<size_type>());
669 std::copy(data_int.begin(), data_int.end(), data_int_small.begin());
671 for (std::size_t j = 0; j != ncol_int; ++j)
672 data_ptr_int[j] = data_int_small.data() + j * nrow;
673 for (std::size_t j = 0; j != ncol_int; ++j)
674 hdf5store_list_insert<int>(
675 nrow, file_name, data_name, data_ptr_int[j], header_int[j]);
678 for (std::size_t j = 0; j != ncol_int; ++j)
679 data_ptr_int[j] = data_int.data() + j * nrow;
680 for (std::size_t j = 0; j != ncol_int; ++j)
681 hdf5store_list_insert<size_type>(
682 nrow, file_name, data_name, data_ptr_int[j], header_int[j]);
689 sampler.template summary_data<ColMajor>(data.begin());
691 for (std::size_t j = 0; j != ncol; ++j)
692 data_ptr[j] = data.data() + j * nrow;
693 for (std::size_t j = 0; j != ncol; ++j)
694 hdf5store_list_insert<double>(
695 nrow, file_name, data_name, data_ptr[j], header[j]);
700 template <MatrixOrder Order, std::
size_t Dim,
typename T>
702 const std::string &file_name,
const std::string &data_name,
705 hdf5store_matrix<Order, T>(
706 state.size(), state.dim(), file_name, data_name, state.data(), append);
711 template <
MatrixOrder Order,
typename T, std::size_t StateSize,
712 typename RealType,
typename ID>
714 const std::string &file_name,
const std::string &data_name,
717 std::size_t nrow = state.size();
718 std::size_t ncol = state.state_size() /
sizeof(T);
719 std::size_t N = nrow * ncol;
721 state.manager().template read_buffer<T>(
722 state.state_buffer().data(), N, data.data());
723 hdf5store_matrix<Order, T>(
724 nrow, ncol, file_name, data_name, data.data(), append);
729 template <
typename T>
731 const std::string &file_name,
const std::string &data_name,
735 hdf5store(particle.
value(), file_name, data_name +
"/value",
true);
736 hdf5store_matrix<ColMajor, double>(particle.
size(), 1, file_name,
737 data_name +
"/weight", particle.
weight().data(),
true);
742 template <MatrixOrder Order,
typename T,
typename U>
744 const std::string &file_name,
const std::string &data_name,
749 particle.
value(), file_name, data_name +
"/value",
true);
750 hdf5store_matrix<ColMajor, double>(particle.
size(), 1, file_name,
751 data_name +
"/weight", particle.
weight().data(),
true);
756 #endif // VSMC_UTILITY_HDF5IO_HPP inline::hid_t hdf5io_datatype< signed char >()
HDF5 data type specialization for signed char.
Particle class representing the whole particle set.
inline::hid_t hdf5io_datatype< double >()
HDF5 data type specialization for double.
std::size_t summary_header_size() const
The size of Sampler summary header (floating point data)
value_type & value()
Read and write access to the value collection object.
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.
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
weight_type & weight()
Read and write access to the weight collection object.
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 (floating point data)
inline::hid_t hdf5io_datatype()
HDF5 data type.
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.
void summary_header_int(OutputIter first) const
Sampler summary header (integer data)
inline::hid_t hdf5io_datatype< unsigned int >()
HDF5 data type specialization for unsigned int.
void copy(std::size_t n, const T *x, std::size_t incx, T *y, std::size_t incy)
Copies vector to another vector.
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.
typename Particle< T >::size_type size_type
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_new(const std::string &file_name)
Create a new HDF5 file for store data.
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.
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, std::integral_constant< std::size_t, 0 >)
void hdf5store_list_empty(const std::string &file_name, const std::string &data_name, bool append=false)
Create an empty list.
std::size_t summary_header_size_int() const
The size of Sampler summary header (integer data, size etc.)
inline::hid_t hdf5io_datatype< unsigned long >()
HDF5 data type specialization for unsigned long.
InputIter set(std::size_t n, InputIter first)
void set(std::size_t n, OutputIter)
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.
size_type size() const
Number of particles.
inline::hid_t hdf5io_datatype< long double >()
HDF5 data type specialization for long double.
bool hdf5store_int(std::size_t n, IntType *r, std::false_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.
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.