optimize.cc 5.6 KB

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