updated NN functions so that they return the distances as well as the indices
[RBC.git] / rbc.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 RBC_CU
6 #define RBC_CU
7
8 #include<sys/time.h>
9 #include<stdio.h>
10 #include<cuda.h>
11 #include "utils.h"
12 #include "defs.h"
13 #include "utilsGPU.h"
14 #include "rbc.h"
15 #include "kernels.h"
16 #include "kernelWrap.h"
17 #include "sKernelWrap.h"
18
19 void queryRBC(const matrix q, const rbcStruct rbcS, unint *NNs, real* NNdists){
20 unint m = q.r;
21 unint numReps = rbcS.dr.r;
22 unint compLength;
23 compPlan dcP;
24 unint *qMap, *dqMap;
25 qMap = (unint*)calloc(PAD(m+(BLOCK_SIZE-1)*PAD(numReps)),sizeof(*qMap));
26 matrix dq;
27 copyAndMove(&dq, &q);
28
29 charMatrix cM;
30 cM.r=cM.c=numReps; cM.pr=cM.pc=cM.ld=PAD(numReps);
31 cM.mat = (char*)calloc( cM.pr*cM.pc, sizeof(*cM.mat) );
32
33 unint *repIDsQ;
34 repIDsQ = (unint*)calloc( m, sizeof(*repIDsQ) );
35 real *distToRepsQ;
36 distToRepsQ = (real*)calloc( m, sizeof(*distToRepsQ) );
37 unint *groupCountQ;
38 groupCountQ = (unint*)calloc( PAD(numReps), sizeof(*groupCountQ) );
39
40 computeReps(dq, rbcS.dr, repIDsQ, distToRepsQ);
41
42 //How many points are assigned to each group?
43 computeCounts(repIDsQ, m, groupCountQ);
44
45 //Set up the mapping from groups to queries (qMap).
46 buildQMap(q, qMap, repIDsQ, numReps, &compLength);
47
48 // Setup the computation matrix. Currently, the computation matrix is
49 // just the identity matrix: each query assigned to a particular
50 // representative is compared only to that representative's points.
51 idIntersection(cM);
52
53 initCompPlan(&dcP, cM, groupCountQ, rbcS.groupCount, numReps);
54
55 checkErr( cudaMalloc( (void**)&dqMap, compLength*sizeof(*dqMap) ) );
56 cudaMemcpy( dqMap, qMap, compLength*sizeof(*dqMap), cudaMemcpyHostToDevice );
57
58 computeNNs(rbcS.dx, rbcS.dxMap, dq, dqMap, dcP, NNs, NNdists, compLength);
59
60 free(qMap);
61 freeCompPlan(&dcP);
62 cudaFree(dq.mat);
63 free(cM.mat);
64 free(repIDsQ);
65 free(distToRepsQ);
66 free(groupCountQ);
67 }
68
69 //This function is very similar to queryRBC, with a couple of basic changes to handle
70 //k-nn.
71 void kqueryRBC(const matrix q, const rbcStruct rbcS, intMatrix NNs, matrix NNdists){
72 unint m = q.r;
73 unint numReps = rbcS.dr.r;
74 unint compLength;
75 compPlan dcP;
76 unint *qMap, *dqMap;
77 qMap = (unint*)calloc(PAD(m+(BLOCK_SIZE-1)*PAD(numReps)),sizeof(*qMap));
78 matrix dq;
79 copyAndMove(&dq, &q);
80
81 charMatrix cM;
82 cM.r=cM.c=numReps; cM.pr=cM.pc=cM.ld=PAD(numReps);
83 cM.mat = (char*)calloc( cM.pr*cM.pc, sizeof(*cM.mat) );
84
85 unint *repIDsQ;
86 repIDsQ = (unint*)calloc( m, sizeof(*repIDsQ) );
87 real *distToRepsQ;
88 distToRepsQ = (real*)calloc( m, sizeof(*distToRepsQ) );
89 unint *groupCountQ;
90 groupCountQ = (unint*)calloc( PAD(numReps), sizeof(*groupCountQ) );
91
92 computeReps(dq, rbcS.dr, repIDsQ, distToRepsQ);
93
94 //How many points are assigned to each group?
95 computeCounts(repIDsQ, m, groupCountQ);
96
97 //Set up the mapping from groups to queries (qMap).
98 buildQMap(q, qMap, repIDsQ, numReps, &compLength);
99
100 // Setup the computation matrix. Currently, the computation matrix is
101 // just the identity matrix: each query assigned to a particular
102 // representative is compared only to that representative's points.
103
104 // NOTE: currently, idIntersection is the *only* computation matrix
105 // that will work properly with k-nn search (this is not true for 1-nn above).
106 idIntersection(cM);
107
108 initCompPlan(&dcP, cM, groupCountQ, rbcS.groupCount, numReps);
109
110 checkErr( cudaMalloc( (void**)&dqMap, compLength*sizeof(*dqMap) ) );
111 cudaMemcpy( dqMap, qMap, compLength*sizeof(*dqMap), cudaMemcpyHostToDevice );
112
113 computeKNNs(rbcS.dx, rbcS.dxMap, dq, dqMap, dcP, NNs, NNdists, compLength);
114
115 free(qMap);
116 freeCompPlan(&dcP);
117 cudaFree(dq.mat);
118 free(cM.mat);
119 free(repIDsQ);
120 free(distToRepsQ);
121 free(groupCountQ);
122 }
123
124
125 void buildRBC(const matrix x, rbcStruct *rbcS, unint numReps, unint s){
126 unint n = x.pr;
127 intMatrix xmap;
128
129 setupReps(x, rbcS, numReps);
130 copyAndMove(&rbcS->dx, &x);
131
132 xmap.r=numReps; xmap.pr=PAD(numReps); xmap.c=s; xmap.pc=xmap.ld=PAD(s);
133 xmap.mat = (unint*)calloc( xmap.pr*xmap.pc, sizeof(*xmap.mat) );
134 copyAndMoveI(&rbcS->dxMap, &xmap);
135 rbcS->groupCount = (uint*)calloc( PAD(numReps), sizeof(*rbcS->groupCount) );
136
137 //Figure out how much fits into memory
138 size_t memFree, memTot;
139 cudaMemGetInfo(&memFree, &memTot);
140 memFree = (unint)(((float)memFree)*MEM_USABLE);
141 /* mem needed per rep:
142 * n*sizeof(real) - dist mat
143 * n*sizeof(char) - dir
144 * n*sizeof(int) - dSums
145 * sizeof(real) - dranges
146 * sizeof(int) - dCnts
147 * MEM_USED_IN_SCAN - memory used internally
148 */
149 unint ptsAtOnce = DPAD(memFree/((n+1)*sizeof(real) + n*sizeof(char) + (n+1)*sizeof(unint) + 2*MEM_USED_IN_SCAN(n)));
150 if(!ptsAtOnce){
151 fprintf(stderr,"error: %lu is not enough memory to build the RBC.. exiting\n", (unsigned long)memFree);
152 exit(1);
153 }
154
155 //Now set everything up for the scans
156 matrix dD;
157 dD.pr=dD.r=ptsAtOnce; dD.c=rbcS->dx.r; dD.pc=rbcS->dx.pr; dD.ld=dD.pc;
158 checkErr( cudaMalloc( (void**)&dD.mat, dD.pr*dD.pc*sizeof(*dD.mat) ) );
159
160 real *dranges;
161 checkErr( cudaMalloc( (void**)&dranges, ptsAtOnce*sizeof(real) ) );
162
163 charMatrix ir;
164 ir.r=dD.r; ir.pr=dD.pr; ir.c=dD.c; ir.pc=dD.pc; ir.ld=dD.ld;
165 ir.mat = (char*)calloc( ir.pr*ir.pc, sizeof(*ir.mat) );
166 charMatrix dir;
167 copyAndMoveC(&dir, &ir);
168
169 intMatrix dSums; //used to compute memory addresses.
170 dSums.r=dir.r; dSums.pr=dir.pr; dSums.c=dir.c; dSums.pc=dir.pc; dSums.ld=dir.ld;
171 checkErr( cudaMalloc( (void**)&dSums.mat, dSums.pc*dSums.pr*sizeof(*dSums.mat) ) );
172
173 unint *dCnts;
174 checkErr( cudaMalloc( (void**)&dCnts, ptsAtOnce*sizeof(*dCnts) ) );
175
176 //Do the scans to build the dxMap
177 unint numLeft = rbcS->dr.r; //points left to process
178 unint row = 0; //base row for iteration of while loop
179 unint pi, pip; //pi=pts per it, pip=pad(pi)
180 while( numLeft > 0 ){
181 pi = MIN(ptsAtOnce, numLeft); //points to do this iteration.
182 pip = PAD(pi);
183 dD.r = pi; dD.pr = pip; dir.r=pi; dir.pr=pip; dSums.r=pi; dSums.pr=pip;
184
185 distSubMat(rbcS->dr, rbcS->dx, dD, row, pip); //compute the distance matrix
186 findRangeWrap(dD, dranges, s); //find an appropriate range
187 rangeSearchWrap(dD, dranges, dir); //set binary vector for points in range
188 sumWrap(dir, dSums); //This and the next call perform the parallel compaction.
189 buildMapWrap(rbcS->dxMap, dir, dSums, row);
190 getCountsWrap(dCnts,dir,dSums); //How many points are assigned to each rep? It is not
191 //*exactly* s, which is why we need to compute this.
192 cudaMemcpy( &rbcS->groupCount[row], dCnts, pi*sizeof(*rbcS->groupCount), cudaMemcpyDeviceToHost );
193
194 numLeft -= pi;
195 row += pi;
196 }
197
198 cudaFree(dCnts);
199 free(ir.mat);
200 free(xmap.mat);
201 cudaFree(dranges);
202 cudaFree(dir.mat);
203 cudaFree(dSums.mat);
204 cudaFree(dD.mat);
205 }
206
207
208 // Choose representatives and move them to device
209 void setupReps(matrix x, rbcStruct *rbcS, int numReps){
210 unint i;
211 unint *randInds;
212 randInds = (unint*)calloc( PAD(numReps), sizeof(*randInds) );
213 subRandPerm(numReps, x.r, randInds);
214
215 matrix r;
216 r.r=numReps; r.pr=PAD(numReps); r.c=x.c; r.pc=r.ld=PAD(r.c);
217 r.mat = (real*)calloc( r.pr*r.pc, sizeof(*r.mat) );
218
219 for(i=0;i<numReps;i++)
220 copyVector(&r.mat[IDX(i,0,r.ld)], &x.mat[IDX(randInds[i],0,x.ld)], x.c);
221
222 copyAndMove(&rbcS->dr, &r);
223
224 free(randInds);
225 free(r.mat);
226 }
227
228
229 //Assign each point in dq to its nearest point in dr.
230 void computeReps(matrix dq, matrix dr, unint *repIDs, real *distToReps){
231 real *dMins;
232 unint *dMinIDs;
233
234 checkErr( cudaMalloc((void**)&(dMins), dq.pr*sizeof(*dMins)) );
235 checkErr( cudaMalloc((void**)&(dMinIDs), dq.pr*sizeof(*dMinIDs)) );
236
237 nnWrap(dq,dr,dMins,dMinIDs);
238
239 cudaMemcpy(distToReps,dMins,dq.r*sizeof(*dMins),cudaMemcpyDeviceToHost);
240 cudaMemcpy(repIDs,dMinIDs,dq.r*sizeof(*dMinIDs),cudaMemcpyDeviceToHost);
241
242 cudaFree(dMins);
243 cudaFree(dMinIDs);
244 }
245
246
247 //Assumes radii is initialized to 0s
248 void computeRadii(unint *repIDs, real *distToReps, real *radii, unint n, unint numReps){
249 unint i;
250
251 for(i=0;i<n;i++)
252 radii[repIDs[i]] = MAX(distToReps[i],radii[repIDs[i]]);
253 }
254
255
256 //Assumes groupCount is initialized to 0s
257 void computeCounts(unint *repIDs, unint n, unint *groupCount){
258 unint i;
259
260 for(i=0;i<n;i++)
261 groupCount[repIDs[i]]++;
262 }
263
264
265 void buildQMap(matrix q, unint *qMap, unint *repIDs, unint numReps, unint *compLength){
266 unint n=q.r;
267 unint i;
268 unint *gS; //groupSize
269
270 gS = (unint*)calloc(numReps+1,sizeof(*gS));
271
272 for( i=0; i<n; i++ )
273 gS[repIDs[i]+1]++;
274 for( i=0; i<numReps+1; i++ )
275 gS[i]=PAD(gS[i]);
276
277 for( i=1; i<numReps+1; i++ )
278 gS[i]=gS[i-1]+gS[i];
279
280 *compLength = gS[numReps];
281
282 for( i=0; i<(*compLength); i++ )
283 qMap[i]=DUMMY_IDX;
284
285 for( i=0; i<n; i++ ){
286 qMap[gS[repIDs[i]]]=i;
287 gS[repIDs[i]]++;
288 }
289
290 free(gS);
291 }
292
293
294 // Sets the computation matrix to the identity.
295 void idIntersection(charMatrix cM){
296 unint i;
297 for(i=0;i<cM.r;i++){
298 if(i<cM.c)
299 cM.mat[IDX(i,i,cM.ld)]=1;
300 }
301 }
302
303
304 void fullIntersection(charMatrix cM){
305 unint i,j;
306 for(i=0;i<cM.r;i++){
307 for(j=0;j<cM.c;j++){
308 cM.mat[IDX(i,j,cM.ld)]=1;
309 }
310 }
311 }
312
313
314 void computeNNs(matrix dx, intMatrix dxMap, matrix dq, unint *dqMap, compPlan dcP, unint *NNs, real *NNdists, unint compLength){
315 real *dNNdists;
316 unint *dMinIDs;
317
318 checkErr( cudaMalloc((void**)&dNNdists,compLength*sizeof(*dNNdists)) );
319 checkErr( cudaMalloc((void**)&dMinIDs,compLength*sizeof(*dMinIDs)) );
320
321 planNNWrap(dq, dqMap, dx, dxMap, dNNdists, dMinIDs, dcP, compLength );
322 cudaMemcpy( NNs, dMinIDs, dq.r*sizeof(*NNs), cudaMemcpyDeviceToHost );
323 cudaMemcpy( NNdists, dNNdists, dq.r*sizeof(*dNNdists), cudaMemcpyDeviceToHost );
324
325 cudaFree(dNNdists);
326 cudaFree(dMinIDs);
327 }
328
329
330 void computeKNNs(matrix dx, intMatrix dxMap, matrix dq, unint *dqMap, compPlan dcP, intMatrix NNs, matrix NNdists, unint compLength){
331 matrix dNNdists;
332 intMatrix dMinIDs;
333 dNNdists.r=compLength; dNNdists.pr=compLength; dNNdists.c=K; dNNdists.pc=K; dNNdists.ld=dNNdists.pc;
334 dMinIDs.r=compLength; dMinIDs.pr=compLength; dMinIDs.c=K; dMinIDs.pc=K; dMinIDs.ld=dMinIDs.pc;
335
336 checkErr( cudaMalloc((void**)&dNNdists.mat,dNNdists.pr*dNNdists.pc*sizeof(*dNNdists.mat)) );
337 checkErr( cudaMalloc((void**)&dMinIDs.mat,dMinIDs.pr*dMinIDs.pc*sizeof(*dMinIDs.mat)) );
338
339 planKNNWrap(dq, dqMap, dx, dxMap, dNNdists, dMinIDs, dcP, compLength);
340 cudaMemcpy( NNs.mat, dMinIDs.mat, dq.r*K*sizeof(*NNs.mat), cudaMemcpyDeviceToHost );
341 cudaMemcpy( NNdists.mat, dNNdists.mat, dq.r*K*sizeof(*NNdists.mat), cudaMemcpyDeviceToHost );
342
343 cudaFree(dNNdists.mat);
344 cudaFree(dMinIDs.mat);
345 }
346
347
348 //This calls the dist1Kernel wrapper, but has it compute only
349 //a submatrix of the all-pairs distance matrix. In particular,
350 //only distances from dr[start,:].. dr[start+length-1] to all of x
351 //are computed, resulting in a distance matrix of size
352 //length by dx.pr. It is assumed that length is padded.
353 void distSubMat(matrix dr, matrix dx, matrix dD, unint start, unint length){
354 dr.r=dr.pr=length;
355 dr.mat = &dr.mat[IDX( start, 0, dr.ld )];
356 dist1Wrap(dr, dx, dD);
357 }
358
359
360 void destroyRBC(rbcStruct *rbcS){
361 cudaFree(rbcS->dx.mat);
362 cudaFree(rbcS->dxMap.mat);
363 cudaFree(rbcS->dr.mat);
364 free(rbcS->groupCount);
365 }
366
367
368 /* Danger: this function allocates memory that it does not free.
369 * Use freeCompPlan to clear mem.
370 * See the readme.txt file for a description of why this function is needed.
371 */
372 void initCompPlan(compPlan *dcP, charMatrix cM, unint *groupCountQ, unint *groupCountX, unint numReps){
373 unint i,j,k;
374 unint maxNumGroups=0;
375 compPlan cP;
376
377 unint sNumGroups = numReps;
378 cP.numGroups = (unint*)calloc(sNumGroups, sizeof(*cP.numGroups));
379
380 for(i=0; i<numReps; i++){
381 cP.numGroups[i] = 0;
382 for(j=0; j<numReps; j++)
383 cP.numGroups[i] += cM.mat[IDX(i,j,cM.ld)];
384 maxNumGroups = MAX(cP.numGroups[i], maxNumGroups);
385 }
386 cP.ld = maxNumGroups;
387
388 unint sQToQGroup;
389 for(i=0, sQToQGroup=0; i<numReps; i++)
390 sQToQGroup += PAD(groupCountQ[i]);
391
392 cP.qToQGroup = (unint*)calloc( sQToQGroup, sizeof(*cP.qToQGroup) );
393
394 for(i=0, k=0; i<numReps; i++){
395 for(j=0; j<PAD(groupCountQ[i]); j++)
396 cP.qToQGroup[k++] = i;
397 }
398
399 unint sQGroupToXGroup = numReps*maxNumGroups;
400 cP.qGroupToXGroup = (unint*)calloc( sQGroupToXGroup, sizeof(*cP.qGroupToXGroup) );
401 unint sGroupCountX = maxNumGroups*numReps;
402 cP.groupCountX = (unint*)calloc( sGroupCountX, sizeof(*cP.groupCountX) );
403
404 for(i=0; i<numReps; i++){
405 for(j=0, k=0; j<numReps; j++){
406 if( cM.mat[IDX( i, j, cM.ld )] ){
407 cP.qGroupToXGroup[IDX( i, k, cP.ld )] = j;
408 cP.groupCountX[IDX( i, k++, cP.ld )] = groupCountX[j];
409 }
410 }
411 }
412
413 //Move to device
414 checkErr( cudaMalloc( (void**)&dcP->numGroups, sNumGroups*sizeof(*dcP->numGroups) ) );
415 cudaMemcpy( dcP->numGroups, cP.numGroups, sNumGroups*sizeof(*dcP->numGroups), cudaMemcpyHostToDevice );
416 checkErr( cudaMalloc( (void**)&dcP->groupCountX, sGroupCountX*sizeof(*dcP->groupCountX) ) );
417 cudaMemcpy( dcP->groupCountX, cP.groupCountX, sGroupCountX*sizeof(*dcP->groupCountX), cudaMemcpyHostToDevice );
418 checkErr( cudaMalloc( (void**)&dcP->qToQGroup, sQToQGroup*sizeof(*dcP->qToQGroup) ) );
419 cudaMemcpy( dcP->qToQGroup, cP.qToQGroup, sQToQGroup*sizeof(*dcP->qToQGroup), cudaMemcpyHostToDevice );
420 checkErr( cudaMalloc( (void**)&dcP->qGroupToXGroup, sQGroupToXGroup*sizeof(*dcP->qGroupToXGroup) ) );
421 cudaMemcpy( dcP->qGroupToXGroup, cP.qGroupToXGroup, sQGroupToXGroup*sizeof(*dcP->qGroupToXGroup), cudaMemcpyHostToDevice );
422 dcP->ld = cP.ld;
423 }
424
425
426 //Frees memory allocated in initCompPlan.
427 void freeCompPlan(compPlan *dcP){
428 cudaFree(dcP->numGroups);
429 cudaFree(dcP->groupCountX);
430 cudaFree(dcP->qToQGroup);
431 cudaFree(dcP->qGroupToXGroup);
432 }
433
434 #endif