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