CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConcurrentModuleTimer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Subsystem/Package
4 // Class : ConcurrentModuleTimer
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Tue, 10 Dec 2013 21:16:00 GMT
11 //
12 #include <vector>
13 #include <atomic>
14 #include <chrono>
15 #include <iostream>
16 
23 
24 
25 namespace edm {
26  namespace service {
28  public:
31  //static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
32  private:
33  void start();
34  void stop();
35 
36  std::unique_ptr<std::atomic<std::chrono::high_resolution_clock::rep>[]> m_timeSums;
37  std::chrono::high_resolution_clock::time_point m_time;
38  unsigned int m_nTimeSums;
39  unsigned int m_nModules;
40  std::atomic<bool> m_spinLock;
42  };
43  }
44 }
45 
46 using namespace edm::service;
47 // system include files
48 
49 // user include files
50 
51 //
52 // constants, enums and typedefs
53 //
54 
55 //
56 // static data member definitions
57 //
58 
59 //
60 // constructors and destructor
61 //
63 m_time(),
64 m_nModules(0),
65 m_spinLock{false},
66 m_startedTiming(false)
67 {
68  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const&){
69  start();
70  });
71  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const&){
72  stop();
73  });
74 
75  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const&){
76  stop();
77  });
78  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const&){
79  start();
80  });
81 
82  iReg.watchPreallocate([this](edm::service::SystemBounds const& iBounds){
83  m_nTimeSums =iBounds.maxNumberOfThreads()+1;
84  m_timeSums.reset(new std::atomic<std::chrono::high_resolution_clock::rep>[m_nTimeSums]);
85  for(unsigned int i=0; i<m_nTimeSums;++i) {
86  m_timeSums[i]=0;
87  }
88  });
89 
90  iReg.watchPreSourceEvent([this](StreamID){
91  if(not m_startedTiming) {
93  m_startedTiming=true;
94  }
95  start();
96  });
97  iReg.watchPostSourceEvent([this](StreamID){
98  stop();
99  });
100 }
101 
103 
104  std::cout <<"Fraction of time running n Modules simultaneously"<<std::endl;
105  for (unsigned int i=0; i<m_nTimeSums; ++i) {
106  std::cout <<i<<" "<<m_timeSums[i]/double(m_timeSums[0])<<" "<<m_timeSums[i]<<std::endl;
107  }
108 
109 }
110 
111 // ConcurrentModuleTimer::ConcurrentModuleTimer(const ConcurrentModuleTimer& rhs)
112 // {
113 // // do actual copying here;
114 // }
115 
116 //
117 // assignment operators
118 //
119 // const ConcurrentModuleTimer& ConcurrentModuleTimer::operator=(const ConcurrentModuleTimer& rhs)
120 // {
121 // //An exception safe implementation is
122 // ConcurrentModuleTimer temp(rhs);
123 // swap(rhs);
124 //
125 // return *this;
126 // }
127 
128 //
129 // member functions
130 //
131 void
133 {
134  auto const newTime =std::chrono::high_resolution_clock::now();
135  std::chrono::high_resolution_clock::time_point oldTime;
136  bool expected = false;
137  unsigned int nModules;
138  while (not m_spinLock.compare_exchange_strong(expected,true,std::memory_order_acq_rel)){
139  expected = false;
140  }
141  {
142  oldTime = m_time;
143  m_time = newTime;
144  nModules = ++m_nModules;
145  m_spinLock.store(false,std::memory_order_release);
146  }
147  assert(nModules <m_nTimeSums);
148  auto diff = newTime - oldTime;
149  for(unsigned int i=0;i<nModules;++i) {
150  m_timeSums[i].fetch_add(diff.count());
151  }
152 }
153 
154 void
156 {
157  auto const newTime =std::chrono::high_resolution_clock::now();
158  std::chrono::high_resolution_clock::time_point oldTime;
159  bool expected = false;
160  unsigned int nModules;
161  while (not m_spinLock.compare_exchange_weak(expected,true,std::memory_order_acq_rel)){
162  expected = false;
163  }
164  {
165  oldTime = m_time;
166  m_time = newTime;
167  nModules = m_nModules--;
168  m_spinLock.store(false,std::memory_order_release);
169  }
170  assert(nModules <m_nTimeSums);
171  auto diff = newTime - oldTime;
172  for(unsigned int i=0;i<=nModules;++i) {
173  m_timeSums[i].fetch_add(diff.count());
174  }
175 }
176 
177 
178 //
179 // const member functions
180 //
181 
182 //
183 // static member functions
184 //
186 
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:46
int i
Definition: DBlmapReader.cc:9
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
ConcurrentModuleTimer(edm::ParameterSet const &iConfig, edm::ActivityRegistry &iAR)
std::chrono::high_resolution_clock::time_point m_time
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
tuple cout
Definition: gather_cfg.py:121
std::unique_ptr< std::atomic< std::chrono::high_resolution_clock::rep >[]> m_timeSums