Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 05:18:46

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