Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:02

0001 #include "TFormula.h"
0002 #include "TThread.h"
0003 #include "TObject.h"
0004 #include "TVirtualStreamerInfo.h"
0005 #include <thread>
0006 #include <memory>
0007 #include <atomic>
0008 #include <cassert>
0009 #include <iostream>
0010 
0011 
0012 void printHelp(const char* iName, int iDefaultNThreads)
0013 {
0014   std::cout << iName <<" [number of threads] \n\n"
0015             <<"If no arguments are given "<<iDefaultNThreads<<" threads will be used"<<std::endl;
0016 }
0017 
0018 int parseOptionsForNumberOfThreads(int argc, char** argv)
0019 {
0020   constexpr int kDefaultNThreads = 4;
0021   int returnValue = kDefaultNThreads;
0022   if( argc == 2 ) {
0023     if(strcmp("-h",argv[1]) ==0) {
0024       printHelp(argv[0],kDefaultNThreads);
0025       exit( 0 );
0026     }
0027     
0028     returnValue = atoi(argv[1]);
0029  }
0030   
0031   if( argc > 2) {
0032     printHelp(argv[0],kDefaultNThreads);
0033     exit(1);
0034   }
0035   return returnValue ;
0036 }
0037 
0038 int main(int argc, char** argv)
0039 {
0040   const int kNThreads = parseOptionsForNumberOfThreads(argc, argv);
0041 
0042   std::atomic<int> canStart{kNThreads};
0043   std::vector<std::thread> threads;
0044   
0045   TThread::Initialize();
0046   //When threading, also have to keep ROOT from logging all TObjects into a list
0047   TObject::SetObjectStat(false);
0048   
0049   //Have to avoid having Streamers modify themselves after they have been used
0050   TVirtualStreamerInfo::Optimize(false);
0051   
0052   
0053   for(int i=0; i<kNThreads; ++i) {
0054     threads.emplace_back([&canStart]() {
0055         static thread_local TThread guard;
0056         --canStart;
0057         while( canStart > 0 ) {}
0058         
0059         TFormula f("testFormula","1./(1.+(4.61587e+06*(((1./(0.5*TMath::Max(1.e-6,x+1.)))-1.)/1.16042e+07)))");
0060         
0061         for(int i=0; i<100;++i) {
0062           double x = double(i)/100.;
0063           f.Eval(x);
0064         }
0065       });
0066   }
0067   canStart = true;
0068   
0069   for(auto& thread: threads) {
0070     thread.join();
0071   }
0072   
0073   return 0;
0074 }