xaccumulator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #ifndef XTENSOR_ACCUMULATOR_HPP
  10. #define XTENSOR_ACCUMULATOR_HPP
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <numeric>
  14. #include <type_traits>
  15. #include "xexpression.hpp"
  16. #include "xstrides.hpp"
  17. #include "xtensor_config.hpp"
  18. #include "xtensor_forward.hpp"
  19. namespace xt
  20. {
  21. #define DEFAULT_STRATEGY_ACCUMULATORS evaluation_strategy::immediate_type
  22. namespace detail
  23. {
  24. template <class V = void>
  25. struct accumulator_identity : xtl::identity
  26. {
  27. using value_type = V;
  28. };
  29. }
  30. /**************
  31. * accumulate *
  32. **************/
  33. template <class ACCUMULATE_FUNC, class INIT_FUNC = detail::accumulator_identity<void>>
  34. struct xaccumulator_functor : public std::tuple<ACCUMULATE_FUNC, INIT_FUNC>
  35. {
  36. using self_type = xaccumulator_functor<ACCUMULATE_FUNC, INIT_FUNC>;
  37. using base_type = std::tuple<ACCUMULATE_FUNC, INIT_FUNC>;
  38. using accumulate_functor_type = ACCUMULATE_FUNC;
  39. using init_functor_type = INIT_FUNC;
  40. using init_value_type = typename init_functor_type::value_type;
  41. xaccumulator_functor()
  42. : base_type()
  43. {
  44. }
  45. template <class RF>
  46. xaccumulator_functor(RF&& accumulate_func)
  47. : base_type(std::forward<RF>(accumulate_func), INIT_FUNC())
  48. {
  49. }
  50. template <class RF, class IF>
  51. xaccumulator_functor(RF&& accumulate_func, IF&& init_func)
  52. : base_type(std::forward<RF>(accumulate_func), std::forward<IF>(init_func))
  53. {
  54. }
  55. };
  56. template <class RF>
  57. auto make_xaccumulator_functor(RF&& accumulate_func)
  58. {
  59. using accumulator_type = xaccumulator_functor<std::remove_reference_t<RF>>;
  60. return accumulator_type(std::forward<RF>(accumulate_func));
  61. }
  62. template <class RF, class IF>
  63. auto make_xaccumulator_functor(RF&& accumulate_func, IF&& init_func)
  64. {
  65. using accumulator_type = xaccumulator_functor<std::remove_reference_t<RF>, std::remove_reference_t<IF>>;
  66. return accumulator_type(std::forward<RF>(accumulate_func), std::forward<IF>(init_func));
  67. }
  68. namespace detail
  69. {
  70. template <class F, class E, class EVS>
  71. xarray<typename std::decay_t<E>::value_type> accumulator_impl(F&&, E&&, std::size_t, EVS)
  72. {
  73. static_assert(
  74. !std::is_same<evaluation_strategy::lazy_type, EVS>::value,
  75. "Lazy accumulators not yet implemented."
  76. );
  77. }
  78. template <class F, class E, class EVS>
  79. xarray<typename std::decay_t<E>::value_type> accumulator_impl(F&&, E&&, EVS)
  80. {
  81. static_assert(
  82. !std::is_same<evaluation_strategy::lazy_type, EVS>::value,
  83. "Lazy accumulators not yet implemented."
  84. );
  85. }
  86. template <class T, class R>
  87. struct xaccumulator_return_type
  88. {
  89. using type = xarray<R>;
  90. };
  91. template <class T, layout_type L, class R>
  92. struct xaccumulator_return_type<xarray<T, L>, R>
  93. {
  94. using type = xarray<R, L>;
  95. };
  96. template <class T, std::size_t N, layout_type L, class R>
  97. struct xaccumulator_return_type<xtensor<T, N, L>, R>
  98. {
  99. using type = xtensor<R, N, L>;
  100. };
  101. template <class T, std::size_t... I, layout_type L, class R>
  102. struct xaccumulator_return_type<xtensor_fixed<T, xshape<I...>, L>, R>
  103. {
  104. using type = xtensor_fixed<R, xshape<I...>, L>;
  105. };
  106. template <class T, class R>
  107. using xaccumulator_return_type_t = typename xaccumulator_return_type<T, R>::type;
  108. template <class T>
  109. struct fixed_compute_size;
  110. template <class T, class R>
  111. struct xaccumulator_linear_return_type
  112. {
  113. using type = xtensor<R, 1>;
  114. };
  115. template <class T, layout_type L, class R>
  116. struct xaccumulator_linear_return_type<xarray<T, L>, R>
  117. {
  118. using type = xtensor<R, 1, L>;
  119. };
  120. template <class T, std::size_t N, layout_type L, class R>
  121. struct xaccumulator_linear_return_type<xtensor<T, N, L>, R>
  122. {
  123. using type = xtensor<R, 1, L>;
  124. };
  125. template <class T, std::size_t... I, layout_type L, class R>
  126. struct xaccumulator_linear_return_type<xtensor_fixed<T, xshape<I...>, L>, R>
  127. {
  128. using type = xtensor_fixed<R, xshape<fixed_compute_size<xshape<I...>>::value>, L>;
  129. };
  130. template <class T, class R>
  131. using xaccumulator_linear_return_type_t = typename xaccumulator_linear_return_type<T, R>::type;
  132. template <class F, class E>
  133. inline auto accumulator_init_with_f(F&& f, E& e, std::size_t axis)
  134. {
  135. // this function is the equivalent (but hopefully faster) to (if axis == 1)
  136. // e[:, 0, :, :, ...] = f(e[:, 0, :, :, ...])
  137. // so that all "first" values are initialized in a first pass
  138. std::size_t outer_loop_size, inner_loop_size, pos = 0;
  139. std::size_t outer_stride, inner_stride;
  140. auto set_loop_sizes = [&outer_loop_size, &inner_loop_size](auto first, auto last, std::ptrdiff_t ax)
  141. {
  142. outer_loop_size = std::accumulate(
  143. first,
  144. first + ax,
  145. std::size_t(1),
  146. std::multiplies<std::size_t>()
  147. );
  148. inner_loop_size = std::accumulate(
  149. first + ax + 1,
  150. last,
  151. std::size_t(1),
  152. std::multiplies<std::size_t>()
  153. );
  154. };
  155. // Note: add check that strides > 0
  156. auto set_loop_strides = [&outer_stride, &inner_stride](auto first, auto last, std::ptrdiff_t ax)
  157. {
  158. outer_stride = static_cast<std::size_t>(ax == 0 ? 1 : *std::min_element(first, first + ax));
  159. inner_stride = static_cast<std::size_t>(
  160. (ax == std::distance(first, last) - 1) ? 1 : *std::min_element(first + ax + 1, last)
  161. );
  162. };
  163. set_loop_sizes(e.shape().begin(), e.shape().end(), static_cast<std::ptrdiff_t>(axis));
  164. set_loop_strides(e.strides().begin(), e.strides().end(), static_cast<std::ptrdiff_t>(axis));
  165. if (e.layout() == layout_type::column_major)
  166. {
  167. // swap for better memory locality (smaller stride in the inner loop)
  168. std::swap(outer_loop_size, inner_loop_size);
  169. std::swap(outer_stride, inner_stride);
  170. }
  171. for (std::size_t i = 0; i < outer_loop_size; ++i)
  172. {
  173. pos = i * outer_stride;
  174. for (std::size_t j = 0; j < inner_loop_size; ++j)
  175. {
  176. e.storage()[pos] = f(e.storage()[pos]);
  177. pos += inner_stride;
  178. }
  179. }
  180. }
  181. template <class F, class E>
  182. inline auto accumulator_impl(F&& f, E&& e, std::size_t axis, evaluation_strategy::immediate_type)
  183. {
  184. using init_type = typename F::init_value_type;
  185. using accumulate_functor_type = typename F::accumulate_functor_type;
  186. using expr_value_type = typename std::decay_t<E>::value_type;
  187. // using return_type = std::conditional_t<std::is_same<init_type, void>::value, typename
  188. // std::decay_t<E>::value_type, init_type>;
  189. using return_type = std::decay_t<decltype(std::declval<accumulate_functor_type>()(
  190. std::declval<init_type>(),
  191. std::declval<expr_value_type>()
  192. ))>;
  193. using result_type = xaccumulator_return_type_t<std::decay_t<E>, return_type>;
  194. if (axis >= e.dimension())
  195. {
  196. XTENSOR_THROW(std::runtime_error, "Axis larger than expression dimension in accumulator.");
  197. }
  198. result_type res = e; // assign + make a copy, we need it anyways
  199. if (res.shape(axis) != std::size_t(0))
  200. {
  201. std::size_t inner_stride = static_cast<std::size_t>(res.strides()[axis]);
  202. std::size_t outer_stride = 1; // either row- or column-wise (strides.back / strides.front)
  203. std::size_t outer_loop_size = 0;
  204. std::size_t inner_loop_size = 0;
  205. std::size_t init_size = e.shape()[axis] != std::size_t(1) ? std::size_t(1) : std::size_t(0);
  206. auto set_loop_sizes =
  207. [&outer_loop_size, &inner_loop_size, init_size](auto first, auto last, std::ptrdiff_t ax)
  208. {
  209. outer_loop_size = std::accumulate(first, first + ax, init_size, std::multiplies<std::size_t>());
  210. inner_loop_size = std::accumulate(
  211. first + ax,
  212. last,
  213. std::size_t(1),
  214. std::multiplies<std::size_t>()
  215. );
  216. };
  217. if (result_type::static_layout == layout_type::row_major)
  218. {
  219. set_loop_sizes(res.shape().cbegin(), res.shape().cend(), static_cast<std::ptrdiff_t>(axis));
  220. }
  221. else
  222. {
  223. set_loop_sizes(res.shape().cbegin(), res.shape().cend(), static_cast<std::ptrdiff_t>(axis + 1));
  224. std::swap(inner_loop_size, outer_loop_size);
  225. }
  226. std::size_t pos = 0;
  227. inner_loop_size = inner_loop_size - inner_stride;
  228. // activate the init loop if we have an init function other than identity
  229. if (!std::is_same<
  230. std::decay_t<typename F::init_functor_type>,
  231. typename detail::accumulator_identity<init_type>>::value)
  232. {
  233. accumulator_init_with_f(xt::get<1>(f), res, axis);
  234. }
  235. pos = 0;
  236. for (std::size_t i = 0; i < outer_loop_size; ++i)
  237. {
  238. for (std::size_t j = 0; j < inner_loop_size; ++j)
  239. {
  240. res.storage()[pos + inner_stride] = xt::get<0>(f)(
  241. res.storage()[pos],
  242. res.storage()[pos + inner_stride]
  243. );
  244. pos += outer_stride;
  245. }
  246. pos += inner_stride;
  247. }
  248. }
  249. return res;
  250. }
  251. template <class F, class E>
  252. inline auto accumulator_impl(F&& f, E&& e, evaluation_strategy::immediate_type)
  253. {
  254. using init_type = typename F::init_value_type;
  255. using expr_value_type = typename std::decay_t<E>::value_type;
  256. using accumulate_functor_type = typename F::accumulate_functor_type;
  257. using return_type = std::decay_t<decltype(std::declval<accumulate_functor_type>()(
  258. std::declval<init_type>(),
  259. std::declval<expr_value_type>()
  260. ))>;
  261. // using return_type = std::conditional_t<std::is_same<init_type, void>::value, typename
  262. // std::decay_t<E>::value_type, init_type>;
  263. using result_type = xaccumulator_return_type_t<std::decay_t<E>, return_type>;
  264. std::size_t sz = e.size();
  265. auto result = result_type::from_shape({sz});
  266. if (sz != std::size_t(0))
  267. {
  268. auto it = e.template begin<XTENSOR_DEFAULT_TRAVERSAL>();
  269. result.storage()[0] = xt::get<1>(f)(*it);
  270. ++it;
  271. for (std::size_t idx = 0; it != e.template end<XTENSOR_DEFAULT_TRAVERSAL>(); ++it)
  272. {
  273. result.storage()[idx + 1] = xt::get<0>(f)(result.storage()[idx], *it);
  274. ++idx;
  275. }
  276. }
  277. return result;
  278. }
  279. }
  280. /**
  281. * Accumulate and flatten array
  282. * **NOTE** This function is not lazy!
  283. *
  284. * @param f functor to use for accumulation
  285. * @param e xexpression to be accumulated
  286. * @param evaluation_strategy evaluation strategy of the accumulation
  287. *
  288. * @return returns xarray<T> filled with accumulated values
  289. */
  290. template <class F, class E, class EVS = DEFAULT_STRATEGY_ACCUMULATORS, XTL_REQUIRES(is_evaluation_strategy<EVS>)>
  291. inline auto accumulate(F&& f, E&& e, EVS evaluation_strategy = EVS())
  292. {
  293. // Note we need to check is_integral above in order to prohibit EVS = int, and not taking the
  294. // std::size_t overload below!
  295. return detail::accumulator_impl(std::forward<F>(f), std::forward<E>(e), evaluation_strategy);
  296. }
  297. /**
  298. * Accumulate over axis
  299. * **NOTE** This function is not lazy!
  300. *
  301. * @param f Functor to use for accumulation
  302. * @param e xexpression to accumulate
  303. * @param axis Axis to perform accumulation over
  304. * @param evaluation_strategy evaluation strategy of the accumulation
  305. *
  306. * @return returns xarray<T> filled with accumulated values
  307. */
  308. template <class F, class E, class EVS = DEFAULT_STRATEGY_ACCUMULATORS>
  309. inline auto accumulate(F&& f, E&& e, std::ptrdiff_t axis, EVS evaluation_strategy = EVS())
  310. {
  311. std::size_t ax = normalize_axis(e.dimension(), axis);
  312. return detail::accumulator_impl(std::forward<F>(f), std::forward<E>(e), ax, evaluation_strategy);
  313. }
  314. }
  315. #endif