test-optimize.C 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // (c) Daniel Llorens - 2014-2016
  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-optimize.C
  7. /// @brief Check that ra::optimize() does what it's supposed to do.
  8. #define RA_OPTIMIZE 0 // disable automatic use, so we can compare with (forced) and without
  9. #define RA_OPTIMIZE_IOTA 1 // enable all definitions
  10. #ifndef RA_OPTIMIZE_SMALLVECTOR // test is for 1; forcing 0 makes that part of the test moot
  11. #define RA_OPTIMIZE_SMALLVECTOR 1
  12. #endif
  13. #include "ra/operators.H"
  14. #include "ra/io.H"
  15. #include "ra/test.H"
  16. using std::cout; using std::endl;
  17. using complex = std::complex<double>;
  18. int main()
  19. {
  20. TestRecorder tr(std::cout);
  21. tr.section("misc/sanity");
  22. {
  23. tr.test_eq(ra::iota(4, 1, 2), ra::Big<int, 1> {1, 3, 5, 7});
  24. {
  25. auto z = ra::iota(5, 1.5);
  26. tr.info("iota with real org I").test_eq(1.5, z.org_);
  27. tr.info("iota with complex org I").test_eq(1.5+ra::start({0, 1, 2, 3, 4}), z);
  28. }
  29. {
  30. auto z = optimize(ra::iota(5, complex(1., 1.)));
  31. tr.info("iota with complex org I").test_eq(complex(1., 1.), z.org_);
  32. tr.info("iota with complex org II").test_eq(complex(1., 1.)+ra::start({0., 1., 2., 3., 4.}), z);
  33. }
  34. {
  35. auto i = ra::iota(5);
  36. auto l = optimize(i*i);
  37. tr.info("optimize is nop by default").test_eq(ra::start({0, 1, 4, 9, 16}), l);
  38. }
  39. {
  40. auto i = ra::iota(5);
  41. auto j = i*3.;
  42. tr.info("ops with non-integers don't reduce iota by default").test(!std::is_same<decltype(i), decltype(j)>::value);
  43. }
  44. }
  45. tr.section("operations with Iota, plus");
  46. {
  47. auto test = [&tr](auto && org)
  48. {
  49. auto i = ra::iota(5, org);
  50. auto j = i+1;
  51. auto k1 = optimize(i+1);
  52. auto k2 = optimize(1+i);
  53. auto k3 = optimize(ra::iota(5)+1);
  54. auto k4 = optimize(1+ra::iota(5));
  55. tr.info("not optimized w/ RA_OPTIMIZE=0").test(!std::is_same<decltype(i), decltype(j)>::value);
  56. // it's actually a Iota
  57. tr.test_eq(org+1, k1.org_);
  58. tr.test_eq(org+1, k1.org_);
  59. tr.test_eq(org+1, k2.org_);
  60. tr.test_eq(org+1, k3.org_);
  61. tr.test_eq(org+1, k4.org_);
  62. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), j);
  63. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k1);
  64. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k2);
  65. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k3);
  66. tr.test_eq(1+ra::start({0, 1, 2, 3, 4}), k4);
  67. };
  68. test(int(0));
  69. test(double(0));
  70. test(float(0));
  71. }
  72. tr.section("operations with Iota, times");
  73. {
  74. auto test = [&tr](auto && org)
  75. {
  76. auto i = ra::iota(5, org);
  77. auto j = i*2;
  78. auto k1 = optimize(i*2);
  79. auto k2 = optimize(2*i);
  80. auto k3 = optimize(ra::iota(5)*2);
  81. auto k4 = optimize(2*ra::iota(5));
  82. tr.info("not optimized w/ RA_OPTIMIZE=0").test(!std::is_same<decltype(i), decltype(j)>::value);
  83. // it's actually a Iota
  84. tr.test_eq(0, k1.org_);
  85. tr.test_eq(0, k2.org_);
  86. tr.test_eq(0, k3.org_);
  87. tr.test_eq(0, k4.org_);
  88. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), j);
  89. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k1);
  90. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k2);
  91. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k3);
  92. tr.test_eq(2*ra::start({0, 1, 2, 3, 4}), k4);
  93. };
  94. test(int(0));
  95. test(double(0));
  96. test(float(0));
  97. }
  98. #if RA_OPTIMIZE_SMALLVECTOR==1
  99. tr.section("small vector ops through vector extensions [opt-small]");
  100. {
  101. using Vec = ra::Small<double, 4>;
  102. auto x = optimize(Vec {1, 2, 3, 4} + Vec {5, 6, 7, 8});
  103. // BUG 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).
  104. Vec a {1, 2, 3, 4}, b {5, 6, 7, 8};
  105. auto y = a + b;
  106. tr.info("bad optimization").test(std::is_same<decltype(x), Vec>::value);
  107. tr.info("bad non-optimization").test(!std::is_same<decltype(y), Vec>::value);
  108. tr.test_eq(y, x);
  109. }
  110. #endif
  111. return tr.summary();
  112. }