knncolle
Collection of KNN methods in C++
Loading...
Searching...
No Matches
find_nearest_neighbors.hpp
Go to the documentation of this file.
1#ifndef KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
2#define KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
3
4#include <vector>
5#include <utility>
6#include "Prebuilt.hpp"
7
14#ifndef KNNCOLLE_CUSTOM_PARALLEL
15#include "subpar/subpar.hpp"
16#endif
17
18namespace knncolle {
19
32template<typename Task_, class Run_>
33void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range) {
34#ifndef KNNCOLLE_CUSTOM_PARALLEL
35 // Don't make this nothrow_ = true, as the derived methods could do anything...
36 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
37#else
38 KNNCOLLE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
39#endif
40}
41
51template<typename Index_ = int, typename Float_ = double>
52using NeighborList = std::vector<std::vector<std::pair<Index_, Float_> > >;
53
71template<typename Dim_, typename Index_, typename Float_>
73 Index_ nobs = index.num_observations();
75
76 parallelize(num_threads, nobs, [&](int, Index_ start, Index_ length) -> void {
77 auto sptr = index.initialize();
78 std::vector<Index_> indices;
79 std::vector<Float_> distances;
80 for (Index_ i = start, end = start + length; i < end; ++i) {
81 sptr->search(i, k, &indices, &distances);
82 int actual_k = indices.size();
83 output[i].reserve(actual_k);
84 for (int j = 0; j < actual_k; ++j) {
85 output[i].emplace_back(indices[j], distances[j]);
86 }
87 }
88 });
89
90 return output;
91}
92
110template<typename Dim_, typename Index_, typename Float_>
111std::vector<std::vector<Index_> > find_nearest_neighbors_index_only(const Prebuilt<Dim_, Index_, Float_>& index, int k, int num_threads = 1) {
112 Index_ nobs = index.num_observations();
113 std::vector<std::vector<Index_> > output(nobs);
114
115 parallelize(num_threads, nobs, [&](int, Index_ start, Index_ length) -> void {
116 auto sptr = index.initialize();
117 for (Index_ i = start, end = start + length; i < end; ++i) {
118 sptr->search(i, k, &(output[i]), NULL);
119 }
120 });
121
122 return output;
123}
124
125}
126
127#endif
Interface for prebuilt nearest-neighbor indices.
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:28
virtual Index_ num_observations() const =0
virtual std::unique_ptr< Searcher< Index_, Float_ > > initialize() const =0
Collection of KNN algorithms.
Definition Bruteforce.hpp:22
std::vector< std::vector< Index_ > > find_nearest_neighbors_index_only(const Prebuilt< Dim_, Index_, Float_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:111
NeighborList< Index_, Float_ > find_nearest_neighbors(const Prebuilt< Dim_, Index_, Float_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:72
std::vector< std::vector< std::pair< Index_, Float_ > > > NeighborList
Definition find_nearest_neighbors.hpp:52
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition find_nearest_neighbors.hpp:33