cleaned up; ready for release
[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 //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){
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, 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.pr=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 unint memFree, memTot;
139 cuMemGetInfo(&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: %d is not enough memory to build the RBC.. exiting\n", 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, unint compLength){
315 real *dMins;
316 unint *dMinIDs;
317
318 checkErr( cudaMalloc((void**)&dMins,compLength*sizeof(*dMins)) );
319 checkErr( cudaMalloc((void**)&dMinIDs,compLength*sizeof(*dMinIDs)) );
320
321 planNNWrap(dq, dqMap, dx, dxMap, dMins, dMinIDs, dcP, compLength);
322 cudaMemcpy( NNs, dMinIDs, dq.r*sizeof(*NNs), cudaMemcpyDeviceToHost);
323
324 cudaFree(dMins);
325 cudaFree(dMinIDs);
326 }
327
328
329 void computeKNNs(matrix dx, intMatrix dxMap, matrix dq, unint *dqMap, compPlan dcP, intMatrix NNs, unint compLength){
330 matrix dMins;
331 intMatrix dMinIDs;
332 dMins.r=compLength; dMins.pr=compLength; dMins.c=K; dMins.pc=K; dMins.ld=dMins.pc;
333 dMinIDs.r=compLength; dMinIDs.pr=compLength; dMinIDs.c=K; dMinIDs.pc=K; dMinIDs.ld=dMinIDs.pc;
334
335 checkErr( cudaMalloc((void**)&dMins.mat,dMins.pr*dMins.pc*sizeof(*dMins.mat)) );
336 checkErr( cudaMalloc((void**)&dMinIDs.mat,dMinIDs.pr*dMinIDs.pc*sizeof(*dMinIDs.mat)) );
337
338 planKNNWrap(dq, dqMap, dx, dxMap, dMins, dMinIDs, dcP, compLength);
339 cudaMemcpy( NNs.mat, dMinIDs.mat, dq.r*K*sizeof(*NNs.mat), cudaMemcpyDeviceToHost);
340
341 cudaFree(dMins.mat);
342 cudaFree(dMinIDs.mat);
343 }
344
345
346 //This calls the dist1Kernel wrapper, but has it compute only
347 //a submatrix of the all-pairs distance matrix. In particular,
348 //only distances from dr[start,:].. dr[start+length-1] to all of x
349 //are computed, resulting in a distance matrix of size
350 //length by dx.pr. It is assumed that length is padded.
351 void distSubMat(matrix dr, matrix dx, matrix dD, unint start, unint length){
352 dr.r=dr.pr=length;
353 dr.mat = &dr.mat[IDX( start, 0, dr.ld )];
354 dist1Wrap(dr, dx, dD);
355 }
356
357
358 void destroyRBC(rbcStruct *rbcS){
359 cudaFree(rbcS->dx.mat);
360 cudaFree(rbcS->dxMap.mat);
361 cudaFree(rbcS->dr.mat);
362 free(rbcS->groupCount);
363 }
364
365
366 /* Danger: this function allocates memory that it does not free.
367 * Use freeCompPlan to clear mem.
368 * See the readme.txt file for a description of why this function is needed.
369 */
370 void initCompPlan(compPlan *dcP, charMatrix cM, unint *groupCountQ, unint *groupCountX, unint numReps){
371 unint i,j,k;
372 unint maxNumGroups=0;
373 compPlan cP;
374
375 unint sNumGroups = numReps;
376 cP.numGroups = (unint*)calloc(sNumGroups, sizeof(*cP.numGroups));
377
378 for(i=0; i<numReps; i++){
379 cP.numGroups[i] = 0;
380 for(j=0; j<numReps; j++)
381 cP.numGroups[i] += cM.mat[IDX(i,j,cM.ld)];
382 maxNumGroups = MAX(cP.numGroups[i], maxNumGroups);
383 }
384 cP.ld = maxNumGroups;
385
386 unint sQToQGroup;
387 for(i=0, sQToQGroup=0; i<numReps; i++)
388 sQToQGroup += PAD(groupCountQ[i]);
389
390 cP.qToQGroup = (unint*)calloc( sQToQGroup, sizeof(*cP.qToQGroup) );
391
392 for(i=0, k=0; i<numReps; i++){
393 for(j=0; j<PAD(groupCountQ[i]); j++)
394 cP.qToQGroup[k++] = i;
395 }
396
397 unint sQGroupToXGroup = numReps*maxNumGroups;
398 cP.qGroupToXGroup = (unint*)calloc( sQGroupToXGroup, sizeof(*cP.qGroupToXGroup) );
399 unint sGroupCountX = maxNumGroups*numReps;
400 cP.groupCountX = (unint*)calloc( sGroupCountX, sizeof(*cP.groupCountX) );
401
402 for(i=0; i<numReps; i++){
403 for(j=0, k=0; j<numReps; j++){
404 if( cM.mat[IDX( i, j, cM.ld )] ){
405 cP.qGroupToXGroup[IDX( i, k, cP.ld )] = j;
406 cP.groupCountX[IDX( i, k++, cP.ld )] = groupCountX[j];
407 }
408 }
409 }
410
411 //Move to device
412 checkErr( cudaMalloc( (void**)&dcP->numGroups, sNumGroups*sizeof(*dcP->numGroups) ) );
413 cudaMemcpy( dcP->numGroups, cP.numGroups, sNumGroups*sizeof(*dcP->numGroups), cudaMemcpyHostToDevice );
414 checkErr( cudaMalloc( (void**)&dcP->groupCountX, sGroupCountX*sizeof(*dcP->groupCountX) ) );
415 cudaMemcpy( dcP->groupCountX, cP.groupCountX, sGroupCountX*sizeof(*dcP->groupCountX), cudaMemcpyHostToDevice );
416 checkErr( cudaMalloc( (void**)&dcP->qToQGroup, sQToQGroup*sizeof(*dcP->qToQGroup) ) );
417 cudaMemcpy( dcP->qToQGroup, cP.qToQGroup, sQToQGroup*sizeof(*dcP->qToQGroup), cudaMemcpyHostToDevice );
418 checkErr( cudaMalloc( (void**)&dcP->qGroupToXGroup, sQGroupToXGroup*sizeof(*dcP->qGroupToXGroup) ) );
419 cudaMemcpy( dcP->qGroupToXGroup, cP.qGroupToXGroup, sQGroupToXGroup*sizeof(*dcP->qGroupToXGroup), cudaMemcpyHostToDevice );
420 dcP->ld = cP.ld;
421 }
422
423
424 //Frees memory allocated in initCompPlan.
425 void freeCompPlan(compPlan *dcP){
426 cudaFree(dcP->numGroups);
427 cudaFree(dcP->groupCountX);
428 cudaFree(dcP->qToQGroup);
429 cudaFree(dcP->qGroupToXGroup);
430 }
431
432 #endif