wrank.cc 15 KB

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