reduction.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Array reductions.
  3. // (c) Daniel Llorens - 2014-2023
  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. #include <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. #include "mpdebug.hh"
  12. using std::cout, std::endl, std::flush, std::tuple, ra::TestRecorder;
  13. using real = double;
  14. using complex = std::complex<double>;
  15. using ra::sqrm;
  16. int main()
  17. {
  18. TestRecorder tr(std::cout);
  19. cout << "RA_DO_FMA is " << RA_DO_FMA << endl;
  20. tr.section("amax with different expr types");
  21. {
  22. auto test_amax_expr = [&tr](auto && a, auto && b)
  23. {
  24. a = ra::Small<real, 2, 2> {1, 2, 9, -10};
  25. tr.test_eq(amax(a), 9);
  26. b = ra::Small<real, 2, 2> {1, 1, 1, 1};
  27. tr.test_eq(amax(a+b), 10);
  28. };
  29. test_amax_expr(ra::Unique<real, 2>({2, 2}, 0.), ra::Unique<real, 2>({2, 2}, 0.));
  30. test_amax_expr(ra::Small<real, 2, 2>(), ra::Small<real, 2, 2>());
  31. // failed in gcc 5.1 when amax() took its args by plain auto (now auto &&).
  32. test_amax_expr(ra::Unique<real, 2>({2, 2}, 0.), ra::Small<real, 2, 2>());
  33. }
  34. tr.section("every / any");
  35. {
  36. tr.test(every(ra::Unique<real, 2>({4, 4}, 10+ra::_0-ra::_1)));
  37. tr.test(any(ra::Unique<real, 2>({4, 4}, ra::_0-ra::_1)));
  38. tr.test(ra::every(true));
  39. tr.test(!ra::every(false));
  40. tr.test(ra::any(true));
  41. tr.test(!ra::any(false));
  42. tr.test(every(ra::Unique<int, 1> {5, 5}==5));
  43. tr.test(!every(ra::Unique<int, 1> {2, 5}==5));
  44. tr.test(!every(ra::Unique<int, 1> {5, 2}==5));
  45. tr.test(!every(ra::Unique<int, 1> {2, 3}==5));
  46. tr.test(any(ra::Unique<int, 1> {5, 5}==5));
  47. tr.test(any(ra::Unique<int, 1> {2, 5}==5));
  48. tr.test(any(ra::Unique<int, 1> {5, 2}==5));
  49. tr.test(!any(ra::Unique<int, 1> {2, 3}==5));
  50. }
  51. tr.section("norm2");
  52. {
  53. ra::Small<real, 2> a {1, 2};
  54. tr.test_abs(std::sqrt(5.), norm2(a), 1e-15);
  55. ra::Small<float, 2> b {1, 2};
  56. tr.test_abs(std::sqrt(5.f), norm2(b), 4e-8);
  57. tr.info("type of norm2(floats)").test(std::is_same_v<float, decltype(norm2(b))>);
  58. tr.info("type of reduce_sqrm(floats)").test(std::is_same_v<float, decltype(reduce_sqrm(b))>);
  59. tr.info("type of sqrm(floats)").test(std::is_same_v<float, decltype(sqrm(b[0]))>);
  60. }
  61. tr.section("normv");
  62. {
  63. ra::Small<real, 2> a {1, 2};
  64. ra::Small<real, 2> b;
  65. b = normv(a);
  66. cout << "normv of lvalue: " << b << endl;
  67. tr.test_eq(b[0], 1./sqrt(5));
  68. tr.test_eq(b[1], 2./sqrt(5));
  69. b = normv(ra::Small<real, 2> {2, 1});
  70. cout << "normv of rvalue: "<< b << endl;
  71. tr.test_eq(b[0], 2./sqrt(5));
  72. tr.test_eq(b[1], 1./sqrt(5));
  73. }
  74. tr.section("reductions");
  75. {
  76. auto test_dot = [](auto && test) // TODO Use this for other real reductions.
  77. {
  78. test(ra::Small<complex, 2>{1, 2}, ra::Small<real, 2>{3, 4});
  79. test(ra::Small<real, 2>{1, 2}, ra::Small<complex, 2>{3, 4});
  80. test(ra::Small<real, 2>{1, 2}, ra::Small<real, 2>{3, 4});
  81. test(ra::Small<complex, 2>{1, 2}, ra::Small<complex, 2>{3, 4});
  82. test(ra::Big<complex, 1>{1, 2}, ra::Big<real, 1>{3, 4});
  83. test(ra::Big<real, 1>{1, 2}, ra::Big<complex, 1>{3, 4});
  84. test(ra::Big<real, 1>{1, 2}, ra::Big<real, 1>{3, 4});
  85. test(ra::Big<complex, 1>{1, 2}, ra::Big<complex, 1>{3, 4});
  86. test(ra::Small<complex, 2>{1, 2}, ra::Big<real, 1>{3, 4});
  87. test(ra::Small<real, 2>{1, 2}, ra::Big<complex, 1>{3, 4});
  88. test(ra::Small<real, 2>{1, 2}, ra::Big<real, 1>{3, 4});
  89. test(ra::Small<complex, 2>{1, 2}, ra::Big<complex, 1>{3, 4});
  90. test(ra::Big<complex, 1>{1, 2}, ra::Small<real, 2>{3, 4});
  91. test(ra::Big<real, 1>{1, 2}, ra::Small<complex, 2>{3, 4});
  92. test(ra::Big<real, 1>{1, 2}, ra::Small<real, 2>{3, 4});
  93. test(ra::Big<complex, 1>{1, 2}, ra::Small<complex, 2>{3, 4});
  94. };
  95. test_dot([&tr](auto && a, auto && b) { tr.test_eq(11., dot(a, b)); });
  96. test_dot([&tr](auto && a, auto && b) { tr.test_eq(11., cdot(a, b)); });
  97. test_dot([&tr](auto && a, auto && b) { tr.test_eq(sqrt(8.), norm2(a-b)); });
  98. test_dot([&tr](auto && a, auto && b) { tr.test_eq(8., reduce_sqrm(a-b)); });
  99. auto test_cdot = [](auto && test)
  100. {
  101. test(ra::Small<complex, 2>{1, complex(2, 3)}, ra::Small<complex, 2>{complex(4, 5), 6});
  102. test(ra::Big<complex, 1>{1, complex(2, 3)}, ra::Small<complex, 2>{complex(4, 5), 6});
  103. test(ra::Small<complex, 2>{1, complex(2, 3)}, ra::Big<complex, 1>{complex(4, 5), 6});
  104. test(ra::Big<complex, 1>{1, complex(2, 3)}, ra::Big<complex, 1>{complex(4, 5), 6});
  105. };
  106. complex value = conj(1.)*complex(4., 5.) + conj(complex(2., 3.))*6.;
  107. tr.test_eq(value, complex(16, -13));
  108. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(complex(16., -13.), cdot(a, b)); });
  109. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(sqrt(59.), norm2(a-b)); });
  110. test_cdot([&tr](auto && a, auto && b) { tr.test_eq(59., reduce_sqrm(a-b)); });
  111. auto test_sum = [](auto && test)
  112. {
  113. test(ra::Small<complex, 2>{complex(4, 5), 6});
  114. test(ra::Big<complex, 1>{complex(4, 5), 6});
  115. };
  116. test_sum([&tr](auto && a) { tr.test_eq(complex(10, 5), sum(a)); });
  117. test_sum([&tr](auto && a) { tr.test_eq(complex(24, 30), prod(a)); });
  118. test_sum([&tr](auto && a) { tr.test_eq(sqrt(41.), amax(abs(a))); });
  119. test_sum([&tr](auto && a) { tr.test_eq(6., amin(abs(a))); });
  120. }
  121. tr.section("amax/amin ignore NaN");
  122. {
  123. constexpr real QNAN = std::numeric_limits<real>::quiet_NaN();
  124. tr.test_eq(std::numeric_limits<real>::lowest(), std::max(std::numeric_limits<real>::lowest(), QNAN));
  125. tr.test_eq(-std::numeric_limits<real>::infinity(), amax(ra::Small<real, 3>(QNAN)));
  126. tr.test_eq(std::numeric_limits<real>::infinity(), amin(ra::Small<real, 3>(QNAN)));
  127. }
  128. // TODO these reductions require a destination argument; there are no exprs really.
  129. tr.section("to sum columns in crude ways");
  130. {
  131. ra::Unique<real, 2> A({100, 111}, ra::_0 - ra::_1);
  132. ra::Unique<real, 1> B({100}, 0.);
  133. for (int i=0, iend=A.len(0); i<iend; ++i) {
  134. B(i) = sum(A(i));
  135. }
  136. {
  137. ra::Unique<real, 1> C({100}, 0.);
  138. for_each([](auto & c, auto a) { c += a; }, C, A);
  139. tr.test_eq(B, C);
  140. }
  141. // This depends on matching frames for += just as for any other op, which is at odds with e.g. amend.
  142. {
  143. ra::Unique<real, 1> C({100}, 0.);
  144. C += A;
  145. tr.test_eq(B, C);
  146. }
  147. // Same as above.
  148. {
  149. ra::Unique<real, 1> C({100}, 0.);
  150. C = C + A;
  151. tr.test_eq(B, C);
  152. }
  153. // It cannot work with a lhs scalar value since += must be a class member, but it will work with a rank 0 array or with ra::Scalar.
  154. {
  155. ra::Unique<real, 0> C({}, 0.);
  156. C += A(0);
  157. tr.test_eq(B(0), C);
  158. real c(0.);
  159. ra::scalar(c) += A(0);
  160. tr.test_eq(B(0), c);
  161. }
  162. // This will fail because the assumed driver (ANY) has lower actual rank than the other argument. TODO check that it fails.
  163. // {
  164. // ra::Unique<real, 2> A({2, 3}, {1, 2, 3, 4 ,5, 6});
  165. // ra::Unique<real> C({}, 0.);
  166. // C += A(0);
  167. // }
  168. }
  169. tr.section("to sum rows in crude ways");
  170. {
  171. ra::Unique<real, 2> A({100, 111}, ra::_0 - ra::_1);
  172. ra::Unique<real, 1> B({111}, 0.);
  173. for (int j=0, jend=A.len(1); j<jend; ++j) {
  174. B(j) = sum(A(ra::all, j));
  175. }
  176. {
  177. ra::Unique<real, 1> C({111}, 0.);
  178. for_each([&C](auto && a) { C += a; }, A.iter<1>());
  179. tr.info("rhs iterator of rank > 0").test_eq(B, C);
  180. }
  181. {
  182. ra::Unique<real, 1> C({111}, 0.);
  183. for_each(ra::wrank<1, 1>([](auto & c, auto && a) { c += a; }), C, A);
  184. tr.info("rank conjuction").test_eq(B, C);
  185. }
  186. {
  187. ra::Unique<real, 1> C({111}, 0.);
  188. for_each(ra::wrank<1, 1>(ra::wrank<0, 0>([](auto & c, auto a) { c += a; })), C, A);
  189. tr.info("double rank conjunction").test_eq(B, C);
  190. }
  191. {
  192. ra::Unique<real, 1> C({111}, 0.);
  193. ra::scalar(C) += A.iter<1>();
  194. tr.info("scalar() and iterators of rank > 0").test_eq(B, C);
  195. }
  196. {
  197. ra::Unique<real, 1> C({111}, 0.);
  198. C.iter<1>() += A.iter<1>();
  199. tr.info("assign to iterators of rank > 0").test_eq(B, C);
  200. }
  201. }
  202. tr.section("reductions with amax");
  203. {
  204. ra::Big<int, 2> c({2, 3}, {1, 3, 2, 7, 1, 3});
  205. tr.info("max of rows").test_eq(ra::Big<int, 1> {3, 7}, map([](auto && a) { return amax(a); }, iter<1>(c)));
  206. ra::Big<int, 1> m({3}, 0);
  207. scalar(m) = max(scalar(m), iter<1>(c)); // requires inner forward in ra.hh: DEF_NAME_OP
  208. tr.info("max of columns I").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  209. m = 0;
  210. iter<1>(m) = max(iter<1>(m), iter<1>(c)); // FIXME
  211. tr.info("max of columns III [ma113]").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  212. m = 0;
  213. for_each([&m](auto && a) { m = max(m, a); }, iter<1>(c));
  214. tr.info("max of columns II").test_eq(ra::Big<int, 1> {7, 3, 3}, m);
  215. ra::Big<double, 1> q({0}, {});
  216. tr.info("amax default").test_eq(std::numeric_limits<double>::infinity(), amin(q));
  217. tr.info("amin default").test_eq(-std::numeric_limits<double>::infinity(), amax(q));
  218. }
  219. tr.section("vector-matrix reductions");
  220. {
  221. auto test = [&tr](auto t, auto s, auto r)
  222. {
  223. using T = decltype(t);
  224. using S = decltype(s);
  225. using R = decltype(r);
  226. S x[4] = {1, 2, 3, 4};
  227. ra::Small<T, 3, 4> a = ra::_0 - ra::_1;
  228. R y[3] = {99, 99, 99};
  229. ra::start(y) = ra::gemv(a, x);
  230. auto z = ra::gemv(a, x);
  231. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, y);
  232. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, z);
  233. };
  234. test(double(0), double(0), double(0));
  235. test(std::complex<double>(0), std::complex<double>(0), std::complex<double>(0));
  236. test(int(0), int(0), int(0));
  237. test(int(0), double(0), double(0));
  238. test(double(0), int(0), double(0));
  239. }
  240. {
  241. auto test = [&tr](auto t, auto s, auto r)
  242. {
  243. using T = decltype(t);
  244. using S = decltype(s);
  245. using R = decltype(r);
  246. S x[4] = {1, 2, 3, 4};
  247. ra::Small<T, 4, 3> a = ra::_1 - ra::_0;
  248. R y[3] = {99, 99, 99};
  249. ra::start(y) = ra::gevm(x, a);
  250. auto z = ra::gevm(x, a);
  251. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, y);
  252. tr.test_eq(ra::Small<R, 3> {-20, -10, 0}, z);
  253. };
  254. test(double(0), double(0), double(0));
  255. test(std::complex<double>(0), std::complex<double>(0), std::complex<double>(0));
  256. test(int(0), int(0), int(0));
  257. test(int(0), double(0), double(0));
  258. test(double(0), int(0), double(0));
  259. }
  260. tr.section("gemm with dynamic shape, corner case");
  261. {
  262. ra::Big<double, 2> A({0, 0}, 2.);
  263. ra::Big<double, 2> B({0, 0}, 3.);
  264. auto C = gemm(A, B);
  265. tr.test_eq(0, C.len(0));
  266. tr.test_eq(0, C.len(1));
  267. }
  268. tr.section("gemm with dynamic shape");
  269. {
  270. ra::Big<complex, 2> A({3, 2}, 2.);
  271. ra::Big<complex, 2> B({2, 4}, 3.);
  272. auto C = gemm(A, B);
  273. tr.test_eq(3, C.len(0));
  274. tr.test_eq(4, C.len(1));
  275. tr.test_eq(12., C);
  276. }
  277. tr.section("gemm with static shape");
  278. {
  279. ra::Small<double, 3, 2> A = 2;
  280. ra::Small<double, 2, 4> B = 3;
  281. auto C = gemm(A, B);
  282. tr.test_eq(3, C.len_s(0));
  283. tr.test_eq(4, C.len_s(1));
  284. tr.test_eq(12, C);
  285. }
  286. tr.section("gemv with static shape");
  287. {
  288. ra::Small<double, 3, 2> A = 2;
  289. ra::Small<double, 2> B = 3;
  290. auto C = gemv(A, B);
  291. tr.test_eq(3, C.len_s(0));
  292. tr.test_eq(12, C);
  293. }
  294. tr.section("gevm with static shape");
  295. {
  296. ra::Small<double, 2> A = 3;
  297. ra::Small<double, 2, 3> B = 2;
  298. auto C = gevm(A, B);
  299. tr.test_eq(3, C.len_s(0));
  300. tr.test_eq(12, C);
  301. }
  302. tr.section("reference reductions");
  303. {
  304. ra::Big<double, 2> A({2, 3}, ra::_1 - ra::_0);
  305. double & mn = refmin(A);
  306. tr.test_eq(-1, mn);
  307. mn = -99;
  308. ra::Big<double, 2> B({2, 3}, ra::_1 - ra::_0);
  309. B(1, 0) = -99;
  310. tr.test_eq(B, A);
  311. double & mx = refmin(A, std::greater<double>());
  312. tr.test_eq(2, mx);
  313. mx = 0;
  314. B(0, 2) = 0;
  315. tr.test_eq(B, A);
  316. double & my = refmax(A);
  317. tr.test_eq(1, my);
  318. my = 77;
  319. B(0, 1) = 77;
  320. tr.test_eq(B, A);
  321. // cout << refmin(A+B) << endl; // compile error
  322. }
  323. return tr.summary();
  324. }