wrank.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Checks operations (verbs) with cell rank>0.
  3. // (c) Daniel Llorens - 2013-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 <atomic>
  9. #include <numeric>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <iterator>
  13. #include "ra/test.hh"
  14. #include "mpdebug.hh"
  15. using std::cout, std::endl, std::flush, std::tuple, ra::dim_t, ra::TestRecorder;
  16. using real = double;
  17. // Find the driver for given axis (This isn't used anymore in ra::; see ra::Match).
  18. template <int iarg, class T>
  19. constexpr int
  20. driver(T && t, int k)
  21. {
  22. if constexpr (iarg<ra::mp::len<std::decay_t<T>>) {
  23. if (k<std::get<iarg>(t).rank()) {
  24. dim_t s = std::get<iarg>(t).len(k);
  25. if (s>=0) {
  26. return iarg;
  27. }
  28. }
  29. return driver<iarg+1>(t, k);
  30. } else {
  31. std::abort(); // no driver
  32. }
  33. }
  34. // ewv = expression-with-verb
  35. template <class V, class A, class B>
  36. void nested_wrank_demo(V && v, A && a, B && b)
  37. {
  38. std::iota(a.begin(), a.end(), 10);
  39. std::iota(b.begin(), b.end(), 1);
  40. {
  41. using FM = ra::Framematch<V, tuple<decltype(a.iter()), decltype(b.iter())>>;
  42. cout << "width of fm: " << ra::mp::len<typename FM::R> << endl;
  43. cout << ra::mp::print_int_list<typename FM::R> {} << endl;
  44. auto af0 = ra::reframe<ra::mp::ref<typename FM::R, 0>>(a.iter());
  45. auto af1 = ra::reframe<ra::mp::ref<typename FM::R, 1>>(b.iter());
  46. cout << "af0: " << sizeof(af0) << endl;
  47. cout << "af1: " << sizeof(af1) << endl;
  48. {
  49. auto ewv = ra::map_(FM::op(v), af0, af1);
  50. cout << sizeof(ewv) << endl;
  51. cout << "ewv rank I: " << ewv.rank() << endl;
  52. for (int k=0; k<ewv.rank(); ++k) {
  53. cout << ewv.len(k) << ": " << driver<0>(ewv.t, k) << endl;
  54. }
  55. // cout << ra::mp::show<decltype(ra::ewv<FM>(FM::op(v), af0, af1))>::value << endl;
  56. cout << "\nusing (ewv &):\n";
  57. ra::ply_ravel(ewv);
  58. cout << endl;
  59. cout << "\nusing (ewv &&):\n";
  60. ra::ply_ravel(ra::map_(FM::op(v), af0, af1));
  61. }
  62. {
  63. // cout << ra::mp::show<decltype(ra::map_(v, a.iter(), b.iter()))>::value << endl;
  64. auto ewv = ra::map_(v, a.iter(), b.iter());
  65. cout << "shape(ewv): " << fmt(ra::nstyle, shape(ewv)) << endl;
  66. #define TEST(plier) \
  67. cout << "\n\nusing " STRINGIZE(plier) " (ewv &):\n"; \
  68. ra::plier(ewv); \
  69. cout << "\n\nusing " STRINGIZE(plier) " ply (ewv &&):\n"; \
  70. ra::plier(ra::map_(v, a.iter(), b.iter()));
  71. TEST(ply_ravel);
  72. TEST(ply_fixed);
  73. }
  74. cout << "\n\n" << endl;
  75. }
  76. }
  77. int main()
  78. {
  79. TestRecorder tr;
  80. auto plus2real = [](real a, real b) { return a + b; };
  81. tr.section("declaring verbs");
  82. {
  83. auto v = ra::wrank<0, 1>(plus2real);
  84. auto vv = ra::wrank<1, 1>(v);
  85. static_assert(ra::is_verb<decltype(v)>);
  86. static_assert(ra::is_verb<decltype(vv)>);
  87. static_assert(!ra::is_verb<decltype(plus2real)>);
  88. }
  89. tr.section("using Framematch");
  90. {
  91. ra::Unique<real, 2> a({3, 2}, ra::none);
  92. ra::Unique<real, 2> b({3, 2}, ra::none);
  93. std::iota(a.begin(), a.end(), 10);
  94. std::iota(b.begin(), b.end(), 1);
  95. auto plus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  96. {
  97. auto v = ra::wrank<0, 2>(plus2real_print);
  98. using FM = ra::Framematch<decltype(v), tuple<decltype(a.iter()), decltype(b.iter())>>;
  99. cout << "width of fm: " << ra::mp::len<FM::R> << endl;
  100. cout << ra::mp::print_int_list<FM::R> {} << endl;
  101. auto af0 = ra::reframe<ra::mp::ref<FM::R, 0>>(a.iter());
  102. auto af1 = ra::reframe<ra::mp::ref<FM::R, 1>>(b.iter());
  103. cout << "af0: " << sizeof(af0) << endl;
  104. cout << "af1: " << sizeof(af1) << endl;
  105. auto ewv = ra::map_(FM::op(v), af0, af1);
  106. cout << sizeof(ewv) << "\n" << endl;
  107. cout << "ewv rank II: " << ewv.rank() << endl;
  108. for (int k=0; k<ewv.rank(); ++k) {
  109. cout << ewv.len(k) << ": " << flush << driver<0>(ewv.t, k) << endl;
  110. }
  111. ra::ply_ravel(ewv);
  112. }
  113. }
  114. tr.section("wrank tests 0-1");
  115. {
  116. auto minus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  117. nested_wrank_demo(ra::wrank<0, 1>(minus2real_print),
  118. ra::Unique<real, 1>({3}, ra::none),
  119. ra::Unique<real, 1>({4}, ra::none));
  120. nested_wrank_demo(ra::wrank<0, 1>(ra::wrank<0, 0>(minus2real_print)),
  121. ra::Unique<real, 1>({3}, ra::none),
  122. ra::Unique<real, 1>({3}, ra::none));
  123. }
  124. tr.section("wrank tests 1-0");
  125. {
  126. auto minus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  127. nested_wrank_demo(ra::wrank<1, 0>(minus2real_print),
  128. ra::Unique<real, 1>({3}, ra::none),
  129. ra::Unique<real, 1>({4}, ra::none));
  130. nested_wrank_demo(ra::wrank<1, 0>(ra::wrank<0, 0>(minus2real_print)),
  131. ra::Unique<real, 1>({3}, ra::none),
  132. ra::Unique<real, 1>({4}, ra::none));
  133. }
  134. tr.section("wrank tests 0-0 (nop), case 1 - exact match");
  135. {
  136. // This uses the reframe specialization for 'do nothing' (TODO if there's one).
  137. auto minus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  138. nested_wrank_demo(ra::wrank<0, 0>(minus2real_print),
  139. ra::Unique<real, 1>({3}, ra::none),
  140. ra::Unique<real, 1>({3}, ra::none));
  141. }
  142. tr.section("wrank tests 0-0 (nop), case 2 - non-exact frame match");
  143. {
  144. // This uses the reframe specialization for 'do nothing' (TODO if there's one).
  145. auto minus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  146. nested_wrank_demo(ra::wrank<0, 0>(minus2real_print),
  147. ra::Unique<real, 2>({3, 4}, ra::none),
  148. ra::Unique<real, 1>({3}, ra::none));
  149. nested_wrank_demo(ra::wrank<0, 0>(minus2real_print),
  150. ra::Unique<real, 1>({3}, ra::none),
  151. ra::Unique<real, 2>({3, 4}, ra::none));
  152. }
  153. tr.section("wrank tests 1-1-0, init array with outer product");
  154. {
  155. auto minus2real = [](real & c, real a, real b) { c = a-b; };
  156. ra::Unique<real, 1> a({3}, ra::none);
  157. ra::Unique<real, 1> b({4}, ra::none);
  158. std::iota(a.begin(), a.end(), 10);
  159. std::iota(b.begin(), b.end(), 1);
  160. ra::Unique<real, 2> c({3, 4}, ra::none);
  161. ra::ply(ra::map_(ra::wrank<1, 0, 1>(minus2real), c.iter(), a.iter(), b.iter()));
  162. real checkc34[3*4] = { /* 10-[1 2 3 4] */ 9, 8, 7, 6,
  163. /* 11-[1 2 3 4] */ 10, 9, 8, 7,
  164. /* 12-[1 2 3 4] */ 11, 10, 9, 8 };
  165. tr.test(std::equal(checkc34, checkc34+3*4, c.begin()));
  166. ra::Unique<real, 2> d34(ra::map_(ra::wrank<0, 1>(std::minus<real>()), a.iter(), b.iter()));
  167. tr.test(std::equal(checkc34, checkc34+3*4, d34.begin()));
  168. real checkc43[3*4] = { /* [10 11 12]-1 */ 9, 10, 11,
  169. /* [10 11 12]-2 */ 8, 9, 10,
  170. /* [10 11 12]-3 */ 7, 8, 9,
  171. /* [10 11 12]-4 */ 6, 7, 8 };
  172. ra::Unique<real, 2> d43(ra::map_(ra::wrank<1, 0>(std::minus<real>()), a.iter(), b.iter()));
  173. tr.test(d43.len(0)==4 && d43.len(1)==3);
  174. tr.test(std::equal(checkc43, checkc43+3*4, d43.begin()));
  175. }
  176. tr.section("recipe for unbeatable subscripts in _from_ operator");
  177. {
  178. ra::Unique<int, 1> a({3}, ra::none);
  179. ra::Unique<int, 1> b({4}, ra::none);
  180. std::iota(a.begin(), a.end(), 10);
  181. std::iota(b.begin(), b.end(), 1);
  182. ra::Unique<real, 2> c({100, 100}, ra::none);
  183. std::iota(c.begin(), c.end(), 0);
  184. real checkd[3*4] = { 1001, 1002, 1003, 1004, 1101, 1102, 1103, 1104, 1201, 1202, 1203, 1204 };
  185. // default auto is value, so need to speficy.
  186. #define MAP ra::map_(ra::wrank<0, 1>([&c](int a, int b) -> decltype(auto) { return c(a, b); } ), \
  187. a.iter(), b.iter())
  188. std::ostringstream os;
  189. os << MAP << endl;
  190. ra::Unique<real, 2> cc {};
  191. std::istringstream is(os.str());
  192. is >> cc;
  193. tr.test(std::equal(checkd, checkd+3*4, cc.begin()));
  194. ra::Unique<real, 2> d(MAP);
  195. tr.test(std::equal(checkd, checkd+3*4, d.begin()));
  196. // Using expr as lvalue.
  197. MAP = 7.;
  198. tr.test_eq(c, where(ra::_0>=10 && ra::_0<=12 && ra::_1>=1 && ra::_1<=4, 7, ra::_0*100+ra::_1));
  199. // looping...
  200. bool valid = true;
  201. for (int i=0; i<c.len(0); ++i) {
  202. for (int j=0; j<c.len(1); ++j) {
  203. valid = valid && ((i>=10 && i<=12 && j>=1 && j<=4 ? 7 : i*100+j) == c(i, j));
  204. }
  205. }
  206. tr.test(valid);
  207. }
  208. tr.section("rank conjunction / empty");
  209. {
  210. }
  211. tr.section("static rank() in ra::expr with reframe()d args");
  212. {
  213. ra::Unique<real, 3> a({2, 2, 2}, 1.);
  214. ra::Unique<real, 3> b({2, 2, 2}, 2.);
  215. real y = 0;
  216. auto e = ra::map_(ra::wrank<0, 0>([&y](real const a, real const b) { y += a*b; }), a.iter(), b.iter());
  217. static_assert(3==e.rank(), "bad rank in static rank expr");
  218. ra::ply_ravel(ra::map_(ra::wrank<0, 0>([&y](real const a, real const b) { y += a*b; }), a.iter(), b.iter()));
  219. tr.test_eq(16, y);
  220. }
  221. tr.section("outer product variants");
  222. {
  223. ra::Big<real, 2> a({2, 3}, ra::_0 - ra::_1);
  224. ra::Big<real, 2> b({3, 2}, ra::_1 - 2*ra::_0);
  225. ra::Big<real, 2> c1 = gemm(a, b);
  226. // matrix product as outer product + reduction (no reductions yet, so manually).
  227. {
  228. ra::Big<real, 3> d = ra::map_(ra::wrank<1, 2>(ra::wrank<0, 1>(std::multiplies<>())), start(a), start(b));
  229. ra::Big<real, 2> c2({d.len(0), d.len(2)}, 0.);
  230. for (int k=0; k<d.len(1); ++k) {
  231. c2 += d(ra::all, k, ra::all);
  232. }
  233. tr.info("d(i,k,j) = a(i,k)*b(k,j)").test_eq(c1, c2);
  234. }
  235. // do the k-reduction by plying with wrank.
  236. {
  237. ra::Big<real, 2> c2({a.len(0), b.len(1)}, 0.);
  238. ra::ply(ra::map_(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto & c, auto && a, auto && b) { c += a*b; })),
  239. start(c2), start(a), start(b)));
  240. tr.info("sum_k a(i,k)*b(k,j)").test_eq(c1, c2);
  241. }
  242. }
  243. tr.section("stencil test for Reframe::keep. Reduced from test/bench-stencil2.cc");
  244. {
  245. int nx = 4;
  246. int ny = 4;
  247. int ts = 4; // must be even bc of swap
  248. auto I = ra::iota(nx-2, 1);
  249. auto J = ra::iota(ny-2, 1);
  250. constexpr ra::Small<real, 3, 3> mask = { 0, 1, 0,
  251. 1, -4, 1,
  252. 0, 1, 0 };
  253. real value = 1;
  254. auto f_raw = [&](ra::ViewBig<real, 2> & A, ra::ViewBig<real, 2> & Anext, ra::ViewBig<real, 4> & Astencil)
  255. {
  256. for (int t=0; t<ts; ++t) {
  257. for (int i=1; i+1<nx; ++i) {
  258. for (int j=1; j+1<ny; ++j) {
  259. Anext(i, j) = -4*A(i, j)
  260. + A(i+1, j) + A(i, j+1)
  261. + A(i-1, j) + A(i, j-1);
  262. }
  263. }
  264. std::swap(A.cp, Anext.cp);
  265. }
  266. };
  267. auto f_sumprod = [&](ra::ViewBig<real, 2> & A, ra::ViewBig<real, 2> & Anext, ra::ViewBig<real, 4> & Astencil)
  268. {
  269. for (int t=0; t!=ts; ++t) {
  270. Astencil.cp = A.data();
  271. Anext(I, J) = 0; // TODO miss notation for sum-of-axes without preparing destination...
  272. Anext(I, J) += map(ra::wrank<2, 2>(std::multiplies<>()), Astencil, mask);
  273. std::swap(A.cp, Anext.cp);
  274. }
  275. };
  276. auto bench = [&](auto & A, auto & Anext, auto & Astencil, auto && ref, auto && tag, auto && f)
  277. {
  278. A = value;
  279. Anext = 0.;
  280. f(A, Anext, Astencil);
  281. tr.info(tag).test_rel(ref, A, 1e-11);
  282. };
  283. ra::Big<real, 2> Aref;
  284. ra::Big<real, 2> A({nx, ny}, 1.);
  285. ra::Big<real, 2> Anext({nx, ny}, 0.);
  286. auto Astencil = stencil(A, 1, 1);
  287. #define BENCH(ref, op) bench(A, Anext, Astencil, ref, STRINGIZE(op), op);
  288. BENCH(A, f_raw);
  289. Aref = ra::Big<real, 2>(A);
  290. BENCH(Aref, f_sumprod);
  291. }
  292. tr.section("iota with dead axes");
  293. {
  294. ra::Big<int, 2> a = from([](auto && i, auto && j) { return i-j; }, ra::iota(3), ra::iota(3));
  295. tr.test_eq(ra::Big<int, 2>({3, 3}, {0, -1, -2, 1, 0, -1, 2, 1, 0}), a);
  296. }
  297. tr.section("vector with dead axes");
  298. {
  299. std::vector i = {0, 1, 2};
  300. ra::Big<int, 2> a = ra::from([](auto && i, auto && j) { return i-j; }, i, i);
  301. tr.test_eq(ra::Big<int, 2>({3, 3}, {0, -1, -2, 1, 0, -1, 2, 1, 0}), a);
  302. }
  303. tr.section("no arguments -> zero rank");
  304. {
  305. int x = ra::from([] { return 3; });
  306. tr.test_eq(3, x);
  307. }
  308. tr.section("counting ops");
  309. {
  310. std::atomic<int> i { 0 };
  311. auto fi = [&i](auto && x) { ++i; return x; };
  312. std::atomic<int> j { 0 };
  313. auto fj = [&j](auto && x) { ++j; return x; };
  314. ra::Big<int, 2> a = from(std::minus<>(), map(fi, ra::iota(7)), map(fj, ra::iota(9)));
  315. tr.test_eq(ra::_0-ra::_1, a);
  316. tr.info("FIXME").skip().test_eq(7, int(i));
  317. tr.info("FIXME").skip().test_eq(9, int(j));
  318. }
  319. tr.section("explicit agreement check with wrank");
  320. {
  321. {
  322. ra::Big<real, 1> a({3}, 2+ra::_0);
  323. ra::Big<real, 2> b({2, 3}, 4-ra::_0);
  324. ra::Big<real, 2> c = {{8, 12, 16}, {6, 9, 12}};
  325. tr.test(agree_op(ra::wrank<1, 1>(std::multiplies<>()), a, b));
  326. tr.test_eq(c, map(ra::wrank<1, 1>(std::multiplies<>()), a, b));
  327. ra::Big<real, 2> d({3, 2}, 4-ra::_0);
  328. tr.test(!agree_op(ra::wrank<1, 1>(std::multiplies<>()), a, d));
  329. }
  330. }
  331. tr.section("Reframe::at");
  332. {
  333. ra::Small<int, 3> a = ra::iota(3)-10;
  334. ra::Small<int, 4> b = 10-ra::iota(4);
  335. ra::Small<int, 3, 4> c = from(std::multiplies<>(), a, b);
  336. tr.strictshape()
  337. .test_eq(c, from([o = from(std::multiplies<>(), a, b)](auto i, auto j) { return o.at(ra::Small<dim_t, 2> {i, j}); },
  338. ra::iota(3), ra::iota(4)));
  339. }
  340. return tr.summary();
  341. }