test-ra-2.C 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // (c) Daniel Llorens - 2013, 2014
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file test-ra-2.C
  7. /// @brief Checks for ra:: arrays, especially cell rank > 0 operations.
  8. #include <iostream>
  9. #include <iterator>
  10. #include <numeric>
  11. #include "ra/complex.H"
  12. #include "ra/test.H"
  13. #include "ra/big.H"
  14. #include "ra/operators.H"
  15. #include "ra/io.H"
  16. using std::cout; using std::endl; using std::flush;
  17. using real = double;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. tr.section("iterators on cell rank > 0");
  22. {
  23. ra::Unique<real, 2> a({4, 3}, ra::unspecified);
  24. std::iota(a.begin(), a.end(), 1);
  25. {
  26. ra::ra_iterator<decltype(a), 1> i;
  27. tr.test_eq(1, i.rank());
  28. }
  29. {
  30. ra::ra_iterator<decltype(a), 1> i(a.dim, a.p);
  31. tr.test_eq(1, i.rank());
  32. ply_ravel(expr([](ra::View<real, 1> const & x) { cout << x << endl; }, i));
  33. }
  34. }
  35. #define ARGa ra::ra_iterator<decltype(a), 1>(a.dim, a.p)
  36. #define ARGi ra::Small<int, 4> {1, 2, 3, 4}.iter()
  37. #define ARGd dump.iter()
  38. tr.section("ply on cell rank > 0");
  39. {
  40. ra::Unique<real, 2> a({4, 3}, ra::unspecified);
  41. std::iota(a.begin(), a.end(), 1);
  42. real check[4] = {6, 15, 24, 33};
  43. tr.section("not driving");
  44. {
  45. auto f = [](int i, ra::View<real, 1> const & a, real & d)
  46. {
  47. cout << i << ": " << a << endl;
  48. d = a[0] + a[1] + a[2];
  49. };
  50. ra::Small<real, 4> dump;
  51. // plier(ra::expr(f, a.iter<0>(), ARGa, ARGd)); // how the hell does this work,
  52. #define TEST(plier) \
  53. dump = 0; \
  54. cout << "-> explicit iterator" << endl; \
  55. plier(ra::expr(f, ARGi, ARGa, ARGd)); \
  56. tr.test(std::equal(check, check+4, dump.begin())); \
  57. dump = 0; \
  58. cout << "-> iter<cell rank>()" << endl; \
  59. plier(ra::expr(f, ARGi, a.iter<1>(), ARGd)); \
  60. tr.test(std::equal(check, check+4, dump.begin())); \
  61. dump = 0; \
  62. cout << "-> iter<frame rank>()" << endl; \
  63. plier(ra::expr(f, ARGi, a.iter<-1>(), ARGd)); \
  64. tr.test(std::equal(check, check+4, dump.begin()));
  65. TEST(ply_index);
  66. TEST(ply_ravel);
  67. TEST(plyf_index);
  68. TEST(plyf);
  69. #undef TEST
  70. }
  71. // TODO Use explicit DRIVER arg to ra::expr; the fixed size ARGi should always drive.
  72. tr.section("driving");
  73. {
  74. auto f = [](ra::View<real, 1> const & a, int i, real & d)
  75. {
  76. cout << i << ": " << a << endl;
  77. d = a[0] + a[1] + a[2];
  78. };
  79. ra::Small<real, 4> dump;
  80. #define TEST(plier) \
  81. dump = 0; \
  82. plier(ra::expr(f, ARGa, ARGi, ARGd)); \
  83. tr.test(std::equal(check, check+4, dump.begin())); \
  84. dump = 0; \
  85. plier(ra::expr(f, a.iter<1>(), ARGi, ARGd)); \
  86. tr.test(std::equal(check, check+4, dump.begin())); \
  87. dump = 0; \
  88. plier(ra::expr(f, a.iter<-1>(), ARGi, ARGd)); \
  89. tr.test(std::equal(check, check+4, dump.begin()));
  90. TEST(ply_index);
  91. TEST(ply_ravel);
  92. TEST(plyf_index);
  93. TEST(plyf);
  94. #undef TEST
  95. }
  96. }
  97. tr.section("ply on cell rank > 0, ref argument");
  98. {
  99. ra::Small<real, 4> dump { 1, 2, 3, 4 };
  100. ra::Unique<real, 2> a({4, 3}, ra::unspecified);
  101. real check[12] = {1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6};
  102. tr.section("not driving");
  103. {
  104. auto f = [](int i, ra::View<real, 1> a, real & d) { std::iota(a.begin(), a.end(), d); };
  105. std::fill(a.begin(), a.end(), 0);
  106. ply_index(ra::expr(f, ARGi, ARGa, ARGd));
  107. tr.test(std::equal(check, check+12, a.begin()));
  108. std::fill(a.begin(), a.end(), 0);
  109. ply_ravel(ra::expr(f, ARGi, ARGa, ARGd));
  110. tr.test(std::equal(check, check+12, a.begin()));
  111. }
  112. tr.section("driving");
  113. {
  114. auto f = [](ra::View<real, 1> a, int i, real & d) { std::iota(a.begin(), a.end(), d); };
  115. std::fill(a.begin(), a.end(), 0);
  116. ply_index(ra::expr(f, ARGa, ARGi, ARGd));
  117. tr.test(std::equal(check, check+12, a.begin()));
  118. std::fill(a.begin(), a.end(), 0);
  119. ply_ravel(ra::expr(f, ARGa, ARGi, ARGd));
  120. tr.test(std::equal(check, check+12, a.begin()));
  121. }
  122. }
  123. tr.section("ply on cell rank = 0 using iter<-1>, ref argument");
  124. {
  125. ra::Small<real, 3> dump { 1, 2, 3 };
  126. ra::Unique<real, 1> a({3}, ra::unspecified);
  127. real check[3] = {1, 2, 3};
  128. tr.section("driving");
  129. {
  130. auto f = [](real & a, real d) { a = d; };
  131. std::fill(a.begin(), a.end(), 0);
  132. ply_index(map(f, a.iter<-1>(), dump.iter<0>()));
  133. tr.test(std::equal(check, check+3, a.begin()));
  134. std::fill(a.begin(), a.end(), 0);
  135. ply_ravel(map(f, a.iter<-1>(), dump.iter<0>()));
  136. tr.test(std::equal(check, check+3, a.begin()));
  137. }
  138. }
  139. tr.section("ply on cell rank > 0, dynamic rank");
  140. {
  141. ra::Big<int> ad({5, 2}, ra::_0 - ra::_1);
  142. ra::Big<int, 2> as({5, 2}, ra::_0 - ra::_1);
  143. ra::Big<int, 2> b({5, 2}, (ra::_0 - ra::_1)*2);
  144. tr.test_eq(1, as.iter<-1>().rank());
  145. auto cellr = as.iter<-1>().cellr; tr.test_eq(1, cellr);
  146. tr.test_eq(1, ad.iter<-1>().rank());
  147. tr.test_eq(ra::RANK_ANY, ad.iter<-1>().rank_s());
  148. auto e = ra::expr([](auto const & a, auto const & b) { cout << (b-2*a) << endl; },
  149. ad.iter<-1>(), b.iter<-1>());
  150. tr.test_eq(1, e.rank());
  151. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  152. as.iter<-1>(), b.iter<-1>()));
  153. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  154. ad.iter<-1>(), b.iter<-1>()));
  155. }
  156. tr.section("ply on cell rank > 0, static sizes (TODO)");
  157. {
  158. ra::Small<int, 2, 3, 4> a(ra::_0 - ra::_1 + ra::_2);
  159. cout << a << endl;
  160. cout << "iter " << a.iter() << endl;
  161. // auto ai = a.iter<1>();
  162. // cout << "iter<1> " << sizeof(a) << endl;
  163. }
  164. tr.section("FYI");
  165. {
  166. cout << "..." << sizeof(ra::View<real, 0>) << endl;
  167. cout << "..." << sizeof(ra::View<real, 1>) << endl;
  168. }
  169. return tr.summary();
  170. }