Project
Loading...
Searching...
No Matches
GPUProcessor.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.
11
14
15#ifndef GPUPROCESSOR_H
16#define GPUPROCESSOR_H
17
18#include "GPUCommonDef.h"
19#include "GPUDef.h"
20
21#ifndef GPUCA_GPUCODE
22#include <cstddef>
23#include <algorithm>
24#endif
25
26namespace o2::gpu
27{
28struct GPUTrackingInOutPointers;
29class GPUReconstruction;
30struct GPUParam;
31struct GPUConstantMem;
32
34{
35 friend class GPUReconstruction;
37 friend class GPUMemoryResource;
38
39 public:
43
44#ifndef GPUCA_GPUCODE
47 GPUProcessor(const GPUProcessor&) = delete;
49#endif
50
51 GPUd() GPUconstantref() const GPUConstantMem* GetConstantMem() const; // Body in GPUConstantMem.h to avoid circular headers
52 GPUd() GPUconstantref() const GPUParam& Param() const; // ...
53 GPUd() void raiseError(uint32_t code, uint32_t param1 = 0, uint32_t param2 = 0, uint32_t param3 = 0) const;
54 GPUd() bool IsDeviceProcessor() const { return mGPUProcessorType == PROCESSOR_TYPE_DEVICE; }
55 const GPUReconstruction& GetRec() const { return *mRec; }
56
57#ifndef __OPENCL__
59 void Clear();
60 template <class T>
62 {
64 }
65
66 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
67 static constexpr inline size_t getAlignmentMod(size_t addr)
68 {
69 static_assert((alignment & (alignment - 1)) == 0, "Invalid alignment, not power of 2");
70 if (alignment <= 1) {
71 return 0;
72 }
73 return addr & (alignment - 1);
74 }
75 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
76 static constexpr inline size_t getAlignment(size_t addr)
77 {
78 size_t mod = getAlignmentMod<alignment>(addr);
79 if (mod == 0) {
80 return 0;
81 }
82 return (alignment - mod);
83 }
84 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
85 static constexpr inline size_t nextMultipleOf(size_t size)
86 {
87 return size + getAlignment<alignment>(size);
88 }
89 static constexpr inline size_t nextMultipleOf(size_t size, size_t alignment)
90 {
91 if (alignment & (alignment - 1)) {
92 size_t tmp = size % alignment;
93 if (tmp) {
94 size += alignment - tmp;
95 }
96 return size;
97 } else {
98 return (size + alignment - 1) & ~(alignment - 1);
99 }
100 }
101 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
102 static inline void* alignPointer(void* ptr)
103 {
104 return (reinterpret_cast<void*>(nextMultipleOf<alignment>(reinterpret_cast<size_t>(ptr))));
105 }
106 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
107 static inline size_t getAlignmentMod(void* addr)
108 {
109 return (getAlignmentMod<alignment>(reinterpret_cast<size_t>(addr)));
110 }
111 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT>
112 static inline size_t getAlignment(void* addr)
113 {
114 return (getAlignment<alignment>(reinterpret_cast<size_t>(addr)));
115 }
116 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT, class S>
117 static inline S* getPointerWithAlignment(size_t& basePtr, size_t nEntries = 1)
118 {
119 if (basePtr == 0) {
120 basePtr = 1;
121 }
122 constexpr const size_t maxAlign = (alignof(S) > alignment) ? alignof(S) : alignment;
123 basePtr += getAlignment<maxAlign>(basePtr);
124 S* retVal = (S*)(basePtr);
125 basePtr += nEntries * sizeof(S);
126 return retVal;
127 }
128
129 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT, class S>
130 static inline S* getPointerWithAlignment(void*& basePtr, size_t nEntries = 1)
131 {
132 size_t tmp = (size_t)basePtr;
133 auto retVal = getPointerWithAlignment<alignment, S>(tmp, nEntries);
134 basePtr = (void*)tmp;
135 return retVal;
136 }
137
138 template <size_t alignment = constants::GPU_BUFFER_ALIGNMENT, class T, class S>
139 static inline void computePointerWithAlignment(T*& basePtr, S*& objPtr, size_t nEntries = 1)
140 {
141 size_t tmp = (size_t)basePtr;
142 objPtr = getPointerWithAlignment<alignment, S>(tmp, nEntries);
143 basePtr = (T*)tmp;
144 }
145
146 template <class T, class S>
147 static inline void computePointerWithoutAlignment(T*& basePtr, S*& objPtr, size_t nEntries = 1)
148 {
149 if ((size_t)basePtr < constants::GPU_BUFFER_ALIGNMENT) {
150 basePtr = (T*)constants::GPU_BUFFER_ALIGNMENT;
151 }
152 size_t tmp = (size_t)basePtr;
153 objPtr = reinterpret_cast<S*>(getPointerWithAlignment<1, char>(tmp, nEntries * sizeof(S)));
154 basePtr = (T*)tmp;
155 }
156#endif
157
158 protected:
159 void AllocateAndInitializeLate() { mAllocateAndInitializeLate = true; }
160
164 GPUconstantref() const GPUConstantMem* mConstantMem;
165
166 private:
167 bool mAllocateAndInitializeLate;
168
170};
171} // namespace o2::gpu
172
173#endif
int32_t retVal
TBranch * ptr
static constexpr size_t nextMultipleOf(size_t size, size_t alignment)
GPUProcessor & operator=(const GPUProcessor &)=delete
uint32_t uint32_t uint32_t param3
GPUconstantref() const GPUConstantMem *mConstantMem
GPUd() GPUconstantref() const GPUConstantMem *GetConstantMem() const
static S * getPointerWithAlignment(size_t &basePtr, size_t nEntries=1)
static size_t getAlignmentMod(void *addr)
uint32_t uint32_t param2
GPUProcessor(const GPUProcessor &)=delete
GPUReconstruction * mRec
static constexpr size_t getAlignmentMod(size_t addr)
static void computePointerWithAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
void InitGPUProcessor(GPUReconstruction *rec, ProcessorType type=PROCESSOR_TYPE_CPU, GPUProcessor *slaveProcessor=nullptr)
static constexpr size_t nextMultipleOf(size_t size)
static S * getPointerWithAlignment(void *&basePtr, size_t nEntries=1)
static void * alignPointer(void *ptr)
ProcessorType mGPUProcessorType
static size_t getAlignment(void *addr)
static constexpr size_t getAlignment(size_t addr)
GPUProcessor * mLinkedProcessor
const GPUReconstruction & GetRec() const
static void computePointerWithoutAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
GLsizeiptr size
Definition glcorearb.h:659
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GPUReconstruction * rec