16#ifndef ITSTRACKINGGPU_UTILS_H_
17#define ITSTRACKINGGPU_UTILS_H_
32#include <thrust/device_ptr.h>
34#define THRUST_NAMESPACE thrust::cuda
36#define THRUST_NAMESPACE thrust::hip
42#define GPULogSync() GPUChkErrS(hipDeviceSynchronize())
43#elif defined(__CUDACC__)
44#define GPULogSync() GPUChkErrS(cudaDeviceSynchronize())
50 LOGP(info, __VA_ARGS__); \
64template <
typename T1,
typename T2>
78 GPUd()
ref operator[](
unsigned int idx)
const {
return _data[idx]; }
117#if defined(__HIPCC__)
118 using Handle = hipStream_t;
120 static constexpr unsigned int DefaultFlag = hipStreamNonBlocking;
121 using Event = hipEvent_t;
122#elif defined(__CUDACC__)
123 using Handle = cudaStream_t;
125 static constexpr unsigned int DefaultFlag = cudaStreamNonBlocking;
126 using Event = cudaEvent_t;
136#if defined(__HIPCC__)
138 GPUChkErrS(hipEventCreateWithFlags(&mEvent, hipEventDisableTiming));
139#elif defined(__CUDACC__)
141 GPUChkErrS(cudaEventCreateWithFlags(&mEvent, cudaEventDisableTiming));
149#if defined(__HIPCC__)
152#elif defined(__CUDACC__)
165#if defined(__HIPCC__)
167#elif defined(__CUDACC__)
173#if defined(__HIPCC__)
175#elif defined(__CUDACC__)
182 Event mEvent{
nullptr};
189 size_t size() const noexcept {
return mStreams.size(); }
197#if defined(__HIPCC__)
199#elif defined(__CUDACC__)
203 for (
auto& s : mStreams) {
210#if defined(__HIPCC__)
211 GPUChkErrS(hipStreamWaitEvent(mStreams[iStream].
get(), mStreams[iEvent].getEvent()));
212#elif defined(__CUDACC__)
213 GPUChkErrS(cudaStreamWaitEvent(mStreams[iStream].
get(), mStreams[iEvent].getEvent()));
218 std::vector<Stream> mStreams;
221#ifdef ITS_MEASURE_GPU_TIME
225 GPUTimer(
const std::string&
name)
231 GPUTimer(Streams& streams,
const std::string&
name)
234 for (
size_t i{0};
i < streams.size(); ++
i) {
235 mStreams.push_back(streams[
i].
get());
242 for (
size_t sta{
start}; sta <
end; ++sta) {
243 mStreams.push_back(streams[sta].
get());
250 mStreams.push_back(
stream.get());
256 for (
size_t i{0};
i < mStreams.size(); ++
i) {
258#if defined(__HIPCC__)
261 GPUChkErrS(hipEventElapsedTime(&ms, mStarts[
i], mStops[
i]));
264#elif defined(__CUDACC__)
267 GPUChkErrS(cudaEventElapsedTime(&ms, mStarts[
i], mStops[
i]));
271 LOGP(info,
"Elapsed time for {}:{} {} ms", mName,
i, ms);
277 mStarts.resize(mStreams.size());
278 mStops.resize(mStreams.size());
279 for (
size_t i{0};
i < mStreams.size(); ++
i) {
280#if defined(__HIPCC__)
284#elif defined(__CUDACC__)
294 std::vector<Stream::Event> mStarts, mStops;
295 std::vector<Stream::Handle> mStreams;
301 template <
typename... Args>
310struct TypedAllocator {
311 using value_type = T;
312 using pointer = thrust::device_ptr<T>;
313 using const_pointer = thrust::device_ptr<const T>;
314 using size_type = std::size_t;
315 using difference_type = std::ptrdiff_t;
317 TypedAllocator() noexcept : mInternalAllocator(
nullptr) {}
318 explicit TypedAllocator(ExternalAllocator*
a) noexcept : mInternalAllocator(
a) {}
320 template <
typename U>
321 TypedAllocator(
const TypedAllocator<U>& o) noexcept : mInternalAllocator(o.mInternalAllocator)
327 void*
raw = mInternalAllocator->allocateStack(
n *
sizeof(T));
328 return thrust::device_pointer_cast(
static_cast<T*
>(
raw));
331 void deallocate(
pointer p, size_type
n)
noexcept
336 void*
raw = thrust::raw_pointer_cast(p);
337 mInternalAllocator->deallocate(
static_cast<char*
>(
raw),
n *
sizeof(T));
340 bool operator==(TypedAllocator
const& o)
const noexcept
342 return mInternalAllocator == o.mInternalAllocator;
344 bool operator!=(TypedAllocator
const& o)
const noexcept
346 return !(*
this == o);
350 ExternalAllocator* mInternalAllocator;
355GPUdii() int deviceLowerBound(const T*
a,
int beg,
int end, const T
key)
358 const int mid = beg + (
end - beg) / 2;
370GPUdii() int deviceUpperBound(const T*
a,
int beg,
int end, const T
key)
373 const int mid = beg + (
end - beg) / 2;
383GPUdii() gpuSpan<const
Cluster> getClustersOnLayer(const
int rof,
386 const
int** roframesClus,
389 if (rof < 0 || rof >= totROFs) {
390 return gpuSpan<const Cluster>();
392 const int start_clus_id{roframesClus[
layer][rof]};
393 const int stop_rof = rof >= totROFs - 1 ? totROFs : rof + 1;
394 const unsigned int delta = roframesClus[
layer][stop_rof] - start_clus_id;
395 return gpuSpan<const Cluster>(&(
clusters[
layer][start_clus_id]), delta);
o2::raw::RawFileWriter * raw
Class for time synchronization of RawReader instances.
HMPID cluster implementation.
const Handle & getStream()
static constexpr Handle DefaultStream
static constexpr unsigned int DefaultFlag
Stream(unsigned int flags=DefaultFlag)
void push_back(const Stream &stream)
void waitEvent(size_t iStream, size_t iEvent)
void sync(bool device=true)
auto & operator[](size_t i)
size_t size() const noexcept
GLuint const GLchar * name
GLenum GLuint GLint GLint layer
GLboolean GLboolean GLboolean GLboolean a
std::pair< T1, T2 > gpuPair
bool operator!=(const DsChannelId &a, const DsChannelId &b)
std::string to_string(gsl::span< T, Size > span)
GPUd() gpuSpan(const gpuSpan< T > &other)
GPUd() bool empty() const
GPUd() ref operator[](unsigned int idx) const
GPUd() unsigned int size() const
GPUd() bool empty() const
GPUd() ref operator[](unsigned int idx) const
GPUd() unsigned int size() const
VectorOfTObjectPtrs other
std::vector< Cluster > clusters