Fill n-tuples in distinct workers.
This tutorial illustrates the basics of how it's possible with ROOT to offload heavy operations on multiple processes and how it's possible to write simultaneously multiple files. The operation performed in this case is the creation of random gaussian numbers.
class TimerRAII {
std::string fMeta;
public:
TimerRAII(const char *meta): fMeta(meta) {
fTimer.Start();
}
~TimerRAII() {
fTimer.Stop();
std::cout << fMeta << " - real time elapsed " << fTimer.RealTime() << "s" << std::endl;
}
};
{
const UInt_t nNumbers = 20000000U;
for (
UInt_t i = 0; i <
n; ++i) ntuple.
Fill(rndm.Gaus());
};
TFile ofile(
"mp101_singleCore.root",
"RECREATE");
TNtuple randomNumbers(
"singleCore",
"Random Numbers",
"r");
{
TimerRAII
t(
"Sequential execution");
randomNumbers.Write();
}
TFile ofile(
Form(
"mp101_multiCore_%u.root", workerID),
"RECREATE");
TNtuple workerRandomNumbers(
"multiCore",
"Random Numbers",
"r");
fillRandom(workerRandomNumbers, workerRndm, workSize);
workerRandomNumbers.Write();
return 0;
};
{
TimerRAII
t(
"Parallel execution");
const auto workSize = nNumbers / nWorkers;
using namespace std::placeholders;
auto workItemOneArg = std::bind(workItem, _1, workSize);
std::forward_list<UInt_t> workerIDs(nWorkers);
std::iota(std::begin(workerIDs), std::end(workerIDs), 0);
workers.Map(workItemOneArg, workerIDs);
}
return 0;
}
- Author
- Danilo Piparo
Definition in file mp101_fillNtuples.C.