expr.hh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Operation nodes for expression templates.
  3. // (c) Daniel Llorens - 2011-2022
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #pragma once
  9. #include "match.hh"
  10. namespace ra {
  11. // ---------------------------
  12. // reframe
  13. // ---------------------------
  14. // Reframe is a variant of transpose that works on any array iterator. As in transpose(), one names
  15. // the destination axis for each original axis. However, unlike general transpose, axes may not be
  16. // repeated. The main application is the rank conjunction below.
  17. template <class T> constexpr T zerostep = 0;
  18. template <class ... T> constexpr std::tuple<T ...> zerostep<std::tuple<T ...>> = { zerostep<T> ... };
  19. // Dest is a list of destination axes [l0 l1 ... li ... l(rank(A)-1)].
  20. // The dimensions of the reframed A are numbered as [0 ... k ... max(l)-1].
  21. // If li = k for some i, then axis k of the reframed A moves on axis i of the original iterator A.
  22. // If not, then axis k of the reframed A is 'dead' and doesn't move the iterator.
  23. // TODO invalid for RANK_ANY (since Dest is compile time). [ra07]
  24. template <class Dest, class A>
  25. struct Reframe
  26. {
  27. A a;
  28. constexpr static int orig(int k) { return mp::int_list_index<Dest>(k); }
  29. constexpr static rank_t rank_s() { return 1+mp::fold<mp::max, mp::int_t<-1>, Dest>::value; }
  30. constexpr static rank_t rank() { return rank_s(); }
  31. constexpr static dim_t len_s(int k)
  32. {
  33. int l = orig(k);
  34. return l>=0 ? std::decay_t<A>::len_s(l) : DIM_BAD;
  35. }
  36. constexpr dim_t len(int k) const
  37. {
  38. int l = orig(k);
  39. return l>=0 ? a.len(l) : DIM_BAD;
  40. }
  41. constexpr void adv(rank_t k, dim_t d)
  42. {
  43. if (int l = orig(k); l>=0) {
  44. a.adv(l, d);
  45. }
  46. }
  47. constexpr auto step(int k) const
  48. {
  49. int l = orig(k);
  50. return l>=0 ? a.step(l) : zerostep<decltype(a.step(l))>;
  51. }
  52. constexpr bool keep_step(dim_t st, int z, int j) const
  53. {
  54. int wz = orig(z);
  55. int wj = orig(j);
  56. return wz>=0 && wj>=0 && a.keep_step(st, wz, wj);
  57. }
  58. template <class I>
  59. constexpr decltype(auto) at(I const & i)
  60. {
  61. return a.at(mp::map_indices<std::array<dim_t, mp::len<Dest>>, Dest>(i));
  62. }
  63. constexpr decltype(auto) flat() { return a.flat(); }
  64. };
  65. // Optimize no-op case.
  66. // TODO If A is cell_iterator_big, etc. beat Dest directly on it, same for eventual transpose_expr<>.
  67. template <class Dest, class A> decltype(auto)
  68. reframe(A && a)
  69. {
  70. if constexpr (std::is_same_v<Dest, mp::iota<1+mp::fold<mp::max, mp::int_t<-1>, Dest>::value>>) {
  71. return std::forward<A>(a);
  72. } else {
  73. return Reframe<Dest, A> { std::forward<A>(a) };
  74. }
  75. }
  76. // ---------------------------
  77. // verbs and rank conjunction
  78. // ---------------------------
  79. template <class cranks_, class Op_>
  80. struct Verb
  81. {
  82. using cranks = cranks_;
  83. using Op = Op_;
  84. Op op;
  85. };
  86. RA_IS_DEF(is_verb, (std::is_same_v<A, Verb<typename A::cranks, typename A::Op>>))
  87. template <class cranks, class Op> inline constexpr auto
  88. wrank(cranks cranks_, Op && op)
  89. {
  90. return Verb<cranks, Op> { std::forward<Op>(op) };
  91. }
  92. template <rank_t ... crank, class Op> inline constexpr auto
  93. wrank(Op && op)
  94. {
  95. return Verb<mp::int_list<crank ...>, Op> { std::forward<Op>(op) };
  96. }
  97. template <class V, class T, class R=mp::makelist<mp::len<T>, mp::nil>, rank_t skip=0>
  98. struct Framematch_def;
  99. template <class V, class T, class R=mp::makelist<mp::len<T>, mp::nil>, rank_t skip=0>
  100. using Framematch = Framematch_def<std::decay_t<V>, T, R, skip>;
  101. template <class A, class B>
  102. struct max_i
  103. {
  104. constexpr static int value = gt_rank(A::value, B::value) ? 0 : 1; // 0 if ra wins, else 1
  105. };
  106. // Get a list (per argument) of lists of live axes. The last frame match is handled by standard prefix matching.
  107. template <class ... crank, class W, class ... Ti, class ... Ri, rank_t skip>
  108. struct Framematch_def<Verb<std::tuple<crank ...>, W>, std::tuple<Ti ...>, std::tuple<Ri ...>, skip>
  109. {
  110. static_assert(sizeof...(Ti)==sizeof...(crank) && sizeof...(Ti)==sizeof...(Ri), "bad args");
  111. // live = number of live axes on this frame, for each argument. // TODO crank negative, inf.
  112. using live = mp::int_list<(rank_s<Ti>() - mp::len<Ri> - crank::value) ...>;
  113. using frameaxes = std::tuple<mp::append<Ri, mp::iota<(rank_s<Ti>() - mp::len<Ri> - crank::value), skip>> ...>;
  114. using FM = Framematch<W, std::tuple<Ti ...>, frameaxes, skip + mp::ref<live, mp::indexof<max_i, live>>::value>;
  115. using R = typename FM::R;
  116. template <class VV> static decltype(auto) op(VV && v) { return FM::op(std::forward<VV>(v).op); } // cf [ra31]
  117. };
  118. // Terminal case where V doesn't have rank (is a raw op()).
  119. template <class V, class ... Ti, class ... Ri, rank_t skip>
  120. struct Framematch_def<V, std::tuple<Ti ...>, std::tuple<Ri ...>, skip>
  121. {
  122. static_assert(sizeof...(Ti)==sizeof...(Ri), "bad args");
  123. // TODO -crank::value when the actual verb rank is used (eg to use cell_iterator_big<A, that_rank> instead of just begin()).
  124. using R = std::tuple<mp::append<Ri, mp::iota<(rank_s<Ti>() - mp::len<Ri>), skip>> ...>;
  125. template <class VV> static decltype(auto) op(VV && v) { return std::forward<VV>(v); }
  126. };
  127. // ---------------------------
  128. // general expression template
  129. // ---------------------------
  130. template <class Op, class T, class K=mp::iota<mp::len<T>>> struct Expr;
  131. template <class Op, class ... P, int ... I>
  132. struct Expr<Op, std::tuple<P ...>, mp::int_list<I ...>>: public Match<true, std::tuple<P ...>>
  133. {
  134. template <class T_>
  135. struct Flat
  136. {
  137. Op & op;
  138. T_ t;
  139. template <class S> constexpr void operator+=(S const & s) { ((std::get<I>(t) += std::get<I>(s)), ...); }
  140. // FIXME cannot figure out why gcc 12.1 flags this (-O3 only).
  141. #pragma GCC diagnostic push
  142. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  143. constexpr decltype(auto) operator*() { return op(*std::get<I>(t) ...); }
  144. #pragma GCC diagnostic pop
  145. };
  146. template <class ... P_> inline constexpr static auto
  147. flat(Op & op, P_ && ... p)
  148. {
  149. return Flat<std::tuple<P_ ...>> { op, std::tuple<P_ ...> { std::forward<P_>(p) ... } };
  150. }
  151. using Match_ = Match<true, std::tuple<P ...>>;
  152. Op op;
  153. // test/ra-9.cc [ra1]
  154. constexpr Expr(Op op_, P ... p_): Match_(std::forward<P>(p_) ...), op(std::forward<Op>(op_)) {}
  155. RA_DEF_ASSIGNOPS_SELF(Expr)
  156. RA_DEF_ASSIGNOPS_DEFAULT_SET
  157. template <class J> constexpr decltype(auto)
  158. at(J const & i)
  159. {
  160. return op(std::get<I>(this->t).at(i) ...);
  161. }
  162. template <class J> constexpr decltype(auto)
  163. at(J const & i) const
  164. {
  165. return op(std::get<I>(this->t).at(i) ...);
  166. }
  167. constexpr decltype(auto)
  168. flat()
  169. {
  170. return flat(op, std::get<I>(this->t).flat() ...);
  171. }
  172. // needed for xpr with rank_s()==RANK_ANY, which don't decay to scalar when used as operator arguments.
  173. operator decltype(*(flat(op, std::get<I>(Match_::t).flat() ...))) ()
  174. {
  175. if constexpr (this->rank_s()!=1 || size_s(*this)!=1) { // for coord types; so fixed only
  176. if constexpr (this->rank_s()!=0) {
  177. static_assert(this->rank_s()==RANK_ANY);
  178. assert(this->rank()==0);
  179. }
  180. }
  181. return *flat();
  182. }
  183. };
  184. template <class V, class ... T, int ... i> inline constexpr auto
  185. expr_verb(mp::int_list<i ...>, V && v, T && ... t)
  186. {
  187. using FM = Framematch<V, std::tuple<T ...>>;
  188. return expr(FM::op(std::forward<V>(v)), reframe<mp::ref<typename FM::R, i>>(std::forward<T>(t)) ...);
  189. }
  190. template <class Op, class ... P> inline constexpr auto
  191. expr(Op && op, P && ... p)
  192. {
  193. if constexpr (is_verb<Op>) {
  194. return expr_verb(mp::iota<sizeof...(P)> {}, std::forward<Op>(op), std::forward<P>(p) ...);
  195. } else {
  196. return Expr<Op, std::tuple<P ...>> { std::forward<Op>(op), std::forward<P>(p) ... };
  197. }
  198. }
  199. template <class Op, class ... A> inline constexpr auto
  200. map(Op && op, A && ... a)
  201. {
  202. return expr(std::forward<Op>(op), start(std::forward<A>(a)) ...);
  203. }
  204. // ---------------
  205. // explicit agreement checks. FIXME provide separate agree_s().
  206. // ---------------
  207. template <class ... P> inline constexpr bool
  208. agree(P && ... p)
  209. {
  210. return agree_(ra::start(std::forward<P>(p)) ...);
  211. }
  212. template <class Op, class ... P> inline constexpr bool
  213. agree_op(Op && op, P && ... p)
  214. {
  215. return agree_op_(std::forward<Op>(op), ra::start(std::forward<P>(p)) ...);
  216. }
  217. template <class ... P> inline constexpr bool
  218. agree_(P && ... p)
  219. {
  220. using Match_ = Match<false, std::tuple<P ...>>;
  221. return check_expr<false>(Match_ { std::forward<P>(p) ... });
  222. }
  223. template <class Op, class ... P> inline constexpr bool
  224. agree_op_(Op && op, P && ... p)
  225. {
  226. if constexpr (is_verb<Op>) {
  227. return agree_verb(mp::iota<sizeof...(P)> {}, std::forward<Op>(op), std::forward<P>(p) ...);
  228. } else {
  229. return agree_(std::forward<P>(p) ...);
  230. }
  231. }
  232. template <class V, class ... T, int ... i> inline constexpr bool
  233. agree_verb(mp::int_list<i ...>, V && v, T && ... t)
  234. {
  235. using FM = Framematch<V, std::tuple<T ...>>;
  236. return agree_op_(FM::op(std::forward<V>(v)), reframe<mp::ref<typename FM::R, i>>(std::forward<T>(t)) ...);
  237. }
  238. } // namespace ra