forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.h
More file actions
155 lines (133 loc) · 3.93 KB
/
Utils.h
File metadata and controls
155 lines (133 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file Utils.h
/// \brief
///
#ifndef ITSTRACKINGGPU_UTILS_H_
#define ITSTRACKINGGPU_UTILS_H_
#include <vector>
#include "GPUCommonDef.h"
#include "GPUCommonHelpers.h"
namespace o2::its
{
template <typename T1, typename T2>
struct gpuPair {
T1 first;
T2 second;
};
namespace gpu
{
// Poor man implementation of a span-like struct. It is very limited.
template <typename T>
struct gpuSpan {
using value_type = T;
using ptr = T*;
using ref = T&;
GPUd() gpuSpan() : _data(nullptr), _size(0) {}
GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
GPUd() unsigned int size() const { return _size; }
GPUd() bool empty() const { return _size == 0; }
GPUd() ref front() const { return _data[0]; }
GPUd() ref back() const { return _data[_size - 1]; }
GPUd() ptr begin() const { return _data; }
GPUd() ptr end() const { return _data + _size; }
protected:
ptr _data;
unsigned int _size;
};
template <typename T>
struct gpuSpan<const T> {
using value_type = T;
using ptr = const T*;
using ref = const T&;
GPUd() gpuSpan() : _data(nullptr), _size(0) {}
GPUd() gpuSpan(ptr data, unsigned int dim) : _data(data), _size(dim) {}
GPUd() gpuSpan(const gpuSpan<T>& other) : _data(other._data), _size(other._size) {}
GPUd() ref operator[](unsigned int idx) const { return _data[idx]; }
GPUd() unsigned int size() const { return _size; }
GPUd() bool empty() const { return _size == 0; }
GPUd() ref front() const { return _data[0]; }
GPUd() ref back() const { return _data[_size - 1]; }
GPUd() ptr begin() const { return _data; }
GPUd() ptr end() const { return _data + _size; }
protected:
ptr _data;
unsigned int _size;
};
enum class Task {
Tracker = 0,
Vertexer = 1
};
// Abstract stream class
class Stream
{
public:
#if defined(__HIPCC__)
using Handle = hipStream_t;
static constexpr Handle Default = 0;
#elif defined(__CUDACC__)
using Handle = cudaStream_t;
static constexpr Handle Default = 0;
#else
using Handle = void*;
static constexpr Handle Default = nullptr;
#endif
Stream(unsigned int flags = 0)
{
#if defined(__HIPCC__)
GPUChkErrS(hipStreamCreateWithFlags(&mHandle, flags));
#elif defined(__CUDACC__)
GPUChkErrS(cudaStreamCreateWithFlags(&mHandle, flags));
#endif
}
Stream(Handle h) : mHandle(h) {}
~Stream()
{
if (mHandle != Default) {
#if defined(__HIPCC__)
GPUChkErrS(hipStreamDestroy(mHandle));
#elif defined(__CUDACC__)
GPUChkErrS(cudaStreamDestroy(mHandle));
#endif
}
}
operator bool() const { return mHandle != Default; }
const Handle& get() { return mHandle; }
void sync() const
{
#if defined(__HIPCC__)
GPUChkErrS(hipStreamSynchronize(mHandle));
#elif defined(__CUDACC__)
GPUChkErrS(cudaStreamSynchronize(mHandle));
#endif
}
private:
Handle mHandle{Default};
};
static_assert(sizeof(Stream) == sizeof(void*), "Stream type must match pointer type!");
// Abstract vector for streams.
// Handles specifically wrap around.
class Streams
{
public:
size_t size() const noexcept { return mStreams.size(); }
void resize(size_t n) { mStreams.resize(n); }
void clear() { mStreams.clear(); }
auto& operator[](size_t i) { return mStreams[i % mStreams.size()]; }
void push_back(const Stream& stream) { mStreams.push_back(stream); }
private:
std::vector<Stream> mStreams;
};
} // namespace gpu
} // namespace o2::its
#endif