main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*{{{
  2. Copyright (C) 2013 Matthias Kretz <kretz@kde.org>
  3. Permission to use, copy, modify, and distribute this software
  4. and its documentation for any purpose and without fee is hereby
  5. granted, provided that the above copyright notice appear in all
  6. copies and that both that the copyright notice and this
  7. permission notice and warranty disclaimer appear in supporting
  8. documentation, and that the name of the author not be used in
  9. advertising or publicity pertaining to distribution of the
  10. software without specific, written prior permission.
  11. The author disclaim all warranties with regard to this
  12. software, including all implied warranties of merchantability
  13. and fitness. In no event shall the author be liable for any
  14. special, indirect or consequential damages or any damages
  15. whatsoever resulting from loss of use, data or profits, whether
  16. in an action of contract, negligence or other tortious action,
  17. arising out of or in connection with the use or performance of
  18. this software.
  19. }}}*/
  20. #include <array>
  21. #include <memory>
  22. #include <Vc/Vc>
  23. #include "../tsc.h"
  24. using Vc::float_v;
  25. /*
  26. * This example shows how an arbitrary problem scales depending on working-set size and FLOPs per
  27. * load/store. Understanding this can help to create better implementations.
  28. */
  29. /*
  30. * The Runner is a method to generate the different scenarios with all parameters to the Work
  31. * available as constant expressions.
  32. * The idea is to have the compiler able to optimize as much as possible so that the actual workload
  33. * alone is benchmarked.
  34. *
  35. * The Runner recursively calls operator() on the Work template class with varying arguments for N
  36. * and FLOPs.
  37. */
  38. template<template<std::size_t N, std::size_t M, int, int> class Work, std::size_t N = 256, std::size_t M = 4> struct Runner
  39. {
  40. static void run() {
  41. Work<N, M, (N > 4096 ? 1 : 4096 / N), 2>()();
  42. Work<N, M, (N > 4096 ? 1 : 4096 / N), 3>()();
  43. Work<N, M, (N > 4096 ? 1 : 4096 / N), 4>()();
  44. Work<N, M, (N > 4096 ? 1 : 4096 / N), 6>()();
  45. Work<N, M, (N > 4096 ? 1 : 4096 / N), 9>()();
  46. Work<N, M, (N > 4096 ? 1 : 4096 / N), 13>()();
  47. Work<N, M, (N > 4096 ? 1 : 4096 / N), 19>()();
  48. Work<N, M, (N > 4096 ? 1 : 4096 / N), 28>()();
  49. Work<N, M, (N > 4096 ? 1 : 4096 / N), 42>()();
  50. Work<N, M, (N > 4096 ? 1 : 4096 / N), 63>()();
  51. Work<N, M, (N > 4096 ? 1 : 4096 / N), 94>()();
  52. Work<N, M, (N > 4096 ? 1 : 4096 / N), 141>()();
  53. Work<N, M, (N > 4096 ? 1 : 4096 / N), 211>()();
  54. Runner<Work, N * 2, M>::run();
  55. }
  56. };
  57. template <template <std::size_t N, std::size_t M, int, int> class Work, std::size_t M>
  58. struct Runner<Work, 256 * 1024 * 32, // don't make this number larger, otherwise GCC6
  59. // blows up with Vc::Scalar to 10GB of memory usage
  60. M> {
  61. static void run() {
  62. }
  63. };
  64. /*
  65. * The Flops helper struct generates code that executes FLOPs many floating-point SIMD instructions
  66. * (add, sub, and mul)
  67. */
  68. template<int FLOPs> struct Flops
  69. {
  70. inline float_v operator()(float_v a, float_v b, float_v c)
  71. {
  72. typedef Flops<(FLOPs - 5) / 2> F1;
  73. typedef Flops<(FLOPs - 4) / 2> F2;
  74. return F1()(a + b, a * b, c) + F2()(a * c, b + c, a);
  75. }
  76. };
  77. template<> inline float_v Flops<2>::operator()(float_v a, float_v b, float_v c)
  78. {
  79. return a * b + c;
  80. }
  81. template<> inline float_v Flops<3>::operator()(float_v a, float_v b, float_v c)
  82. {
  83. return a * b + (c - a);
  84. }
  85. template<> inline float_v Flops<4>::operator()(float_v a, float_v b, float_v c)
  86. {
  87. return (a * b + c) + a * c;
  88. }
  89. template<> inline float_v Flops<5>::operator()(float_v a, float_v b, float_v c)
  90. {
  91. return a * b + (a + c) + a * c;
  92. }
  93. template<> inline float_v Flops<6>::operator()(float_v a, float_v b, float_v c)
  94. {
  95. return (a * b + (a + c)) + (a * c - b);
  96. }
  97. template<> inline float_v Flops<7>::operator()(float_v a, float_v b, float_v c)
  98. {
  99. return (a * b + (a + c)) + (a * c - (b + c));
  100. }
  101. template<> inline float_v Flops<8>::operator()(float_v a, float_v b, float_v c)
  102. {
  103. return (a * b + (a + c) + b) + (a * c - (b + c));
  104. }
  105. /*
  106. * This is the benchmark code. It is called from Runner and uses Flops to do the work.
  107. */
  108. template<std::size_t _N, std::size_t M, int Repetitions, int FLOPs>
  109. struct ScaleWorkingSetSize
  110. {
  111. void operator()()
  112. {
  113. constexpr std::size_t N = _N / sizeof(float_v) + 3 * 16 / float_v::Size;
  114. typedef std::array<std::array<float_v, N>, M> Cont;
  115. auto data = Vc::make_unique<Cont, Vc::AlignOnPage>();
  116. for (auto &arr : *data) {
  117. for (auto &value : arr) {
  118. value = float_v::Random();
  119. }
  120. }
  121. TimeStampCounter tsc;
  122. double throughput = 0.;
  123. for (std::size_t i = 0; i < 2 + 512 / N; ++i) {
  124. tsc.start();
  125. // ------------- start of the benchmarked code ---------------
  126. for (int repetitions = 0; repetitions < Repetitions; ++repetitions) {
  127. for (std::size_t m = 0; m < M; ++m) {
  128. for (std::size_t n = 0; n < N; ++n) {
  129. (*data)[m][n] = Flops<FLOPs>()((*data)[(m + 1) % M][n],
  130. (*data)[(m + 2) % M][n],
  131. (*data)[(m + 3) % M][n]);
  132. }
  133. }
  134. }
  135. // -------------- end of the benchmarked code ----------------
  136. tsc.stop();
  137. throughput = std::max(throughput, (Repetitions * M * N * float_v::Size * FLOPs) / static_cast<double>(tsc.cycles()));
  138. }
  139. const long bytes = N * M * sizeof(float_v);
  140. printf("%10lu Byte | %4.2f FLOP/Byte | %4.1f FLOP/cycle\n", bytes, static_cast<double>(float_v::Size * FLOPs) / (4 * sizeof(float_v)), throughput
  141. );
  142. }
  143. };
  144. int Vc_CDECL main()
  145. {
  146. ScaleWorkingSetSize<256, 4, 10, 2>()();
  147. printf("%10s | %4s | %4s\n", "Working-Set Size", "FLOPs per Byte", "Throughput (FLOPs/Cycle)");
  148. Runner<ScaleWorkingSetSize>::run();
  149. return 0;
  150. }