ra-1.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file ra-1.cc
  3. /// @brief Fundamental tests for ra::.
  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 <iterator>
  11. #include <numeric>
  12. #include "ra/complex.hh"
  13. #include "ra/test.hh"
  14. #include "ra/ra.hh"
  15. using std::cout, std::endl, std::flush, ra::TestRecorder;
  16. using A2 = ra::Unique<int, 2>;
  17. using A1 = ra::Unique<int, 1>;
  18. using int3 = ra::Small<int, 3>;
  19. using int2 = ra::Small<int, 2>;
  20. using std_int3 = std::array<int, 3>;
  21. using std_int2 = std::array<int, 2>;
  22. template <int i> using TI = ra::TensorIndex<i, int>;
  23. template <class AA>
  24. void CheckPlyReverse1(TestRecorder & tr, AA && a)
  25. {
  26. std::iota(a.begin(), a.end(), 1);
  27. auto invert = [](int & a) { a = -a; return a; };
  28. ply_ravel(ra::expr(invert, a.iter()));
  29. for (int i=0; i<6; ++i) {
  30. tr.test_eq(-(i+1), a(i));
  31. }
  32. auto b = reverse(a, 0);
  33. ply_ravel(ra::expr(invert, b.iter()));
  34. for (int i=0; i<6; ++i) {
  35. tr.test_eq(6-i, b(i));
  36. tr.test_eq(i+1, a(i));
  37. }
  38. }
  39. template <class CC, class AA, class BB>
  40. void CheckPly(TestRecorder & tr, char const * tag, AA && A, BB && B)
  41. {
  42. // need to slice because B may be Unique (!) and I have left own-type constructors as default on purpose. Here, I need C's contents to be a fresh copy of B's.
  43. CC C(B());
  44. auto sub = [](int & b, int const a) -> int { return b -= a; };
  45. ra::ply_ravel(ra::expr(sub, B.iter(), A.iter()));
  46. for (int i=0; i!=A.size(0); ++i) {
  47. for (int j=0; j!=A.size(1); ++j) {
  48. tr.info(tag, " ravel").test_eq(C(i, j)-A(i, j), B(i, j));
  49. }
  50. }
  51. auto add = [](int & b, int const a) -> int { return b += a; };
  52. ra::ply_ravel(ra::expr(add, B.iter(), A.iter()));
  53. ra::ply(ra::expr(sub, B.iter(), A.iter()));
  54. for (int i=0; i!=A.size(0); ++i) {
  55. for (int j=0; j!=A.size(1); ++j) {
  56. tr.info(tag, " index").test_eq(C(i, j)-A(i, j), B(i, j));
  57. }
  58. }
  59. }
  60. using complex = std::complex<double>;
  61. int main()
  62. {
  63. TestRecorder tr(std::cout);
  64. tr.section("nested, with references, ply or ply_ravel");
  65. {
  66. int check[3] = {0, 2, 4};
  67. ra::Small<int, 3> A {1, 0, -1};
  68. ra::Small<int, 3> B {1, 2, 3};
  69. #define TEST(plier) \
  70. [&tr, &A, &B, check](auto && C) \
  71. { \
  72. std::fill(C.begin(), C.end(), -99); \
  73. plier(ra::expr([](int & k, int const i) { k = -i; }, \
  74. C.iter(), \
  75. ra::expr([](int const i, int const j) { return i-j; }, \
  76. A.iter(), B.iter()))); \
  77. tr.test(std::equal(check, check+3, C.begin())); \
  78. }
  79. #define TEST2(plier) \
  80. TEST(plier)(ra::Small<int, 3> {}); \
  81. TEST(plier)(ra::Unique<int, 1>({3}, ra::none));
  82. TEST2(ply_ravel)
  83. TEST2(plyf)
  84. #undef TEST2
  85. #undef TEST
  86. }
  87. tr.section("with ref terms only");
  88. {
  89. #define TEST(plier, Biter, Citer) \
  90. [&tr](auto && B, auto && C) \
  91. { \
  92. plier(ra::expr([](int & k, int const i, int const j) { k = i+j; return k; }, \
  93. Citer, Biter, Biter)); \
  94. tr.test_eq(2, C[0]); \
  95. tr.test_eq(4, C[1]); \
  96. tr.test_eq(6, C[2]); \
  97. }
  98. #define TEST2(plier) \
  99. TEST(plier, B.iter(), C.iter())(int3 { 1, 2, 3 }, int3 { 77, 88, 99 }); \
  100. TEST(plier, ra::vector(B), ra::vector(C))(std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  101. TEST2(ply_ravel)
  102. TEST2(plyf)
  103. #undef TEST2
  104. #undef TEST
  105. }
  106. tr.section("with ref & value terms");
  107. {
  108. #define TEST(plier, Biter, Citer, Btemp) \
  109. [&tr](auto && B, auto && C) \
  110. { \
  111. plier(ra::expr([](int & k, int const i, int const j) { k = i*j; return k; }, \
  112. Citer, Btemp, Biter)); \
  113. tr.test_eq(1, C[0]); \
  114. tr.test_eq(4, C[1]); \
  115. tr.test_eq(9, C[2]); \
  116. }
  117. TEST(ply_ravel, B.iter(), C.iter(), (int3 {1, 2, 3}.iter()))(int3 { 1, 2, 3 }, int3 { 77, 88, 99 });
  118. TEST(ply_ravel, ra::vector(B), ra::vector(C), ra::vector(std_int3 {{1, 2, 3}}))
  119. (std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  120. #undef TEST
  121. }
  122. tr.section("complex or nested types");
  123. {
  124. using A2of2 = ra::Unique<int2, 2>;
  125. auto sum2 = [](int2 const i, int2 const j, int2 & x) { x = { i[0]+j[0], i[1]+j[1] }; };
  126. A2of2 A({2, 3}, { {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6} });
  127. ply(ra::expr([](int2 & a, int i, int j) { int k = i*3+j; a = {k, k}; },
  128. A.iter(), TI<0>(), TI<1>()));
  129. A2of2 B({2, 3}, ra::scalar(int2 {0, 0}));
  130. cout << "A: " << A << endl;
  131. cout << "B: " << B << endl;
  132. cout << "\ntraverse_index..." << endl;
  133. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  134. ply(ra::expr(sum2, A.iter(), ra::scalar(int2{2, 2}), B.iter()));
  135. cout << B << endl;
  136. for (int i=2; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  137. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  138. ply(ra::expr(sum2, ra::scalar(int2{3, 3}), A.iter(), B.iter()));
  139. cout << B << endl;
  140. for (int i=3; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  141. cout << "\ntraverse..." << endl;
  142. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  143. ply_ravel(ra::expr(sum2, A.iter(), ra::scalar(int2{4, 5}), B.iter()));
  144. cout << B << endl;
  145. for (int i=4; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i+1, b[1]); ++i; }
  146. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  147. ply_ravel(ra::expr(sum2, ra::scalar(int2{5, 5}), A.iter(), B.iter()));
  148. cout << B << endl;
  149. for (int i=5; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  150. }
  151. tr.section("reversed arrays");
  152. {
  153. ra::Unique<int, 1> A({ 6 }, ra::none);
  154. std::iota(A.begin(), A.end(), 1);
  155. ra::Unique<int, 1> B { {6}, ra::scalar(99) };
  156. auto copy = [](int & b, int const a) { b = a; return b; };
  157. ply(ra::expr(copy, B.iter(), A.iter()));
  158. for (int i=0; i<6; ++i) {
  159. tr.test_eq(i+1, B(i));
  160. }
  161. ply(ra::expr(copy, B.iter(), reverse(A, 0).iter()));
  162. for (int i=0; i<6; ++i) {
  163. tr.test_eq(6-i, B(i));
  164. }
  165. }
  166. tr.section("reversed arrays, traverse, only one");
  167. {
  168. CheckPlyReverse1(tr, ra::Unique<int, 1>({ 6 }, ra::none));
  169. CheckPlyReverse1(tr, ra::Unique<int>({ 6 }, ra::none));
  170. }
  171. tr.section("mismatched strides");
  172. {
  173. auto sum2 = [](int a, int b, int & c) { return c = a-b; };
  174. A2 a = A2({2, 3}, ra::none); std::iota(a.begin(), a.end(), 1);
  175. A2 b = A2({3, 2}, ra::none); std::iota(b.begin(), b.end(), 1);
  176. A2 c = A2({2, 3}, ra::none);
  177. int check[6] = {0, -1, -2, 2, 1, 0};
  178. #define TEST(plier) \
  179. { \
  180. std::fill(c.begin(), c.end(), 0); \
  181. plier(ra::expr(sum2, a.iter(), transpose<1, 0>(b).iter(), c.iter())); \
  182. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  183. } \
  184. { \
  185. std::fill(c.begin(), c.end(), 0); \
  186. plier(ra::expr(sum2, transpose<1, 0>(a).iter(), b.iter(), transpose<1, 0>(c).iter())); \
  187. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  188. }
  189. TEST(ply_ravel);
  190. TEST(plyf);
  191. #undef TEST
  192. }
  193. tr.section("reverse 1/1 axis, traverse");
  194. #define TEST(plier) \
  195. { \
  196. A1 a({ 6 }, ra::none); \
  197. std::iota(a.begin(), a.end(), 1); \
  198. A1 b { {6}, ra::scalar(99) }; \
  199. auto copy = [](int & b, int const a) { b = a; }; \
  200. plier(ra::expr(copy, b.iter(), a.iter())); \
  201. cout << flush; \
  202. for (int i=0; i<6; ++i) { \
  203. tr.test_eq(i+1, b[i]); \
  204. } \
  205. plier(ra::expr(copy, b.iter(), reverse(a, 0).iter())); \
  206. for (int i=0; i<6; ++i) { \
  207. tr.test_eq(6-i, b(i)); \
  208. } \
  209. }
  210. TEST(ply_ravel)
  211. TEST(plyf)
  212. #undef TEST
  213. tr.section("reverse (ref & non ref), traverse");
  214. {
  215. A2 A({2, 3}, { 1, 2, 3, 4, 5, 6 });
  216. A2 B({2, 3}, { 1, 2, 3, 4, 5, 6 });
  217. CheckPly<A2>(tr, "(a)", A, B);
  218. CheckPly<A2>(tr, "(b)", reverse(A, 0), B);
  219. CheckPly<A2>(tr, "(c)", A, reverse(B, 0));
  220. CheckPly<A2>(tr, "(d)", reverse(A, 0), reverse(B, 0));
  221. CheckPly<A2>(tr, "(e)", reverse(A, 1), B);
  222. CheckPly<A2>(tr, "(f)", A, reverse(B, 1));
  223. CheckPly<A2>(tr, "(g)", reverse(A, 1), reverse(B, 1));
  224. // When BOTH strides are negative, B is still compact and this can be reduced to a single loop.
  225. // TODO Enforce that the loop is linearized over both dimensions.
  226. CheckPly<A2>(tr, "(h)", A, reverse(reverse(B, 0), 1));
  227. CheckPly<A2>(tr, "(i)", reverse(reverse(A, 0), 1), B);
  228. CheckPly<A2>(tr, "(j)", reverse(reverse(A, 0), 1), reverse(reverse(B, 0), 1));
  229. }
  230. tr.section("reverse & transpose (ref & non ref), traverse");
  231. {
  232. using A2 = ra::Unique<int, 2>;
  233. A2 A({2, 2}, { 1, 2, 3, 4 });
  234. A2 B({2, 2}, { 1, 2, 3, 4 });
  235. CheckPly<A2>(tr, "(a)", transpose({1, 0}, A), B);
  236. CheckPly<A2>(tr, "(b)", A, transpose({1, 0}, B));
  237. CheckPly<A2>(tr, "(c)", reverse(reverse(transpose({1, 0}, A), 1), 0), B);
  238. CheckPly<A2>(tr, "(d)", A, reverse(reverse(transpose({1, 0}, B), 1), 0));
  239. CheckPly<A2>(tr, "(e)", transpose<1, 0>(A), B);
  240. CheckPly<A2>(tr, "(f)", A, transpose<1, 0>(B));
  241. CheckPly<A2>(tr, "(g)", reverse(reverse(transpose<1, 0>(A), 1), 0), B);
  242. CheckPly<A2>(tr, "(h)", A, reverse(reverse(transpose<1, 0>(B), 1), 0));
  243. CheckPly<A2>(tr, "(i)", transpose(mp::int_list<1, 0>(), A), B);
  244. CheckPly<A2>(tr, "(j)", A, transpose(mp::int_list<1, 0>(), B));
  245. CheckPly<A2>(tr, "(k)", reverse(reverse(transpose(mp::int_list<1, 0>(), A), 1), 0), B);
  246. CheckPly<A2>(tr, "(l)", A, reverse(reverse(transpose(mp::int_list<1, 0>(), B), 1), 0));
  247. }
  248. return tr.summary();
  249. }