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