optimize.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file optimize.cc
  3. /// @brief Check that ra::optimize() does what it's supposed to do.
  4. // (c) Daniel Llorens - 2014-2016
  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. #define RA_DO_OPT 0 // disable automatic use, so we can compare with (forced) and without
  10. #define RA_DO_OPT_IOTA 1
  11. #ifndef RA_DO_OPT_SMALLVECTOR // test is for 1; forcing 0 skips that part of the test.
  12. #define RA_DO_OPT_SMALLVECTOR 1
  13. #endif
  14. #include "ra/operators.hh"
  15. #include "ra/test.hh"
  16. #include "ra/mpdebug.hh"
  17. using std::cout, std::endl, ra::TestRecorder;
  18. using complex = std::complex<double>;
  19. int main()
  20. {
  21. TestRecorder tr(std::cout);
  22. tr.section("misc/sanity");
  23. {
  24. tr.test_eq(ra::iota(4, 1, 2), ra::Big<int, 1> {1, 3, 5, 7});
  25. {
  26. auto z = ra::iota(5, 1.5);
  27. tr.info("iota with real org I").test_eq(1.5, z.i_);
  28. tr.info("iota with complex org I").test_eq(1.5+ra::start({0, 1, 2, 3, 4}), z);
  29. }
  30. {
  31. auto z = optimize(ra::iota(5, complex(1., 1.)));
  32. tr.info("iota with complex org I").test_eq(complex(1., 1.), z.i_);
  33. tr.info("iota with complex org II").test_eq(complex(1., 1.)+ra::start({0., 1., 2., 3., 4.}), z);
  34. }
  35. {
  36. auto i = ra::iota(5);
  37. auto l = optimize(i*i);
  38. tr.info("optimize is nop by default").test_eq(ra::start({0, 1, 4, 9, 16}), l);
  39. }
  40. {
  41. auto i = ra::iota(5);
  42. auto j = i*3.;
  43. tr.info("ops with non-integers don't reduce iota by default").test(!std::is_same_v<decltype(i), decltype(j)>);
  44. }
  45. }
  46. tr.section("operations with Iota, plus");
  47. {
  48. static_assert(ra::iota_op<ra::Scalar<int>>);
  49. static_assert(ra::is_iota<ra::Iota<long>>);
  50. auto test = [&tr](auto && org)
  51. {
  52. auto i = ra::iota(5, org);
  53. auto j = i+1;
  54. auto k1 = optimize(i+1);
  55. static_assert(ra::is_iota<decltype(k1)>);
  56. auto k2 = optimize(1+i);
  57. auto k3 = optimize(ra::iota(5)+1);
  58. auto k4 = optimize(1+ra::iota(5));
  59. tr.info("not optimized w/ RA_DO_OPT=0").test(!std::is_same_v<decltype(i), decltype(j)>);
  60. // it's actually a Iota
  61. tr.test_eq(org+1, k1.i_);
  62. tr.test_eq(org+1, k1.i_);
  63. tr.test_eq(org+1, k2.i_);
  64. tr.test_eq(org+1, k3.i_);
  65. tr.test_eq(org+1, k4.i_);
  66. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), j);
  67. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k1);
  68. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k2);
  69. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k3);
  70. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k4);
  71. };
  72. test(int(0));
  73. test(double(0));
  74. test(float(0));
  75. }
  76. tr.section("operations with Iota, times");
  77. {
  78. auto test = [&tr](auto && org)
  79. {
  80. auto i = ra::iota(5, org);
  81. auto j = i*2;
  82. auto k1 = optimize(i*2);
  83. auto k2 = optimize(2*i);
  84. auto k3 = optimize(ra::iota(5)*2);
  85. auto k4 = optimize(2*ra::iota(5));
  86. tr.info("not optimized w/ RA_DO_OPT=0").test(!std::is_same_v<decltype(i), decltype(j)>);
  87. // it's actually a Iota
  88. tr.test_eq(0, k1.i_);
  89. tr.test_eq(0, k2.i_);
  90. tr.test_eq(0, k3.i_);
  91. tr.test_eq(0, k4.i_);
  92. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), j);
  93. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k1);
  94. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k2);
  95. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k3);
  96. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k4);
  97. };
  98. test(int(0));
  99. test(double(0));
  100. test(float(0));
  101. }
  102. #if RA_DO_OPT_SMALLVECTOR==1
  103. tr.section("small vector ops through vector extensions");
  104. {
  105. using Vec = ra::Small<double, 4>;
  106. Vec const r {6, 8, 10, 12};
  107. // [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 b/c it's optimized.
  108. auto x = optimize(Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8});
  109. tr.info("optimization rvalue terms").test(std::is_same_v<decltype(x), Vec>);
  110. tr.test_eq(r, x);
  111. Vec a {1, 2, 3, 4}, b {5, 6, 7, 8};
  112. auto y = a + b;
  113. auto z = optimize(a + b);
  114. tr.info("optimization of lvalue terms").test(std::is_same_v<decltype(z), Vec>);
  115. tr.info("not optimized by default, yet").test(!std::is_same_v<decltype(y), Vec>);
  116. tr.test_eq(r, y);
  117. tr.test_eq(r, z);
  118. auto q = optimize(a + r);
  119. tr.info("optimization of const lvalue terms").test(std::is_same_v<decltype(q), Vec>);
  120. tr.test_eq(ra::start({7, 10, 13, 16}), q);
  121. ra::Small<double, 4, 4> c = 1 + ra::_1;
  122. auto d = optimize(c(0) + b);
  123. tr.info("optimization of view").test(std::is_same_v<decltype(d), Vec>);
  124. tr.test_eq(r, d);
  125. }
  126. tr.section("small vector ops through vector extensions, other types / sizes");
  127. {
  128. ra::Small<double, 8> a = 1 + ra::_0;
  129. ra::Small<double, 4, 8> b = 33 - ra::_1;
  130. auto c = optimize(a + b(3));
  131. tr.info("optimization of view").test(std::is_same_v<decltype(c), ra::Small<double, 8>>);
  132. tr.test_eq(34, c);
  133. }
  134. #endif
  135. return tr.summary();
  136. }