histogram_threaded_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 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 <boost/core/lightweight_test.hpp>
  7. #include <boost/histogram/accumulators/thread_safe.hpp>
  8. #include <boost/histogram/algorithm/sum.hpp>
  9. #include <boost/histogram/axis/integer.hpp>
  10. #include <boost/histogram/axis/ostream.hpp>
  11. #include <boost/histogram/ostream.hpp>
  12. #include <boost/histogram/storage_adaptor.hpp>
  13. #include <iostream>
  14. #include <random>
  15. #include <thread>
  16. #include "throw_exception.hpp"
  17. #include "utility_histogram.hpp"
  18. using namespace boost::histogram;
  19. constexpr auto n_fill = 80000;
  20. static_assert(n_fill % 4 == 0, "must be multiple of 4");
  21. template <class Tag, class A1, class A2, class X, class Y>
  22. void fill_test(const A1& a1, const A2& a2, const X& x, const Y& y) {
  23. auto h1 = make_s(Tag{}, dense_storage<int>(), a1, a2);
  24. auto xy = {x, y};
  25. h1.fill(xy);
  26. auto h2 = make_s(Tag{}, dense_storage<accumulators::thread_safe<int>>(), a1, a2);
  27. auto run = [&h2, &x, &y](int k) {
  28. constexpr auto shift = n_fill / 4;
  29. auto xit = x.cbegin() + k * shift;
  30. auto yit = y.cbegin() + k * shift;
  31. for (unsigned i = 0; i < shift; ++i) h2(*xit++, *yit++);
  32. };
  33. std::thread t1([&] { run(0); });
  34. std::thread t2([&] { run(1); });
  35. std::thread t3([&] { run(2); });
  36. std::thread t4([&] { run(3); });
  37. t1.join();
  38. t2.join();
  39. t3.join();
  40. t4.join();
  41. BOOST_TEST_EQ(algorithm::sum(h1), n_fill);
  42. BOOST_TEST_EQ(algorithm::sum(h2), n_fill);
  43. BOOST_TEST_EQ(h1, h2);
  44. }
  45. template <class T>
  46. void tests() {
  47. std::mt19937 gen(1);
  48. std::uniform_int_distribution<> id(-5, 5);
  49. std::vector<int> vi(n_fill), vj(n_fill);
  50. std::generate(vi.begin(), vi.end(), [&] { return id(gen); });
  51. std::generate(vj.begin(), vj.end(), [&] { return id(gen); });
  52. using i = axis::integer<>;
  53. using ig = axis::integer<int, use_default, axis::option::growth_t>;
  54. fill_test<T>(i{0, 1}, i{0, 1}, vi, vj);
  55. fill_test<T>(ig{0, 1}, i{0, 1}, vi, vj);
  56. fill_test<T>(i{0, 1}, ig{0, 1}, vi, vj);
  57. fill_test<T>(ig{0, 1}, ig{0, 1}, vi, vj);
  58. }
  59. int main() {
  60. tests<static_tag>();
  61. tests<dynamic_tag>();
  62. return boost::report_errors();
  63. }