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