Project
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
15
16#ifndef ITSTRACKINGGPU_UTILS_H_
17#define ITSTRACKINGGPU_UTILS_H_
18
19#include <vector>
20#include <string>
21#include <tuple>
22
23#include "GPUCommonDef.h"
24#include "GPUCommonHelpers.h"
25#include "GPUCommonLogger.h"
26
27#ifndef __HIPCC__
28#define THRUST_NAMESPACE thrust::cuda
29#else
30#define THRUST_NAMESPACE thrust::hip
31#endif
32
33#ifdef ITS_GPU_LOG
34#define GPULog(...) LOGP(info, __VA_ARGS__)
35#else
36#define GPULog(...)
37#endif
38
39namespace o2::its
40{
41
42template <typename T1, typename T2>
43using gpuPair = std::pair<T1, T2>;
44
45namespace gpu
46{
47
48template <typename T>
49struct gpuSpan {
50 using value_type = T;
51 using ptr = T*;
52 using ref = T&;
53
54 GPUd() gpuSpan() : _data(nullptr), _size(0) {}
55 GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
56 GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
57 GPUd() unsigned int size() const { return _size; }
58 GPUd() bool empty() const { return _size == 0; }
59 GPUd() ref front() const { return _data[0]; }
60 GPUd() ref back() const { return _data[_size - 1]; }
61 GPUd() ptr begin() const { return _data; }
62 GPUd() ptr end() const { return _data + _size; }
63
64 protected:
66 unsigned int _size;
67};
68
69template <typename T>
70struct gpuSpan<const T> {
71 using value_type = T;
72 using ptr = const T*;
73 using ref = const T&;
74
75 GPUd() gpuSpan() : _data(nullptr), _size(0) {}
76 GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
78 GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
79 GPUd() unsigned int size() const { return _size; }
80 GPUd() bool empty() const { return _size == 0; }
81 GPUd() ref front() const { return _data[0]; }
82 GPUd() ref back() const { return _data[_size - 1]; }
83 GPUd() ptr begin() const { return _data; }
84 GPUd() ptr end() const { return _data + _size; }
85
86 protected:
88 unsigned int _size;
89};
90
91// Abstract stream class
92class Stream
93{
94 public:
95#if defined(__HIPCC__)
96 using Handle = hipStream_t;
97 static constexpr Handle DefaultStream = 0;
98 static constexpr unsigned int DefaultFlag = hipStreamNonBlocking;
99 using Event = hipEvent_t;
100#elif defined(__CUDACC__)
101 using Handle = cudaStream_t;
102 static constexpr Handle DefaultStream = 0;
103 static constexpr unsigned int DefaultFlag = cudaStreamNonBlocking;
104 using Event = cudaEvent_t;
105#else
106 using Handle = void*;
107 static constexpr Handle DefaultStream = nullptr;
108 static constexpr unsigned int DefaultFlag = 0;
109 using Event = void*;
110#endif
111
112 Stream(unsigned int flags = DefaultFlag)
113 {
114#if defined(__HIPCC__)
115 GPUChkErrS(hipStreamCreateWithFlags(&mHandle, flags));
116 GPUChkErrS(hipEventCreateWithFlags(&mEvent, hipEventDisableTiming));
117#elif defined(__CUDACC__)
118 GPUChkErrS(cudaStreamCreateWithFlags(&mHandle, flags));
119 GPUChkErrS(cudaEventCreateWithFlags(&mEvent, cudaEventDisableTiming));
120#endif
121 }
122
123 Stream(Handle h) : mHandle(h) {}
125 {
126 if (mHandle != DefaultStream) {
127#if defined(__HIPCC__)
128 GPUChkErrS(hipStreamDestroy(mHandle));
129 GPUChkErrS(hipEventDestroy(mEvent));
130#elif defined(__CUDACC__)
131 GPUChkErrS(cudaStreamDestroy(mHandle));
132 GPUChkErrS(cudaEventDestroy(mEvent));
133#endif
134 }
135 }
136
137 operator bool() const { return mHandle != DefaultStream; }
138 const Handle& get() { return mHandle; }
139 const Handle& getStream() { return mHandle; }
140 const Event& getEvent() { return mEvent; }
141 void sync() const
142 {
143#if defined(__HIPCC__)
144 GPUChkErrS(hipStreamSynchronize(mHandle));
145#elif defined(__CUDACC__)
146 GPUChkErrS(cudaStreamSynchronize(mHandle));
147#endif
148 }
149 void record()
150 {
151#if defined(__HIPCC__)
152 GPUChkErrS(hipEventRecord(mEvent, mHandle));
153#elif defined(__CUDACC__)
154 GPUChkErrS(cudaEventRecord(mEvent, mHandle));
155#endif
156 }
157
158 private:
159 Handle mHandle{DefaultStream};
160 Event mEvent{nullptr};
161};
162
163// Abstract vector for streams.
165{
166 public:
167 size_t size() const noexcept { return mStreams.size(); }
168 void resize(size_t n) { mStreams.resize(n); }
169 void clear() { mStreams.clear(); }
170 auto& operator[](size_t i) { return mStreams[i]; }
171 void push_back(const Stream& stream) { mStreams.push_back(stream); }
172 void sync(bool device = true)
173 {
174 if (device) {
175#if defined(__HIPCC__)
176 GPUChkErrS(hipDeviceSynchronize());
177#elif defined(__CUDACC__)
178 GPUChkErrS(cudaDeviceSynchronize());
179#endif
180 } else {
181 for (auto& s : mStreams) {
182 s.sync();
183 }
184 }
185 }
186 void waitEvent(size_t iStream, size_t iEvent)
187 {
188#if defined(__HIPCC__)
189 GPUChkErrS(hipStreamWaitEvent(mStreams[iStream].get(), mStreams[iEvent].getEvent()));
190#elif defined(__CUDACC__)
191 GPUChkErrS(cudaStreamWaitEvent(mStreams[iStream].get(), mStreams[iEvent].getEvent()));
192#endif
193 }
194
195 private:
196 std::vector<Stream> mStreams;
197};
198
199#ifdef ITS_MEASURE_GPU_TIME
200class GPUTimer
201{
202 public:
203 GPUTimer(const std::string& name)
204 : mName(name)
205 {
206 mStreams.emplace_back(Stream::DefaultStream);
207 startTimers();
208 }
209 GPUTimer(Streams& streams, const std::string& name)
210 : mName(name)
211 {
212 for (size_t i{0}; i < streams.size(); ++i) {
213 mStreams.push_back(streams[i].get());
214 }
215 startTimers();
216 }
217 GPUTimer(Streams& streams, const std::string& name, size_t end, size_t start = 0)
218 : mName(name)
219 {
220 for (size_t sta{start}; sta < end; ++sta) {
221 mStreams.push_back(streams[sta].get());
222 }
223 startTimers();
224 }
225 GPUTimer(Stream& stream, const std::string& name, const int id = 0)
226 : mName(name)
227 {
228 mStreams.push_back(stream.get());
229 mName += ":id" + std::to_string(id);
230 startTimers();
231 }
232 ~GPUTimer()
233 {
234 for (size_t i{0}; i < mStreams.size(); ++i) {
235 float ms = 0.0f;
236#if defined(__HIPCC__)
237 GPUChkErrS(hipEventRecord(mStops[i], mStreams[i]));
238 GPUChkErrS(hipEventSynchronize(mStops[i]));
239 GPUChkErrS(hipEventElapsedTime(&ms, mStarts[i], mStops[i]));
240 GPUChkErrS(hipEventDestroy(mStarts[i]));
241 GPUChkErrS(hipEventDestroy(mStops[i]));
242#elif defined(__CUDACC__)
243 GPUChkErrS(cudaEventRecord(mStops[i], mStreams[i]));
244 GPUChkErrS(cudaEventSynchronize(mStops[i]));
245 GPUChkErrS(cudaEventElapsedTime(&ms, mStarts[i], mStops[i]));
246 GPUChkErrS(cudaEventDestroy(mStarts[i]));
247 GPUChkErrS(cudaEventDestroy(mStops[i]));
248#endif
249 LOGP(info, "Elapsed time for {}:{} {} ms", mName, i, ms);
250 }
251 }
252
253 void startTimers()
254 {
255 mStarts.resize(mStreams.size());
256 mStops.resize(mStreams.size());
257 for (size_t i{0}; i < mStreams.size(); ++i) {
258#if defined(__HIPCC__)
259 GPUChkErrS(hipEventCreate(&mStarts[i]));
260 GPUChkErrS(hipEventCreate(&mStops[i]));
261 GPUChkErrS(hipEventRecord(mStarts[i], mStreams[i]));
262#elif defined(__CUDACC__)
263 GPUChkErrS(cudaEventCreate(&mStarts[i]));
264 GPUChkErrS(cudaEventCreate(&mStops[i]));
265 GPUChkErrS(cudaEventRecord(mStarts[i], mStreams[i]));
266#endif
267 }
268 }
269
270 private:
271 std::string mName;
272 std::vector<Stream::Event> mStarts, mStops;
273 std::vector<Stream::Handle> mStreams;
274};
275#else // ITS_MEASURE_GPU_TIME not defined
277{
278 public:
279 template <typename... Args>
280 GPUTimer(Args&&...)
281 {
282 }
283};
284#endif
285} // namespace gpu
286} // namespace o2::its
287
288#endif
int32_t i
#define GPUChkErrS(x)
Class for time synchronization of RawReader instances.
GPUTimer(Args &&...)
Definition Utils.h:280
const Handle & getStream()
Definition Utils.h:139
void sync() const
Definition Utils.h:141
const Handle & get()
Definition Utils.h:138
static constexpr Handle DefaultStream
Definition Utils.h:107
static constexpr unsigned int DefaultFlag
Definition Utils.h:108
const Event & getEvent()
Definition Utils.h:140
Stream(unsigned int flags=DefaultFlag)
Definition Utils.h:112
Stream(Handle h)
Definition Utils.h:123
void resize(size_t n)
Definition Utils.h:168
void push_back(const Stream &stream)
Definition Utils.h:171
void waitEvent(size_t iStream, size_t iEvent)
Definition Utils.h:186
void sync(bool device=true)
Definition Utils.h:172
auto & operator[](size_t i)
Definition Utils.h:170
size_t size() const noexcept
Definition Utils.h:167
GLdouble n
Definition glcorearb.h:1982
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean * data
Definition glcorearb.h:298
GLbitfield flags
Definition glcorearb.h:1570
GLuint start
Definition glcorearb.h:469
GLuint GLuint stream
Definition glcorearb.h:1806
auto get(const std::byte *buffer, size_t=0)
Definition DataHeader.h:454
std::pair< T1, T2 > gpuPair
Definition Utils.h:43
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
void empty(int)
GPUd() gpuSpan(const gpuSpan< T > &other)
Definition Utils.h:77
GPUd() ref front() const
Definition Utils.h:81
GPUd() bool empty() const
Definition Utils.h:80
GPUd() ref operator[](unsigned int idx) const
Definition Utils.h:78
GPUd() ptr end() const
Definition Utils.h:84
GPUd() unsigned int size() const
Definition Utils.h:79
GPUd() ptr begin() const
Definition Utils.h:83
GPUd() ref back() const
Definition Utils.h:82
GPUd() bool empty() const
Definition Utils.h:58
unsigned int dim
Definition Utils.h:55
GPUd() ptr begin() const
Definition Utils.h:61
GPUd() ref operator[](unsigned int idx) const
Definition Utils.h:56
GPUd() gpuSpan()
Definition Utils.h:54
GPUd() ptr end() const
Definition Utils.h:62
GPUd() unsigned int size() const
Definition Utils.h:57
unsigned int _size
Definition Utils.h:66
GPUd() ref back() const
Definition Utils.h:60
GPUd() ref front() const
Definition Utils.h:59
VectorOfTObjectPtrs other