buffer.h
Go to the documentation of this file.
1 #ifndef TTG_BUFFER_H
2 #define TTG_BUFFER_H
3 
4 #include <memory>
5 
6 #include "ttg/fwd.h"
7 #include "ttg/util/meta.h"
8 #include "ttg/serialization.h"
9 #include <memory>
10 
11 namespace ttg {
12 
13 template<typename T, typename Allocator = std::allocator<std::decay_t<T>>>
14 using Buffer = TTG_IMPL_NS::Buffer<T, Allocator>;
15 
16 namespace meta {
17 
18  /* Specialize some traits */
19 
20  template<typename T, typename A>
21  struct is_buffer<ttg::Buffer<T, A>> : std::true_type
22  { };
23 
24  template<typename T, typename A>
25  struct is_buffer<const ttg::Buffer<T, A>> : std::true_type
26  { };
27 
28  /* buffers are const if their value types are const */
29  template<typename T, typename A>
30  struct is_const<ttg::Buffer<T, A>> : std::is_const<T>
31  { };
32 
33 } // namespace meta
34 
35 namespace detail {
41  template<typename T, typename Enabler = void>
42  struct has_buffer_apply_helper : std::false_type
43  { };
44 
45  template<typename T>
47  { };
48 
49  template<typename T>
51 
52 } // namespace detail
53 
54 } // namespace ttg
55 
56 
57 
58 #ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
59 #include <madness/world/buffer_archive.h>
60 
61 namespace madness {
62  namespace archive {
63  template<typename Fn>
64  struct BufferVisitorArchive : public madness::archive::BaseOutputArchive {
65  private:
66  Fn m_fn;
67 
68  public:
69  template<typename _Fn>
70  BufferVisitorArchive(_Fn&& fn)
71  : m_fn(fn)
72  { }
73 
75 
81  template <typename T, typename Allocator>
82  void store(const ttg::Buffer<T, Allocator>* t, long n) const {
83  /* invoke the function on each buffer */
84  for (std::size_t i = 0; i < n; ++i) {
85  m_fn(t[i]);
86  }
87  }
88 
89  template <typename T>
90  void store(const T* t, long n) const {
91  /* nothing to be done for other types */
92  }
93 
95  void open(std::size_t /*hint*/) {}
96 
98  void close() {}
99 
101  void flush() {}
102 
105  std::size_t size() const {
106  return 0;
107  };
108  };
109 
110  /* deduction guide */
111  template<typename Fn>
112  BufferVisitorArchive(Fn&&) -> BufferVisitorArchive<Fn>;
113  } // namespace archive
114 
115  template <typename Fn>
116  struct is_archive<archive::BufferVisitorArchive<Fn>> : std::true_type {};
117 
118  template <typename Fn>
119  struct is_output_archive<archive::BufferVisitorArchive<Fn>> : std::true_type {};
120 
121  template <typename Fn, typename T>
122  struct is_default_serializable_helper<archive::BufferVisitorArchive<Fn>, T,
123  std::enable_if_t<is_trivially_serializable<T>::value>>
124  : std::true_type {};
125 
126  template <typename Fn, typename T, typename Allocator>
127  struct is_default_serializable_helper<archive::BufferVisitorArchive<Fn>, ttg::Buffer<T, Allocator>>
128  : std::true_type {};
129 } // namespace madness
130 
131 namespace ttg::detail {
132  template<typename T, typename Fn>
133  requires(madness::is_serializable_v<madness::archive::BufferVisitorArchive<Fn>, std::decay<T>>)
134  void buffer_apply(T&& t, Fn&& fn) {
135  madness::archive::BufferVisitorArchive ar(std::forward<Fn>(fn));
136  ar & t;
137  }
138 
139  /* dummy function type used to check whether buffer_apply is available */
140  using buffer_apply_dummy_fn = decltype([]<typename T, typename A>(const ttg::Buffer<T, A>&){});
141 
142  template<typename T>
143  struct has_buffer_apply_helper<T, std::enable_if_t<madness::is_serializable_v<madness::archive::BufferVisitorArchive<buffer_apply_dummy_fn>, std::decay_t<T>>>>
144  : std::true_type
145  { };
146 
147 } // namespace ttg::detail
148 
149 #else
150 
151 namespace ttg::detail {
152  template<typename T, typename Fn>
153  void buffer_apply(T&& t, Fn&& fn) {
154  static_assert(ttg::meta::is_void_v<Fn>, "Types using ttg::Buffer must be MADNESS serializable.");
155  }
156 
157 } // namespace ttg::detail
158 
159 #endif // TTG_SERIALIZATION_SUPPORTS_MADNESS
160 
161 #endif // TTG_buffer_H
std::integral_constant< bool,(Flags &const_) !=0 > is_const
constexpr const bool has_buffer_apply_v
Definition: buffer.h:50
void buffer_apply(T &&t, Fn &&fn)
Definition: buffer.h:153
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8
int size(World world=default_execution_context())
Definition: run.h:89
N.B. contains values of F_n and F_{n-1}.
Definition: fibonacci.cc:5