29 #ifndef RBC_HELPERFUNCS_HPP
30 #define RBC_HELPERFUNCS_HPP
37 #if defined(__APPLE__) || defined(__MACOSX)
38 #include <OpenCL/cl.hpp>
62 for (pow = 1; pow < (uint64_t) num; pow <<= 1) ;
77 void printBuffer (
const char *title, T *ptr, uint32_t width, uint32_t height)
79 std::cout << title << std::endl;
81 for (
int row = 0; row < height; ++row)
83 for (
int col = 0; col < width; ++col)
85 std::cout << std::setw (3 *
sizeof (T)) << +ptr[row * width + col] <<
" ";
87 std::cout << std::endl;
90 std::cout << std::endl;
103 template <
typename T>
104 void printBufferF (
const char *title, T *ptr, uint32_t width, uint32_t height, uint32_t prec)
106 std::ios::fmtflags f (std::cout.flags ());
107 std::cout << title << std::endl;
108 std::cout << std::fixed << std::setprecision (prec);
110 for (
int row = 0; row < height; ++row)
112 for (
int col = 0; col < width; ++col)
114 std::cout << std::setw (5 + prec) << ptr[row * width + col] <<
" ";
116 std::cout << std::endl;
119 std::cout << std::endl;
133 template <
typename T>
134 void cpuReduce (T *in, T *out, uint32_t cols, uint32_t rows, std::function<
bool (T, T)> func)
136 for (uint r = 0; r < rows; ++r)
138 T rec = in[r * cols];
139 for (uint c = 1; c < cols; ++c)
141 T tmp = in[r * cols + c];
142 if (func (tmp, rec)) rec = tmp;
158 template <
typename T>
159 void cpuInScan (T *in, T *out, uint32_t width, uint32_t height)
162 for (uint32_t row = 0; row < height; ++row)
163 out[row * width] = in[row * width];
165 for (uint32_t row = 0; row < height; ++row)
166 for (uint32_t col = 1; col < width; ++col)
167 out[row * width + col] = out[row * width + col - 1] + in[row * width + col];
180 template <
typename T>
181 void cpuExScan (T *in, T *out, uint32_t width, uint32_t height)
184 for (uint32_t row = 0; row < height; ++row)
185 out[row * width] = 0;
187 for (uint32_t row = 0; row < height; ++row)
188 for (uint32_t col = 1; col < width; ++col)
189 out[row * width + col] = out[row * width + col - 1] + in[row * width + col - 1];
205 template <
typename T>
208 for (uint x = 0; x < nx; ++x)
210 for (uint r = 0; r < nr; ++r)
213 for (uint j = 0; j < d; ++j)
214 dist += std::pow (X[x * d + j] - R[r * d + j], 2);
216 D[x * nr + r] = dist;
235 template <
typename T>
238 for (uint x = 0; x < nx; ++x)
240 for (uint r = 0; r < nr; ++r)
243 for (uint j = 0; j < 4; ++j)
244 dist += 1.f / (1.f + a) * std::pow (X[x * d + j] - R[r * d + j], 2);
245 for (uint j = 4; j < 8; ++j)
246 dist += a * std::pow (X[x * d + j] - R[r * d + j], 2);
248 D[x * nr + r] = dist;
269 template <
typename T>
271 uint32_t cols, uint32_t rows,
bool accCounters)
274 for (uint c = 0; c < cols; ++c)
279 for (uint r = 0; r < rows; ++r)
281 rbcMin.
dist = in[r * cols];
284 for (uint c = 1; c < cols; ++c)
286 T tmp = in[r * cols + c];
287 if (tmp < rbcMin.
dist)
297 Rnk[r] = N[rbcMin.
id]++;
320 template <
typename T>
322 uint32_t nx, uint32_t nr, uint32_t d,
bool permID)
324 for (uint32_t x = 0; x < nx; ++x)
330 for (uint32_t j = 0; j < d; ++j)
331 Xp[(offset + rank) * d + j] = X[x * d + j];
334 IDp[offset + rank] = ID[x];
347 template <
typename T>
352 for (
int i = 0; i < d; ++i)
353 dist += std::pow (p1[i] - p2[i], 2);
355 return std::sqrt (dist);
368 template <
typename T>
373 for (
int i = 0; i < 4; ++i)
374 dist += 1.f / (1.f + a) * std::pow (p1[i] - p2[i], 2);
375 for (
int i = 4; i < 8; ++i)
376 dist += a * std::pow (p1[i] - p2[i], 2);
399 template <
typename T>
401 uint32_t nq, uint32_t nr, uint32_t nx, uint32_t d)
403 cl_uint max_n = std::accumulate (N, N + nr, 0,
404 [](cl_uint a, cl_uint b) -> cl_uint {
return std::max (a, b); });
406 cl_float *D =
new cl_float[nq * max_n];
409 for (uint q = 0; q < nq; ++q)
411 cl_uint rID = RID[q].
id;
415 for (uint x = 0; x < max_n; ++x)
420 for (uint j = 0; j < d; ++j)
421 dist += std::pow (Qp[q * d + j] - Xp[(o + x) * d + j], 2);
423 dist = std::numeric_limits<float>::infinity ();
425 D[q * max_n + x] = dist;
431 for (uint32_t q = 0; q < nq; ++q)
433 uint nnID = O[RID[q].
id] + NNID[q].
id;
435 for (uint32_t j = 0; j < d; ++j)
436 NN[q * d + j] = Xp[nnID * d + j];
461 template <
typename T>
463 uint32_t nq, uint32_t nr, uint32_t nx, T a)
465 const unsigned int d = 8;
466 cl_uint max_n = std::accumulate (N, N + nr, 0,
467 [](cl_uint a, cl_uint b) -> cl_uint {
return std::max (a, b); });
469 cl_float *D =
new cl_float[nq * max_n];
472 for (uint q = 0; q < nq; ++q)
474 cl_uint rID = RID[q].
id;
478 for (uint x = 0; x < max_n; ++x)
484 for (uint j = 0; j < 4; ++j)
485 dist += 1.f / (1.f + a) * std::pow (Qp[q * d + j] - Xp[(o + x) * d + j], 2);
486 for (uint j = 4; j < d; ++j)
487 dist += a * std::pow (Qp[q * d + j] - Xp[(o + x) * d + j], 2);
490 dist = std::numeric_limits<float>::infinity ();
492 D[q * max_n + x] = dist;
498 for (uint32_t q = 0; q < nq; ++q)
500 uint nnID = O[RID[q].
id] + NNID[q].
id;
502 for (uint32_t j = 0; j < d; ++j)
503 NN[q * d + j] = Xp[nnID * d + j];
521 template <
typename T>
522 void cpuNNSearch (T *Q, T *X, T *NN, uint32_t nq, uint32_t nx, uint32_t d)
525 std::vector<T> D (nq * nx);
527 for (uint32_t q = 0; q < nq; ++q)
528 for (uint32_t x = 0; x < nx; ++x)
532 std::vector<uint32_t> minID (nq);
534 for (uint32_t q = 0; q < nq; ++q)
536 T minDist = D[q * nx];
539 for (uint32_t x = 1; x < nx; ++x)
541 T tmp = D[q * nx + x];
553 for (uint32_t q = 0; q < nq; ++q)
554 for (uint32_t j = 0; j < d; ++j)
555 NN[q * d + j] = X[minID[q] * d + j];
568 template <
typename T>
571 std::vector<T> D (n);
574 std::generate (D.begin (), D.end (),
577 T sumD = std::accumulate (D.begin (), D.end (), 0.f);
584 #endif // RBC_HELPERFUNCS_HPP
void printBufferF(const char *title, T *ptr, uint32_t width, uint32_t height, uint32_t prec)
Prints an array of floating-point type to standard output.
Definition: helper_funcs.hpp:104
void cpuRBCPermute(T *X, rbc_dist_id *ID, T *Xp, rbc_dist_id *IDp, uint32_t *O, uint32_t *Rnk, uint32_t nx, uint32_t nr, uint32_t d, bool permID)
Performs a permutation of the RBC database to form the representative lists.
Definition: helper_funcs.hpp:321
uint64_t nextPow2(T num)
Returns the first power of 2 greater than or equal to the input.
Definition: helper_funcs.hpp:57
void cpuExScan(T *in, T *out, uint32_t width, uint32_t height)
Performs an exclusive scan operation on the columns of an array.
Definition: helper_funcs.hpp:181
T meanError(T *Q, T *NN, uint32_t n, uint32_t d)
Computes the mean euclidean distance from the queries to their NNs.
Definition: helper_funcs.hpp:569
Declarations of data types used by the Random Ball Cover data structure.
void cpuRBCComputeDists(T *X, T *R, T *D, uint32_t nx, uint32_t nr, uint32_t d)
Computes the distances between two sets of points in a brute force way.
Definition: helper_funcs.hpp:206
Struct holding a value and a key.
Definition: data_types.hpp:43
void cpuReduce(T *in, T *out, uint32_t cols, uint32_t rows, std::function< bool(T, T)> func)
Reduces each row of an array to a single element.
Definition: helper_funcs.hpp:134
T euclideanMetric(T *p1, T *p2, uint32_t d)
Calculates the euclidean distance betweeen two points.
Definition: helper_funcs.hpp:348
void cpuRBCMinDists(T *in, rbc_dist_id *out, uint32_t *N, uint32_t *Rnk, uint32_t cols, uint32_t rows, bool accCounters)
Computes the minimum element, and its corresponding column id, for each row in an array...
Definition: helper_funcs.hpp:270
Definition: helper_funcs.hpp:44
void cpuRBCSearch(T *Qp, rbc_dist_id *RID, T *Xp, cl_uint *O, cl_uint *N, rbc_dist_id *NNID, T *NN, uint32_t nq, uint32_t nr, uint32_t nx, uint32_t d)
Uses the RBC data structure to search for the nearest neighbors.
Definition: helper_funcs.hpp:400
void cpuNNSearch(T *Q, T *X, T *NN, uint32_t nq, uint32_t nx, uint32_t d)
Computes (brute force) the nearest neighbors of a set of queries.
Definition: helper_funcs.hpp:522
void cpuInScan(T *in, T *out, uint32_t width, uint32_t height)
Performs an inclusive scan operation on the columns of an array.
Definition: helper_funcs.hpp:159
bool setProfilingFlag(int argc, char **argv)
Checks the command line arguments for the profiling flag, --profiling.
Definition: helper_funcs.cpp:66
T euclideanMetric8Squared(T *p1, T *p2, float a)
Calculates the euclidean distance betweeen two points in ..
Definition: helper_funcs.hpp:369
cl_uint id
Definition: data_types.hpp:46
void cpuRBCComputeDists8(T *X, T *R, T *D, uint32_t nx, uint32_t nr, uint32_t d, T a)
Computes the distances between two sets of points in a brute force way.
Definition: helper_funcs.hpp:236
void printBuffer(const char *title, T *ptr, uint32_t width, uint32_t height)
Prints an array of an integer type to standard output.
Definition: helper_funcs.hpp:77
void cpuRBCSearch8(T *Qp, rbc_dist_id *RID, T *Xp, cl_uint *O, cl_uint *N, rbc_dist_id *NNID, T *NN, uint32_t nq, uint32_t nr, uint32_t nx, T a)
Uses the RBC data structure to search for the nearest neighbors.
Definition: helper_funcs.hpp:462
cl_float dist
Definition: data_types.hpp:45