Project
Loading...
Searching...
No Matches
GPUReconstructionIO.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#if !defined(GPURECONSTRUCTIONIO_H)
16#define GPURECONSTRUCTIONIO_H
17
18#include "GPUReconstruction.h"
19#include "GPUSettings.h"
20
21namespace o2::gpu
22{
23
24template <class T>
25inline T* GPUReconstruction::AllocateIOMemoryHelper(size_t n, const T*& ptr, std::unique_ptr<T[]>& u)
26{
27 if (n == 0) {
28 u.reset(nullptr);
29 return nullptr;
30 }
31 T* retVal;
33 u.reset(nullptr);
36 if ((size_t)((char*)mInputControl.ptrCurrent - (char*)mInputControl.ptrBase) > mInputControl.size) {
37 throw std::bad_alloc();
38 }
39 } else {
40 u.reset(new T[n]);
41 retVal = u.get();
42 if (GetProcessingSettings().registerStandaloneInputMemory) {
43 if (registerMemoryForGPU(u.get(), n * sizeof(T))) {
44 GPUError("Error registering memory for GPU: %p - %ld bytes\n", (void*)u.get(), (int64_t)(n * sizeof(T)));
45 throw std::bad_alloc();
46 }
47 }
48 }
49 ptr = retVal;
50 return retVal;
51}
52
53template <class T, class S>
54inline uint32_t GPUReconstruction::DumpData(FILE* fp, const T* const* entries, const S* num, InOutPointerType type)
55{
57 uint32_t numTotal = 0;
58 for (int32_t i = 0; i < count; i++) {
59 numTotal += num[i];
60 }
61 if (numTotal == 0) {
62 return 0;
63 }
64 fwrite(&type, sizeof(type), 1, fp);
65 for (int32_t i = 0; i < count; i++) {
66 fwrite(&num[i], sizeof(num[i]), 1, fp);
67 if (num[i]) {
68 fwrite(entries[i], sizeof(*entries[i]), num[i], fp);
69 }
70 }
71 if (GetProcessingSettings().debugLevel >= 2) {
72 GPUInfo("Dumped %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
73 }
74 return numTotal;
75}
76
77template <class T, class S>
78inline size_t GPUReconstruction::ReadData(FILE* fp, const T** entries, S* num, std::unique_ptr<T[]>* mem, InOutPointerType type, T** nonConstPtrs)
79{
80 if (feof(fp)) {
81 return 0;
82 }
83 InOutPointerType inType;
84 size_t r, pos = ftell(fp);
85 r = fread(&inType, sizeof(inType), 1, fp);
86 if (r != 1 || inType != type) {
87 fseek(fp, pos, SEEK_SET);
88 return 0;
89 }
90
92 size_t numTotal = 0;
93 for (int32_t i = 0; i < count; i++) {
94 r = fread(&num[i], sizeof(num[i]), 1, fp);
95 T* m = AllocateIOMemoryHelper(num[i], entries[i], mem[i]);
96 if (nonConstPtrs) {
97 nonConstPtrs[i] = m;
98 }
99 if (num[i]) {
100 r = fread(m, sizeof(*entries[i]), num[i], fp);
101 }
102 numTotal += num[i];
103 }
104 (void)r;
105 if (GetProcessingSettings().debugLevel >= 2) {
106 GPUInfo("Read %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
107 }
108 return numTotal;
109}
110
111template <class T>
112inline void GPUReconstruction::DumpFlatObjectToFile(const T* obj, const char* file)
113{
114 FILE* fp = fopen(file, "w+b");
115 if (fp == nullptr) {
116 return;
117 }
118 size_t size[2] = {sizeof(*obj), obj->getFlatBufferSize()};
119 fwrite(size, sizeof(size[0]), 2, fp);
120 fwrite(obj, 1, size[0], fp);
121 fwrite(obj->getFlatBufferPtr(), 1, size[1], fp);
122 fclose(fp);
123}
124
125template <class T>
126inline std::unique_ptr<T> GPUReconstruction::ReadFlatObjectFromFile(const char* file)
127{
128 FILE* fp = fopen(file, "rb");
129 if (fp == nullptr) {
130 return nullptr;
131 }
132 size_t size[2] = {0}, r;
133 r = fread(size, sizeof(size[0]), 2, fp);
134 if (r == 0 || size[0] != sizeof(T)) {
135 fclose(fp);
136 GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size[0], (int64_t)sizeof(T));
137 throw std::runtime_error("invalid size");
138 }
139 std::unique_ptr<T> retVal(new T);
140 retVal->destroy();
141 char* buf = new char[size[1]]; // Not deleted as ownership is transferred to FlatObject
142 r = fread((void*)retVal.get(), 1, size[0], fp);
143 r = fread(buf, 1, size[1], fp);
144 fclose(fp);
145 if (GetProcessingSettings().debugLevel >= 2) {
146 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
147 }
148 retVal->clearInternalBufferPtr();
149 retVal->setActualBufferAddress(buf);
150 retVal->adoptInternalBuffer(buf);
151 return retVal;
152}
153
154template <class T>
155inline void GPUReconstruction::DumpStructToFile(const T* obj, const char* file)
156{
157 FILE* fp = fopen(file, "w+b");
158 if (fp == nullptr) {
159 return;
160 }
161 size_t size = sizeof(*obj);
162 fwrite(&size, sizeof(size), 1, fp);
163 fwrite(obj, 1, size, fp);
164 fclose(fp);
165}
166
167template <class T>
168inline std::unique_ptr<T> GPUReconstruction::ReadStructFromFile(const char* file)
169{
170 FILE* fp = fopen(file, "rb");
171 if (fp == nullptr) {
172 return nullptr;
173 }
174 size_t size, r;
175 r = fread(&size, sizeof(size), 1, fp);
176 if (r == 0 || size != sizeof(T)) {
177 fclose(fp);
178 GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size, (int64_t)sizeof(T));
179 throw std::runtime_error("invalid size");
180 }
181 std::unique_ptr<T> newObj(new T);
182 r = fread(newObj.get(), 1, size, fp);
183 fclose(fp);
184 if (GetProcessingSettings().debugLevel >= 2) {
185 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
186 }
187 return newObj;
188}
189
190template <class T>
191inline int32_t GPUReconstruction::ReadStructFromFile(const char* file, T* obj)
192{
193 FILE* fp = fopen(file, "rb");
194 if (fp == nullptr) {
195 return 1;
196 }
197 size_t size, r;
198 r = fread(&size, sizeof(size), 1, fp);
199 if (r == 0) {
200 fclose(fp);
201 return 1;
202 }
203 r = fread(obj, 1, size, fp);
204 fclose(fp);
205 if (GetProcessingSettings().debugLevel >= 2) {
206 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
207 }
208 return 0;
209}
210
211} // namespace o2::gpu
212
213#endif
int32_t i
int32_t retVal
uint16_t pos
Definition RawData.h:3
TBranch * ptr
double num
static void computePointerWithAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
static uint32_t getNIOTypeMultiplicity(InOutPointerType type)
uint32_t DumpData(FILE *fp, const T *const *entries, const S *num, InOutPointerType type)
std::unique_ptr< T > ReadStructFromFile(const char *file)
std::unique_ptr< T > ReadFlatObjectFromFile(const char *file)
void DumpStructToFile(const T *obj, const char *file)
T * AllocateIOMemoryHelper(size_t n, const T *&ptr, std::unique_ptr< T[]> &u)
static constexpr const char *const IOTYPENAMES[]
size_t ReadData(FILE *fp, const T **entries, S *num, std::unique_ptr< T[]> *mem, InOutPointerType type, T **nonConstPtrs=nullptr)
const GPUSettingsProcessing & GetProcessingSettings() const
void DumpFlatObjectToFile(const T *obj, const char *file)
int32_t registerMemoryForGPU(const void *ptr, size_t size)
GLdouble n
Definition glcorearb.h:1982
const GLfloat * m
Definition glcorearb.h:4066
GLint GLsizei count
Definition glcorearb.h:399
GLsizeiptr size
Definition glcorearb.h:659
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean r
Definition glcorearb.h:1233
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514