ALPSCore reference
archive_read_vector_data_helper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #include <iostream>
8 #include <vector>
9 
10 #include <memory>
11 
12 #include <alps/hdf5/archive.hpp>
14 #include <alps/utilities/cast.hpp>
15 
16 #include "common.hpp"
17 
18 namespace alps {
19  namespace hdf5 {
20  namespace detail {
21 
22  template<typename T, typename U, typename... UTail>
23  inline bool hdf5_read_vector_data_helper_impl(T * value, data_type const &data_id, type_type const &native_id,
24  std::vector<std::size_t> const &chunk,
25  std::vector<std::size_t> const &offset,
26  std::vector<std::size_t> const &data_size,
27  std::true_type) {
28  if (check_error(
29  H5Tequal(type_type(H5Tcopy(native_id)), type_type(get_native_type(U())))
30  ) > 0) {
31  std::size_t len = std::accumulate(chunk.begin(), chunk.end(), std::size_t(1), std::multiplies<std::size_t>());
32  std::unique_ptr<U[]> raw(
33  new U[len]
34  );
35  if (std::equal(chunk.begin(), chunk.end(), data_size.begin())) {
36  check_error(H5Dread(data_id, native_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, raw.get()));
37  cast(raw.get(), raw.get() + len, value);
38  } else {
39  std::vector<hsize_t> offset_hid(offset.begin(), offset.end()),
40  chunk_hid(chunk.begin(), chunk.end());
41  space_type space_id(H5Dget_space(data_id));
42  check_error(H5Sselect_hyperslab(space_id, H5S_SELECT_SET, &offset_hid.front(), NULL, &chunk_hid.front(), NULL));
43  space_type mem_id(H5Screate_simple(static_cast<int>(chunk_hid.size()), &chunk_hid.front(), NULL));
44  check_error(H5Dread(data_id, native_id, mem_id, space_id, H5P_DEFAULT, raw.get()));
45  cast(raw.get(), raw.get() + len, value);
46  }
47  return true;
48  } else
49  return hdf5_read_vector_data_helper_impl<T, UTail...>(value,
50  data_id,
51  native_id,
52  chunk,
53  offset,
54  data_size,
55  std::integral_constant<bool, sizeof...(UTail) != 0>());
56  }
57  template<typename T, typename... UTail>
58  inline bool hdf5_read_vector_data_helper_impl(T *, data_type const &, type_type const &,
59  std::vector<std::size_t> const &,
60  std::vector<std::size_t> const &,
61  std::vector<std::size_t> const &,
62  std::false_type)
63  { return false; }
64 
65  template<typename T>
66  bool hdf5_read_vector_data_helper(T * value, data_type const &data_id, type_type const &native_id,
67  std::vector<std::size_t> const &chunk,
68  std::vector<std::size_t> const &offset,
69  std::vector<std::size_t> const &data_size) {
70  return hdf5_read_vector_data_helper_impl<T, ALPS_HDF5_NATIVE_INTEGRAL_TYPES>(value,
71  data_id,
72  native_id,
73  chunk,
74  offset,
75  data_size,
76  std::true_type());
77  }
78 
79  #define ALPS_HDF5_READ_VECTOR_DATA_HELPER(T) \
80  template bool hdf5_read_vector_data_helper(T *, data_type const &, type_type const &, \
81  std::vector<std::size_t> const &, \
82  std::vector<std::size_t> const &, \
83  std::vector<std::size_t> const &);
85  }
86  }
87 }
ALPS_FOREACH_NATIVE_HDF5_TYPE(ALPS_HDF5_READ_SCALAR)
#define ALPS_HDF5_READ_VECTOR_DATA_HELPER(T)
U cast(T const &)
Definition: cast.hpp:151