ATLAS Offline Software
Loading...
Searching...
No Matches
CaloGPUTimed.h
Go to the documentation of this file.
1//
2// Copyright (C) 2002-2024 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 <shared_mutex>
12#include <string>
13#include <fstream>
15
16#include <GaudiKernel/IProperty.h>
17
23
25{
26
27 protected:
28
29
32 mutable std::shared_mutex m_timeMutex;
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(const size_t) const
65 {
66 //Do nothing
67 }
68
69 template <class Arg>
70 inline void record_times_helper(const size_t index, Arg && arg) const
71 {
72 // coverity[missing_lock]
73 m_times[index] = std::forward<Arg>(arg);
74
75 //This is called within a function that holds the lock itself.
76 }
77
78 template <class ... Args>
79 inline void record_times_helper(size_t index, Args && ... args) const
80 {
81 (record_times_helper(index++, std::forward<Args>(args)), ...);
82 }
83
84 protected:
85
86 inline void record_times(const size_t event_num, const std::vector<size_t> & times) const
87 {
88 size_t old_size;
89 {
90 std::unique_lock<std::shared_mutex> lock(m_timeMutex);
91 old_size = m_times.size();
92 m_times.resize(old_size + times.size());
93 m_eventNumbers.push_back(event_num);
94 }
95 {
96 std::shared_lock<std::shared_mutex> lock(m_timeMutex);
97 for (size_t i = 0; i < times.size(); ++i)
98 {
99 m_times[old_size + i] = times[i];
100 }
101 }
102 }
103
104 template <class ... Args>
105 inline void record_times(const size_t event_num, const size_t & value) const
106 {
107 const size_t time_size = 1;
108
109 size_t old_size;
110
111 {
112 std::unique_lock<std::shared_mutex> lock(m_timeMutex);
113 old_size = m_times.size();
114 m_times.resize(old_size + time_size);
115 m_eventNumbers.push_back(event_num);
116 }
117 {
118 std::shared_lock<std::shared_mutex> lock(m_timeMutex);
119 record_times_helper(old_size, value);
120 }
121 }
122
123 template <class ... Args>
124 inline void record_times(const size_t event_num, const size_t & value, Args && ... args) const
125 {
126 const size_t time_size = sizeof...(args) + 1;
127
128 size_t old_size;
129
130 {
131 std::unique_lock<std::shared_mutex> lock(m_timeMutex);
132 old_size = m_times.size();
133 m_times.resize(old_size + time_size);
134 m_eventNumbers.push_back(event_num);
135 }
136 {
137 std::shared_lock<std::shared_mutex> lock(m_timeMutex);
138 record_times_helper(old_size, value, std::forward<Args>(args)...);
139 }
140
141 }
142
143 inline void print_times(const std::string & header, const size_t time_size) const
144 {
145 std::shared_lock<std::shared_mutex> lock(m_timeMutex);
146
147 if (m_timeFileName.size() == 0)
148 {
149 return;
150 }
151
152 std::vector<size_t> indices(m_eventNumbers.size());
153
154 std::iota(indices.begin(), indices.end(), 0);
155 std::sort(indices.begin(), indices.end(), [&](size_t a, size_t b)
156 {
157 return m_eventNumbers[a] < m_eventNumbers[b];
158 }
159 );
160 std::ofstream out(m_timeFileName);
161
162 out << "Event_Number Total " << header << "\n";
163
164 for (const size_t idx : indices)
165 {
166 out << m_eventNumbers[idx] << " ";
167
168 size_t total = 0;
169
170 for (size_t i = 0; i < time_size; ++i)
171 {
172 total += m_times[idx * time_size + i];
173 }
174
175 out << total << " ";
176
177 for (size_t i = 0; i < time_size; ++i)
178 {
179 out << m_times[idx * time_size + i] << " ";
180 }
181 out << "\n";
182 }
183
184 out << std::endl;
185
186 out.close();
187 }
188};
189
190
191#endif //CALORECGPU_CALOGPUTIMED_H
static Double_t a
Define macros for attributes used to control the static checker.
void record_times(const size_t event_num, const size_t &value) const
Gaudi::Property< bool > m_measureTimes
If true, times are recorded to the file given by m_timeFileName.
Gaudi::Property< std::string > m_timeFileName
File to which times should be saved.
CaloGPUTimed(T *ptr)
std::vector< size_t > m_times ATLAS_THREAD_SAFE
Vector to hold execution times to be recorded if necessary.
void record_times(const size_t event_num, const size_t &value, Args &&... args) const
std::shared_mutex m_timeMutex
Mutex that is locked when recording times.
void print_times(const std::string &header, const size_t time_size) const
void record_times_helper(const size_t index, Arg &&arg) const
void record_times_helper(size_t index, Args &&... args) const
void record_times_helper(const size_t) const
void record_times(const size_t event_num, const std::vector< size_t > &times) const
Definition index.py:1
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.