view-ops.H 10 KB

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