k-nn brute implemented
[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 __global__ void knnKernel(const matrix Q, unint numDone, const matrix X, matrix dMins, intMatrix dMinIDs){
153
154 unint qB = blockIdx.y * BLOCK_SIZE + numDone; //indexes Q
155 unint xB; //indexes X;
156 unint cB; //colBlock
157 unint offQ = threadIdx.y; //the offset of qPos in this block
158 unint offX = threadIdx.x; //ditto for x
159 unint i;
160 real ans;
161
162 __shared__ real Xs[BLOCK_SIZE][BLOCK_SIZE];
163 __shared__ real Qs[BLOCK_SIZE][BLOCK_SIZE];
164
165 __shared__ real D[BLOCK_SIZE][BLOCK_SIZE];
166 __shared__ unint id[BLOCK_SIZE][BLOCK_SIZE];
167 __shared__ real dNN[BLOCK_SIZE][K+BLOCK_SIZE];
168 __shared__ unint idNN[BLOCK_SIZE][K+BLOCK_SIZE];
169
170 dNN[offQ][offX] = MAX_REAL;
171 dNN[offQ][offX+16] = MAX_REAL;
172 idNN[offQ][offX] = DUMMY_IDX;
173 idNN[offQ][offX+16] = DUMMY_IDX;
174
175 __syncthreads();
176
177 for(xB=0; xB<X.pr; xB+=BLOCK_SIZE){
178 ans=0;
179 for(cB=0; cB<X.pc; cB+=BLOCK_SIZE){
180
181 //Each thread loads one element of X and Q into memory.
182 Xs[offX][offQ] = X.mat[IDX( xB+offQ, cB+offX, X.ld )];
183 Qs[offX][offQ] = Q.mat[IDX( qB+offQ, cB+offX, Q.ld )];
184
185 __syncthreads();
186
187 for(i=0;i<BLOCK_SIZE;i++)
188 ans += DIST( Xs[i][offX], Qs[i][offQ] );
189
190 __syncthreads();
191 }
192 D[offQ][offX] = (xB+offX<X.r)? ans:MAX_REAL;
193 id[offQ][offX] = xB + offX;
194 __syncthreads();
195
196 /* if(offX==0 && offQ==0 & qB==0){ */
197 /* printf("before sort: \n"); */
198 /* for(i=0;i<BLOCK_SIZE;i++) */
199 /* printf("%6.2f ",D[0][i]); */
200 /* printf("\n"); */
201 /* } */
202
203 sort16( D, id );
204 /* if(offX==0 && offQ==0 & qB==0){ */
205 /* printf("after sort:\n"); */
206 /* for(i=0;i<BLOCK_SIZE;i++) */
207 /* printf("%6.2f ",D[0][i]); */
208 /* printf("\n"); */
209 /* } */
210
211 __syncthreads();
212 dNN[offQ][offX+32] = D[offQ][offX];
213 idNN[offQ][offX+32] = id[offQ][offX];
214 __syncthreads();
215 merge32x16( dNN, idNN );
216 /* if(offX==0 && offQ==0 & qB==0){ */
217 /* for(i=0;i<K+BLOCK_SIZE;i++) */
218 /* printf("%6.2f ",dNN[0][i]); */
219 /* printf("\n"); */
220 /* for(i=0;i<K+BLOCK_SIZE;i++) */
221 /* printf("%d ",idNN[0][i]); */
222 /* printf("\n"); */
223 /* } */
224 }
225 __syncthreads();
226
227 dMins.mat[IDX(qB+offQ, offX, dMins.ld)] = dNN[offQ][offX];
228 dMins.mat[IDX(qB+offQ, offX+16, dMins.ld)] = dNN[offQ][offX+16];
229 dMinIDs.mat[IDX(qB+offQ, offX, dMins.ld)] = idNN[offQ][offX];
230 dMinIDs.mat[IDX(qB+offQ, offX+16, dMins.ld)] = idNN[offQ][offX+16];
231
232 }
233
234
235 __global__ void dist1Kernel(const matrix Q, unint qStart, const matrix X, unint xStart, matrix D){
236 unint c, i, j;
237
238 unint qB = blockIdx.y*BLOCK_SIZE + qStart;
239 unint q = threadIdx.y;
240 unint xB = blockIdx.x*BLOCK_SIZE + xStart;
241 unint x = threadIdx.x;
242
243 real ans=0;
244
245 //This thread is responsible for computing the dist between Q[qB+q] and X[xB+x]
246
247 __shared__ real Qs[BLOCK_SIZE][BLOCK_SIZE];
248 __shared__ real Xs[BLOCK_SIZE][BLOCK_SIZE];
249
250
251 for(i=0 ; i<Q.pc/BLOCK_SIZE ; i++){
252 c=i*BLOCK_SIZE; //current col block
253
254 Qs[x][q] = Q.mat[ IDX(qB+q, c+x, Q.ld) ];
255 Xs[x][q] = X.mat[ IDX(xB+q, c+x, X.ld) ];
256
257 __syncthreads();
258
259 for(j=0 ; j<BLOCK_SIZE ; j++)
260 ans += DIST( Qs[j][q], Xs[j][x] );
261
262 __syncthreads();
263 }
264
265 D.mat[ IDX( qB+q, xB+x, D.ld ) ] = ans;
266
267 }
268
269
270
271 __global__ void findRangeKernel(const matrix D, unint numDone, real *ranges, unint cntWant){
272
273 unint row = blockIdx.y*(BLOCK_SIZE/4)+threadIdx.y + numDone;
274 unint ro = threadIdx.y;
275 unint co = threadIdx.x;
276 unint i, c;
277 real t;
278
279 const unint LB = (90*cntWant)/100 ;
280 const unint UB = cntWant;
281
282 __shared__ real smin[BLOCK_SIZE/4][4*BLOCK_SIZE];
283 __shared__ real smax[BLOCK_SIZE/4][4*BLOCK_SIZE];
284
285 real min=MAX_REAL;
286 real max=0;
287 for(c=0 ; c<D.pc ; c+=(4*BLOCK_SIZE)){
288 if( c+co < D.c ){
289 t = D.mat[ IDX( row, c+co, D.ld ) ];
290 min = MIN(t,min);
291 max = MAX(t,max);
292 }
293 }
294
295 smin[ro][co] = min;
296 smax[ro][co] = max;
297 __syncthreads();
298
299 for(i=2*BLOCK_SIZE ; i>0 ; i/=2){
300 if( co < i ){
301 smin[ro][co] = MIN( smin[ro][co], smin[ro][co+i] );
302 smax[ro][co] = MAX( smax[ro][co], smax[ro][co+i] );
303 }
304 __syncthreads();
305 }
306
307 //Now start range counting.
308
309 unint itcount=0;
310 unint cnt;
311 real rg;
312 __shared__ unint scnt[BLOCK_SIZE/4][4*BLOCK_SIZE];
313 __shared__ char cont[BLOCK_SIZE/4];
314
315 if(co==0)
316 cont[ro]=1;
317
318 do{
319 itcount++;
320 __syncthreads();
321
322 if( cont[ro] ) //if we didn't actually need to cont, leave rg as it was.
323 rg = ( smax[ro][0] + smin[ro][0] ) / ((real)2.0) ;
324
325 cnt=0;
326 for(c=0 ; c<D.pc ; c+=(4*BLOCK_SIZE)){
327 cnt += (c+co < D.c && row < D.r && D.mat[ IDX( row, c+co, D.ld ) ] <= rg);
328 }
329
330 scnt[ro][co] = cnt;
331 __syncthreads();
332
333 for(i=2*BLOCK_SIZE ; i>0 ; i/=2){
334 if( co < i ){
335 scnt[ro][co] += scnt[ro][co+i];
336 }
337 __syncthreads();
338 }
339
340 if(co==0){
341 if( scnt[ro][0] < cntWant )
342 smin[ro][0]=rg;
343 else
344 smax[ro][0]=rg;
345 }
346
347 // cont[ro] == this row needs to continue
348 if(co==0)
349 cont[ro] = row<D.r && ( scnt[ro][0] < LB || scnt[ro][0] > UB );
350 __syncthreads();
351
352 // Determine if *any* of the rows need to continue
353 for(i=BLOCK_SIZE/8 ; i>0 ; i/=2){
354 if( ro < i && co==0)
355 cont[ro] |= cont[ro+i];
356 __syncthreads();
357 }
358
359 } while(cont[0]);
360
361 if(co==0 && row<D.r )
362 ranges[row]=rg;
363
364 }
365
366
367 __global__ void rangeSearchKernel(const matrix D, unint xOff, unint yOff, const real *ranges, charMatrix ir){
368 unint col = blockIdx.x*BLOCK_SIZE + threadIdx.x + xOff;
369 unint row = blockIdx.y*BLOCK_SIZE + threadIdx.y + yOff;
370
371 ir.mat[IDX( row, col, ir.ld )] = D.mat[IDX( row, col, D.ld )] < ranges[row];
372
373 }
374
375
376 __global__ void rangeCountKernel(const matrix Q, unint numDone, const matrix X, real *ranges, unint *counts){
377 unint q = blockIdx.y*BLOCK_SIZE + numDone;
378 unint qo = threadIdx.y;
379 unint xo = threadIdx.x;
380
381 real rg = ranges[q+qo];
382
383 unint r,c,i;
384
385 __shared__ unint scnt[BLOCK_SIZE][BLOCK_SIZE];
386
387 __shared__ real xs[BLOCK_SIZE][BLOCK_SIZE];
388 __shared__ real qs[BLOCK_SIZE][BLOCK_SIZE];
389
390 unint cnt=0;
391 for( r=0; r<X.pr; r+=BLOCK_SIZE ){
392
393 real dist=0;
394 for( c=0; c<X.pc; c+=BLOCK_SIZE){
395 xs[xo][qo] = X.mat[IDX( r+qo, c+xo, X.ld )];
396 qs[xo][qo] = Q.mat[IDX( q+qo, c+xo, Q.ld )];
397 __syncthreads();
398
399 for( i=0; i<BLOCK_SIZE; i++)
400 dist += DIST( xs[i][xo], qs[i][qo] );
401
402 __syncthreads();
403
404 }
405 cnt += r+xo<X.r && dist<rg;
406
407 }
408
409 scnt[qo][xo]=cnt;
410 __syncthreads();
411
412 for( i=BLOCK_SIZE/2; i>0; i/=2 ){
413 if( xo<i ){
414 scnt[qo][xo] += scnt[qo][xo+i];
415 }
416 __syncthreads();
417 }
418
419 if( xo==0 && q+qo<Q.r )
420 counts[q+qo] = scnt[qo][0];
421 }
422
423
424 __device__ void sort16(real x[][16], unint xi[][16]){
425 int i = threadIdx.x;
426 int j = threadIdx.y;
427
428 if(i%2==0)
429 mmGateI( x[j]+i, x[j]+i+1, xi[j]+i, xi[j]+i+1 );
430 __syncthreads();
431
432 if(i%4<2)
433 mmGateI( x[j]+i, x[j]+i+2, xi[j]+i, xi[j]+i+2 );
434 __syncthreads();
435
436 if(i%4==1)
437 mmGateI( x[j]+i, x[j]+i+1, xi[j]+i, xi[j]+i+1 );
438 __syncthreads();
439
440 if(i%8<4)
441 mmGateI( x[j]+i, x[j]+i+4, xi[j]+i, xi[j]+i+4 );
442 __syncthreads();
443
444 if(i%8==2 || i%8==3)
445 mmGateI( x[j]+i, x[j]+i+2, xi[j]+i, xi[j]+i+2 );
446 __syncthreads();
447
448 if( i%2 && i%8 != 7 )
449 mmGateI( x[j]+i, x[j]+i+1, xi[j]+i, xi[j]+i+1 );
450 __syncthreads();
451
452 //0-7; 8-15 now sorted. merge time.
453 if( i<8)
454 mmGateI( x[j]+i, x[j]+i+8, xi[j]+i, xi[j]+i+8 );
455 __syncthreads();
456
457 if( i>3 && i<8 )
458 mmGateI( x[j]+i, x[j]+i+4, xi[j]+i, xi[j]+i+4 );
459 __syncthreads();
460
461 int os = (i/2)*4+2 + i%2;
462 if(i<6)
463 mmGateI( x[j]+os, x[j]+os+2, xi[j]+os, xi[j]+os+2 );
464 __syncthreads();
465
466 if( i%2 && i<15)
467 mmGateI( x[j]+i, x[j]+i+1, xi[j]+i, xi[j]+i+1 );
468
469 }
470
471
472 __device__ void merge32x16(real x[][48], unint xi[][48]){
473 int i = threadIdx.x;
474 int j = threadIdx.y;
475
476 mmGateI( x[j]+i, x[j]+i+32, xi[j]+i, xi[j]+i+32 );
477 __syncthreads();
478
479 mmGateI( x[j]+i+16, x[j]+i+32, xi[j]+i+16, xi[j]+i+32 );
480 __syncthreads();
481
482 int os = (i<8)? 24: 0;
483 mmGateI( x[j]+os+i, x[j]+os+i+8, xi[j]+os+i, xi[j]+os+i+8 );
484 __syncthreads();
485
486 os = (i/4)*8+4 + i%4;
487 mmGateI( x[j]+os, x[j]+os+4, xi[j]+os, xi[j]+os+4 );
488 if(i<4)
489 mmGateI(x[j]+36+i, x[j]+36+i+4, xi[j]+36+i, xi[j]+36+i+4 );
490 __syncthreads();
491
492 os = (i/2)*4+2 + i%2;
493 mmGateI( x[j]+os, x[j]+os+2, xi[j]+os, xi[j]+os+2 );
494
495 os = (i/2)*4+34 + i%2;
496 if(i<6)
497 mmGateI( x[j]+os, x[j]+os+2, xi[j]+os, xi[j]+os+2 );
498 __syncthreads();
499
500 os = 2*i+1;
501 mmGateI(x[j]+os, x[j]+os+1, xi[j]+os, xi[j]+os+1 );
502
503 os = 2*i+33;
504 if(i<7)
505 mmGateI(x[j]+os, x[j]+os+1, xi[j]+os, xi[j]+os+1 );
506
507 }
508
509
510 __device__ void mmGateI(real *x, real *y, unint *xi, unint *yi){
511 int ti = MINi( *x, *y, *xi, *yi );
512 *yi = MAXi( *x, *y, *xi, *yi );
513 *xi = ti;
514 real t = MIN( *x, *y );
515 *y = MAX( *x, *y );
516 *x = t;
517 }
518
519 #endif