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