optimize.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Check that ra::optimize() does what it's supposed to do.
  3. // (c) Daniel Llorens - 2014-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #define RA_DO_OPT 0 // disable automatic use, so we can compare with (forced) and without
  9. #define RA_DO_OPT_IOTA 1
  10. #ifndef RA_DO_OPT_SMALLVECTOR // test is for 1; forcing 0 skips that part of the test.
  11. #define RA_DO_OPT_SMALLVECTOR 1
  12. #endif
  13. #include "ra/test.hh"
  14. #include "mpdebug.hh"
  15. using std::cout, std::endl, ra::TestRecorder;
  16. using complex = std::complex<double>;
  17. int main()
  18. {
  19. TestRecorder tr(std::cout);
  20. tr.section("optimizing static size Iotas");
  21. {
  22. tr.test_eq(4, optimize(ra::iota(ra::int_c<4>()) + ra::iota(4, 0, 2)).nn);
  23. tr.test_eq(4, optimize(ra::iota(4) + ra::iota(ra::int_c<4>(), 0, 2)).nn);
  24. tr.test_eq(ra::start({0, 3, 6, 9}), optimize(ra::iota(ra::int_c<4>()) + ra::iota(4, 0, 2)));
  25. tr.test_eq(4, optimize(ra::iota(ra::int_c<4>()) - ra::iota(4, 0, 2)).nn);
  26. tr.test_eq(4, optimize(ra::iota(4) - ra::iota(ra::int_c<4>(), 0, 2)).nn);
  27. tr.test_eq(ra::start({0, -1, -2, -3}), optimize(ra::iota(4) - ra::iota(ra::int_c<4>(), 0, 2)));
  28. }
  29. tr.section("Iota ops, expr Iotas WIP");
  30. {
  31. tr.info("naked").test(ra::is_iota<decltype(ra::iota(ra::len))>);
  32. tr.info("nop").test(ra::is_iota<decltype(with_len(10, ra::iota(ra::len)))>);
  33. // works unopt bc Match avoid checking if has_len
  34. tr.test_eq(ra::iota(10, 0, 2), with_len(10, ra::iota(ra::len) + ra::iota(ra::len)));
  35. // works, but opt runs at + site, not after with_len (FIXME?)
  36. tr.test_eq(ra::iota(10, 0, 2), optimize(with_len(10, ra::iota(ra::len) + ra::iota(ra::len))));
  37. // FIXME don't work, because optimize() can't determine the match-length of the result iota
  38. // tr.info("+, naked").test(ra::is_iota<decltype(optimize(ra::iota(ra::len) + ra::iota(ra::len)))>);
  39. // tr.info("+").test(ra::is_iota<decltype(with_len(10, optimize(ra::iota(ra::len) + ra::iota(ra::len))))>);
  40. // tr.test_eq(ra::iota(10, 0, 2), with_len(10, optimize(ra::iota(ra::len) + ra::iota(ra::len))));
  41. }
  42. tr.section("misc/sanity");
  43. {
  44. cout << ra::is_iota<ra::Len> << endl;
  45. tr.test_eq(ra::iota(4, 1, 2), ra::Big<int, 1> {1, 3, 5, 7});
  46. {
  47. auto z = ra::iota(5, 1.5);
  48. tr.info("iota with real org I").test_eq(1.5, z.i);
  49. tr.info("iota with complex org I").test_eq(1.5+ra::start({0, 1, 2, 3, 4}), z);
  50. }
  51. {
  52. auto z = optimize(ra::iota(5, complex(1., 1.)));
  53. tr.info("iota with complex org I").test_eq(complex(1., 1.), z.i);
  54. tr.info("iota with complex org II").test_eq(complex(1., 1.)+ra::start({0., 1., 2., 3., 4.}), z);
  55. }
  56. {
  57. auto i = ra::iota(5);
  58. auto l = optimize(i*i);
  59. tr.info("optimize is nop by default").test_eq(ra::start({0, 1, 4, 9, 16}), l);
  60. }
  61. {
  62. auto i = ra::iota(5);
  63. auto j = i*3.;
  64. tr.info("ops with non-integers don't reduce iota by default").test(!std::is_same_v<decltype(i), decltype(j)>);
  65. }
  66. }
  67. tr.section("Iota ops, plus");
  68. {
  69. static_assert(ra::iota_op<ra::Scalar<int>>);
  70. static_assert(ra::is_iota<decltype(ra::iota(10, long(10)))>);
  71. auto test = [&tr](auto && org)
  72. {
  73. auto i = ra::iota(5, org);
  74. auto j = i+1;
  75. auto k1 = optimize(i+1);
  76. static_assert(ra::is_iota<decltype(k1)>);
  77. auto k2 = optimize(1+i);
  78. auto k3 = optimize(ra::iota(5)+1);
  79. auto k4 = optimize(1+ra::iota(5));
  80. auto k5 = optimize(1.5+ra::iota(5));
  81. auto k6 = optimize(ra::iota(5)-0.5);
  82. tr.info("not optimized w/ RA_DO_OPT=0").test(!std::is_same_v<decltype(i), decltype(j)>);
  83. // it's actually a Iota
  84. tr.test_eq(org+1, k1.i);
  85. tr.test_eq(org+1, k1.i);
  86. tr.test_eq(org+1, k2.i);
  87. tr.test_eq(org+1, k3.i);
  88. tr.test_eq(org+1, k4.i);
  89. tr.test_eq(org+1.5, k5.i);
  90. tr.test_eq(org-0.5, k6.i);
  91. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), j);
  92. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k1);
  93. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k2);
  94. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k3);
  95. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k4);
  96. tr.test_eq(1.5+ra::start({0, 1, 2, 3, 4}), k5);
  97. tr.test_eq(ra::start({0, 1, 2, 3, 4})-0.5, k6);
  98. };
  99. test(int(0));
  100. test(double(0));
  101. test(float(0));
  102. }
  103. tr.section("Iota ops, negate");
  104. {
  105. auto test = [&tr](auto && org)
  106. {
  107. auto i = ra::iota(5, org);
  108. auto j = -i;
  109. auto k1 = optimize(-i);
  110. static_assert(ra::is_iota<decltype(k1)>);
  111. tr.info("not optimized w/ RA_DO_OPT=0").test(!std::is_same_v<decltype(i), decltype(j)>);
  112. // it's actually a Iota
  113. tr.test_eq(-org, k1.i);
  114. tr.test_eq(-ra::start({0, 1, 2, 3, 4}), j);
  115. tr.test_eq(-ra::start({0, 1, 2, 3, 4}), k1);
  116. };
  117. test(int(0));
  118. test(double(0));
  119. test(float(0));
  120. }
  121. tr.section("Iota ops, multiplies");
  122. {
  123. auto test = [&tr](auto && org)
  124. {
  125. auto i = ra::iota(5, org);
  126. auto j = i*2;
  127. auto k1 = optimize(i*2);
  128. auto k2 = optimize(2*i);
  129. auto k3 = optimize(ra::iota(5)*2);
  130. auto k4 = optimize(2*ra::iota(5));
  131. tr.info("not optimized w/ RA_DO_OPT=0").test(!std::is_same_v<decltype(i), decltype(j)>);
  132. // it's actually a Iota
  133. tr.test_eq(0, k1.i);
  134. tr.test_eq(0, k2.i);
  135. tr.test_eq(0, k3.i);
  136. tr.test_eq(0, k4.i);
  137. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), j);
  138. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k1);
  139. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k2);
  140. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k3);
  141. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k4);
  142. };
  143. test(int(0));
  144. test(double(0));
  145. test(float(0));
  146. }
  147. #if RA_DO_OPT_SMALLVECTOR==1
  148. tr.section("small vector ops through vector extensions");
  149. {
  150. using Vec = ra::Small<double, 4>;
  151. Vec const r {6, 8, 10, 12};
  152. // [ra4] Expr holds iterators which hold pointers so auto y = Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8} would hold pointers to lost temps. This is revealed by gcc 6.2. Cf ra::start(iter). So this example only works bc it's optimized.
  153. auto x = optimize(Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8});
  154. tr.info("optimization rvalue terms").test(std::is_same_v<decltype(x), Vec>);
  155. tr.test_eq(r, x);
  156. Vec a {1, 2, 3, 4}, b {5, 6, 7, 8};
  157. auto y = a + b;
  158. auto z = optimize(a + b);
  159. tr.info("optimization of lvalue terms").test(std::is_same_v<decltype(z), Vec>);
  160. tr.info("not optimized by default, yet").test(!std::is_same_v<decltype(y), Vec>);
  161. tr.test_eq(r, y);
  162. tr.test_eq(r, z);
  163. auto q = optimize(a + r);
  164. tr.info("optimization of const lvalue terms").test(std::is_same_v<decltype(q), Vec>);
  165. tr.test_eq(ra::start({7, 10, 13, 16}), q);
  166. ra::Small<double, 4, 4> c = 1 + ra::_1;
  167. auto d = optimize(c(0) + b);
  168. tr.info("optimization of view").test(std::is_same_v<decltype(d), Vec>);
  169. tr.test_eq(r, d);
  170. }
  171. tr.section("small vector ops through vector extensions, other types / sizes");
  172. {
  173. ra::Small<double, 8> a = 1 + ra::_0;
  174. ra::Small<double, 4, 8> b = 33 - ra::_1;
  175. auto c = optimize(a + b(3));
  176. tr.info("optimization of view").test(std::is_same_v<decltype(c), ra::Small<double, 8>>);
  177. tr.test_eq(34, c);
  178. }
  179. #endif
  180. return tr.summary();
  181. }