Project
Loading...
Searching...
No Matches
Vector.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_VECTOR_H_
17#define ITSTRACKINGGPU_VECTOR_H_
18
19#include <cassert>
20#include <new>
21#include <type_traits>
22#include <vector>
23
24#include "Stream.h"
25#include "Utils.h"
26
27namespace o2
28{
29namespace its
30{
31namespace gpu
32{
33
34template <typename T>
35class Vector final
36{
37 static_assert(std::is_trivially_destructible<T>::value, "Vector only supports trivially destructible objects.");
38
39 public:
40 Vector();
41 explicit Vector(const size_t, const size_t = 0);
42 Vector(const T* const, const size_t, const size_t = 0);
44
45 Vector(const Vector&) = delete;
46 Vector& operator=(const Vector&) = delete;
47
49 Vector& operator=(Vector&&);
50
51 size_t getSizeFromDevice() const;
52
53 T getElementFromDevice(const size_t) const;
54
55 void resize(const size_t);
56 void reset(const size_t, const size_t = 0);
57 void reset(const T* const, const size_t, const size_t = 0);
58
59 void resetWithInt(const size_t, const int value = 0);
60 void copyIntoSizedVector(std::vector<T>&);
61
62 GPUhd() T* get() const;
63 GPUhd() size_t capacity() const;
64 GPUhd() Vector<T> getWeakCopy() const;
65 GPUd() T& operator[](const size_t) const;
66
67 GPUd() size_t size() const;
68 GPUhd() void dump();
69
70 template <typename... Args>
71 GPUd() void emplace(const size_t, Args&&...);
72
74 void destroy();
75
76 private:
77 GPUhd() Vector(const Vector&, const bool);
78
79 T* mArrayPtr = nullptr;
80 size_t* mDeviceSizePtr = nullptr;
81 size_t mCapacity;
82 bool mIsWeak;
83};
84
85template <typename T>
86Vector<T>::Vector() : Vector{nullptr, 0}
87{
88 // Nothing to do
89}
90
91template <typename T>
92Vector<T>::Vector(const size_t capacity, const size_t initialSize) : Vector{nullptr, capacity, initialSize}
93{
94 // Nothing to do
95}
96
97template <typename T>
98Vector<T>::Vector(const T* const source, const size_t size, const size_t initialSize) : mCapacity{size}, mIsWeak{false}
99{
100 if (size > 0) {
101 try {
102
103 utils::gpuMalloc(reinterpret_cast<void**>(&mArrayPtr), size * sizeof(T));
104 utils::gpuMalloc(reinterpret_cast<void**>(&mDeviceSizePtr), sizeof(size_t));
105
106 if (source != nullptr) {
107
108 utils::gpuMemcpyHostToDevice(mArrayPtr, source, size * sizeof(T));
109 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t));
110
111 } else {
112
113 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &initialSize, sizeof(size_t));
114 }
115
116 } catch (...) {
117
118 destroy();
119
120 throw;
121 }
122 }
123}
124
125template <typename T>
126GPUhd() Vector<T>::Vector(const Vector& other, const bool isWeak)
127 : mArrayPtr{other.mArrayPtr},
128 mDeviceSizePtr{other.mDeviceSizePtr},
129 mCapacity{other.mCapacity},
130 mIsWeak{isWeak}
131{
132 // Nothing to do
133}
134
135template <typename T>
136GPUhd() Vector<T>::~Vector()
137{
138 if (mIsWeak) {
139 return;
140 } else {
141#if defined(TRACKINGITSU_GPU_DEVICE)
142 assert(0);
143#else
144 destroy();
145#endif
146 }
147}
148
149template <typename T>
150GPUhd() Vector<T>::Vector(Vector<T>&& other)
151 : mArrayPtr{other.mArrayPtr},
152 mDeviceSizePtr{other.mDeviceSizePtr},
153 mCapacity{other.mCapacity},
154 mIsWeak{other.mIsWeak}
155{
156 other.mArrayPtr = nullptr;
157 other.mDeviceSizePtr = nullptr;
158}
159
160template <typename T>
162{
163 destroy();
164
165 mArrayPtr = other.mArrayPtr;
166 mDeviceSizePtr = other.mDeviceSizePtr;
167 mCapacity = other.mCapacity;
168 mIsWeak = other.mIsWeak;
169
170 other.mArrayPtr = nullptr;
171 other.mDeviceSizePtr = nullptr;
172
173 return *this;
174}
175
176template <typename T>
178{
179 size_t size;
180 utils::gpuMemcpyDeviceToHost(&size, mDeviceSizePtr, sizeof(size_t));
181
182 return size;
183}
184
185template <typename T>
186void Vector<T>::resize(const size_t size)
187{
188 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t));
189}
190
191template <typename T>
192void Vector<T>::reset(const size_t capacity, const size_t initialSize)
193{
194 reset(nullptr, capacity, initialSize);
195}
196
197template <typename T>
198void Vector<T>::reset(const T* const source, const size_t size, const size_t initialSize)
199{
200 if (size > mCapacity) {
201 if (mArrayPtr != nullptr) {
202 utils::gpuFree(mArrayPtr);
203 }
204 utils::gpuMalloc(reinterpret_cast<void**>(&mArrayPtr), size * sizeof(T));
205 mCapacity = size;
206 }
207 if (mDeviceSizePtr == nullptr) {
208 utils::gpuMalloc(reinterpret_cast<void**>(&mDeviceSizePtr), sizeof(size_t));
209 }
210
211 if (source != nullptr) {
212 utils::gpuMemcpyHostToDevice(mArrayPtr, source, size * sizeof(T));
213 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t));
214 } else {
215 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &initialSize, sizeof(size_t));
216 }
217}
218
219template <typename T>
220void Vector<T>::resetWithInt(const size_t size, const int value)
221{
222 if (size > mCapacity) {
223 if (mArrayPtr != nullptr) {
224 utils::gpuFree(mArrayPtr);
225 }
226 utils::gpuMalloc(reinterpret_cast<void**>(&mArrayPtr), size * sizeof(int));
227 mCapacity = size;
228 }
229 if (mDeviceSizePtr == nullptr) {
230 utils::gpuMalloc(reinterpret_cast<void**>(&mDeviceSizePtr), sizeof(int));
231 }
232
233 utils::gpuMemset(mArrayPtr, value, size * sizeof(int));
234 utils::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(int));
235}
236
237template <typename T>
238void Vector<T>::copyIntoSizedVector(std::vector<T>& destinationVector)
239{
240 utils::gpuMemcpyDeviceToHost(destinationVector.data(), mArrayPtr, destinationVector.size() * sizeof(T));
241}
242
243template <typename T>
245{
246 if (mArrayPtr != nullptr) {
247 utils::gpuFree(mArrayPtr);
248 }
249 if (mDeviceSizePtr != nullptr) {
250 utils::gpuFree(mDeviceSizePtr);
251 }
252}
253
254template <typename T>
255GPUhd() T* Vector<T>::get() const
256{
257 return mArrayPtr;
258}
259
260template <typename T>
261GPUhd() size_t Vector<T>::capacity() const
262{
263 return mCapacity;
264}
265
266template <typename T>
267GPUhd() Vector<T> Vector<T>::getWeakCopy() const
268{
269 return Vector{*this, true};
270}
271
272template <typename T>
273GPUd() T& Vector<T>::operator[](const size_t index) const
274{
275 return mArrayPtr[index];
276}
277
278template <typename T>
279GPUh() T Vector<T>::getElementFromDevice(const size_t index) const
280{
281 T element;
282 utils::gpuMemcpyDeviceToHost(&element, mArrayPtr + index, sizeof(T));
283
284 return element;
285}
286
287template <typename T>
288GPUd() size_t Vector<T>::size() const
289{
290 return *mDeviceSizePtr;
291}
292
293template <typename T>
294template <typename... Args>
295GPUd() void Vector<T>::emplace(const size_t index, Args&&... arguments)
296{
297 new (mArrayPtr + index) T(std::forward<Args>(arguments)...);
298}
299
300template <typename T>
301GPUhd() void Vector<T>::dump()
302{
303 printf("mArrayPtr = %p\nmDeviceSize = %p\nmCapacity = %d\nmIsWeak = %s\n",
304 mArrayPtr, mDeviceSizePtr, mCapacity, mIsWeak ? "true" : "false");
305}
306} // namespace gpu
307} // namespace its
308} // namespace o2
309
310#endif
#define GPUh()
#define GPUd()
#define protected
void resetWithInt(const size_t, const int value=0)
Definition Vector.h:220
GPUhd() Vector(Vector &&)
Vector(const Vector &)=delete
void resize(const size_t)
Definition Vector.h:186
size_t getSizeFromDevice() const
Definition Vector.h:177
Vector & operator=(const Vector &)=delete
void copyIntoSizedVector(std::vector< T > &)
Definition Vector.h:238
void reset(const size_t, const size_t=0)
Definition Vector.h:192
GPUd() size_t size() const
T getElementFromDevice(const size_t) const
void dump(const std::string what, DPMAP m, int verbose)
Definition dcs-ccdb.cxx:79
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLsizei const GLfloat * value
Definition glcorearb.h:819
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
bpo::variables_map arguments
auto get(const std::byte *buffer, size_t=0)
Definition DataHeader.h:454
void gpuMemcpyHostToDevice(void *, const void *, int)
void gpuMemset(void *, int, int)
void gpuFree(void *)
void gpuMalloc(void **, const int)
void gpuMemcpyDeviceToHost(void *, const void *, int)
GPUhd() T *getPtrFromRuler(int index
Definition Vector.h:126
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.
VectorOfTObjectPtrs other