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
20namespace o2::gpu
21{
22
23template <class T, class S>
24inline uint32_t GPUReconstruction::DumpData(FILE* fp, const T* const* entries, const S* num, InOutPointerType type)
25{
27 uint32_t numTotal = 0;
28 for (int32_t i = 0; i < count; i++) {
29 numTotal += num[i];
30 }
31 if (numTotal == 0) {
32 return 0;
33 }
34 fwrite(&type, sizeof(type), 1, fp);
35 for (int32_t i = 0; i < count; i++) {
36 fwrite(&num[i], sizeof(num[i]), 1, fp);
37 if (num[i]) {
38 fwrite(entries[i], sizeof(*entries[i]), num[i], fp);
39 }
40 }
41 if (mProcessingSettings.debugLevel >= 2) {
42 GPUInfo("Dumped %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
43 }
44 return numTotal;
45}
46
47template <class T, class S>
48inline size_t GPUReconstruction::ReadData(FILE* fp, const T** entries, S* num, std::unique_ptr<T[]>* mem, InOutPointerType type, T** nonConstPtrs)
49{
50 if (feof(fp)) {
51 return 0;
52 }
53 InOutPointerType inType;
54 size_t r, pos = ftell(fp);
55 r = fread(&inType, sizeof(inType), 1, fp);
56 if (r != 1 || inType != type) {
57 fseek(fp, pos, SEEK_SET);
58 return 0;
59 }
60
62 size_t numTotal = 0;
63 for (int32_t i = 0; i < count; i++) {
64 r = fread(&num[i], sizeof(num[i]), 1, fp);
65 T* m = AllocateIOMemoryHelper(num[i], entries[i], mem[i]);
66 if (nonConstPtrs) {
67 nonConstPtrs[i] = m;
68 }
69 if (num[i]) {
70 r = fread(m, sizeof(*entries[i]), num[i], fp);
71 }
72 numTotal += num[i];
73 }
74 (void)r;
75 if (mProcessingSettings.debugLevel >= 2) {
76 GPUInfo("Read %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
77 }
78 return numTotal;
79}
80
81template <class T>
82inline void GPUReconstruction::DumpFlatObjectToFile(const T* obj, const char* file)
83{
84 FILE* fp = fopen(file, "w+b");
85 if (fp == nullptr) {
86 return;
87 }
88 size_t size[2] = {sizeof(*obj), obj->getFlatBufferSize()};
89 fwrite(size, sizeof(size[0]), 2, fp);
90 fwrite(obj, 1, size[0], fp);
91 fwrite(obj->getFlatBufferPtr(), 1, size[1], fp);
92 fclose(fp);
93}
94
95template <class T>
96inline std::unique_ptr<T> GPUReconstruction::ReadFlatObjectFromFile(const char* file)
97{
98 FILE* fp = fopen(file, "rb");
99 if (fp == nullptr) {
100 return nullptr;
101 }
102 size_t size[2] = {0}, r;
103 r = fread(size, sizeof(size[0]), 2, fp);
104 if (r == 0 || size[0] != sizeof(T)) {
105 fclose(fp);
106 GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size[0], (int64_t)sizeof(T));
107 throw std::runtime_error("invalid size");
108 }
109 std::unique_ptr<T> retVal(new T);
110 retVal->destroy();
111 char* buf = new char[size[1]]; // Not deleted as ownership is transferred to FlatObject
112 r = fread((void*)retVal.get(), 1, size[0], fp);
113 r = fread(buf, 1, size[1], fp);
114 fclose(fp);
115 if (mProcessingSettings.debugLevel >= 2) {
116 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
117 }
118 retVal->clearInternalBufferPtr();
119 retVal->setActualBufferAddress(buf);
120 retVal->adoptInternalBuffer(buf);
121 return retVal;
122}
123
124template <class T>
125inline void GPUReconstruction::DumpStructToFile(const T* obj, const char* file)
126{
127 FILE* fp = fopen(file, "w+b");
128 if (fp == nullptr) {
129 return;
130 }
131 size_t size = sizeof(*obj);
132 fwrite(&size, sizeof(size), 1, fp);
133 fwrite(obj, 1, size, fp);
134 fclose(fp);
135}
136
137template <class T>
138inline std::unique_ptr<T> GPUReconstruction::ReadStructFromFile(const char* file)
139{
140 FILE* fp = fopen(file, "rb");
141 if (fp == nullptr) {
142 return nullptr;
143 }
144 size_t size, r;
145 r = fread(&size, sizeof(size), 1, fp);
146 if (r == 0 || size != sizeof(T)) {
147 fclose(fp);
148 GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size, (int64_t)sizeof(T));
149 throw std::runtime_error("invalid size");
150 }
151 std::unique_ptr<T> newObj(new T);
152 r = fread(newObj.get(), 1, size, fp);
153 fclose(fp);
154 if (mProcessingSettings.debugLevel >= 2) {
155 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
156 }
157 return newObj;
158}
159
160template <class T>
161inline int32_t GPUReconstruction::ReadStructFromFile(const char* file, T* obj)
162{
163 FILE* fp = fopen(file, "rb");
164 if (fp == nullptr) {
165 return 1;
166 }
167 size_t size, r;
168 r = fread(&size, sizeof(size), 1, fp);
169 if (r == 0) {
170 fclose(fp);
171 return 1;
172 }
173 r = fread(obj, 1, size, fp);
174 fclose(fp);
175 if (mProcessingSettings.debugLevel >= 2) {
176 GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
177 }
178 return 0;
179}
180
181} // namespace o2::gpu
182
183#endif
int32_t i
int32_t retVal
uint16_t pos
Definition RawData.h:3
double num
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)
GPUSettingsProcessing mProcessingSettings
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)
void DumpFlatObjectToFile(const T *obj, const char *file)
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