updated text files
[RBC.git] / brute.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 BRUTE_CU
6 #define BRUTE_CU
7
8 #include "utilsGPU.h"
9 #include "utils.h"
10 #include "rbc.h"
11 #include "defs.h"
12 #include "kernels.h"
13 #include "kernelWrap.h"
14 #include "brute.h"
15 #include<stdio.h>
16 #include<cuda.h>
17 //#include<gsl/gsl_sort.h>
18
19 void bruteRangeCount(matrix x, matrix q, real *ranges, unint *cnts){
20 matrix dx, dq;
21 real *dranges;
22 unint *dcnts;
23
24 copyAndMove(&dx, &x);
25 copyAndMove(&dq, &q);
26
27 checkErr( cudaMalloc( (void**)&dranges, q.pr*sizeof(*dranges) ) );
28 cudaMemcpy( dranges, ranges, q.r*sizeof(*dranges), cudaMemcpyHostToDevice );
29
30 checkErr( cudaMalloc( (void**)&dcnts, q.pr*sizeof(*dcnts) ) );
31
32 rangeCountWrap(dq, dx, dranges, dcnts);
33
34 cudaMemcpy(cnts, dcnts, q.r*sizeof(*cnts), cudaMemcpyDeviceToHost );
35
36 cudaFree(dcnts);
37 cudaFree(dranges);
38 cudaFree(dx.mat);
39 cudaFree(dq.mat);
40 }
41
42
43 void bruteSearch(matrix x, matrix q, unint *NNs){
44 real *dMins;
45 unint *dMinIDs;
46 matrix dx, dq;
47
48 copyAndMove( &dx, &x );
49 copyAndMove( &dq, &q );
50
51 checkErr( cudaMalloc((void**)&dMins, q.pr*sizeof(*dMins)) );
52 checkErr( cudaMalloc((void**)&dMinIDs, q.pr*sizeof(*dMinIDs)) );
53
54 nnWrap( dq, dx, dMins, dMinIDs );
55
56 cudaMemcpy( NNs, dMinIDs, dq.r*sizeof(*NNs), cudaMemcpyDeviceToHost );
57
58 cudaFree( dMins );
59 cudaFree( dMinIDs );
60 cudaFree( dx.mat );
61 cudaFree( dq.mat );
62 }
63
64
65 void bruteK(matrix x, matrix q, intMatrix NNs, matrix NNdists){
66 matrix dNNdists;
67 intMatrix dMinIDs;
68 matrix dx, dq;
69
70 copyAndMove( &dx, &x );
71 copyAndMove( &dq, &q );
72
73 initMat( &dNNdists, q.r, KMAX );
74 checkErr( cudaMalloc((void**)&dNNdists.mat, sizeOfMatB(dNNdists) ) );
75
76 initIntMat( &dMinIDs, q.r, KMAX );
77 checkErr( cudaMalloc((void**)&dMinIDs.mat, sizeOfIntMatB(dMinIDs) ) );
78
79 knnWrap( dq, dx, dNNdists, dMinIDs );
80
81 cudaMemcpy( NNs.mat, dMinIDs.mat, sizeOfIntMatB(NNs), cudaMemcpyDeviceToHost );
82 cudaMemcpy( NNdists.mat, dNNdists.mat, sizeOfMatB(NNdists), cudaMemcpyDeviceToHost );
83
84 cudaFree( dNNdists.mat );
85 cudaFree( dMinIDs.mat );
86 cudaFree( dx.mat );
87 cudaFree( dq.mat );
88 }
89
90
91 void bruteCPU(matrix X, matrix Q, unint *NNs){
92 real *dtoNNs;
93 real temp;
94
95 unint i, j;
96
97 dtoNNs = (real*)calloc(Q.r,sizeof(*dtoNNs));
98
99 for( i=0; i<Q.r; i++ ){
100 dtoNNs[i] = MAX_REAL;
101 NNs[i] = 0;
102 for(j=0; j<X.r; j++ ){
103 temp = distVec( Q, X, i, j );
104 if( temp < dtoNNs[i]){
105 NNs[i] = j;
106 dtoNNs[i] = temp;
107 }
108 }
109 }
110
111 free(dtoNNs);
112 }
113
114
115 //The following method works properly, but requires the GNU scientific
116 //library. If you want to use it, uncomment the code, uncomment the include
117 //above, and adjust the makefile. It was used for debugging purposes, but
118 //is not required by anything else.
119
120 /* void bruteKCPU(matrix x, matrix q, intMatrix NNs){ */
121 /* int i, j; */
122
123 /* float **d; */
124 /* d = (float**)calloc(q.pr, sizeof(*d)); */
125 /* size_t **t; */
126 /* t = (size_t**)calloc(q.pr, sizeof(*t)); */
127 /* for( i=0; i<q.pr; i++){ */
128 /* d[i] = (float*)calloc(x.pr, sizeof(**d)); */
129 /* t[i] = (size_t*)calloc(x.pr, sizeof(**t)); */
130 /* } */
131
132 /* for( i=0; i<q.r; i++){ */
133 /* for( j=0; j<x.r; j++) */
134 /* d[i][j] = distVec( q, x, i, j ); */
135 /* gsl_sort_float_index(t[i], d[i], 1, x.r); */
136 /* for ( j=0; j<KMAX; j++) */
137 /* NNs.mat[IDX( i, j, NNs.ld )] = t[i][j]; */
138 /* } */
139
140 /* for( i=0; i<q.pr; i++){ */
141 /* free(t[i]); */
142 /* free(d[i]); */
143 /* } */
144 /* free(t); */
145 /* free(d); */
146 /* } */
147 #endif