-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzfunction.hpp
More file actions
358 lines (301 loc) · 11.9 KB
/
zfunction.hpp
File metadata and controls
358 lines (301 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ZFUNCTION_HPP
#define XTENSOR_ZFUNCTION_HPP
#include <tuple>
#include <utility>
#include "zdispatcher.hpp"
#include "zarray_impl_register.hpp"
#include "zarray_temporary_pool.hpp"
#include "zwrappers.hpp"
namespace xt
{
template <class F, class... CT>
class zfunction : public xexpression<zfunction<F, CT...>>
{
public:
using expression_tag = zarray_expression_tag;
using self_type = zfunction<F, CT...>;
using tuple_type = std::tuple<CT...>;
using functor_type = F;
using shape_type = dynamic_shape<std::size_t>;
template <class Func, class... CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
zfunction(Func&& f, CTA&&... e) noexcept;
std::size_t dimension() const;
const shape_type& shape() const;
bool broadcast_shape(shape_type& shape, bool reuse_cache = false) const;
std::unique_ptr<zarray_impl> allocate_result() const;
std::size_t get_result_type_index() const;
zarray_impl& assign_to(zarray_impl& res, const zassign_args& args) const;
zarray_impl& assign_to(detail::zarray_temporary_pool & res, const zassign_args& args) const;
private:
std::size_t get_result_type_index_impl() const;
using dispatcher_type = zdispatcher_t<F, sizeof...(CT)>;
std::size_t compute_dimension() const;
template <std::size_t... I>
std::size_t get_result_type_index_impl(std::index_sequence<I...>) const;
template<std::size_t ... I>
zarray_impl& assign_to_impl(std::index_sequence<I ...>, detail::zarray_temporary_pool & temporary_pool, const zassign_args& args) const;
struct cache
{
cache() : m_shape(), m_initialized(false), m_trivial_broadcast(false) {}
shape_type m_shape;
bool m_initialized;
bool m_trivial_broadcast;
};
tuple_type m_e;
mutable cache m_cache;
std::size_t m_result_type_index;
};
namespace detail
{
template <class E>
struct zargument_type
{
using type = E;
};
template <class T>
struct zargument_type<xscalar<T>>
{
using type = zscalar_wrapper<xscalar<T>>;
};
template <class E>
using zargument_type_t = typename zargument_type<E>::type;
template <class F, class... E>
struct select_xfunction_expression<zarray_expression_tag, F, E...>
{
using type = zfunction<F, zargument_type_t<E>...>;
};
}
/****************************
* zfunction implementation *
****************************/
class zarray;
namespace detail
{
// this can be a zreducer or something similar
template <class E>
struct zfunction_argument
{
using argument_type = E;
static std::size_t get_index(const argument_type& e)
{
return e.get_result_type_index();
}
static const std::tuple<const zarray_impl*, bool> get_array_impl(const argument_type & e, detail::zarray_temporary_pool & temporary_pool, const zassign_args& args)
{
auto buffer_ptr = temporary_pool.get_free_buffer(e.get_result_type_index());
const auto & array_impl = e.assign_to(*buffer_ptr, args);
return std::make_tuple(&array_impl, true);
}
};
template <class F, class... CT>
struct zfunction_argument<zfunction<F, CT ...>>
{
using argument_type = zfunction<F, CT ...>;
static std::size_t get_index(const argument_type & e)
{
return e.get_result_type_index();
}
static std::tuple<const zarray_impl*, bool> get_array_impl(const argument_type & e, detail::zarray_temporary_pool & temporary_pool, const zassign_args& args)
{
const auto & array_impl = e.assign_to(temporary_pool, args);
return std::make_tuple(&array_impl, true);
}
};
template <>
struct zfunction_argument<zarray>
{
using argument_type = zarray;
template <class E>
static std::size_t get_index(const E& e)
{
return e.get_implementation().get_class_index();
}
template <class E>
static std::tuple<const zarray_impl*, bool> get_array_impl(const E& e, detail::zarray_temporary_pool & , const zassign_args&)
{
auto & impl = e.get_implementation();
return std::make_tuple(&impl, false);
}
};
template <class CTE>
struct zfunction_argument<zscalar_wrapper<CTE>>
{
using argument_type = zscalar_wrapper<CTE>;
static std::size_t get_index(const argument_type& e)
{
return e.get_class_index();
}
static std::tuple<const zarray_impl*, bool> get_array_impl(const argument_type& e, detail::zarray_temporary_pool &, const zassign_args&)
{
const zarray_impl & impl = e;
return std::make_tuple(&impl, false);
}
};
template <class E>
inline size_t get_result_type_index(const E& e)
{
return zfunction_argument<E>::get_index(e);
}
template <class E>
inline std::tuple<const zarray_impl*, bool> get_array_impl(const E& e, detail::zarray_temporary_pool & temporary_pool, const zassign_args& args)
{
return zfunction_argument<E>::get_array_impl(e, temporary_pool, args);
}
}
template <class F, class... CT>
template <class Func, class... CTA, class U>
inline zfunction<F, CT...>::zfunction(Func&&, CTA&&... e) noexcept
: m_e(std::forward<CTA>(e)...),
m_cache(),
m_result_type_index()
{
m_result_type_index = this->get_result_type_index_impl(std::make_index_sequence<sizeof...(CT)>());
}
template <class F, class... CT>
inline std::size_t zfunction<F, CT...>::dimension() const
{
return m_cache.m_initialized ? m_cache.m_shape.size() : compute_dimension();
}
template <class F, class... CT>
inline auto zfunction<F, CT...>::shape() const -> const shape_type&
{
if (!m_cache.m_initialized)
{
m_cache.m_shape = uninitialized_shape<shape_type>(compute_dimension());
m_cache.m_trivial_broadcast = broadcast_shape(m_cache.m_shape, false);
m_cache.m_initialized = true;
}
return m_cache.m_shape;
}
template <class F, class... CT>
inline bool zfunction<F, CT...>::broadcast_shape(shape_type& shape, bool reuse_cache) const
{
if (reuse_cache && m_cache.m_initialized)
{
std::copy(m_cache.m_shape.cbegin(), m_cache.m_shape.cend(), shape.begin());
return m_cache.m_trivial_broadcast;
}
else
{
auto func = [&shape](bool b, const auto& e) { return e.broadcast_shape(shape) && b; };
return accumulate(func, true, m_e);
}
}
template <class F, class... CT>
inline std::unique_ptr<zarray_impl> zfunction<F, CT...>::allocate_result() const
{
std::size_t idx = get_result_type_index();
return std::unique_ptr<zarray_impl>(zarray_impl_register::get(idx).clone());
}
template <class F, class... CT>
inline std::size_t zfunction<F, CT...>::get_result_type_index() const
{
return m_result_type_index;
}
template <class F, class... CT>
inline zarray_impl& zfunction<F, CT...>::assign_to(zarray_impl& res, const zassign_args& args) const
{
detail::zarray_temporary_pool temporary_pool(res);
auto & r = this->assign_to(temporary_pool, args);
if(&r != &res)
{
// this has great overlap with zarrays assigment operator
// and detail::run_chunked_assign_loop
zassign_args args;
args.trivial_broadcast = true;
if (res.is_chunked())
{
const zchunked_array& arr = dynamic_cast<const zchunked_array&>(res);
args.chunk_iter = arr.chunk_begin();
args.chunk_assign = true;
auto chunk_end = arr.chunk_end();
while (args.chunk_iter != chunk_end)
{
zdispatcher_t<detail::xassign_dummy_functor, 1>::dispatch(r, res, args);
++args.chunk_iter;
}
}
else
{
zdispatcher_t<detail::xassign_dummy_functor, 1>::dispatch(r, res, args);
}
}
return res;
}
template <class F, class... CT>
inline zarray_impl& zfunction<F, CT...>::assign_to(detail::zarray_temporary_pool & buffer, const zassign_args& args) const
{
return assign_to_impl(std::make_index_sequence<sizeof...(CT)>(), buffer, args);
}
template <class F, class... CT>
inline std::size_t zfunction<F, CT...>::compute_dimension() const
{
auto func = [](std::size_t d, auto&& e) noexcept { return (std::max)(d, e.dimension()); };
return accumulate(func, std::size_t(0), m_e);
}
template <class F, class... CT>
template <std::size_t... I>
std::size_t zfunction<F, CT...>::get_result_type_index_impl(std::index_sequence<I...>) const
{
return dispatcher_type::get_type_index(
zarray_impl_register::get(
detail::get_result_type_index(std::get<I>(m_e))
)...
);
}
template <class F, class... CT>
template<std::size_t ... I>
inline zarray_impl& zfunction<F, CT...>::assign_to_impl
(
std::index_sequence<I ...>,
detail::zarray_temporary_pool & temporary_pool,
const zassign_args& args
) const
{
// inputs
constexpr std::size_t arity = sizeof...(CT);
std::array< std::tuple<const zarray_impl *, bool>, arity> inputs = {
detail::get_array_impl(std::get<I>(m_e), temporary_pool, args) ...
};
// the output
zarray_impl * result_ptr = nullptr;
// reuse / free buffers of the arguments
for(std::size_t i=0; i<arity; ++i)
{
const auto is_buffer = std::get<1>(inputs[i]);
const auto impl_ptr = std::get<0>(inputs[i]);
// only consider buffers
if(is_buffer)
{
if(result_ptr==nullptr && m_result_type_index == impl_ptr->get_class_index())
{
// get the same buffer as non-const!
result_ptr = const_cast<zarray_impl *>(impl_ptr);
}
else
{
temporary_pool.mark_as_free(impl_ptr);
}
}
}
// in case we did not find a matching temporary
if(result_ptr == nullptr)
{
result_ptr = temporary_pool.get_free_buffer(m_result_type_index);
}
// call the operator dispatcher
dispatcher_type::dispatch(
*std::get<0>(std::get<I>(inputs))...,
*result_ptr, args);
return *result_ptr;
}
}
#endif