Cleaned up, debugged. Ready for 1st release
[RBC.git] / kernels.cu
1 /* This file is part of the Random Ball Cover (RBC) library.
2 * (C) Copyright 2010, Lawrence Cayton [lcayton@tuebingen.mpg.de]
3 */
4
5 #ifndef KERNELS_CU
6 #define KERNELS_CU
7
8 #include<cuda.h>
9 #include "defs.h"
10 #include "kernels.h"
11 #include<stdio.h>
12
13 // This kernel does the same thing as nnKernel, except it only considers pairs as
14 // specified by the compPlan.
15 __global__ void planNNKernel(const matrix Q, const unint *qMap, const matrix X, const intMatrix xMap, real *dMins, unint *dMinIDs, compPlan cP, unint qStartPos ){
16 unint qB = qStartPos + blockIdx.y * BLOCK_SIZE; //indexes Q
17 unint xB; //X (DB) Block;
18 unint cB; //column Block
19 unint offQ = threadIdx.y; //the offset of qPos in this block
20 unint offX = threadIdx.x; //ditto for x
21 unint i,j,k;
22 unint groupIts;
23
24 __shared__ real min[BLOCK_SIZE][BLOCK_SIZE];
25 __shared__ unint minPos[BLOCK_SIZE][BLOCK_SIZE];
26
27 __shared__ real Xs[BLOCK_SIZE][BLOCK_SIZE];
28 __shared__ real Qs[BLOCK_SIZE][BLOCK_SIZE];
29
30 unint g; //query group of q
31 unint xG; //DB group currently being examined
32 unint numGroups;
33 unint groupCount;
34
35 g = cP.qToQGroup[qB];
36 numGroups = cP.numGroups[g];
37
38 min[offQ][offX]=MAX_REAL;
39 __syncthreads();
40
41
42 for(i=0; i<numGroups; i++){ //iterate over DB groups
43 xG = cP.qGroupToXGroup[IDX( g, i, cP.ld )];
44 groupCount = cP.groupCountX[IDX( g, i, cP.ld )];
45 groupIts = (groupCount+BLOCK_SIZE-1)/BLOCK_SIZE;
46
47 for(j=0; j<groupIts; j++){ //iterate over elements of group
48 xB=j*BLOCK_SIZE;
49
50 real ans=0;
51 for(cB=0; cB<X.pc; cB+=BLOCK_SIZE){ // iterate over cols to compute distances
52
53 Xs[offX][offQ] = X.mat[IDX( xMap.mat[IDX( xG, xB+offQ, xMap.ld )], cB+offX, X.ld )];
54 Qs[offX][offQ] = ( (qMap[qB+offQ]==DUMMY_IDX) ? 0 : Q.mat[IDX( qMap[qB+offQ], cB+offX, Q.ld )] );
55 __syncthreads();
56
57 for(k=0; k<BLOCK_SIZE; k++)
58 ans+=DIST( Xs[k][offX], Qs[k][offQ] );
59
60 __syncthreads();
61 }
62
63 //compare to previous min and store into shared mem if needed.
64 if(xB+offX<groupCount && ans<min[offQ][offX]){
65 min[offQ][offX]=ans;
66 minPos[offQ][offX]= xMap.mat[IDX( xG, xB+offX, xMap.ld )];
67 }
68 __syncthreads();
69 }
70 }
71
72 //Reduce across threads
73 for(i=BLOCK_SIZE/2; i>0; i/=2){
74 if( offX<i ){
75 if( min[offQ][offX+i] < min[offQ][offX] ){
76 min[offQ][offX] = min[offQ][offX+i];
77 minPos[offQ][offX] = minPos[offQ][offX+i];
78 }
79 }
80 __syncthreads();
81 }
82
83 if(offX==0 && qMap[qB+offQ]!=DUMMY_IDX){
84 dMins[qMap[qB+offQ]] = min[offQ][0];
85 dMinIDs[qMap[qB+offQ]] = minPos[offQ][0];
86 }
87 }
88
89
90
91 __global__ void nnKernel(const matrix Q, unint numDone, const matrix X, real *dMins, unint *dMinIDs){
92
93 unint qB = blockIdx.y * BLOCK_SIZE + numDone; //indexes Q
94 unint xB; //indexes X;
95 unint cB; //colBlock
96 unint offQ = threadIdx.y; //the offset of qPos in this block
97 unint offX = threadIdx.x; //ditto for x
98 unint i;
99 real ans;
100
101 __shared__ real min[BLOCK_SIZE][BLOCK_SIZE];
102 __shared__ unint minPos[BLOCK_SIZE][BLOCK_SIZE];
103
104 __shared__ real Xs[BLOCK_SIZE][BLOCK_SIZE];
105 __shared__ real Qs[BLOCK_SIZE][BLOCK_SIZE];
106
107 min[offQ][offX]=MAX_REAL;
108 __syncthreads();
109
110 for(xB=0; xB<X.pr; xB+=BLOCK_SIZE){
111 ans=0;
112 for(cB=0; cB<X.pc; cB+=BLOCK_SIZE){
113
114 //Each thread loads one element of X and Q into memory.
115 Xs[offX][offQ] = X.mat[IDX( xB+offQ, cB+offX, X.ld )];
116 Qs[offX][offQ] = Q.mat[IDX( qB+offQ, cB+offX, Q.ld )];
117
118 __syncthreads();
119
120 for(i=0;i<BLOCK_SIZE;i++)
121 ans += DIST( Xs[i][offX], Qs[i][offQ] );
122
123 __syncthreads();
124 }
125
126 if( xB+offX<X.r && ans<min[offQ][offX] ){
127 minPos[offQ][offX] = xB+offX;
128 min[offQ][offX] = ans;
129 }
130 }
131 __syncthreads();
132
133
134 //reduce across threads
135 for(i=BLOCK_SIZE/2; i>0; i/=2){
136 if(offX<i){
137 if(min[offQ][offX+i]<min[offQ][offX]){
138 min[offQ][offX] = min[offQ][offX+i];
139 minPos[offQ][offX] = minPos[offQ][offX+i];
140 }
141 }
142 __syncthreads();
143 }
144
145 if(offX==0){
146 dMins[qB+offQ] = min[offQ][0];
147 dMinIDs[qB+offQ] = minPos[offQ][0];
148 }
149 }
150
151
152
153
154 __global__ void dist1Kernel(const matrix Q, unint qStart, const matrix X, unint xStart, matrix D){
155 unint c, i, j;
156
157 unint qB = blockIdx.y*BLOCK_SIZE + qStart;
158 unint q = threadIdx.y;
159 unint xB = blockIdx.x*BLOCK_SIZE + xStart;
160 unint x = threadIdx.x;
161
162 real ans=0;
163
164 //This thread is responsible for computing the dist between Q[qB+q] and X[xB+x]
165
166 __shared__ real Qs[BLOCK_SIZE][BLOCK_SIZE];
167 __shared__ real Xs[BLOCK_SIZE][BLOCK_SIZE];
168
169
170 for(i=0 ; i<Q.pc/BLOCK_SIZE ; i++){
171 c=i*BLOCK_SIZE; //current col block
172
173 Qs[x][q] = Q.mat[ IDX(qB+q, c+x, Q.ld) ];
174 Xs[x][q] = X.mat[ IDX(xB+q, c+x, X.ld) ];
175
176 __syncthreads();
177
178 for(j=0 ; j<BLOCK_SIZE ; j++)
179 ans += DIST( Qs[j][q], Xs[j][x] );
180
181 __syncthreads();
182 }
183
184 D.mat[ IDX( qB+q, xB+x, D.ld ) ] = ans;
185
186 }
187
188
189
190 __global__ void findRangeKernel(const matrix D, unint numDone, real *ranges, unint cntWant){
191
192 unint row = blockIdx.y*(BLOCK_SIZE/4)+threadIdx.y + numDone;
193 unint ro = threadIdx.y;
194 unint co = threadIdx.x;
195 unint i, c;
196 real t;
197
198 const unint LB = (90*cntWant)/100 ;
199 const unint UB = cntWant;
200
201 __shared__ real smin[BLOCK_SIZE/4][4*BLOCK_SIZE];
202 __shared__ real smax[BLOCK_SIZE/4][4*BLOCK_SIZE];
203
204 real min=MAX_REAL;
205 real max=0;
206 for(c=0 ; c<D.pc ; c+=(4*BLOCK_SIZE)){
207 if( c+co < D.c ){
208 t = D.mat[ IDX( row, c+co, D.ld ) ];
209 min = MIN(t,min);
210 max = MAX(t,max);
211 }
212 }
213
214 smin[ro][co] = min;
215 smax[ro][co] = max;
216 __syncthreads();
217
218 for(i=2*BLOCK_SIZE ; i>0 ; i/=2){
219 if( co < i ){
220 smin[ro][co] = MIN( smin[ro][co], smin[ro][co+i] );
221 smax[ro][co] = MAX( smax[ro][co], smax[ro][co+i] );
222 }
223 __syncthreads();
224 }
225
226 //Now start range counting.
227
228 unint itcount=0;
229 unint cnt;
230 real rg;
231 __shared__ unint scnt[BLOCK_SIZE/4][4*BLOCK_SIZE];
232 __shared__ char cont[BLOCK_SIZE/4];
233
234 if(co==0)
235 cont[ro]=1;
236
237 do{
238 itcount++;
239 __syncthreads();
240
241 if( cont[ro] ) //if we didn't actually need to cont, leave rg as it was.
242 rg = ( smax[ro][0] + smin[ro][0] ) / ((real)2.0) ;
243
244 cnt=0;
245 for(c=0 ; c<D.pc ; c+=(4*BLOCK_SIZE)){
246 cnt += (c+co < D.c && row < D.r && D.mat[ IDX( row, c+co, D.ld ) ] <= rg);
247 }
248
249 scnt[ro][co] = cnt;
250 __syncthreads();
251
252 for(i=2*BLOCK_SIZE ; i>0 ; i/=2){
253 if( co < i ){
254 scnt[ro][co] += scnt[ro][co+i];
255 }
256 __syncthreads();
257 }
258
259 if(co==0){
260 if( scnt[ro][0] < cntWant )
261 smin[ro][0]=rg;
262 else
263 smax[ro][0]=rg;
264 }
265
266 // cont[ro] == this row needs to continue
267 if(co==0)
268 cont[ro] = row<D.r && ( scnt[ro][0] < LB || scnt[ro][0] > UB );
269 __syncthreads();
270
271 // Determine if *any* of the rows need to continue
272 for(i=BLOCK_SIZE/8 ; i>0 ; i/=2){
273 if( ro < i && co==0)
274 cont[ro] |= cont[ro+i];
275 __syncthreads();
276 }
277
278 } while(cont[0]);
279
280 if(co==0 && row<D.r )
281 ranges[row]=rg;
282
283 }
284
285
286 __global__ void rangeSearchKernel(const matrix D, unint xOff, unint yOff, const real *ranges, charMatrix ir){
287 unint col = blockIdx.x*BLOCK_SIZE + threadIdx.x + xOff;
288 unint row = blockIdx.y*BLOCK_SIZE + threadIdx.y + yOff;
289
290 ir.mat[IDX( row, col, ir.ld )] = D.mat[IDX( row, col, D.ld )] < ranges[row];
291
292 }
293
294
295 __global__ void rangeCountKernel(const matrix Q, unint numDone, const matrix X, real *ranges, unint *counts){
296 unint q = blockIdx.y*BLOCK_SIZE + numDone;
297 unint qo = threadIdx.y;
298 unint xo = threadIdx.x;
299
300 real rg = ranges[q+qo];
301
302 unint r,c,i;
303
304 __shared__ unint scnt[BLOCK_SIZE][BLOCK_SIZE];
305
306 __shared__ real xs[BLOCK_SIZE][BLOCK_SIZE];
307 __shared__ real qs[BLOCK_SIZE][BLOCK_SIZE];
308
309 unint cnt=0;
310 for( r=0; r<X.pr; r+=BLOCK_SIZE ){
311
312 real dist=0;
313 for( c=0; c<X.pc; c+=BLOCK_SIZE){
314 xs[xo][qo] = X.mat[IDX( r+qo, c+xo, X.ld )];
315 qs[xo][qo] = Q.mat[IDX( q+qo, c+xo, Q.ld )];
316 __syncthreads();
317
318 for( i=0; i<BLOCK_SIZE; i++)
319 dist += DIST( xs[i][xo], qs[i][qo] );
320
321 __syncthreads();
322
323 }
324 cnt += r+xo<X.r && dist<rg;
325
326 }
327
328 scnt[qo][xo]=cnt;
329 __syncthreads();
330
331 for( i=BLOCK_SIZE/2; i>0; i/=2 ){
332 if( xo<i ){
333 scnt[qo][xo] += scnt[qo][xo+i];
334 }
335 __syncthreads();
336 }
337
338 if( xo==0 && q+qo<Q.r )
339 counts[q+qo] = scnt[qo][0];
340 }
341
342
343 #endif