forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUReconstructionIO.h
More file actions
264 lines (247 loc) · 7.6 KB
/
GPUReconstructionIO.h
File metadata and controls
264 lines (247 loc) · 7.6 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// 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 GPUReconstructionIO.h
/// \author David Rohr
#if !defined(GPURECONSTRUCTIONIO_H)
#define GPURECONSTRUCTIONIO_H
#include "GPUReconstruction.h"
#include "GPUSettings.h"
namespace o2::gpu
{
template <class T>
inline T* GPUReconstruction::AllocateIOMemoryHelper(size_t n, const T*& ptr, std::unique_ptr<T[]>& u)
{
if (n == 0) {
u.reset(nullptr);
return nullptr;
}
T* retVal;
if (mInputControl.useExternal()) {
u.reset(nullptr);
mInputControl.checkCurrent();
GPUProcessor::computePointerWithAlignment(mInputControl.ptrCurrent, retVal, n);
if ((size_t)((char*)mInputControl.ptrCurrent - (char*)mInputControl.ptrBase) > mInputControl.size) {
throw std::bad_alloc();
}
} else {
u.reset(new T[n]);
retVal = u.get();
if (GetProcessingSettings().registerStandaloneInputMemory) {
if (registerMemoryForGPU(u.get(), n * sizeof(T))) {
GPUError("Error registering memory for GPU: %p - %ld bytes\n", (void*)u.get(), (int64_t)(n * sizeof(T)));
throw std::bad_alloc();
}
}
}
ptr = retVal;
return retVal;
}
template <class T, class S>
inline uint32_t GPUReconstruction::DumpData(FILE* fp, const T* const* entries, const S* num, InOutPointerType type)
{
int32_t count = getNIOTypeMultiplicity(type);
uint32_t numTotal = 0;
for (int32_t i = 0; i < count; i++) {
numTotal += num[i];
}
if (numTotal == 0) {
return 0;
}
fwrite(&type, sizeof(type), 1, fp);
for (int32_t i = 0; i < count; i++) {
fwrite(&num[i], sizeof(num[i]), 1, fp);
if (num[i]) {
fwrite(entries[i], sizeof(*entries[i]), num[i], fp);
}
}
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Dumped %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
}
return numTotal;
}
template <class T, class S>
inline size_t GPUReconstruction::ReadData(FILE* fp, const T** entries, S* num, std::unique_ptr<T[]>* mem, InOutPointerType type, T** nonConstPtrs)
{
if (feof(fp)) {
return 0;
}
InOutPointerType inType;
size_t r, pos = ftell(fp);
r = fread(&inType, sizeof(inType), 1, fp);
if (r != 1 || inType != type) {
fseek(fp, pos, SEEK_SET);
return 0;
}
int32_t count = getNIOTypeMultiplicity(type);
size_t numTotal = 0;
for (int32_t i = 0; i < count; i++) {
r = fread(&num[i], sizeof(num[i]), 1, fp);
T* m = AllocateIOMemoryHelper(num[i], entries[i], mem[i]);
if (nonConstPtrs) {
nonConstPtrs[i] = m;
}
if (num[i]) {
r = fread(m, sizeof(*entries[i]), num[i], fp);
}
numTotal += num[i];
}
(void)r;
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Read %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
}
return numTotal;
}
template <class T>
inline void GPUReconstruction::DumpFlatObjectToFile(const T* obj, const char* file)
{
FILE* fp = fopen(file, "w+b");
if (fp == nullptr) {
return;
}
size_t size[2] = {sizeof(*obj), obj->getFlatBufferSize()};
fwrite(size, sizeof(size[0]), 2, fp);
fwrite(obj, 1, size[0], fp);
fwrite(obj->getFlatBufferPtr(), 1, size[1], fp);
fclose(fp);
}
template <class T>
inline std::unique_ptr<T> GPUReconstruction::ReadFlatObjectFromFile(const char* file)
{
FILE* fp = fopen(file, "rb");
if (fp == nullptr) {
return nullptr;
}
size_t size[2] = {0}, r;
r = fread(size, sizeof(size[0]), 2, fp);
if (r == 0 || size[0] != sizeof(T)) {
fclose(fp);
GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size[0], (int64_t)sizeof(T));
throw std::runtime_error("invalid size");
}
std::unique_ptr<T> retVal(new T);
retVal->destroy();
char* buf = new char[size[1]]; // Not deleted as ownership is transferred to FlatObject
r = fread((void*)retVal.get(), 1, size[0], fp);
r = fread(buf, 1, size[1], fp);
fclose(fp);
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
}
retVal->clearInternalBufferPtr();
retVal->setActualBufferAddress(buf);
retVal->adoptInternalBuffer(buf);
return retVal;
}
template <class T>
inline void GPUReconstruction::DumpStructToFile(const T* obj, const char* file)
{
FILE* fp = fopen(file, "w+b");
if (fp == nullptr) {
return;
}
size_t size = sizeof(*obj);
fwrite(&size, sizeof(size), 1, fp);
fwrite(obj, 1, size, fp);
fclose(fp);
}
template <class T>
inline std::unique_ptr<T> GPUReconstruction::ReadStructFromFile(const char* file)
{
FILE* fp = fopen(file, "rb");
if (fp == nullptr) {
return nullptr;
}
size_t size, r;
r = fread(&size, sizeof(size), 1, fp);
if (r == 0 || size != sizeof(T)) {
fclose(fp);
GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size, (int64_t)sizeof(T));
throw std::runtime_error("invalid size");
}
std::unique_ptr<T> newObj(new T);
r = fread(newObj.get(), 1, size, fp);
fclose(fp);
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
}
return newObj;
}
template <class T>
inline int32_t GPUReconstruction::ReadStructFromFile(const char* file, T* obj)
{
FILE* fp = fopen(file, "rb");
if (fp == nullptr) {
return 1;
}
size_t size, r;
r = fread(&size, sizeof(size), 1, fp);
if (r == 0) {
fclose(fp);
return 1;
}
r = fread(obj, 1, size, fp);
fclose(fp);
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
}
return 0;
}
template <class T>
inline void GPUReconstruction::DumpDynamicStructToFile(const T* obj, size_t dynamicSize, const char* file)
{
FILE* fp = fopen(file, "w+b");
if (fp == nullptr) {
return;
}
size_t size = sizeof(*obj);
fwrite(&size, sizeof(size), 1, fp);
fwrite(&dynamicSize, sizeof(dynamicSize), 1, fp);
fwrite(obj, 1, dynamicSize, fp);
fclose(fp);
}
template <class T, auto F>
inline aligned_unique_buffer_ptr<T> GPUReconstruction::ReadDynamicStructFromFile(const char* file)
{
FILE* fp = fopen(file, "rb");
if (fp == nullptr) {
return nullptr;
}
size_t size, dynsize, r, r2;
r = fread(&size, sizeof(size), 1, fp);
r2 = fread(&dynsize, sizeof(dynsize), 1, fp);
if (r == 0 || r2 == 0 || size != sizeof(T) || dynsize < size) {
fclose(fp);
GPUError("ERROR reading %s, invalid size: %ld (%ld buffer size, %ld object size expected)", file, (int64_t)size, (int64_t)dynsize, (int64_t)sizeof(T));
throw std::runtime_error("invalid size");
}
std::unique_ptr<T> tmp = std::make_unique<T>();
r = fread(tmp.get(), sizeof(T), 1, fp);
if (r == 0) {
fclose(fp);
GPUError("ERROR reading %s", file, (int64_t)size, (int64_t)sizeof(T));
throw std::runtime_error("read error");
}
if ((tmp.get()->*F)() != dynsize) {
fclose(fp);
GPUError("ERROR: invalid size: %ld (%ld expected)", file, (int64_t)dynsize, (int64_t)(tmp.get()->*F)());
throw std::runtime_error("invalid size");
}
aligned_unique_buffer_ptr<T> newObj(dynsize);
memcpy(newObj.get(), tmp.get(), sizeof(T));
r = fread(newObj.getraw() + sizeof(T), 1, dynsize - sizeof(T), fp);
fclose(fp);
if (GetProcessingSettings().debugLevel >= 2) {
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
}
return newObj;
}
} // namespace o2::gpu
#endif