-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdataArray.h
More file actions
322 lines (272 loc) · 9.53 KB
/
dataArray.h
File metadata and controls
322 lines (272 loc) · 9.53 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
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
#ifndef I3D_LINE3D_DATAARRAY_H_
#define I3D_LINE3D_DATAARRAY_H_
/*
Line3D - Line-based Multi View Stereo
Copyright (C) 2015 Manuel Hofer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// external
#include "cuda.h"
#include "cuda_runtime.h"
#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
*/
namespace L3D
{
// 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>())
{
// init
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;
if(allocate_GPU_memory)
{
allocateGPU();
}
}
~DataArray()
{
// delete GPU data
removeFromGPU();
}
// 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;
}
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()
{
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;
}
}
// 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;
if(uploadToGPU)
upload();
}
// 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];
if(uploadToGPU)
dst->upload();
}
// basics
unsigned int width(){return width_;}
unsigned int height(){return height_;}
size_t pitchCPU(){return pitchCPU_;}
size_t strideCPU(){return strideCPU_;}
size_t pitchGPU()
{
if(dataGPU_ == NULL)
{
std::cerr << "DataArray::pitchGPU(): data is _not_ on GPU!" << std::endl;
}
return pitchGPU_;
}
size_t strideGPU()
{
if(dataGPU_ == NULL)
{
std::cerr << "DataArray::strideGPU(): data is _not_ on GPU!" << std::endl;
}
return strideGPU_;
}
bool onGPU(){return (dataGPU_ != NULL);}
size_t bytes(){return height_*pitchCPU_;}
private:
// 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();
}
// 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_DATAARRAY_H_