1616#ifndef ITSTRACKINGGPU_UTILS_H_
1717#define ITSTRACKINGGPU_UTILS_H_
1818
19+ #include < vector>
20+
1921#include " GPUCommonDef.h"
22+ #include " GPUCommonHelpers.h"
2023
21- namespace o2
22- {
23- namespace its
24+ namespace o2 ::its
2425{
26+
2527template <typename T1, typename T2>
2628struct gpuPair {
2729 T1 first;
@@ -31,11 +33,6 @@ struct gpuPair {
3133namespace gpu
3234{
3335
34- template <typename T>
35- void discardResult (const T&)
36- {
37- }
38-
3936// Poor man implementation of a span-like struct. It is very limited.
4037template <typename T>
4138struct gpuSpan {
@@ -85,19 +82,74 @@ enum class Task {
8582 Vertexer = 1
8683};
8784
88- template < class T >
89- GPUhd () T* getPtrFromRuler ( int index, T* src, const int * ruler, const int stride = 1 )
85+ // Abstract stream class
86+ class Stream
9087{
91- return src + ruler[index] * stride;
92- }
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+ }
93108
94- template <class T >
95- GPUhd () const T* getPtrFromRuler (int index, const T* src, const int * ruler, const int stride = 1 )
109+ Stream (Handle h) : mHandle (h) {}
110+ ~Stream ()
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+ };
135+ static_assert (sizeof (Stream) == sizeof (void *), " Stream type must match pointer type!" );
136+
137+ // Abstract vector for streams.
138+ // Handles specifically wrap around.
139+ class Streams
96140{
97- return src + ruler[index] * stride;
98- }
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+
99152} // namespace gpu
100- } // namespace its
101- } // namespace o2
153+ } // namespace o2::its
102154
103- #endif
155+ #endif
0 commit comments