#ifndef multithread_h #define multithread_h #include using std::map; #include "tbb/blocked_range.h" #include "tbb/task_scheduler_init.h" #include "tbb/task_scheduler_observer.h" #include "tbb/parallel_for.h" #include "tbb/spin_mutex.h" #include "tbb/tick_count.h" #include "pthread.h" #include "InputDataArray.h" #include "L1AlgoInputMCData.h" #include "TaskManager.h" // #define DEBUG_THREADS using namespace tbb; typedef spin_mutex TMyMutex; TMyMutex mutex; #ifdef DEBUG_THREADS typedef spin_mutex TMyMutex2; TMyMutex2 mutex2; typedef spin_mutex TMyMutex3; TMyMutex3 mutex3; #endif // DEBUG_THREADS tick_count time0 = tick_count::now(); // reference time map threadToCpuMap; // let get cpuId by threadId map threadNumberToCpuMap; // let get cpuId by threadNumber (see threads_counter) class ApplyReco{ InputDataArray *const InpData; int nEvPerThr; int* threadNumberToCpuMap; public: void operator()(const blocked_range &range) const { for(unsigned int i = range.begin(); i != range.end(); ++i){ pthread_t I = pthread_self(); int s; cpu_set_t cpuset; pthread_t thread = I; CPU_ZERO(&cpuset); CPU_SET(threadNumberToCpuMap[i], &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); int nEv = nEvPerThr; TaskManager manager; manager.Init( InpData->fSettings, 0 ); for (int j = 0; j < nEv; j++){ manager.SetData( &(InpData->fInput[j]) ); manager.Run(); } } } ApplyReco(InputDataArray* const InpD_, const int nEvPerThr_, int* threadNumberToCpuMap_): InpData(InpD_),nEvPerThr(nEvPerThr_) { threadNumberToCpuMap = new int[20]; for(int i=0; i<20; i++) threadNumberToCpuMap[i] = threadNumberToCpuMap_[i]; } ~ApplyReco() { if(threadNumberToCpuMap) delete [] threadNumberToCpuMap; } }; class ApplyRecoWithPerf{ InputDataArray *const InpData; InputMCDataArray *const InpMCData; Performance * const Perf; int nEvPerThr; int iVerbose; public: // void operator()(const blocked_range &range) const // { // for(unsigned int i = range.begin(); i != range.end(); ++i){ // L1Algo Tracker; // Tracker.Init( InpData[i].fSettings->GetSettings() ); // Perf[i].Init("",&Tracker,nEvPerThr); // for (int j = 0; j < nEvPerThr; j++){ // Tracker.SetData( InpData[i].fInput[j].GetStsHits(), // InpData[i].fInput[j].GetStsStrips(), // InpData[i].fInput[j].GetStsStripsB(), // InpData[i].fInput[j].GetStsZPos(), // InpData[i].fInput[j].GetSFlag(), // InpData[i].fInput[j].GetSFlagB(), // InpData[i].fInput[j].GetStsHitsStartIndex(), // InpData[i].fInput[j].GetStsHitsStopIndex()); // Tracker.CATrackFinder(); // Perf[i].SetData(InpMCData[i].fMCInput[j].GetHitStore(), // InpMCData[i].fMCInput[j].GetStsHits(), // InpMCData[i].fMCInput[j].GetMCPoints(), // InpMCData[i].fMCInput[j].GetMCTracks(), // InpMCData[i].fMCInput[j].GetHitMCRef() ); // Perf[i].RunEv(); // } // } // } ApplyRecoWithPerf(InputDataArray* const InpD_, InputMCDataArray* const InpMCD_, Performance * const Perf_, const int nEvPerThr_, const int iVerbose_): InpData(InpD_), InpMCData(InpMCD_), Perf(Perf_), nEvPerThr(nEvPerThr_), iVerbose(iVerbose_) {} ~ApplyRecoWithPerf() {} }; class ApplyMem{ const InputDataArray& InpDataPerThread; InputDataArray* const InpData; int nEvPerThr; int* threadNumberToCpuMap; public: void operator()(const blocked_range &range) const { const int NEvLocal = nEvPerThr; for(unsigned int i = range.begin(); i != range.end(); ++i){ pthread_t I = pthread_self(); int s; cpu_set_t cpuset; pthread_t thread = I; CPU_ZERO(&cpuset); CPU_SET(threadNumberToCpuMap[i], &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); InpData[i].fSettings = new L1AlgoInputSettings; InpData[i].fInput = new L1AlgoInputData[NEvLocal]; *(InpData[i].fSettings) = *(InpDataPerThread.fSettings); for(int j=0; j &range) const { const int NEvLocal = nEvPerThr; for(unsigned int i = range.begin(); i != range.end(); ++i){ pthread_t I = pthread_self(); int s; cpu_set_t cpuset; pthread_t thread = I; CPU_ZERO(&cpuset); CPU_SET(threadNumberToCpuMap[i], &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); Perf[i] = Performance(); InpMCData[i].fMCInput = new L1AlgoInputMCData[NEvLocal]; for(int j=0; j &range) const // { // for(unsigned int i = range.begin(); i != range.end(); ++i){ // algos[i].algo = new L1Algo[L1AlgoInter::maxNEvents]; // for (int j = 0; j < nEvPerThr; j++){ // algos[i].algo[j] = algoForOneCore.algo[j]; // // cout << i << " " << j << endl; // } // } // } // ApplyMem(const AlgoArray & _algoFOC, AlgoArray *const _algos, const int _nEvPerThr): algoForOneCore(_algoFOC), algos(_algos),nEvPerThr(_nEvPerThr){} // ~ApplyMem() // { // } // }; #undef DEBUG_THREADS #endif // multithread_h