view-ops.H 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file view-ops.H
  3. /// @brief Operations specific to Views
  4. // (c) Daniel Llorens - 2013-2014, 2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #pragma once
  10. #include "ra/concrete.H"
  11. #include <complex>
  12. #if defined(RA_CHECK_BOUNDS) && RA_CHECK_BOUNDS==0
  13. #define CHECK_BOUNDS( cond )
  14. #else
  15. #define CHECK_BOUNDS( cond ) RA_ASSERT( cond, 0 )
  16. #endif
  17. namespace ra {
  18. template <class T, rank_t RANK> inline
  19. View<T, RANK> reverse(View<T, RANK> const & view, int k)
  20. {
  21. View<T, RANK> r = view;
  22. auto & dim = r.dim[k];
  23. if (dim.size!=0) {
  24. r.p += dim.stride*(dim.size-1);
  25. dim.stride *= -1;
  26. }
  27. return r;
  28. }
  29. // dynamic transposed axes list.
  30. template <class T, rank_t RANK, class S> inline
  31. View<T, RANK_ANY> transpose_(S && s, View<T, RANK> const & view)
  32. {
  33. CHECK_BOUNDS(view.rank()==dim_t(ra::size(s)));
  34. auto rp = std::max_element(s.begin(), s.end());
  35. rank_t dstrank = (rp==s.end() ? 0 : *rp+1);
  36. View<T, RANK_ANY> r { decltype(r.dim)(dstrank, Dim { DIM_BAD, 0 }), view.data() };
  37. int k = 0;
  38. for (int sk: s) {
  39. Dim & dest = r.dim[sk];
  40. dest.stride += view.dim[k].stride;
  41. dest.size = dest.size>=0 ? std::min(dest.size, view.dim[k].size) : view.dim[k].size;
  42. ++k;
  43. }
  44. CHECK_BOUNDS(every(map([](auto && dest) { return dest.size!=DIM_BAD; }, r.dim)) && "bad transpose axes");
  45. return r;
  46. }
  47. template <class T, rank_t RANK, class S> inline
  48. View<T, RANK_ANY> transpose(S && s, View<T, RANK> const & view)
  49. {
  50. return transpose_(std::forward<S>(s), view);
  51. }
  52. // Note that we need the compile time values and not the sizes to deduce the rank of the output, so it would be useless to provide a builtin array shim as we do with reshape().
  53. template <class T, rank_t RANK> inline
  54. View<T, RANK_ANY> transpose(std::initializer_list<ra::rank_t> s, View<T, RANK> const & view)
  55. {
  56. return transpose_(s, view);
  57. }
  58. // static transposed axes list.
  59. template <int ... Iarg, class T, rank_t RANK> inline
  60. auto transpose(View<T, RANK> const & view)
  61. {
  62. static_assert(RANK==RANK_ANY || RANK==sizeof...(Iarg), "bad output rank");
  63. CHECK_BOUNDS((view.rank()==sizeof...(Iarg)) && "bad output rank");
  64. using dummy_s = mp::makelist<sizeof...(Iarg), mp::int_t<0>>;
  65. using ti = axes_list_indices<mp::int_list<Iarg ...>, dummy_s, dummy_s>;
  66. constexpr rank_t DSTRANK = mp::len<typename ti::dst>;
  67. View<T, DSTRANK> r { decltype(r.dim)(Dim { DIM_BAD, 0 }), view.data() };
  68. int k = 0;
  69. std::array<int, sizeof...(Iarg)> s {{ Iarg ... }};
  70. for (int sk: s) {
  71. Dim & dest = r.dim[sk];
  72. dest.stride += view.dim[k].stride;
  73. dest.size = dest.size>=0 ? std::min(dest.size, view.dim[k].size) : view.dim[k].size;
  74. ++k;
  75. }
  76. return r;
  77. }
  78. template <class T, rank_t RANK> inline
  79. auto diag(View<T, RANK> const & view)
  80. {
  81. return transpose<0, 0>(view);
  82. }
  83. template <class T, rank_t RANK> inline
  84. bool is_ravel_free(View<T, RANK> const & a)
  85. {
  86. if (a.rank()>1) {
  87. auto s = a.stride(a.rank()-1);
  88. for (int i=a.rank()-2; i>=0; --i) {
  89. s *= a.size(i+1);
  90. // on size=1 we don't care about the stride.
  91. if (a.stride(i)!=s || a.size(i)==1) {
  92. return false;
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. template <class T, rank_t RANK> inline
  99. View<T, 1> ravel_free(View<T, RANK> const & a)
  100. {
  101. CHECK_BOUNDS(is_ravel_free(a));
  102. return ra::View<T, 1>({{size(a), a.stride(a.rank()-1)}}, a.p);
  103. }
  104. template <class T, rank_t RANK, class S> inline
  105. auto reshape_(View<T, RANK> const & a, S && sb_)
  106. {
  107. auto sb = concrete(std::forward<S>(sb_));
  108. // FIXME when we need to copy, accept/return Shared
  109. dim_t la = ra::size(a);
  110. dim_t lb = 1;
  111. for (int i=0; i<ra::size(sb); ++i) {
  112. if (sb[i]==-1) {
  113. dim_t quot = lb;
  114. for (int j=i+1; j<ra::size(sb); ++j) {
  115. quot *= sb[j];
  116. CHECK_BOUNDS(quot>0 && "cannot deduce dimensions");
  117. }
  118. auto pv = la/quot;
  119. CHECK_BOUNDS((la%quot==0 && pv>=0) && "bad placeholder");
  120. sb[i] = pv;
  121. lb = la;
  122. break;
  123. } else {
  124. lb *= sb[i];
  125. }
  126. }
  127. auto sa = shape(a);
  128. // FIXME should be able to reshape Scalar etc.
  129. View<T, ra::size_s(sb)> b(map([](auto i) { return Dim { DIM_BAD, 0 }; }, ra::iota(ra::size(sb))), a.data());
  130. rank_t i = 0;
  131. for (; i<a.rank() && i<b.rank(); ++i) {
  132. if (sa[a.rank()-i-1]!=sb[b.rank()-i-1]) {
  133. assert(is_ravel_free(a) && "reshape w/copy not implemented");
  134. if (la>=lb) {
  135. // FIXME View(SS const & s, T * p). Cf [ra37].
  136. for_each([](auto & dim, auto && s) { dim.size = s; }, b.dim, sb);
  137. filldim(b.dim.size(), b.dim.end());
  138. for (int j=0; j!=b.rank(); ++j) {
  139. b.dim[j].stride *= a.stride(a.rank()-1);
  140. }
  141. return b;
  142. } else {
  143. assert(0 && "reshape case not implemented");
  144. }
  145. } else {
  146. // select
  147. b.dim[b.rank()-i-1] = a.dim[a.rank()-i-1];
  148. }
  149. }
  150. if (i==a.rank()) {
  151. // tile & return
  152. for (rank_t j=i; j<b.rank(); ++j) {
  153. b.dim[b.rank()-j-1] = { sb[b.rank()-j-1], 0 };
  154. }
  155. }
  156. return b;
  157. }
  158. template <class T, rank_t RANK, class S> inline
  159. auto reshape(View<T, RANK> const & a, S && sb_)
  160. {
  161. return reshape_(a, std::forward<S>(sb_));
  162. }
  163. // We need dimtype b/c {1, ...} deduces to int and that fails to match ra::dim_t.
  164. // We could use initializer_list to handle the general case, but that would produce a var rank result because its size cannot be deduced at compile time :-/. Unfortunately an initializer_list specialization would override this one, so we cannot provide it as a fallback.
  165. template <class T, rank_t RANK, class dimtype, int N> inline
  166. auto reshape(View<T, RANK> const & a, dimtype const (&sb_)[N])
  167. {
  168. return reshape_(a, sb_);
  169. }
  170. // lo = lower bounds, hi = upper bounds.
  171. // The stencil indices are in [0 lo+1+hi] = [-lo +hi].
  172. template <class LO, class HI, class T, rank_t N> inline
  173. View<T, rank_sum(N, N)>
  174. stencil(View<T, N> const & a, LO && lo, HI && hi)
  175. {
  176. View<T, rank_sum(N, N)> s;
  177. s.p = a.data();
  178. ra::resize(s.dim, 2*a.rank());
  179. CHECK_BOUNDS(every(lo>=0));
  180. CHECK_BOUNDS(every(hi>=0));
  181. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  182. {
  183. CHECK_BOUNDS(dima.size>=lo+hi && "stencil is too large for array");
  184. dims = {dima.size-lo-hi, dima.stride};
  185. },
  186. ptr(s.dim.data()), a.dim, lo, hi);
  187. for_each([](auto & dims, auto && dima, auto && lo, auto && hi)
  188. { dims = {lo+hi+1, dima.stride}; },
  189. ptr(s.dim.data()+a.rank()), a.dim, lo, hi);
  190. return s;
  191. }
  192. // Make last sizes of View<> be compile-time constants.
  193. template <class super_t, rank_t SUPERR, class T, rank_t RANK> inline
  194. auto explode_(View<T, RANK> const & a)
  195. {
  196. // TODO Reduce to single check, either the first or the second.
  197. static_assert(RANK>=SUPERR || RANK==RANK_ANY, "rank of a is too low");
  198. CHECK_BOUNDS(a.rank()>=SUPERR && "rank of a is too low");
  199. View<super_t, rank_sum(RANK, -SUPERR)> b;
  200. ra::resize(b.dim, a.rank()-SUPERR);
  201. dim_t r = 1;
  202. for (int i=0; i<SUPERR; ++i) {
  203. r *= a.size(i+b.rank());
  204. }
  205. CHECK_BOUNDS(r*sizeof(T)==sizeof(super_t) && "size of SUPERR axes doesn't match super type");
  206. for (int i=0; i<b.rank(); ++i) {
  207. CHECK_BOUNDS(a.stride(i) % r==0 && "stride of SUPERR axes doesn't match super type");
  208. b.dim[i].stride = a.stride(i) / r;
  209. b.dim[i].size = a.size(i);
  210. }
  211. CHECK_BOUNDS((b.rank()==0 || a.stride(b.rank()-1)==r) && "super type is not compact in array");
  212. b.p = reinterpret_cast<super_t *>(a.data());
  213. return b;
  214. }
  215. template <class super_t, class T, rank_t RANK> inline
  216. auto explode(View<T, RANK> const & a)
  217. {
  218. return explode_<super_t, (std::is_same_v<super_t, std::complex<T>> ? 1 : ra_traits<super_t>::rank_s())>(a);
  219. }
  220. // TODO Consider these in ra_traits<>.
  221. template <class T, std::enable_if_t<is_scalar<T>, int> =0> inline int gstride(int i) { return 1; }
  222. template <class T, std::enable_if_t<!is_scalar<T>, int> =0> inline int gstride(int i) { return T::stride(i); }
  223. template <class T, std::enable_if_t<is_scalar<T>, int> =0> inline int gsize(int i) { return 1; }
  224. template <class T, std::enable_if_t<!is_scalar<T>, int> =0> inline int gsize(int i) { return T::size(i); }
  225. // TODO This routine is not totally safe; the ranks below SUBR must be compact, which is not checked.
  226. template <class sub_t, class super_t, rank_t RANK> inline
  227. auto collapse(View<super_t, RANK> const & a)
  228. {
  229. using super_v = typename ra_traits<super_t>::value_type;
  230. using sub_v = typename ra_traits<sub_t>::value_type;
  231. constexpr int subtype = sizeof(super_v)/sizeof(sub_t);
  232. constexpr int SUBR = ra_traits<super_t>::rank_s()-ra_traits<sub_t>::rank_s();
  233. View<sub_t, rank_sum(RANK, SUBR+int(subtype>1))> b;
  234. resize(b.dim, a.rank()+SUBR+int(subtype>1));
  235. constexpr dim_t r = sizeof(super_t)/sizeof(sub_t);
  236. static_assert(sizeof(super_t)==r*sizeof(sub_t), "cannot make axis of super_t from sub_t");
  237. for (int i=0; i<a.rank(); ++i) {
  238. b.dim[i].stride = a.stride(i) * r;
  239. b.dim[i].size = a.size(i);
  240. }
  241. constexpr int t = sizeof(super_v)/sizeof(sub_v);
  242. constexpr int s = sizeof(sub_t)/sizeof(sub_v);
  243. static_assert(t*sizeof(sub_v)>=1, "bad subtype");
  244. for (int i=0; i<SUBR; ++i) {
  245. CHECK_BOUNDS(((gstride<super_t>(i)/s)*s==gstride<super_t>(i)) && "bad strides"); // TODO is actually static
  246. b.dim[a.rank()+i].stride = gstride<super_t>(i) / s * t;
  247. b.dim[a.rank()+i].size = gsize<super_t>(i);
  248. }
  249. if (subtype>1) {
  250. b.dim[a.rank()+SUBR].stride = 1;
  251. b.dim[a.rank()+SUBR].size = t;
  252. }
  253. b.p = reinterpret_cast<sub_t *>(a.data());
  254. return b;
  255. }
  256. // For functions that require compact arrays (TODO they really shouldn't).
  257. template <class A> inline
  258. bool const crm(A const & a)
  259. {
  260. return ra::size(a)==0 || is_c_order(a);
  261. }
  262. } // namespace ra
  263. #undef CHECK_BOUNDS