ra-2.C 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file ra-2.C
  3. /// @brief Checks for ra:: arrays, especially cell rank > 0 operations.
  4. // (c) Daniel Llorens - 2013, 2014
  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 <iterator>
  11. #include <numeric>
  12. #include "ra/complex.H"
  13. #include "ra/test.H"
  14. #include "ra/big.H"
  15. #include "ra/operators.H"
  16. #include "ra/io.H"
  17. #include "test/old.H"
  18. using std::cout, std::endl, std::flush;
  19. using real = double;
  20. int main()
  21. {
  22. TestRecorder tr;
  23. tr.section("iterators on cell rank > 0");
  24. {
  25. ra::Unique<real, 2> a({4, 3}, ra::none);
  26. std::iota(a.begin(), a.end(), 1);
  27. {
  28. ra::cell_iterator<decltype(a), 1> i(a.dim, a.p);
  29. tr.test_eq(1, i.rank());
  30. ply_ravel(expr([](ra::View<real, 1> const & x) { cout << x << endl; }, i));
  31. }
  32. }
  33. #define ARGa iter<1>(a)
  34. #define ARGi ra::Small<int, 4> {1, 2, 3, 4}.iter()
  35. #define ARGd dump.iter()
  36. tr.section("ply on cell rank > 0");
  37. {
  38. ra::Unique<real, 2> a({4, 3}, ra::none);
  39. std::iota(a.begin(), a.end(), 1);
  40. real check[4] = {6, 15, 24, 33};
  41. tr.section("not driving");
  42. {
  43. auto f = [](int i, ra::View<real, 1> const & a, real & d)
  44. {
  45. cout << i << ": " << a << endl;
  46. d = a[0] + a[1] + a[2];
  47. };
  48. ra::Small<real, 4> dump;
  49. // plier(ra::expr(f, a.iter<0>(), ARGa, ARGd)); // how the hell does this work,
  50. #define TEST(plier) \
  51. dump = 0; \
  52. cout << "-> explicit iterator" << endl; \
  53. plier(ra::expr(f, ARGi, ARGa, ARGd)); \
  54. tr.test(std::equal(check, check+4, dump.begin())); \
  55. dump = 0; \
  56. cout << "-> iter<cell rank>()" << endl; \
  57. plier(ra::expr(f, ARGi, a.iter<1>(), ARGd)); \
  58. tr.test(std::equal(check, check+4, dump.begin())); \
  59. dump = 0; \
  60. cout << "-> iter<frame rank>()" << endl; \
  61. plier(ra::expr(f, ARGi, a.iter<-1>(), ARGd)); \
  62. tr.test(std::equal(check, check+4, dump.begin()));
  63. TEST(ply_index);
  64. TEST(ply_ravel);
  65. TEST(plyf_index);
  66. TEST(plyf);
  67. #undef TEST
  68. }
  69. // TODO Use explicit DRIVER arg to ra::expr; the fixed size ARGi should always drive.
  70. tr.section("driving");
  71. {
  72. auto f = [](ra::View<real, 1> const & a, int i, real & d)
  73. {
  74. cout << i << ": " << a << endl;
  75. d = a[0] + a[1] + a[2];
  76. };
  77. ra::Small<real, 4> dump;
  78. #define TEST(plier) \
  79. dump = 0; \
  80. plier(ra::expr(f, ARGa, ARGi, ARGd)); \
  81. tr.test(std::equal(check, check+4, dump.begin())); \
  82. dump = 0; \
  83. plier(ra::expr(f, a.iter<1>(), ARGi, ARGd)); \
  84. tr.test(std::equal(check, check+4, dump.begin())); \
  85. dump = 0; \
  86. plier(ra::expr(f, a.iter<-1>(), ARGi, ARGd)); \
  87. tr.test(std::equal(check, check+4, dump.begin()));
  88. TEST(ply_index);
  89. TEST(ply_ravel);
  90. TEST(plyf_index);
  91. TEST(plyf);
  92. #undef TEST
  93. }
  94. }
  95. // Higher level tests are in test/iterator-small.C (FIXME how about we square them).
  96. tr.section("ply on cell rank > 0, ref argument");
  97. {
  98. auto test_cell_rank_positive =
  99. [&](auto && a)
  100. {
  101. ra::Small<real, 4> dump { 1, 2, 3, 4 };
  102. real check[12] = {1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6};
  103. tr.section("not driving");
  104. {
  105. auto f = [](int i, auto && a, real & d) { std::iota(a.begin(), a.end(), d); };
  106. std::fill(a.begin(), a.end(), 0);
  107. ply_index(ra::expr(f, ARGi, ARGa, ARGd));
  108. tr.test(std::equal(check, check+12, a.begin()));
  109. std::fill(a.begin(), a.end(), 0);
  110. ply_ravel(ra::expr(f, ARGi, ARGa, ARGd));
  111. tr.test(std::equal(check, check+12, a.begin()));
  112. }
  113. tr.section("driving");
  114. {
  115. auto f = [](auto && a, int i, real & d) { std::iota(a.begin(), a.end(), d); };
  116. std::fill(a.begin(), a.end(), 0);
  117. ply_index(ra::expr(f, ARGa, ARGi, ARGd));
  118. tr.test(std::equal(check, check+12, a.begin()));
  119. std::fill(a.begin(), a.end(), 0);
  120. ply_ravel(ra::expr(f, ARGa, ARGi, ARGd));
  121. tr.test(std::equal(check, check+12, a.begin()));
  122. }
  123. };
  124. test_cell_rank_positive(ra::Unique<real, 2>({4, 3}, ra::none));
  125. test_cell_rank_positive(ra::Small<real, 4, 3> {}); // FIXME maybe ra::none should also work for Small
  126. }
  127. tr.section("ply on cell rank = 0 using iter<-1>, ref argument");
  128. {
  129. ra::Small<real, 3> dump { 1, 2, 3 };
  130. ra::Unique<real, 1> a({3}, ra::none);
  131. real check[3] = {1, 2, 3};
  132. tr.section("driving");
  133. {
  134. auto f = [](real & a, real d) { a = d; };
  135. std::fill(a.begin(), a.end(), 0);
  136. ply_index(map(f, a.iter<-1>(), dump.iter<0>()));
  137. tr.test(std::equal(check, check+3, a.begin()));
  138. std::fill(a.begin(), a.end(), 0);
  139. ply_ravel(map(f, a.iter<-1>(), dump.iter<0>()));
  140. tr.test(std::equal(check, check+3, a.begin()));
  141. }
  142. }
  143. tr.section("FYI");
  144. {
  145. cout << "..." << sizeof(ra::View<real, 0>) << endl;
  146. cout << "..." << sizeof(ra::View<real, 1>) << endl;
  147. }
  148. tr.section("ply on cell rank > 0, dynamic rank");
  149. {
  150. ra::Big<int> ad({5, 2}, ra::_0 - ra::_1);
  151. ra::Big<int, 2> as({5, 2}, ra::_0 - ra::_1);
  152. ra::Big<int, 2> b({5, 2}, (ra::_0 - ra::_1)*2);
  153. tr.test_eq(1, as.iter<-1>().rank());
  154. auto cellr = as.iter<-1>().cellr; tr.test_eq(1, cellr);
  155. tr.test_eq(1, ad.iter<-1>().rank());
  156. tr.test_eq(ra::RANK_ANY, ad.iter<-1>().rank_s());
  157. auto e = ra::expr([](auto const & a, auto const & b) { cout << (b-2*a) << endl; },
  158. ad.iter<-1>(), b.iter<-1>());
  159. tr.test_eq(1, e.rank());
  160. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  161. as.iter<-1>(), b.iter<-1>()));
  162. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  163. ad.iter<-1>(), b.iter<-1>()));
  164. }
  165. tr.section("FIXME strange need for assert in ply_ravel with cell_iterator on VAR_RANK array [ra40], gcc 7.3 & 8.1.");
  166. {
  167. ra::Big<int> ad({5, 2}, ra::_0 - ra::_1);
  168. auto ii = iter<1>(ad);
  169. auto ee = map([&](auto && a) { tr.test_eq(1, a.rank()); }, ii);
  170. ply(ee);
  171. }
  172. return tr.summary();
  173. }