CbmRoot
CbmRichParallelQa.cxx
Go to the documentation of this file.
1 
7 #include "CbmRichParallelQa.h"
8 #include "CbmRichHit.h"
9 #include "FairRootManager.h"
10 #include "tbb/tick_count.h"
11 
12 #include "tbb/parallel_for.h"
13 #include "tbb/parallel_invoke.h"
14 #include "tbb/spin_mutex.h"
15 #include "tbb/task.h"
16 #include "tbb/task_scheduler_init.h"
17 #include "tbb/task_scheduler_observer.h"
18 
19 #include <fstream>
20 #include <iostream>
21 #include <map>
22 
23 using namespace std;
24 using namespace tbb;
25 
26 int threads_counter = -1;
27 map<int, long> threadToCpuMap; // let get cpuId by threadId
28 map<int, int>
29  threadNumberToCpuMap; // let get cpuId by threadNumber (see threads_counter)
30 spin_mutex mutex;
31 
32 class TMyObserver : public task_scheduler_observer {
33 public:
34  void FInit(); // set cpu - thread correspondence
35 protected:
36  void
37  on_scheduler_entry(bool Is_worker); // run at begin of each thread execution
38  void
39  on_scheduler_exit(bool Is_worker); // run at end of each thread execution
40 };
41 
42 // set cpu - thread correspondence
44  for (int i = 0; i < 8; i++) {
45  //threadNumberToCpuMap[2 * i + 0] = i;
46  //threadNumberToCpuMap[2 * i + 1] = i + 8;
48  threadNumberToCpuMap[8 + i] = i + 8;
49  };
50  observe(true);
51 }
52 
54 #define handle_error_en(en, msg) \
55  do { \
56  errno = en; \
57  perror(msg); \
58  exit(EXIT_FAILURE); \
59  } while (0)
60 void TMyObserver::on_scheduler_entry(bool Is_worker) {
61  //cout << "-I-Scheduler entry" <<endl;
62  pthread_t I = pthread_self();
63  spin_mutex::scoped_lock lock;
64  lock.acquire(mutex);
66  int cpuId = threadNumberToCpuMap[threads_counter % 16];
67 
68  //cout << "ThrId=" << I << " thread have been created " << threads_counter << "-th.";
69  //cout << " And was run on cpu " << cpuId << endl;
70 
71  lock.release();
72  threadToCpuMap[I] = cpuId;
73 
74  int s, j;
75  cpu_set_t cpuset;
76  pthread_t thread = I;
77  CPU_ZERO(&cpuset);
78  CPU_SET(cpuId, &cpuset);
79  // cout << "before" << endl; //FIXME: segmentation fault somethere.
80  s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
81  if (s != 0) {
82  cout << " pthread_setaffinity_np " << endl;
83  // handle_error_en(s, "pthread_setaffinity_np");
84  }
85 };
86 
87 // run at end of each thread execution
88 void TMyObserver::on_scheduler_exit(bool Is_worker) //FIXME: don't run
89 {
90  pthread_t I = pthread_self();
91  cout << "Thread with number " << I << " was ended " << --threads_counter;
92 };
93 
94 
95 class FinderTaskQa : public task {
97  //CbmRichRingFinderHough* fHT;
98  std::vector<std::vector<CbmRichHoughHit>> fData;
99  // std::vector<CbmRichHoughHit> fData;
100 public:
102  const std::vector<std::vector<CbmRichHoughHit>>& data) {
103  fHT = HTImpl;
104  fData = data;
105  //fData.assign(data[0].begin(), data[0].end());
106  }
108  //for (int j = 0; j < 10; j ++){
109  for (int i = 0; i < fData.size(); i++) {
110  //cout<< "rec event "<< i << endl;
111  fHT->DoFind(fData[i]);
112  }
113  //}//
114  return NULL;
115  }
116 };
117 
118 
120  for (int i = 0; i < kMAX_NOF_THREADS; i++) {
121  //fHT[i] = new CbmRichRingFinderHough(0, "compact");
122  fHT[i] = new CbmL1RichENNRingFinder(0);
123  }
124 
125  fEventNumber = 0;
126  fExecTime = 0.;
127 }
128 
130 
132  cout << "InitStatus CbmRichParallelQa::Init()" << endl;
133 
134  FairRootManager* ioman = FairRootManager::Instance();
135  if (NULL == ioman) {
136  Fatal("CbmRichParallelQa::Init", "RootManager not instantised!");
137  }
138 
139  fRichHits = (TClonesArray*) ioman->GetObject("RichHit");
140  if (NULL == fRichHits) {
141  Fatal("CbmRichParallelQa::Init", "No RichHit array!");
142  }
143 
144  fRichRings = (TClonesArray*) ioman->GetObject("RichRing");
145  if (NULL == fRichRings) {
146  Fatal("CbmRichParallelQa::Init", "No RichRing array!");
147  }
148 
149  for (int i = 0; i < kMAX_NOF_THREADS; i++) {
150  fHT[i]->Init();
151  }
152 
153  // tbb::task_scheduler_init init();
154  // TMyObserver obs;
155  // obs.FInit(); // set cpu-threads correspondence
156 
157  return kSUCCESS;
158 }
159 
160 void CbmRichParallelQa::Exec(Option_t* option) {
161  fEventNumber++;
162  cout << "-I- Read event " << fEventNumber << endl;
163  std::vector<CbmRichHoughHit> data;
164 
165  const Int_t nhits = fRichHits->GetEntriesFast();
166  if (!nhits) {
167  cout << "-E- CbmRichRingFinderHough::DoFind:No hits in this event." << endl;
168  return;
169  }
170  data.reserve(nhits);
171 
172  for (Int_t iHit = 0; iHit < nhits; iHit++) {
173  CbmRichHit* hit = (CbmRichHit*) fRichHits->At(iHit);
174  if (hit) {
175  CbmRichHoughHit tempPoint;
176  tempPoint.fHit.fX = hit->GetX();
177  tempPoint.fHit.fY = hit->GetY();
178  tempPoint.fX2plusY2 =
179  hit->GetX() * hit->GetX() + hit->GetY() * hit->GetY();
180  tempPoint.fId = iHit;
181  tempPoint.fIsUsed = false;
182  data.push_back(tempPoint);
183  }
184  }
185 
186  fData.push_back(data);
187 
188  if (fEventNumber == fNofEvents) {
189  cout << "-I- NofTasks = " << fNofTasks << endl;
190  TMyObserver obs;
191  obs.FInit(); // set cpu-threads correspondence
192  obs.observe(true);
193  tbb::task_scheduler_init init(fNofTasks);
194  DoTestWithTask();
195  }
196 }
197 
199  tbb::tick_count t0 = tbb::tick_count::now();
200  task* root_task = new (task::allocate_root()) empty_task;
201  root_task->set_ref_count(fNofTasks + 1);
202  task_list list;
203 
204  for (int iT = 0; iT < fNofTasks; iT++) {
205  list.push_back(*new (root_task->allocate_child())
206  FinderTaskQa(fHT[iT], fData));
207  }
208 
209  root_task->spawn_and_wait_for_all(list);
210  tbb::tick_count t1 = tbb::tick_count::now();
211 
212  root_task->destroy(*root_task);
213  fExecTime += (t1 - t0).seconds();
214  cout << 1000. * fExecTime << " ms for " << fData.size() << " events" << endl;
215  cout << 1000. * fExecTime / (fData.size()) << " ms per event " << endl;
216  cout << fNofTasks * fData.size() / fExecTime << " events per sec" << endl;
217 
218  std::ofstream fout;
219  fout.open("parallel.txt", std::ios_base::app);
220  fout << (int) (fNofTasks * fData.size() / fExecTime) << ",";
221 }
CbmPixelHit::GetX
Double_t GetX() const
Definition: CbmPixelHit.h:83
CbmPixelHit::GetY
Double_t GetY() const
Definition: CbmPixelHit.h:84
threadNumberToCpuMap
map< int, int > threadNumberToCpuMap
Definition: CbmRichParallelQa.cxx:29
TMyObserver::on_scheduler_entry
void on_scheduler_entry(bool Is_worker)
Definition: CbmRichParallelQa.cxx:60
CbmRichHoughHit::fX2plusY2
float fX2plusY2
Definition: CbmRichRingFinderData.h:31
CbmRichHoughHit
Implementation of RICH hit for ring finder algorithm.
Definition: CbmRichRingFinderData.h:21
CbmL1RichENNRingFinder
Definition: CbmL1RichENNRingFinder.h:31
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
threads_counter
int threads_counter
Definition: CbmRichParallelQa.cxx:26
CbmRichParallelQa::DoTestWithTask
void DoTestWithTask()
Definition: CbmRichParallelQa.cxx:198
CbmRichHitLight::fX
float fX
Definition: CbmRichRingLight.h:34
FinderTaskQa::fData
std::vector< std::vector< CbmRichHoughHit > > fData
Definition: CbmRichParallelQa.cxx:98
CbmRichParallelQa::Init
virtual InitStatus Init()
Definition: CbmRichParallelQa.cxx:131
CbmRichParallelQa.h
TMyObserver::FInit
void FInit()
Definition: CbmRichParallelQa.cxx:43
CbmL1RichENNRingFinder::DoFind
Int_t DoFind(TClonesArray *hitArray, TClonesArray *projArray, TClonesArray *ringArray)
Definition: CbmL1RichENNRingFinder.cxx:54
task
@ task
Definition: CbmMvdSensorPlugin.h:22
threadToCpuMap
map< int, long > threadToCpuMap
Definition: CbmRichParallelQa.cxx:27
FinderTaskQa::FinderTaskQa
FinderTaskQa(CbmL1RichENNRingFinder *HTImpl, const std::vector< std::vector< CbmRichHoughHit >> &data)
Definition: CbmRichParallelQa.cxx:101
mutex
spin_mutex mutex
Definition: CbmRichParallelQa.cxx:30
TMyObserver
Definition: CbmRichParallelQa.cxx:32
CbmRichParallelQa::Exec
virtual void Exec(Option_t *option)
Definition: CbmRichParallelQa.cxx:160
CbmRichHitLight::fY
float fY
Definition: CbmRichRingLight.h:35
FinderTaskQa::fHT
CbmL1RichENNRingFinder * fHT
Definition: CbmRichParallelQa.cxx:96
CbmRichHoughHit::fIsUsed
bool fIsUsed
Definition: CbmRichRingFinderData.h:33
CbmRichParallelQa::CbmRichParallelQa
CbmRichParallelQa()
Definition: CbmRichParallelQa.cxx:119
FinderTaskQa
Definition: CbmRichParallelQa.cxx:95
CbmRichHit.h
TMyObserver::on_scheduler_exit
void on_scheduler_exit(bool Is_worker)
Definition: CbmRichParallelQa.cxx:88
CbmRichHoughHit::fHit
CbmRichHitLight fHit
Definition: CbmRichRingFinderData.h:30
CbmRichHit
Definition: CbmRichHit.h:19
FinderTaskQa::execute
task * execute()
Definition: CbmRichParallelQa.cxx:107
CbmRichParallelQa::~CbmRichParallelQa
virtual ~CbmRichParallelQa()
Definition: CbmRichParallelQa.cxx:129