Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
concurrentfill.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_exp
3///
4/// \macro_code
5///
6/// \date 2015-07-09
7/// \warning This is part of the experimental API, which might change in the future. Feedback is welcome!
8/// \author Axel Naumann <axel@cern.ch>
9
10/*************************************************************************
11 * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
12 * All rights reserved. *
13 * *
14 * For the licensing terms see $ROOTSYS/LICENSE. *
15 * For the list of contributors see $ROOTSYS/README/CREDITS. *
16 *************************************************************************/
17
18#include "ROOT/RHist.hxx"
20
21#include <iostream>
22#include <future>
23#include <random>
24
25using namespace ROOT;
26
27double wasteCPUTime(std::mt19937 &gen)
28{
29 // Simulate number crunching through gen and ridiculous num bits
30 return std::generate_canonical<double, 100>(gen) + std::generate_canonical<double, 100>(gen) +
31 std::generate_canonical<double, 100>(gen) + std::generate_canonical<double, 100>(gen) +
32 std::generate_canonical<double, 100>(gen);
33}
34
36
37/// This function is called within each thread: it spends some CPU time and then
38/// fills a number into the histogram, through the Filler_t. This is repeated
39/// several times.
41{
42 std::mt19937 gen;
43
44 for (int i = 0; i < 3000000; ++i)
45 filler.Fill({wasteCPUTime(gen), wasteCPUTime(gen)});
46}
47
48/// This example fills a histogram concurrently, from several threads.
50{
51 // RHistConcurrentFillManager allows multiple threads to fill the histogram
52 // concurrently.
53 //
54 // Details: each thread's Fill() calls are buffered. once the buffer is full,
55 // the RHistConcurrentFillManager locks and flushes the buffer into the
56 // histogram.
58
59 std::array<std::thread, 8> threads;
60
61 // Let the threads fill the histogram concurrently.
62 for (auto &thr: threads) {
63 // Each thread calls fill(), passing a dedicated filler per thread.
64 thr = std::thread(theTask, fillMgr.MakeFiller());
65 }
66
67 // Join them.
68 for (auto &thr: threads)
69 thr.join();
70}
71
72void concurrentfill()
73{
74 // This histogram will be filled from several threads.
75 Experimental::RH2D hist{{100, 0., 1.}, {{0., 1., 2., 3., 10.}}};
76
78
79 std::cout << hist.GetEntries() << '\n';
80}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Manages the synchronization of calls to FillN().
Buffers a thread's Fill calls and submits them to the RHistConcurrentFillManager.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...