wrank.C 17 KB

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