Logo ROOT   6.14/05
Reference Guide
concurrentfill.cxx
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_v7
3 ///
4 /// \macro_code
5 ///
6 /// \date 2015-07-09
7 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
8 /// is welcome!
9 /// \author Axel Naumann <axel@cern.ch>
10 
11 /*************************************************************************
12  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
13  * All rights reserved. *
14  * *
15  * For the licensing terms see $ROOTSYS/LICENSE. *
16  * For the list of contributors see $ROOTSYS/README/CREDITS. *
17  *************************************************************************/
18 
19 #include "ROOT/THist.hxx"
21 
22 #include <iostream>
23 #include <future>
24 #include <random>
25 
26 using namespace ROOT;
27 
28 double wasteCPUTime(std::mt19937 &gen)
29 {
30  // Simulate number crunching through gen and ridiculous num bits
31  return std::generate_canonical<double, 100>(gen) + std::generate_canonical<double, 100>(gen) +
32  std::generate_canonical<double, 100>(gen) + std::generate_canonical<double, 100>(gen) +
33  std::generate_canonical<double, 100>(gen);
34 }
35 
37 
38 /// This function is called within each thread: it spends some CPU time and then
39 /// fills a number into the histogram, through the Filler_t. This is repeated
40 /// several times.
41 void theTask(Filler_t filler)
42 {
43  std::mt19937 gen;
44 
45  for (int i = 0; i < 3000000; ++i)
46  filler.Fill({wasteCPUTime(gen), wasteCPUTime(gen)});
47 }
48 
49 /// This example fills a histogram concurrently, from several threads.
50 void concurrentHistFill(Experimental::TH2D &hist)
51 {
52  // THistConcurrentFillManager allows multiple threads to fill the histogram
53  // concurrently.
54  //
55  // Details: each thread's Fill() calls are buffered. once the buffer is full,
56  // the THistConcurrentFillManager locks and flushes the buffer into the
57  // histogram.
59 
60  std::array<std::thread, 8> threads;
61 
62  // Let the threads fill the histogram concurrently.
63  for (auto &thr: threads) {
64  // Each thread calls fill(), passing a dedicated filler per thread.
65  thr = std::thread(theTask, fillMgr.MakeFiller());
66  }
67 
68  // Join them.
69  for (auto &thr: threads)
70  thr.join();
71 }
72 
73 void concurrentfill()
74 {
75  // This histogram will be filled from several threads.
76  Experimental::TH2D hist{{100, 0., 1.}, {{0., 1., 2., 3., 10.}}};
77 
78  concurrentHistFill(hist);
79 
80  std::cout << hist.GetEntries() << '\n';
81 }
void Fill(const CoordArray_t &x, Weight_t weight=1.)
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
int64_t GetEntries() const noexcept
Get the number of entries this histogram was filled with.
Definition: THist.hxx:152
Buffers a thread&#39;s Fill calls and submits them to the THistConcurrentFillManager. ...
Histogram class for histograms with DIMENSIONS dimensions, where each bin count is stored by a value ...
Definition: THist.hxx:33
Manages the synchronization of calls to FillN().