histogram_parallel_filling.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015-2018 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <benchmark/benchmark.h>
  7. #include <boost/histogram/accumulators/thread_safe.hpp>
  8. #include <boost/histogram/axis/regular.hpp>
  9. #include <boost/histogram/histogram.hpp>
  10. #include <boost/histogram/make_histogram.hpp>
  11. #include <chrono>
  12. #include <functional>
  13. #include <mutex>
  14. #include <numeric>
  15. #include <random>
  16. #include <thread>
  17. #include <vector>
  18. #include "../test/throw_exception.hpp"
  19. #include <boost/assert.hpp>
  20. struct assert_check {
  21. assert_check() {
  22. BOOST_ASSERT(false); // don't run with asserts enabled
  23. }
  24. } _;
  25. using namespace boost::histogram;
  26. using namespace std::chrono_literals;
  27. using DS = dense_storage<unsigned>;
  28. using DSTS = dense_storage<accumulators::thread_safe<unsigned>>;
  29. static void NoThreads(benchmark::State& state) {
  30. std::default_random_engine gen(1);
  31. std::uniform_real_distribution<> dis(0, 1);
  32. const unsigned nbins = state.range(0);
  33. auto hist = make_histogram_with(DS(), axis::regular<>(nbins, 0, 1));
  34. for (auto _ : state) {
  35. // simulate some work
  36. for (volatile unsigned n = 0; n < state.range(1); ++n)
  37. ;
  38. hist(dis(gen));
  39. }
  40. }
  41. std::mutex init;
  42. static auto hist = make_histogram_with(DSTS(), axis::regular<>());
  43. static void AtomicStorage(benchmark::State& state) {
  44. init.lock();
  45. if (state.thread_index == 0) {
  46. const unsigned nbins = state.range(0);
  47. hist = make_histogram_with(DSTS(), axis::regular<>(nbins, 0, 1));
  48. }
  49. init.unlock();
  50. std::default_random_engine gen(state.thread_index);
  51. std::uniform_real_distribution<> dis(0, 1);
  52. for (auto _ : state) {
  53. // simulate some work
  54. for (volatile unsigned n = 0; n < state.range(1); ++n)
  55. ;
  56. hist(dis(gen));
  57. }
  58. }
  59. BENCHMARK(NoThreads)
  60. ->UseRealTime()
  61. ->Args({1 << 4, 0})
  62. ->Args({1 << 6, 0})
  63. ->Args({1 << 8, 0})
  64. ->Args({1 << 10, 0})
  65. ->Args({1 << 14, 0})
  66. ->Args({1 << 18, 0})
  67. ->Args({1 << 4, 5})
  68. ->Args({1 << 6, 5})
  69. ->Args({1 << 8, 5})
  70. ->Args({1 << 10, 5})
  71. ->Args({1 << 14, 5})
  72. ->Args({1 << 18, 5})
  73. ->Args({1 << 4, 10})
  74. ->Args({1 << 6, 10})
  75. ->Args({1 << 8, 10})
  76. ->Args({1 << 10, 10})
  77. ->Args({1 << 14, 10})
  78. ->Args({1 << 18, 10})
  79. ->Args({1 << 4, 50})
  80. ->Args({1 << 6, 50})
  81. ->Args({1 << 8, 50})
  82. ->Args({1 << 10, 50})
  83. ->Args({1 << 14, 50})
  84. ->Args({1 << 18, 50})
  85. ->Args({1 << 4, 100})
  86. ->Args({1 << 6, 100})
  87. ->Args({1 << 8, 100})
  88. ->Args({1 << 10, 100})
  89. ->Args({1 << 14, 100})
  90. ->Args({1 << 18, 100})
  91. ;
  92. BENCHMARK(AtomicStorage)
  93. ->UseRealTime()
  94. ->Threads(1)
  95. ->Threads(2)
  96. ->Threads(4)
  97. ->Args({1 << 4, 0})
  98. ->Args({1 << 6, 0})
  99. ->Args({1 << 8, 0})
  100. ->Args({1 << 10, 0})
  101. ->Args({1 << 14, 0})
  102. ->Args({1 << 18, 0})
  103. ->Args({1 << 4, 5})
  104. ->Args({1 << 6, 5})
  105. ->Args({1 << 8, 5})
  106. ->Args({1 << 10, 5})
  107. ->Args({1 << 14, 5})
  108. ->Args({1 << 18, 5})
  109. ->Args({1 << 4, 10})
  110. ->Args({1 << 6, 10})
  111. ->Args({1 << 8, 10})
  112. ->Args({1 << 10, 10})
  113. ->Args({1 << 14, 10})
  114. ->Args({1 << 18, 10})
  115. ->Args({1 << 4, 50})
  116. ->Args({1 << 6, 50})
  117. ->Args({1 << 8, 50})
  118. ->Args({1 << 10, 50})
  119. ->Args({1 << 14, 50})
  120. ->Args({1 << 18, 50})
  121. ->Args({1 << 4, 100})
  122. ->Args({1 << 6, 100})
  123. ->Args({1 << 8, 100})
  124. ->Args({1 << 10, 100})
  125. ->Args({1 << 14, 100})
  126. ->Args({1 << 18, 100})
  127. ;