bench-stencil3.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - Stencil-as-view (rank 3).
  3. // (c) Daniel Llorens - 2016-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. // TODO Bad performance, see also bench-stencil[12].cc.
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <random>
  12. #include "ra/bench.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. using real = double;
  15. int nx = 100;
  16. int ny = 100;
  17. int nz = 100;
  18. int ts = 10;
  19. auto I = ra::iota(nx-2, 1);
  20. auto J = ra::iota(ny-2, 1);
  21. auto K = ra::iota(nz-2, 1);
  22. constexpr ra::Small<real, 3, 3, 3> mask = { 0, 0, 0, 0, 1, 0, 0, 0, 0,
  23. 0, 1, 0, 1, -6, 1, 0, 1, 0,
  24. 0, 0, 0, 0, 1, 0, 0, 0, 0 };
  25. #define THEOP template <class A_, class Anext_, class Astencil_> __attribute__((noinline)) \
  26. auto operator()(A_ & A, Anext_ & Anext, Astencil_ & Astencil)
  27. // sensitive to RA_DO_CHECK.
  28. struct f_raw
  29. {
  30. THEOP
  31. {
  32. for (int i=1; i+1<nx; ++i) {
  33. for (int j=1; j+1<ny; ++j) {
  34. for (int k=1; k+1<nz; ++k) {
  35. Anext(i, j, k) = -6*A(i, j, k)
  36. + A(i+1, j, k) + A(i, j+1, k) + A(i, j, k+1)
  37. + A(i-1, j, k) + A(i, j-1, k) + A(i, j, k-1);
  38. }
  39. }
  40. }
  41. std::swap(A.cp, Anext.cp);
  42. };
  43. };
  44. // about as fast as f_raw, but no stencil. Insensitive to RA_DO_CHECK.
  45. struct f_slices
  46. {
  47. THEOP
  48. {
  49. Anext(I, J, K) = -6*A(I, J, K)
  50. + A(I+1, J, K) + A(I, J+1, K) + A(I, J, K+1)
  51. + A(I-1, J, K) + A(I, J-1, K) + A(I, J, K-1);
  52. std::swap(A.cp, Anext.cp);
  53. };
  54. };
  55. // with stencil, about as fast as f_raw. Sensitive to RA_DO_CHECK.
  56. struct f_stencil_explicit
  57. {
  58. THEOP
  59. {
  60. Astencil.cp = A.data();
  61. Anext(I, J, K) = map([](auto && A) { return -6*A(1, 1, 1)
  62. + A(2, 1, 1) + A(1, 2, 1) + A(1, 1, 2)
  63. + A(0, 1, 1) + A(1, 0, 1) + A(1, 1, 0); },
  64. iter<3>(Astencil));
  65. std::swap(A.cp, Anext.cp);
  66. };
  67. };
  68. // sum() inside uses run time sizes and 3-dim ply_ravel loop which is much (10x w/gcc) slower. TODO
  69. struct f_stencil_arrayop
  70. {
  71. THEOP
  72. {
  73. Astencil.cp = A.data();
  74. Anext(I, J, K) = map([](auto && s) { return sum(s*mask); }, iter<3>(Astencil));
  75. std::swap(A.cp, Anext.cp);
  76. };
  77. };
  78. // allows traversal order to be chosen between all 6 axes in ply_ravel. 30x slower. TODO
  79. struct f_sumprod
  80. {
  81. THEOP
  82. {
  83. Astencil.cp = A.data();
  84. Anext(I, J, K) = 0; // TODO miss notation for sum-of-axes without preparing destination...
  85. Anext(I, J, K) += map(ra::wrank<3, 3>(std::multiplies<>()), Astencil, mask);
  86. std::swap(A.cp, Anext.cp);
  87. };
  88. };
  89. // variant of the above, much faster somehow (TODO).
  90. struct f_sumprod2
  91. {
  92. THEOP
  93. {
  94. Astencil.cp = A.data();
  95. Anext(I, J, K) = 0;
  96. ply_fixed(map(ra::wrank<0, 3, 3>([](auto && A, auto && B, auto && C) { A += B*C; }), Anext(I, J, K), Astencil, mask));
  97. std::swap(A.cp, Anext.cp);
  98. };
  99. };
  100. int main()
  101. {
  102. TestRecorder tr(std::cout);
  103. std::random_device rand;
  104. real value = rand();
  105. auto bench = [&](auto & A, auto & Anext, auto & Astencil, auto && ref, auto && tag, auto && f)
  106. {
  107. auto bv = Benchmark().repeats(ts).runs(3)
  108. .once_f([&](auto && repeat)
  109. {
  110. Anext = 0.;
  111. A = value;
  112. repeat([&]() { f(A, Anext, Astencil); });
  113. });
  114. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/A.size()/1e-9, " ns [",
  115. Benchmark::stddev(bv)/A.size()/1e-9 ,"] ", tag)
  116. .test_rel(ref, A, 1e-11);
  117. };
  118. ra::Big<real, 3> Aref;
  119. tr.section("static rank");
  120. {
  121. ra::Big<real, 3> A({nx, ny, nz}, 1.);
  122. ra::Big<real, 3> Anext({nx, ny, nz}, 0.);
  123. auto Astencil = stencil(A, 1, 1);
  124. cout << "Astencil " << format_array(Astencil(0, 0, 0, ra::dots<3>), "|", " ") << endl;
  125. #define BENCH(ref, op) bench(A, Anext, Astencil, ref, STRINGIZE(op), op {});
  126. BENCH(A, f_raw);
  127. Aref = ra::Big<real, 3>(A);
  128. BENCH(Aref, f_slices);
  129. BENCH(Aref, f_stencil_explicit);
  130. BENCH(Aref, f_stencil_arrayop);
  131. BENCH(Aref, f_sumprod);
  132. BENCH(Aref, f_sumprod2);
  133. #undef BENCH
  134. }
  135. tr.section("dynamic rank");
  136. {
  137. ra::Big<real> B({nx, ny, nz}, 1.);
  138. ra::Big<real> Bnext({nx, ny, nz}, 0.);
  139. auto Bstencil = stencil(B, 1, 1);
  140. cout << "Bstencil " << format_array(Bstencil(0, 0, 0, ra::dots<3>), "|", " ") << endl;
  141. #define BENCH(ref, op) bench(B, Bnext, Bstencil, ref, STRINGIZE(op), op {});
  142. // BENCH(Aref, f_raw); // TODO very slow
  143. BENCH(Aref, f_slices);
  144. BENCH(Aref, f_stencil_explicit);
  145. BENCH(Aref, f_stencil_arrayop);
  146. #undef BENCH
  147. }
  148. return tr.summary();
  149. }