-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcudaThrustOGL.cu
265 lines (212 loc) · 7.71 KB
/
cudaThrustOGL.cu
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
#include "cudaThrustOGL.hpp"
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/remove.h>
#include <thrust/copy.h>
#include <thrust/random.h>
#include <thrust/unique.h>
#include <thrust/iterator/counting_iterator.h>
#include <cassert>
typedef unsigned int uint;
typedef GLubyte mask_t;
//texture binding point for cuda access (for depth map)
texture<uchar1, cudaTextureType2D, cudaReadModeElementType> cudaTex;
__constant__ GLuint texwidth;
cudaThrustOGL::cudaThrustOGL(){
err_=cudaDeviceReset();
err_=cudaGLSetGLDevice(0);
err_=cudaSetDevice(0);
assert(err_==cudaSuccess);
}
void cudaThrustOGL::cudaInit(const GLuint& texID,
const GLuint& bufID,
const GLuint& emptybufID,
const GLuint& resultsBufID,
const size_t& w, const size_t& h){
width_ = w;
height_ = h;
//cuda register GL resources
err_=cudaGraphicsGLRegisterImage(&cuda_res_[0],texID,
GL_TEXTURE_2D,
cudaGraphicsMapFlagsReadOnly);
err_=cudaGraphicsGLRegisterBuffer(&cuda_res_[1],bufID,
cudaGraphicsMapFlagsNone);
err_=cudaGraphicsGLRegisterBuffer(&cuda_res_[2],emptybufID,
cudaGraphicsMapFlagsNone);
err_=cudaGraphicsGLRegisterBuffer(&cuda_res_[3],resultsBufID,
cudaGraphicsMapFlagsNone);
//upload the texture width to device
GLuint uintw=(GLuint)width_;
err_=cudaMemcpyToSymbol("texwidth", &uintw,sizeof(uintw));
seed_ = (unsigned int) time(NULL);//12345
reset();
assert(err_==cudaSuccess);
}
void cudaThrustOGL::reset(){
//init number of remaining darts to the size of the texture
rem_darts_ = width_*height_;
iter_=0;
rngoffset_=0; //keep using the random numbers
}
//Operator structs for counting empty pixels
struct isEmpty{
__device__
bool operator()(const GLuint& i){
GLuint x = i % texwidth;
GLuint y = i / texwidth;
return (tex2D(cudaTex,x,y).x == 0);
}
};
struct notEmpty{
__device__
bool operator()(const GLuint& i){
GLuint x = i % texwidth;
GLuint y = i / texwidth;
return (tex2D(cudaTex,x,y).x != 0);
}
};
size_t cudaThrustOGL::thrustCountEmptyPixels(){
err_=cudaGraphicsMapResources(3,&cuda_res_[0]);
//get the texture array
cudaArray* cuda_array;
err_=cudaGraphicsSubResourceGetMappedArray(&cuda_array,
cuda_res_[0],0,0);
//bind the texture to cuda
err_=cudaBindTextureToArray(cudaTex, cuda_array);
//get the emptylist buffer
GLuint* emptylistbuf;
size_t bufsize;
err_=cudaGraphicsResourceGetMappedPointer((void**)&emptylistbuf,&bufsize,
cuda_res_[2]);
//convert raw ptr to thrust ptr
thrust::device_ptr<GLuint> dev_ptr
=thrust::device_pointer_cast(emptylistbuf);
//cout << "before thrust " << dev_ptr[0] << " " << dev_ptr[1] << endl;
//declare the newend of the emptylist
thrust::device_ptr<GLuint> newend;
if(iter_==0){ // use counting itr to accelerate the first iteration
newend = thrust::copy_if(thrust::make_counting_iterator<GLuint>(0),
thrust::make_counting_iterator<GLuint>(
rem_darts_),dev_ptr, isEmpty());
}
else{
newend = thrust::remove_if(dev_ptr,dev_ptr+rem_darts_ ,notEmpty());
}
//cout << "after thrust " << dev_ptr[0] << " " << dev_ptr[1] << endl;
err_=cudaUnbindTexture(cudaTex);
err_=cudaGraphicsUnmapResources(3,&cuda_res_[0]);
size_t newrem_darts=newend-dev_ptr;
assert(newrem_darts < rem_darts_);
rem_darts_ = newrem_darts;
iter_++;
assert(err_==cudaSuccess);
return rem_darts_;
}
void cudaThrustOGL::cudaCleanup(){
cudaGraphicsUnmapResources(3,&cuda_res_[0]);
for(size_t i=0; i< 3; i++){
cudaGraphicsUnregisterResource(cuda_res_[i]);
}
//cudaFree(drandvec);
}
size_t cudaThrustOGL::freeGPUMem(){
glFinish();
err_=cudaDeviceSynchronize();
size_t avail;
size_t total;
err_=cudaMemGetInfo( &avail, &total );
//cout << "Device memory available: " << avail*1.0/1048576 << "MB" <<endl;
assert(err_==cudaSuccess);
return avail;
}
void cudaThrustOGL::remDuplicateSamples(size_t dartCount){
unsigned int *buf;
size_t bufSize;
err_=cudaGraphicsMapResources(1,&cuda_res_[3]);
err_=cudaGraphicsResourceGetMappedPointer((void **)&buf,
&bufSize, cuda_res_[3]);
thrust::device_ptr<unsigned int> buf_first(buf);
thrust::device_ptr<unsigned int> buf_last = buf_first+dartCount;
thrust::device_ptr<unsigned int> buf_last_new
= thrust::unique(buf_first,buf_last);
cudaGraphicsUnmapResources(1,&cuda_res_[3]);
}
//Random uniform distribution for transform
template <typename T> class random_uniform{
private:
thrust::random::default_random_engine rng;
//thrust::random::ranlux48 rng;
thrust::uniform_real_distribution<float> dist;
//Pointer to the empty list
const T* empty_ptr_;//,*r_ptr_;
//Offset for the rng
const size_t offset_;
//Number of elements in the empty list
T umax_;
//Size of the texture
const size_t w_, h_;
//scale normalized coord to ushort variables
float wscale_,hscale_;
//subpixel variables
float sppixelw_,sprowarea_;
public:
// provide constructor to initialize the distribution:
random_uniform(const size_t& w, const size_t& h, const T& umax,
const T* dp, const size_t& o, const unsigned int& s)
:dist(),empty_ptr_(dp), offset_(o),umax_(umax),w_(w),h_(h){
rng.seed(s);
hscale_ = (USHRT_MAX*1.0f/h_);
wscale_ = (USHRT_MAX*1.0f/w_);
sppixelw_ = h_*1.0f/USHRT_MAX * w_*1.0f/USHRT_MAX;
sprowarea_ = h_*1.0f/USHRT_MAX;
}
// OK, now the actual operator:
__device__
T operator()(size_t index){
// skip past numbers used in previous threads
rng.discard(index+offset_); //offset the used random numbers
float fidx = dist(rng)*umax_;
T coord =floor(fidx);
float frac=fidx-floor(fidx);
if(empty_ptr_ != NULL){
coord = empty_ptr_[coord]; //sample from the empty list
}
//pack the coordinates into ushorts
GLushort x = (coord % w_)* wscale_;
GLushort y = (coord / w_)* hscale_;
//subpixel
x += floor(fmod(frac, sprowarea_)/sppixelw_); // div subpix width
y += floor(frac / sprowarea_); //div subpix row area
//pack x y into uint
T res = (y << 16 | x & 0xffff);
return res;
}
};
//Generate some vertices
void cudaThrustOGL::makeVertices(const size_t& ndarts){
err_=cudaGraphicsMapResources(3,&cuda_res_[0]);
//get the dart buffer
GLuint* dartbuf;
size_t bufsize;
err_=cudaGraphicsResourceGetMappedPointer((void**)&dartbuf,&bufsize,
cuda_res_[1]);
//get the emptylist buffer
GLuint* emptylistbuf;
err_=cudaGraphicsResourceGetMappedPointer((void**)&emptylistbuf,&bufsize,
cuda_res_[2]);
//convert raw ptr to thrust ptr
thrust::device_ptr<GLuint> dart_ptr=thrust::device_pointer_cast(dartbuf);
if(iter_==0){
emptylistbuf=NULL; //do not use the emptylist lookup in iter 0
}
//pick ndarts locations from the emptylist
thrust::transform(thrust::make_counting_iterator<GLuint>(0),
thrust::make_counting_iterator<GLuint>(ndarts),
dart_ptr,random_uniform<GLuint>(width_,height_,
rem_darts_-1,
emptylistbuf,
rngoffset_,seed_));
rngoffset_+=ndarts;
err_=cudaGraphicsUnmapResources(3,&cuda_res_[0]);
assert(err_==cudaSuccess);
}