-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathdataArray.h
378 lines (323 loc) · 10.7 KB
/
dataArray.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#ifndef I3D_LINE3D_PP_DATAARRAY_H_
#define I3D_LINE3D_PP_DATAARRAY_H_
/*
* Line3D++ - Line-based Multi View Stereo
* Copyright (C) 2015 Manuel Hofer
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// internal
#include "configLIBS.h"
// external
#ifdef L3DPP_CUDA
#include "cuda.h"
#include "cuda_runtime.h"
#endif //L3DPP_CUDA
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/array.hpp>
// std
#include <vector>
#include <iostream>
/**
* Line3D++ - DataArray CPU/GPU
* ====================
* DataArray that can be moved from CPU to GPU
* and vice versa. Adapted from the ImageUtilities lib
* by Manuel Werlberger.
* ====================
* Author: M.Hofer, 2015
*/
#ifndef L3DPP_CUDA
// float4 - for non-CUDA machines
struct float4
{
float x;
float y;
float z;
float w;
};
// float2 - for non-CUDA machines
struct float2
{
float x;
float y;
};
#endif //L3DPP_CUDA
// define serialization for float2/4
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, float4& f, const unsigned int version)
{
ar & boost::serialization::make_nvp("x", f.x);
ar & boost::serialization::make_nvp("y", f.y);
ar & boost::serialization::make_nvp("z", f.z);
ar & boost::serialization::make_nvp("w", f.w);
}
template<class Archive>
void serialize(Archive & ar, float2& f, const unsigned int version)
{
ar & boost::serialization::make_nvp("x", f.x);
ar & boost::serialization::make_nvp("y", f.y);
}
}
}
namespace L3DPP
{
// floatN array (2D)
template <class PixelType>
class DataArray
{
public:
DataArray()
{
width_ = 0;
height_ = 0;
real_width_ = 0;
// CPU
pitchCPU_ = 0;
strideCPU_ = 0;
// GPU
dataGPU_ = NULL;
pitchGPU_ = 0;
strideGPU_ = 0;
}
DataArray(unsigned int width,
unsigned int height,
const bool allocate_GPU_memory=false,
const std::vector<PixelType>& data=std::vector<PixelType>()) :
width_(width), height_(height)
{
// pitch (CPU)
pitchCPU_ = width_*sizeof(PixelType);
unsigned int elements2pitch;
if(pitchCPU_ % 32 == 0)
elements2pitch = 0;
else
elements2pitch = (32-(pitchCPU_ % 32))/sizeof(PixelType);
width += elements2pitch;
pitchCPU_ = width*sizeof(PixelType);
strideCPU_ = pitchCPU_/sizeof(PixelType);
real_width_ = width;
// CPU --> stored line by line
dataCPU_ = std::vector<PixelType>(width*height_);
if(data.size() == width_*height_)
{
for(unsigned int h=0; h<height_; ++h)
for(unsigned int w=0; w<width_; ++w)
dataCPU(w,h)[0] = data[h*width_+w];
}
// GPU
dataGPU_ = NULL;
pitchGPU_ = 0;
strideGPU_ = 0;
#ifdef L3DPP_CUDA
if(allocate_GPU_memory)
{
allocateGPU();
if(data.size() == width_*height_)
{
upload();
}
}
#endif //L3DPP_CUDA
}
~DataArray()
{
#ifdef L3DPP_CUDA
// delete GPU data
removeFromGPU();
#endif //L3DPP_CUDA
}
// data access
PixelType* dataCPU(unsigned int x=0,
unsigned int y=0){
if(dataCPU_.size() > 0 && x < width_ && y < height_)
return &dataCPU_[y*strideCPU_+x];
else
return NULL;
}
#ifdef L3DPP_CUDA
PixelType* dataGPU(unsigned int x=0,
unsigned int y=0)
{
if(dataGPU_ == NULL)
{
std::cerr << "DataArray::dataGPU(): data is _not_ on GPU!" << std::endl;
return NULL;
}
if(dataGPU_ != NULL && x < width_ && y < height_)
return &dataGPU_[y*strideGPU_+x];
else
return NULL;
}
// data transfer CPU/GPU
void upload(const bool allocate_only=false)
{
if(dataGPU_ == NULL)
allocateGPU();
// host -> device
if(dataGPU_ != NULL)
{
cudaError_t status = cudaMemcpy2D(dataGPU_,pitchGPU_,
&dataCPU_[0],pitchCPU_,
width_*sizeof(PixelType),height_,
cudaMemcpyHostToDevice);
if(status != cudaSuccess)
{
std::cerr << "DataArray::upload(): copying from CPU to GPU failed...[" << cudaGetErrorString(status) << "]" << std::endl;
}
}
else
{
std::cerr << "DataArray::upload(): GPU memory not allocated..." << std::endl;
}
cudaDeviceSynchronize();
}
void download()
{
// device -> host
if(dataGPU_ != NULL)
{
cudaError_t status = cudaMemcpy2D(&dataCPU_[0],pitchCPU_,
dataGPU_,pitchGPU_,
width_*sizeof(PixelType),height_,
cudaMemcpyDeviceToHost);
if(status != cudaSuccess)
{
std::cerr << "DataArray::download(): copying from GPU to CPU failed... [" << cudaGetErrorString(status) << "]" << std::endl;
}
}
}
void removeFromGPU()
{
if(dataGPU_ != NULL)
{
cudaError_t status = cudaFree((void *)dataGPU_);
if(status != cudaSuccess)
{
std::cerr << "DataArray::removeFromGPU(): could not remove data from GPU...[" << cudaGetErrorString(status) << "]" << std::endl;
}
dataGPU_ = NULL;
pitchGPU_ = 0;
strideGPU_ = 0;
}
}
#endif //L3DPP_CUDA
// set constant value (CPU only!)
void setValue(const PixelType p, const bool uploadToGPU=false)
{
for(unsigned int i=0; i<dataCPU_.size(); ++i)
dataCPU_[i] = p;
#ifdef L3DPP_CUDA
if(uploadToGPU)
upload();
#endif //L3DPP_CUDA
}
// copy function (cpu tp cpu!)
void copyTo(DataArray* dst, const bool uploadToGPU=false)
{
// cpu --> cpu
for(unsigned int h=0; h<height_; ++h)
for(unsigned int w=0; w<width_; ++w)
dst->dataCPU(w,h)[0] = dataCPU(w,h)[0];
#ifdef L3DPP_CUDA
if(uploadToGPU)
dst->upload();
#endif //L3DPP_CUDA
}
// basics
unsigned int width() const {return width_;}
unsigned int real_width() const {return real_width_;}
unsigned int height() const {return height_;}
size_t pitchCPU() const {return pitchCPU_;}
size_t strideCPU() const {return strideCPU_;}
#ifdef L3DPP_CUDA
size_t pitchGPU() const
{
if(dataGPU_ == NULL)
{
std::cerr << "DataArray::pitchGPU(): data is _not_ on GPU!" << std::endl;
}
return pitchGPU_;
}
size_t strideGPU() const
{
if(dataGPU_ == NULL)
{
std::cerr << "DataArray::strideGPU(): data is _not_ on GPU!" << std::endl;
}
return strideGPU_;
}
bool onGPU() const {return (dataGPU_ != NULL);}
#endif //L3DPP_CUDA
size_t bytes() const {return height_*pitchCPU_;}
private:
#ifdef L3DPP_CUDA
// allocate GPU memory
void allocateGPU()
{
if(dataGPU_ != NULL)
return;
if(width_ > 0 && height_ > 0)
{
dataGPU_ = 0;
cudaError_t status = cudaMallocPitch((void **)&dataGPU_, &pitchGPU_,
width_*sizeof(PixelType), height_);
if(status != cudaSuccess)
{
std::cerr << "DataArray::allocateGPU(): GPU memory could not be allocated...[" << cudaGetErrorString(status) << "]" << std::endl;
dataGPU_ = NULL;
pitchGPU_ = 0;
strideGPU_ = 0;
return;
}
strideGPU_ = pitchGPU_/sizeof(PixelType);
}
else
{
std::cerr << "DataArray::allocateGPU(): width or height are zero! w=" << width_ << " h=" << height_ << std::endl;
}
cudaDeviceSynchronize();
}
#endif //L3DPP_CUDA
// basic
unsigned int width_;
unsigned int height_;
unsigned int real_width_;
// CPU
std::vector<PixelType> dataCPU_;
size_t pitchCPU_;
size_t strideCPU_;
// GPU
PixelType* dataGPU_;
size_t pitchGPU_;
size_t strideGPU_;
// serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::make_nvp("width_", width_);
ar & boost::serialization::make_nvp("height_", height_);
ar & boost::serialization::make_nvp("real_width_", real_width_);
ar & boost::serialization::make_nvp("pitchCPU_", pitchCPU_);
ar & boost::serialization::make_nvp("strideCPU_", strideCPU_);
ar & boost::serialization::make_nvp("pitchGPU_", pitchGPU_);
ar & boost::serialization::make_nvp("strideGPU_", strideGPU_);
if(Archive::is_loading::value)
{
dataGPU_ = NULL;
pitchGPU_ = 0;
strideGPU_ = 0;
dataCPU_ = std::vector<PixelType>(real_width_*height_);
}
ar & boost::serialization::make_array<PixelType>(&dataCPU_[0],dataCPU_.size());
}
};
}
#endif //I3D_LINE3D_PP_DATAARRAY_H_