1616#ifndef ITSTRACKINGGPU_UTILS_H_
1717#define ITSTRACKINGGPU_UTILS_H_
1818
19+ #include < vector>
20+
1921#include " GPUCommonDef.h"
22+ #include " GPUCommonHelpers.h"
2023
2124namespace o2
2225{
@@ -31,11 +34,6 @@ struct gpuPair {
3134namespace gpu
3235{
3336
34- template <typename T>
35- void discardResult (const T&)
36- {
37- }
38-
3937// Poor man implementation of a span-like struct. It is very limited.
4038template <typename T>
4139struct gpuSpan {
@@ -96,8 +94,76 @@ GPUhd() const T* getPtrFromRuler(int index, const T* src, const int* ruler, cons
9694{
9795 return src + ruler[index] * stride;
9896}
97+
98+ // Abstract stream class
99+ class Stream
100+ {
101+ public:
102+ #if defined(__HIPCC__)
103+ using Handle = hipStream_t;
104+ static constexpr Handle Default = 0 ;
105+ #elif defined(__CUDACC__)
106+ using Handle = cudaStream_t;
107+ static constexpr Handle Default = 0 ;
108+ #else
109+ using Handle = void *;
110+ static constexpr Handle Default = nullptr ;
111+ #endif
112+
113+ Stream (unsigned int flags = 0 )
114+ {
115+ #if defined(__HIPCC__)
116+ GPUChkErrS (hipStreamCreateWithFlags (&mHandle , flags));
117+ #elif defined(__CUDACC__)
118+ GPUChkErrS (cudaStreamCreateWithFlags (&mHandle , flags));
119+ #endif
120+ }
121+
122+ Stream (Handle h) : mHandle (h) {}
123+ ~Stream ()
124+ {
125+ if (mHandle != Default) {
126+ #if defined(__HIPCC__)
127+ GPUChkErrS (hipStreamDestroy (mHandle ));
128+ #elif defined(__CUDACC__)
129+ GPUChkErrS (cudaStreamDestroy (mHandle ));
130+ #endif
131+ }
132+ }
133+
134+ operator bool () const { return mHandle != Default; }
135+ const Handle& get () { return mHandle ; }
136+ void sync () const
137+ {
138+ #if defined(__HIPCC__)
139+ GPUChkErrS (hipStreamSynchronize (mHandle ));
140+ #elif defined(__CUDACC__)
141+ GPUChkErrS (cudaStreamSynchronize (mHandle ));
142+ #endif
143+ }
144+
145+ private:
146+ Handle mHandle {Default};
147+ };
148+ static_assert (sizeof (Stream) == sizeof (void *), " Stream type must match pointer type!" );
149+
150+ // Abstract vector for streams.
151+ // Handles specifically wrap around.
152+ class Streams
153+ {
154+ public:
155+ size_t size () const noexcept { return mStreams .size (); }
156+ void resize (size_t n) { mStreams .resize (n); }
157+ void clear () { mStreams .clear (); }
158+ auto & operator [](size_t i) { return mStreams [i % mStreams .size ()]; }
159+ void push_back (const Stream& stream) { mStreams .push_back (stream); }
160+
161+ private:
162+ std::vector<Stream> mStreams ;
163+ };
164+
99165} // namespace gpu
100166} // namespace its
101167} // namespace o2
102168
103- #endif
169+ #endif
0 commit comments