Dear expert,
I read a topic about activating multi-thread in ROOT using ROOT::EnableImplicitMT(nthreads).
I tested my new “discovery” on a tree I created myself. The tree is simple, it contains 4 branches. I fill the tree with 10 million events.
I wrote a simple code to read the tree and calculate the execution time. My code allows me to either activate or deactivate the multi-thread mode.
The result is unexpected (to me) as it cost 22 seconds to read the tree with multi-thread on (4 threads), while the mode is off, it took only 1 second.
I used top command to check the cpu usage, it was 400% when I activated the multi-thread as expected.
I use two version of root, one is from CMSSW_9_4_0 and one sourced from: /cvmfs/sft.cern.ch/lcg/views/dev3/latest/x86_64-centos7-gcc7-opt/setup.sh
Here is the code I used to generate the tree:
#include <stdio.h>
#include <iostream>
#include <pthread.h>
float myPi = 3.1415927;
void xTest_Wmulticore (bool usenthread = 1, int nthreads = 4)
{
if (usenthread==true) ROOT::EnableImplicitMT(nthreads);
//system ("mkdir -p /home/hoa/Task_TestCode/File_Root/");
system ("mkdir -p /afs/cern.ch/work/l/lcaophuc/Task_Test/Output/");
TRandom *rand = new TRandom3 (0);
//TFile *file = new TFile ("/home/hoa/Task_TestCode/File_Root/multiThread_Write.root", "recreate");
TFile *file = new TFile ("/afs/cern.ch/work/l/lcaophuc/Task_Test/Output/multiThread_Write.root", "recreate");
TTree *tree = new TTree ("tree", "my out tree");
float mass = 0.0;
float energy = 0.0;
float theta = 0.0;
float phi = 0.0;
tree -> Branch ("mass", &mass);
tree -> Branch ("energy", &energy);
tree -> Branch ("theta", &theta);
tree -> Branch ("phi", &phi);
printf ("Clock starts\n");
TStopwatch* clock = new TStopwatch();
clock -> Start();
for (long ev=0; ev<10000000; ev++)
{
mass = rand -> Gaus (90, 1);
energy = rand -> Landau (40, 5);
float tmptheta = rand -> Gaus (myPi/2.0, 0.5);
float tmpphi = rand -> Rndm ();
float sinetheta = sin(tmptheta);
theta = abs(asin(sinetheta));
phi = tmpphi * myPi;
tree -> Fill ();
}
float mytime = clock -> RealTime();
int nHour, nMinute;
float nSecond;
nHour = mytime / 3600;
nMinute = (mytime - 3600*nHour)/60;
nSecond = (mytime - 3600*nHour - 60*nMinute);
printf ("it takes %02d hour(s) %02d minute(s) and %010.7f second(s)\n", nHour, nMinute, nSecond);
tree ->Write("", TObject::kOverwrite);
}
and here is the one I used to read:
#include <stdio.h>
#include <iostream>
void xTest_Rmulticore (bool usenthread)
{
int nthreads = 4;
if (usenthread==true) ROOT::EnableImplicitMT(nthreads);
//TFile *file = new TFile ("/home/hoa/Task_ScaleFactor/Input/TnPTreeZ_SingleMuon_Run2016.root", "read");
TFile *file = new TFile ("/afs/cern.ch/work/l/lcaophuc/Task_Test/Output/multiThread_Write.root", "read");
TTree *tree = (TTree*)file -> Get ("tree");
long nEvent = tree -> GetEntries();
long nSum = 0;
printf ("There are %ld events, Clock starts\n", nEvent);
TStopwatch* clock = new TStopwatch();
clock -> Start();
for (long i=0; i<nEvent; i++)
{
tree -> GetEntry (i);
nSum ++;
}
Float_t mytime = clock -> RealTime();
int nHour, nMinute;
float nSecond;
nHour = mytime / 3600;
nMinute = (mytime - 3600*nHour)/60;
nSecond = (mytime - 3600*nHour - 60*nMinute);
printf ("File is read. The sum has been added %ld times\n", nSum);
printf ("it takes %02d hour(s) %02d minute(s) and %010.7f second(s)\n", nHour, nMinute, nSecond);
}
Are the processing times expected. If multithread should run faster, then what could be wrong with my code?
Thank you and best regards,
Hoa.
ROOT Version: 6.10/09
Platform: Not Provided
Compiler: Not Provided