xtensor
Loading...
Searching...
No Matches
xassign.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_ASSIGN_HPP
11#define XTENSOR_ASSIGN_HPP
12
13#include <algorithm>
14#include <functional>
15#include <type_traits>
16#include <utility>
17
18#include <xtl/xcomplex.hpp>
19#include <xtl/xsequence.hpp>
20
21#include "../core/xassign_proxy.hpp"
22#include "../core/xassigner.hpp"
23#include "../core/xexpression.hpp"
24#include "../core/xfunction.hpp"
25#include "../core/xiterator.hpp"
26#include "../core/xstrides.hpp"
27#include "../core/xtensor_config.hpp"
28#include "../core/xtensor_forward.hpp"
29#include "../utils/xutils.hpp"
30
31namespace xt
32{
33
34 /********************
35 * Assign functions *
36 ********************/
37
38 template <class E1, class E2, class P>
39 void computed_assign(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
40
41 template <class E1, class E2, class F, class P>
42 void scalar_computed_assign(xexpression<E1>& e1, const E2& e2, F&& f, P p);
43
44 template <class E1, class E2, class P>
45 void assign_to_container(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
46
47 template <class E1, class E2, class P>
48 void assign_to_view(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
49
50 /************************
51 * xexpression_assigner *
52 ************************/
53
54 template <class Tag>
56
57 template <>
59 {
60 public:
61
62 template <class E1, class E2, class P>
63 static void assign_data(xexpression<E1>& e1, const xexpression<E2>& e2, bool trivial, P p);
64 };
65
66 template <class Tag>
68 {
69 public:
70
71 using base_type = xexpression_assigner_base<Tag>;
72
73 template <class E1, class E2, class P>
74 static void computed_assign(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
75
76 template <class E1, class E2, class F, class P>
77 static void scalar_computed_assign(xexpression<E1>& e1, const E2& e2, F&& f, P p);
78
79 template <class E1, class E2, class P>
80 static void assign_to_container(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
81
82 template <class E1, class E2, class P>
83 static void assign_to_view(xexpression<E1>& e1, const xexpression<E2>& e2, P p);
84
85 private:
86
87 template <class S, class E>
88 static S get_nobroadcast_shape(const E& e);
89
90 template <class S, class F, class... CT>
91 static S get_nobroadcast_shape(const xfunction<F, CT...>& f);
92
93 template <class S, size_t I, class... CT>
94 static S get_nobroadcast_shape(const std::tuple<CT...>& args);
95
96 template <class E1, class E2, class P>
97 static bool resize(E1& e1, const E2& e2, P);
98
99 template <class E1, class F, class... CT, class P>
100 static bool resize(E1& e1, const xfunction<F, CT...>& e2, P);
101
102 template <class E1, class E2, class P>
103 static void assert_compatible_shape(const xexpression<E1>& e1, const xexpression<E2>& e2, P);
104 };
105
106 /***********************************
107 * Assign functions implementation *
108 ***********************************/
109
110 template <class E1, class E2, class P>
111 inline void computed_assign(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
112 {
113 using tag = xexpression_tag_t<E1, E2>;
114 xexpression_assigner<tag>::computed_assign(e1, e2, p);
115 }
116
117 template <class E1, class E2, class F, class P>
118 inline void scalar_computed_assign(xexpression<E1>& e1, const E2& e2, F&& f, P p)
119 {
120 using tag = xexpression_tag_t<E1, E2>;
121 xexpression_assigner<tag>::scalar_computed_assign(e1, e2, std::forward<F>(f), p);
122 }
123
124 template <class E1, class E2, class P>
125 inline void assign_to_container(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
126 {
128 {
129 e2.derived_cast().assign_to(e1);
130 }
131 else
132 {
133 using tag = xexpression_tag_t<E1, E2>;
134 xexpression_assigner<tag>::assign_to_container(e1, e2, p);
135 }
136 }
137
138 template <class E1, class E2, class P>
139 inline void assign_to_view(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
140 {
141 using tag = xexpression_tag_t<E1, E2>;
142 xexpression_assigner<tag>::assign_to_view(e1, e2, p);
143 }
144
145 /***************************************
146 * xexpression_assigner implementation *
147 ***************************************/
148
149 namespace detail
150 {
151 template <class E1, class E2>
152 constexpr bool linear_static_layout()
153 {
154 // A row_major or column_major container with a dimension <= 1 is computed as
155 // layout any, leading to some performance improvements, for example when
156 // assigning a col-major vector to a row-major vector etc
157 return compute_layout(
158 select_layout<E1::static_layout, typename E1::shape_type>::value,
159 select_layout<E2::static_layout, typename E2::shape_type>::value
160 )
162 }
163
164 template <class E1, class E2>
165 inline auto is_linear_assign(const E1& e1, const E2& e2)
166 -> std::enable_if_t<strided_expression<E1>, bool>
167 {
168 return (E1::contiguous_layout && E2::contiguous_layout && linear_static_layout<E1, E2>())
169 || (e1.is_contiguous() && e2.has_linear_assign(e1.strides()));
170 }
171
172 template <class E1, class E2>
173 inline auto is_linear_assign(const E1&, const E2&) -> std::enable_if_t<!strided_expression<E1>, bool>
174 {
175 return false;
176 }
177
178 template <class E1, class E2>
179 inline bool linear_dynamic_layout(const E1& e1, const E2& e2)
180 {
181 return e1.is_contiguous() && e2.is_contiguous()
182 && compute_layout(e1.layout(), e2.layout()) != layout_type::dynamic;
183 }
184
185 template <class E, class = void>
186 struct has_step_leading : std::false_type
187 {
188 };
189
190 template <class E>
191 struct has_step_leading<E, void_t<decltype(std::declval<E>().step_leading())>> : std::true_type
192 {
193 };
194
195 template <class T>
196 struct use_strided_loop
197 {
198 static constexpr bool stepper_deref()
199 {
200 return std::is_reference<typename T::stepper::reference>::value;
201 }
202
203 static constexpr bool value = strided_expression<T>
204 && has_step_leading<typename T::stepper>::value && stepper_deref();
205 };
206
207 template <class T>
208 struct use_strided_loop<xscalar<T>>
209 {
210 static constexpr bool value = true;
211 };
212
213 template <class F, class... CT>
214 struct use_strided_loop<xfunction<F, CT...>>
215 {
216 static constexpr bool value = std::conjunction<use_strided_loop<std::decay_t<CT>>...>::value;
217 };
218 }
219
220 template <class E1, class E2>
222 {
223 private:
224
225 using e1_value_type = typename E1::value_type;
226 using e2_value_type = typename E2::value_type;
227
228 template <class T>
229 using is_bool = std::is_same<T, bool>;
230
231 static constexpr bool is_bool_conversion()
232 {
233 return is_bool<e2_value_type>::value && !is_bool<e1_value_type>::value;
234 }
235
236 static constexpr bool contiguous_layout()
237 {
238 return E1::contiguous_layout && E2::contiguous_layout;
239 }
240
241 static constexpr bool convertible_types()
242 {
243 return std::is_convertible<e2_value_type, e1_value_type>::value && !is_bool_conversion();
244 }
245
246 static constexpr bool use_xsimd()
247 {
248 return xt_simd::simd_traits<int8_t>::size > 1;
249 }
250
251 template <class T>
252 static constexpr bool simd_size_impl()
253 {
254 return xt_simd::simd_traits<T>::size > 1 || (is_bool<T>::value && use_xsimd());
255 }
256
257 static constexpr bool simd_size()
258 {
259 return simd_size_impl<e1_value_type>() && simd_size_impl<e2_value_type>();
260 }
261
262 static constexpr bool simd_interface()
263 {
264 using rvt = requested_value_type_t<E1, E2>;
266 }
267
268 public:
269
270 // constexpr methods instead of constexpr data members avoid the need of definitions at namespace
271 // scope of these data members (since they are odr-used).
272
273 static constexpr bool simd_assign()
274 {
275 return convertible_types() && simd_size() && simd_interface();
276 }
277
278 static constexpr bool linear_assign(const E1& e1, const E2& e2, bool trivial)
279 {
280 return trivial && detail::is_linear_assign(e1, e2);
281 }
282
283 static constexpr bool strided_assign()
284 {
285 return detail::use_strided_loop<E1>::value && detail::use_strided_loop<E2>::value;
286 }
287
288 static constexpr bool simd_linear_assign()
289 {
290 return contiguous_layout() && simd_assign();
291 }
292
293 static constexpr bool simd_strided_assign()
294 {
295 return strided_assign() && simd_assign();
296 }
297
298 static constexpr bool simd_linear_assign(const E1& e1, const E2& e2)
299 {
300 return simd_assign() && detail::linear_dynamic_layout(e1, e2);
301 }
302 };
303
304 template <class E1, class E2, class P>
306 xexpression<E1>& e1,
307 const xexpression<E2>& e2,
308 bool trivial,
309 P
310 )
311 {
312 E1& de1 = e1.derived_cast();
313 const E2& de2 = e2.derived_cast();
314 using traits = xassign_traits<E1, E2>;
315
316 bool linear_assign = traits::linear_assign(de1, de2, trivial);
317 constexpr bool simd_assign = traits::simd_assign();
318 constexpr bool simd_linear_assign = traits::simd_linear_assign();
319 constexpr bool simd_strided_assign = traits::simd_strided_assign();
320
321 parallel_assigner_t<E1, E2, typename P::parallelism> assigner;
322
323 if (linear_assign)
324 {
325 if (simd_linear_assign || traits::simd_linear_assign(de1, de2))
326 {
327 // Do not use linear_assigner<true> here since it will make the compiler
328 // instantiate this branch even if the runtime condition is false, resulting
329 // in compilation error for expressions that do not provide a SIMD interface.
330 // simd_assign is true if simd_linear_assign() or simd_linear_assign(de1, de2)
331 // is true.
332 assigner.template linear_assign<simd_assign>(de1, de2);
333 }
334 else
335 {
336 assigner.template linear_assign<false>(de1, de2);
337 }
338 }
339 else
340 {
341 if constexpr (simd_strided_assign)
342 {
343 assigner.strided_loop_assign(de1, de2);
344 }
345 else
346 {
347 assigner.template stepper_assign<default_assignable_layout(E1::static_layout)>(de1, de2);
348 }
349 }
350 }
351
352 template <class Tag>
353 template <class E1, class E2, class P>
354 inline void
355 xexpression_assigner<Tag>::assign_to_container(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
356 {
357 bool trivial_broadcast = resize(e1.derived_cast(), e2.derived_cast(), p);
358 base_type::assign_data(e1, e2, trivial_broadcast, p);
359 }
360
361 namespace detail
362 {
363 template <class F, class P>
364 bool get_rhs_triviality(const F&, P)
365 {
366 return true;
367 }
368
369 template <class F, class R, class... CT, class P>
370 bool get_rhs_triviality(const xfunction<F, R, CT...>& rhs, P)
371 {
372 if constexpr (has_broadcasting<P>)
373 {
374 using index_type = xindex_type_t<typename xfunction<F, R, CT...>::shape_type>;
375 using size_type = typename index_type::size_type;
376 size_type size = rhs.dimension();
377 index_type shape = uninitialized_shape<index_type>(size);
378 bool trivial_broadcast = rhs.broadcast_shape(shape, true);
379 return trivial_broadcast;
380 }
381 else
382 {
383 return true;
384 }
385 }
386 }
387
388 template <class Tag>
389 template <class E1, class E2, class P>
390 inline void xexpression_assigner<Tag>::assign_to_view(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
391 {
392 assert_compatible_shape(e1, e2, p);
393 base_type::assign_data(e1, e2, detail::get_rhs_triviality(e2.derived_cast(), p), p);
394 }
395
396 template <class Tag>
397 template <class E1, class E2, class P>
398 inline void xexpression_assigner<Tag>::computed_assign(xexpression<E1>& e1, const xexpression<E2>& e2, P p)
399 {
400 using shape_type = typename E1::shape_type;
401 using comperator_type = std::greater<typename shape_type::value_type>;
402
403 using size_type = typename E1::size_type;
404
405 E1& de1 = e1.derived_cast();
406 const E2& de2 = e2.derived_cast();
407
408 if constexpr (has_broadcasting<P>)
409 {
410 size_type dim2 = de2.dimension();
411 shape_type shape = uninitialized_shape<shape_type>(dim2);
412
413 bool trivial_broadcast = de2.broadcast_shape(shape, true);
414
415 auto&& de1_shape = de1.shape();
416 if (dim2 > de1.dimension()
417 || std::lexicographical_compare(
418 shape.begin(),
419 shape.end(),
420 de1_shape.begin(),
421 de1_shape.end(),
422 comperator_type()
423 ))
424 {
425 typename E1::temporary_type tmp(shape);
426 base_type::assign_data(tmp, e2, trivial_broadcast, p);
427 de1.assign_temporary(std::move(tmp));
428 }
429 else
430 {
431 base_type::assign_data(e1, e2, trivial_broadcast, p);
432 }
433 }
434 else
435 {
436 XTENSOR_ASSERT(same_shape(de1.shape(), de2.shape()));
437 base_type::assign_data(e1, e2, true, p);
438 }
439 }
440
441 template <class Tag>
442 template <class E1, class E2, class F, class P>
443 inline void xexpression_assigner<Tag>::scalar_computed_assign(xexpression<E1>& e1, const E2& e2, F&& f, P)
444 {
445 E1& d = e1.derived_cast();
446 using size_type = typename E1::size_type;
447 auto dst = d.storage().begin();
448 for (size_type i = d.size(); i > 0; --i)
449 {
450 *dst = f(*dst, e2);
451 ++dst;
452 }
453 }
454
455 namespace detail
456 {
457 template <bool B, class... CT>
458 struct static_trivial_broadcast;
459
460 template <class... CT>
461 struct static_trivial_broadcast<true, CT...>
462 {
463 static constexpr bool value = detail::promote_index<typename std::decay_t<CT>::shape_type...>::value;
464 };
465
466 template <class... CT>
467 struct static_trivial_broadcast<false, CT...>
468 {
469 static constexpr bool value = false;
470 };
471 }
472
473 template <class Tag>
474 template <class E1, class E2, class P>
475 inline bool xexpression_assigner<Tag>::resize(E1& e1, const E2& e2, P)
476 {
477 // If our RHS is not a xfunction, we know that the RHS is at least potentially trivial
478 // We check the strides of the RHS in detail::is_trivial_broadcast to see if they match up!
479 // So we can skip a shape copy and a call to broadcast_shape(...)
480 e1.resize(e2.shape());
481 return true;
482 }
483
484 template <class Tag>
485 template <class S, class E>
486 inline S xexpression_assigner<Tag>::get_nobroadcast_shape(const E& e)
487 {
488 const auto& shape = e.shape();
489 return xtl::forward_sequence<S, decltype(shape)>(shape);
490 }
491
492 template <class Tag>
493 template <class S, class F, class... CT>
494 inline S xexpression_assigner<Tag>::get_nobroadcast_shape(const xfunction<F, CT...>& f)
495 {
496 return get_nobroadcast_shape<S, 0>(f.arguments());
497 }
498
499 template <class Tag>
500 template <class S, size_t I, class... CT>
501 inline S xexpression_assigner<Tag>::get_nobroadcast_shape(const std::tuple<CT...>& args)
502 {
503 if constexpr (I == sizeof...(CT))
504 {
505 return S{};
506 }
507 else if constexpr (xscalar_concept<std::tuple_element_t<I, std::tuple<CT...>>>)
508 {
509 return get_nobroadcast_shape<S, I + 1>(args);
510 }
511 else
512 {
513 return get_nobroadcast_shape<S>(std::get<I>(args));
514 }
515 }
516
517 template <class Tag>
518 template <class E1, class F, class... CT, class P>
519 inline bool xexpression_assigner<Tag>::resize(E1& e1, const xfunction<F, CT...>& e2, P)
520 {
521 if constexpr (detail::is_fixed<typename xfunction<F, CT...>::shape_type>::value)
522 {
523 /*
524 * If the shape of the xfunction is statically known, we can compute the broadcast triviality
525 * at compile time plus we can resize right away.
526 */
527 // resize in case LHS is not a fixed size container. If it is, this is a NOP
528 e1.resize(typename xfunction<F, CT...>::shape_type{});
529 return detail::static_trivial_broadcast<
530 detail::is_fixed<typename xfunction<F, CT...>::shape_type>::value,
531 CT...>::value;
532 }
533 else
534 {
535 if constexpr (has_broadcasting<P>)
536 {
537 using index_type = xindex_type_t<typename E1::shape_type>;
538 using size_type = typename E1::size_type;
539 size_type size = e2.dimension();
540 index_type shape = uninitialized_shape<index_type>(size);
541 bool trivial_broadcast = e2.broadcast_shape(shape, true);
542 e1.resize(std::move(shape));
543 return trivial_broadcast;
544 }
545 else
546 {
547 using shape_type = typename E1::shape_type;
548 shape_type shape = get_nobroadcast_shape<shape_type>(e2);
549 e1.resize(std::move(shape));
550 return true;
551 }
552 }
553 }
554
555 template <class Tag>
556 template <class E1, class E2, class P>
557 inline void
558 xexpression_assigner<Tag>::assert_compatible_shape(const xexpression<E1>& e1, const xexpression<E2>& e2, P)
559 {
560 const E1& de1 = e1.derived_cast();
561 const E2& de2 = e2.derived_cast();
562 if constexpr (has_broadcasting<P>)
563 {
564 if (!broadcastable(de2.shape(), de1.shape()))
565 {
566 throw_broadcast_error(de2.shape(), de1.shape());
567 }
568 }
569 else
570 {
571 if (!same_shape(de1.shape(), de2.shape()))
572 {
573 throw_broadcast_error(de2.shape(), de1.shape());
574 }
575 }
576 }
577}
578
579#endif
Base class for xexpressions.
derived_type & derived_cast() &noexcept
Returns a reference to the actual derived type of the xexpression.
Multidimensional function operating on xtensor expressions.
bool same_shape(const S1 &s1, const S2 &s2) noexcept
Check if two objects have the same shape.
Definition xshape.hpp:109
standard mathematical functions for xexpressions
constexpr layout_type compute_layout(Args... args) noexcept
Implementation of the following logical table:
Definition xlayout.hpp:88