Parallel fill of a histogram.
This tutorial shows how a histogram can be filled in parallel with a multithreaded approach. The difference with the multiprocess case, see mp201, is that here we cannot count on the copy-on-write mechanism, but we rather need to protect the histogram resource with a TThreadedObject class. The result of the filling is monitored with the SnapshotMerge method. This method is not thread safe: in presence of ROOT histograms, the system will not crash but the result is not uniquely defined.
Int_t mt201_parallelHistoFill()
{
auto fillRandomHisto = [&](int seed = 0) {
auto histogram = ts_h.Get();
for (
auto i :
ROOT::TSeqI(1000000)) {
histogram->Fill(rndm.Gaus(0, 1));
}
};
std::vector<std::thread> pool;
auto monitor = [&]() {
std::this_thread::sleep_for(std::chrono::duration<double, std::nano>(500));
auto h = ts_h.SnapshotMerge();
std::cout <<
"Entries for the snapshot " <<
h->GetEntries() << std::endl;
}
};
pool.emplace_back(monitor);
pool.emplace_back(fillRandomHisto, seed);
}
for (auto &&t : pool)
t.join();
auto sumRandomHisto = ts_h.Merge();
std::cout << "Entries for the total sum " << sumRandomHisto->GetEntries() << std::endl;
sumRandomHisto->DrawClone();
return 0;
}
A wrapper to make object instances thread private, lazily.
Random number generator class based on M.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
- Date
- January 2016
- Author
- Danilo Piparo
Definition in file mt201_parallelHistoFill.C.