Added text files; minor cleanup
[RBC.git] / brute.cu
1 #ifndef BRUTE_CU
2 #define BRUTE_CU
3
4 #include "utilsGPU.h"
5 #include "utils.h"
6 #include "rbc.h"
7 #include "defs.h"
8 #include "kernels.h"
9 #include "kernelWrap.h"
10 #include "brute.h"
11 #include<stdio.h>
12 #include<cuda.h>
13
14 void bruteRangeCount(matrix x, matrix q, real *ranges, int *cnts){
15 matrix dx, dq;
16 real *dranges;
17 int *dcnts;
18
19 copyAndMove(&dx, &x);
20 copyAndMove(&dq, &q);
21
22 cudaMalloc( (void**)&dranges, q.pr*sizeof(*dranges) );
23 cudaMemcpy( dranges, ranges, q.r*sizeof(*dranges), cudaMemcpyHostToDevice );
24
25 cudaMalloc( (void**)&dcnts, q.pr*sizeof(*dcnts) );
26
27 rangeCountWrap(dq, dx, dranges, dcnts);
28
29 cudaMemcpy(cnts, dcnts, q.r*sizeof(*cnts), cudaMemcpyDeviceToHost );
30
31 cudaFree(dcnts);
32 cudaFree(dranges);
33 cudaFree(dx.mat);
34 cudaFree(dq.mat);
35 }
36
37
38 void bruteSearch(matrix x, matrix q, int *NNs){
39 real *dMins;
40 int *dMinIDs;
41 matrix dx, dq;
42
43
44 dx.r=x.r; dx.pr=x.pr; dx.c=x.c; dx.pc=x.pc; dx.ld=x.ld;
45 dq.r=q.r; dq.pr=q.pr; dq.c=q.c; dq.pc=q.pc; dq.ld=q.ld;
46
47 cudaMalloc((void**)&dMins, q.pr*sizeof(*dMins));
48 cudaMalloc((void**)&dMinIDs, q.pr*sizeof(*dMinIDs));
49 cudaMalloc((void**)&dx.mat, dx.pr*dx.pc*sizeof(*dx.mat));
50 cudaMalloc((void**)&dq.mat, dq.pr*dq.pc*sizeof(*dq.mat));
51
52 cudaMemcpy(dx.mat,x.mat,x.pr*x.pc*sizeof(*dx.mat),cudaMemcpyHostToDevice);
53 cudaMemcpy(dq.mat,q.mat,q.pr*q.pc*sizeof(*dq.mat),cudaMemcpyHostToDevice);
54
55 nnWrap(dq,dx,dMins,dMinIDs);
56
57 cudaMemcpy(NNs,dMinIDs,dq.r*sizeof(*NNs),cudaMemcpyDeviceToHost);
58
59 cudaFree(dMins);
60 cudaFree(dMinIDs);
61 cudaFree(dx.mat);
62 cudaFree(dq.mat);
63 }
64
65
66 void bruteCPU(matrix X, matrix Q, int *NNs){
67 real *dtoNNs;
68 real temp;
69
70 int i, j;
71
72 dtoNNs = (real*)calloc(Q.r,sizeof(*dtoNNs));
73
74 for( i=0; i<Q.r; i++ ){
75 dtoNNs[i] = MAX_REAL;
76 NNs[i] = 0;
77 for(j=0; j<X.r; j++ ){
78 temp = distL1( Q, X, i, j );
79 if( temp < dtoNNs[i]){
80 NNs[i] = j;
81 dtoNNs[i] = temp;
82 }
83 }
84 }
85
86 free(dtoNNs);
87 }
88 #endif