bench-from.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - Selection ops in ra::
  3. // (c) Daniel Llorens - 2015, 2017
  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. #include <iostream>
  9. #include <iomanip>
  10. #include <string>
  11. #include "ra/test.hh"
  12. using std::cout, std::endl, std::flush, ra::TestRecorder, ra::Benchmark;
  13. using real = double;
  14. int main()
  15. {
  16. TestRecorder tr(cout);
  17. cout.precision(4);
  18. tr.section("rank1(rank1)");
  19. {
  20. auto rank1_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
  21. {
  22. cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
  23. using Array1 = std::decay_t<decltype(A_)>;
  24. Array1 A = ra::iota(Asize);
  25. ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
  26. Array1 B({Isize}, 0);
  27. auto II = I.data();
  28. auto AA = A.data();
  29. auto BB = B.data();
  30. Benchmark bm { N, 3 };
  31. auto report = [&](std::string const & tag, auto && bv)
  32. {
  33. tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
  34. .test_eq(ra::iota(Isize)*Istep, B);
  35. };
  36. report("indexing on raw pointers",
  37. bm.run([&] {
  38. for (int i=0; i<Isize; ++i) {
  39. BB[i] = AA[II[i]];
  40. }
  41. }));
  42. report("vectorized selection",
  43. bm.run([&] {
  44. B = A(I);
  45. }));
  46. report("write out the indexing loop",
  47. bm.run([&] {
  48. for_each([&A](auto & b, auto i) { b = A(i); }, B, I);
  49. }));
  50. report("loop on scalar selection",
  51. bm.run([&]() {
  52. for (int i=0; i<Isize; ++i) {
  53. B(i) = A(I(i));
  54. }
  55. }));
  56. };
  57. tr.section("fixed rank");
  58. rank1_test(ra::Unique<real, 1>(), 10000, 500, 20, 5000);
  59. rank1_test(ra::Unique<real, 1>(), 1000, 50, 20, 10*5000);
  60. rank1_test(ra::Unique<real, 1>(), 100, 5, 20, 100*5000);
  61. rank1_test(ra::Unique<real, 1>(), 10000, 500, 2, 5000);
  62. rank1_test(ra::Unique<real, 1>(), 1000, 50, 2, 10*5000);
  63. rank1_test(ra::Unique<real, 1>(), 100, 5, 2, 100*5000);
  64. tr.section("var rank");
  65. rank1_test(ra::Unique<real>(), 10000, 500, 20, 5000);
  66. rank1_test(ra::Unique<real>(), 1000, 50, 20, 10*5000);
  67. rank1_test(ra::Unique<real>(), 100, 5, 20, 100*5000);
  68. rank1_test(ra::Unique<real>(), 10000, 500, 2, 5000);
  69. rank1_test(ra::Unique<real>(), 1000, 50, 2, 10*5000);
  70. rank1_test(ra::Unique<real>(), 100, 5, 2, 100*5000);
  71. }
  72. tr.section("rank2(rank1, rank1)");
  73. {
  74. auto rank1_11_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
  75. {
  76. cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
  77. using Array2 = std::decay_t<decltype(A_)>;
  78. Array2 A({Asize, Asize}, ra::_0 + ra::_1);
  79. ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
  80. Array2 B({Isize, Isize}, 0);
  81. auto II = I.data();
  82. auto AA = A.data();
  83. auto BB = B.data();
  84. Benchmark bm { N, 3 };
  85. auto report = [&](std::string const &tag, auto && bv)
  86. {
  87. tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
  88. .test_eq(Istep*(ra::_0 + ra::_1), B);
  89. };
  90. report("2D indexing on raw pointers",
  91. bm.run([&] {
  92. for (int i=0; i<Isize; ++i) {
  93. for (int j=0; j<Isize; ++j) {
  94. BB[i*Isize + j] = AA[II[i]*Asize + II[j]];
  95. }
  96. }
  97. }));
  98. report("vectorized selection",
  99. bm.run([&] {
  100. B = A(I, I);
  101. }));
  102. };
  103. tr.section("fixed rank");
  104. rank1_11_test(ra::Unique<real, 2>(), 1000, 50, 20, 5000);
  105. rank1_11_test(ra::Unique<real, 2>(), 100, 5, 20, 10*10*5000);
  106. rank1_11_test(ra::Unique<real, 2>(), 1000, 50, 2, 5000);
  107. rank1_11_test(ra::Unique<real, 2>(), 100, 5, 2, 10*10*5000);
  108. rank1_11_test(ra::Unique<real, 2>(), 10, 5, 2, 10*10*5000);
  109. tr.section("var rank");
  110. rank1_11_test(ra::Unique<real>(), 1000, 50, 20, 5000);
  111. rank1_11_test(ra::Unique<real>(), 100, 5, 20, 10*10*5000);
  112. rank1_11_test(ra::Unique<real>(), 1000, 50, 2, 5000);
  113. rank1_11_test(ra::Unique<real>(), 100, 5, 2, 10*10*5000);
  114. rank1_11_test(ra::Unique<real>(), 10, 5, 2, 10*10*5000);
  115. }
  116. tr.section("rank3(rank1, rank1, rank1)");
  117. {
  118. auto rank1_111_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
  119. {
  120. cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
  121. using Array3 = std::decay_t<decltype(A_)>;
  122. Array3 A({Asize, Asize, Asize}, 10000*ra::_0 + 100*ra::_1 + 1*ra::_2);
  123. ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
  124. Array3 B({Isize, Isize, Isize}, 0);
  125. auto II = I.data();
  126. auto AA = A.data();
  127. auto BB = B.data();
  128. Benchmark bm { N, 3 };
  129. auto report = [&](std::string const &tag, auto && bv)
  130. {
  131. tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
  132. .test_eq(Istep*(10000*ra::_0 + 100*ra::_1 + 1*ra::_2), B);
  133. };
  134. report("3D indexing on raw pointers",
  135. bm.run([&] {
  136. for (int i=0; i<Isize; ++i) {
  137. for (int j=0; j<Isize; ++j) {
  138. for (int k=0; k<Isize; ++k) {
  139. BB[k+Isize*(j+Isize*i)] = AA[II[k]+Asize*(II[j]+Asize*II[i])];
  140. }
  141. }
  142. }
  143. }));
  144. report("vectorized selection",
  145. bm.run([&] {
  146. B = A(I, I, I);
  147. }));
  148. };
  149. tr.section("fixed rank");
  150. rank1_111_test(ra::Unique<real, 3>(), 40, 20, 2, 2000);
  151. rank1_111_test(ra::Unique<real, 3>(), 100, 5, 20, 4*4*4*2000);
  152. rank1_111_test(ra::Unique<real, 3>(), 10, 5, 2, 4*4*4*2000);
  153. }
  154. tr.section("rank4(rank1, rank1, rank1, rank1)");
  155. {
  156. auto rank1_1111_test = [&tr](auto A_, int Asize, int Isize, int Istep, int N)
  157. {
  158. cout << "select " << Isize << " step " << Istep << " from " << Asize << endl;
  159. using Array4 = std::decay_t<decltype(A_)>;
  160. ra::Unique<real, 4> A(ra::Small<int, 4>(Asize), 1000000*ra::_0 + 10000*ra::_1 + 100*ra::_2 + 1*ra::_3);
  161. ra::Unique<int, 1> I = ra::iota(Isize)*Istep;
  162. Array4 B(ra::Small<int, 4>(Isize), 0);
  163. auto II = I.data();
  164. auto AA = A.data();
  165. auto BB = B.data();
  166. Benchmark bm { N, 3 };
  167. auto report = [&](std::string const &tag, auto && bv)
  168. {
  169. tr.info(std::setw(5), std::fixed, bm.avg(bv)/B.size()/1e-9, " ns [", bm.stddev(bv)/B.size()/1e-9, "] ", tag)
  170. .test_eq(Istep*(1000000*ra::_0 + 10000*ra::_1 + 100*ra::_2 + 1*ra::_3), B);
  171. };
  172. report("3D indexing on raw pointers",
  173. bm.run([&] {
  174. for (int i=0; i<Isize; ++i) {
  175. for (int j=0; j<Isize; ++j) {
  176. for (int k=0; k<Isize; ++k) {
  177. for (int l=0; l<Isize; ++l) {
  178. BB[l+Isize*(k+Isize*(j+Isize*i))] = AA[II[l]+Asize*(II[k]+Asize*(II[j]+Asize*II[i]))];
  179. }
  180. }
  181. }
  182. }
  183. }));
  184. report("vectorized selection",
  185. bm.run([&] {
  186. B = A(I, I, I, I);
  187. }));
  188. report("slice one axis at a time", // TODO one way A(i, i, i, i) could work
  189. bm.run([&] {
  190. for (int i=0; i<Isize; ++i) {
  191. for (int j=0; j<Isize; ++j) {
  192. for (int k=0; k<Isize; ++k) {
  193. B(i, j, k) = A(I[i], I[j], I[k])(I);
  194. }
  195. }
  196. }
  197. }));
  198. };
  199. tr.section("fixed rank");
  200. rank1_1111_test(ra::Unique<real, 4>(), 40, 20, 2, 100);
  201. rank1_1111_test(ra::Unique<real, 4>(), 10, 5, 2, 4*4*4*4*100);
  202. }
  203. return tr.summary();
  204. }