ATLAS Offline Software
CaloGPUTimed.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 //
4 // Dear emacs, this is -*- c++ -*-
5 //
6 
7 #ifndef CALORECGPU_CALOGPUTIMED_H
8 #define CALORECGPU_CALOGPUTIMED_H
9 
10 #include <vector>
11 #include <mutex>
12 #include <string>
13 #include <fstream>
15 
16 #include <GaudiKernel/IProperty.h>
17 
25 {
26 
27  protected:
28 
29 
35  mutable std::vector<size_t> m_times ATLAS_THREAD_SAFE;
36  //Mutexes should ensure no problems with thread safety.
37 
40  mutable std::vector<size_t> m_eventNumbers ATLAS_THREAD_SAFE;
41  //Mutexes should ensure no problems with thread safety.
42 
46  Gaudi::Property<bool> m_measureTimes;
47 
50  Gaudi::Property<std::string> m_timeFileName;
51 
52  //Use CaloGPUTimed(this) in the derived classes for everything to work.
53 
54  template <class T>
55  CaloGPUTimed(T * ptr):
56  m_measureTimes(ptr, "MeasureTimes", false, "Save time measurements"),
57  m_timeFileName(ptr, "TimeFileOutput", "times.txt", "File to which time measurements should be saved")
58  {
59  }
60 
61 
62  private:
63 
64  inline void record_times_helper(size_t) const
65  {
66  //Do nothing
67  }
68 
69 
70  inline void record_times_helper(size_t index, size_t t) const
71  {
72  m_times[index] = t;
73  }
74 
75  template <class ... Args>
76  inline void record_times_helper(size_t index, size_t t, Args && ... args) const
77  {
79  record_times_helper(index + 1, std::forward<Args>(args)...);
80  }
81 
82  protected:
83 
84  inline void record_times(const size_t event_num, const std::vector<size_t> & times) const
85  {
86  size_t old_size;
87  //Scope just for the lock_guard.
88  {
89  std::lock_guard<std::mutex> lock_guard(m_timeMutex);
90  old_size = m_times.size();
91  m_times.resize(old_size + times.size());
92  m_eventNumbers.push_back(event_num);
93  }
94 
95  for (size_t i = 0; i < times.size(); ++i)
96  {
97  m_times[old_size + i] = times[i];
98  }
99  }
100 
101  template <class ... Args>
102  inline void record_times(const size_t event_num, const size_t & value) const
103  {
104  const size_t time_size = 1;
105 
106  size_t old_size;
107 
108  //Scope just for the lock_guard.
109  {
110  std::lock_guard<std::mutex> lock_guard(m_timeMutex);
111  old_size = m_times.size();
112  m_times.resize(old_size + time_size);
113  m_eventNumbers.push_back(event_num);
114  }
115 
116  record_times_helper(old_size, value);
117  }
118 
119  template <class ... Args>
120  inline void record_times(const size_t event_num, const size_t & value, Args && ... args) const
121  {
122  const size_t time_size = sizeof...(args) + 1;
123 
124  size_t old_size;
125 
126  //Scope just for the lock_guard.
127  {
128  std::lock_guard<std::mutex> lock_guard(m_timeMutex);
129  old_size = m_times.size();
130  m_times.resize(old_size + time_size);
131  m_eventNumbers.push_back(event_num);
132  }
133 
134  record_times_helper(old_size, value, std::forward<Args>(args)...);
135 
136  }
137 
138  inline void print_times(const std::string & header, const size_t time_size) const
139  {
140  if (m_timeFileName.size() == 0)
141  {
142  return;
143  }
144 
145  std::vector<size_t> indices(m_eventNumbers.size());
146  std::iota(indices.begin(), indices.end(), 0);
147  std::sort(indices.begin(), indices.end(), [&](size_t a, size_t b)
148  {
149  return m_eventNumbers[a] < m_eventNumbers[b];
150  }
151  );
152  std::ofstream out(m_timeFileName);
153 
154  out << "Event_Number Total " << header << "\n";
155 
156  for (const size_t idx : indices)
157  {
158  out << m_eventNumbers[idx] << " ";
159 
160  size_t total = 0;
161 
162  for (size_t i = 0; i < time_size; ++i)
163  {
164  total += m_times[idx * time_size + i];
165  }
166 
167  out << total << " ";
168 
169  for (size_t i = 0; i < time_size; ++i)
170  {
171  out << m_times[idx * time_size + i] << " ";
172  }
173  out << "\n";
174  }
175 
176  out << std::endl;
177 
178  out.close();
179  }
180 };
181 
182 
183 #endif //CALORECGPU_CALOGPUTIMED_H
header
Definition: hcg.cxx:526
index
Definition: index.py:1
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
CaloGPUTimed::record_times_helper
void record_times_helper(size_t index, size_t t, Args &&... args) const
Definition: CaloGPUTimed.h:76
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
CaloGPUTimed::m_timeFileName
Gaudi::Property< std::string > m_timeFileName
File to which times should be saved.
Definition: CaloGPUTimed.h:50
CaloGPUTimed::ATLAS_THREAD_SAFE
std::vector< size_t > m_times ATLAS_THREAD_SAFE
Vector to hold execution times to be recorded if necessary.
Definition: CaloGPUTimed.h:35
athena.value
value
Definition: athena.py:122
CaloGPUTimed::record_times_helper
void record_times_helper(size_t) const
Definition: CaloGPUTimed.h:64
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Args
Definition: test_lwtnn_fastgraph.cxx:12
CaloGPUTimed
Base class to provide some basic common infrastructure for timing measurements...
Definition: CaloGPUTimed.h:25
lumiFormat.i
int i
Definition: lumiFormat.py:92
CaloGPUTimed::record_times_helper
void record_times_helper(size_t index, size_t t) const
Definition: CaloGPUTimed.h:70
CaloGPUTimed::print_times
void print_times(const std::string &header, const size_t time_size) const
Definition: CaloGPUTimed.h:138
CaloGPUTimed::ATLAS_THREAD_SAFE
std::vector< size_t > m_eventNumbers ATLAS_THREAD_SAFE
Vector to hold the event numbers to be recorded if necessary.
Definition: CaloGPUTimed.h:40
CaloGPUTimed::record_times
void record_times(const size_t event_num, const size_t &value, Args &&... args) const
Definition: CaloGPUTimed.h:120
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
CaloGPUTimed::m_timeMutex
std::mutex m_timeMutex
Mutex that is locked when recording times.
Definition: CaloGPUTimed.h:32
CaloGPUTimed::CaloGPUTimed
CaloGPUTimed(T *ptr)
Definition: CaloGPUTimed.h:55
CaloGPUTimed::record_times
void record_times(const size_t event_num, const size_t &value) const
Definition: CaloGPUTimed.h:102
DeMoScan.index
string index
Definition: DeMoScan.py:362
a
TList * a
Definition: liststreamerinfos.cxx:10
CaloGPUTimed::record_times
void record_times(const size_t event_num, const std::vector< size_t > &times) const
Definition: CaloGPUTimed.h:84
CaloGPUTimed::m_measureTimes
Gaudi::Property< bool > m_measureTimes
If true, times are recorded to the file given by m_timeFileName.
Definition: CaloGPUTimed.h:46
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
checker_macros.h
Define macros for attributes used to control the static checker.
python.CaloScaleNoiseConfig.args
args
Definition: CaloScaleNoiseConfig.py:80
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
plot_times.times
def times(fn)
Definition: plot_times.py:11