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
21#include "GPUCommonDef.h"
22#include "GPUCommonHelpers.h"
23
24namespace o2::its
25{
26
27template <typename T1, typename T2>
28struct gpuPair {
31};
32
33namespace gpu
34{
35
36// Poor man implementation of a span-like struct. It is very limited.
37template <typename T>
38struct gpuSpan {
39 using value_type = T;
40 using ptr = T*;
41 using ref = T&;
42
43 GPUd() gpuSpan() : _data(nullptr), _size(0) {}
44 GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
45 GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
46 GPUd() unsigned int size() const { return _size; }
47 GPUd() bool empty() const { return _size == 0; }
48 GPUd() ref front() const { return _data[0]; }
49 GPUd() ref back() const { return _data[_size - 1]; }
50 GPUd() ptr begin() const { return _data; }
51 GPUd() ptr end() const { return _data + _size; }
52
53 protected:
55 unsigned int _size;
56};
57
58template <typename T>
59struct gpuSpan<const T> {
60 using value_type = T;
61 using ptr = const T*;
62 using ref = const T&;
63
64 GPUd() gpuSpan() : _data(nullptr), _size(0) {}
65 GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
67 GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
68 GPUd() unsigned int size() const { return _size; }
69 GPUd() bool empty() const { return _size == 0; }
70 GPUd() ref front() const { return _data[0]; }
71 GPUd() ref back() const { return _data[_size - 1]; }
72 GPUd() ptr begin() const { return _data; }
73 GPUd() ptr end() const { return _data + _size; }
74
75 protected:
77 unsigned int _size;
78};
79
80enum class Task {
81 Tracker = 0,
82 Vertexer = 1
83};
84
85// Abstract stream class
86class Stream
87{
88 public:
89#if defined(__HIPCC__)
90 using Handle = hipStream_t;
91 static constexpr Handle Default = 0;
92#elif defined(__CUDACC__)
93 using Handle = cudaStream_t;
94 static constexpr Handle Default = 0;
95#else
96 using Handle = void*;
97 static constexpr Handle Default = nullptr;
98#endif
99
100 Stream(unsigned int flags = 0)
101 {
102#if defined(__HIPCC__)
103 GPUChkErrS(hipStreamCreateWithFlags(&mHandle, flags));
104#elif defined(__CUDACC__)
105 GPUChkErrS(cudaStreamCreateWithFlags(&mHandle, flags));
106#endif
107 }
108
109 Stream(Handle h) : mHandle(h) {}
111 {
112 if (mHandle != Default) {
113#if defined(__HIPCC__)
114 GPUChkErrS(hipStreamDestroy(mHandle));
115#elif defined(__CUDACC__)
116 GPUChkErrS(cudaStreamDestroy(mHandle));
117#endif
118 }
119 }
120
121 operator bool() const { return mHandle != Default; }
122 const Handle& get() { return mHandle; }
123 void sync() const
124 {
125#if defined(__HIPCC__)
126 GPUChkErrS(hipStreamSynchronize(mHandle));
127#elif defined(__CUDACC__)
128 GPUChkErrS(cudaStreamSynchronize(mHandle));
129#endif
130 }
131
132 private:
133 Handle mHandle{Default};
134};
135static_assert(sizeof(Stream) == sizeof(void*), "Stream type must match pointer type!");
136
137// Abstract vector for streams.
138// Handles specifically wrap around.
140{
141 public:
142 size_t size() const noexcept { return mStreams.size(); }
143 void resize(size_t n) { mStreams.resize(n); }
144 void clear() { mStreams.clear(); }
145 auto& operator[](size_t i) { return mStreams[i % mStreams.size()]; }
146 void push_back(const Stream& stream) { mStreams.push_back(stream); }
147
148 private:
149 std::vector<Stream> mStreams;
150};
151
152} // namespace gpu
153} // namespace o2::its
154
155#endif
int32_t i
#define GPUChkErrS(x)
Class for time synchronization of RawReader instances.
void sync() const
Definition Utils.h:123
const Handle & get()
Definition Utils.h:122
static constexpr Handle Default
Definition Utils.h:97
Stream(unsigned int flags=0)
Definition Utils.h:100
Stream(Handle h)
Definition Utils.h:109
void resize(size_t n)
Definition Utils.h:143
void push_back(const Stream &stream)
Definition Utils.h:146
auto & operator[](size_t i)
Definition Utils.h:145
size_t size() const noexcept
Definition Utils.h:142
GLdouble n
Definition glcorearb.h:1982
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLboolean * data
Definition glcorearb.h:298
GLbitfield flags
Definition glcorearb.h:1570
GLuint GLuint stream
Definition glcorearb.h:1806
void empty(int)
GPUd() gpuSpan(const gpuSpan< T > &other)
Definition Utils.h:66
GPUd() ref front() const
Definition Utils.h:70
GPUd() bool empty() const
Definition Utils.h:69
GPUd() ref operator[](unsigned int idx) const
Definition Utils.h:67
GPUd() ptr end() const
Definition Utils.h:73
GPUd() unsigned int size() const
Definition Utils.h:68
GPUd() ptr begin() const
Definition Utils.h:72
GPUd() ref back() const
Definition Utils.h:71
GPUd() bool empty() const
Definition Utils.h:47
unsigned int dim
Definition Utils.h:44
GPUd() ptr begin() const
Definition Utils.h:50
GPUd() ref operator[](unsigned int idx) const
Definition Utils.h:45
GPUd() gpuSpan()
Definition Utils.h:43
GPUd() ptr end() const
Definition Utils.h:51
GPUd() unsigned int size() const
Definition Utils.h:46
unsigned int _size
Definition Utils.h:55
GPUd() ref back() const
Definition Utils.h:49
GPUd() ref front() const
Definition Utils.h:48
VectorOfTObjectPtrs other