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 "Prebuilt.hpp"
5
6#include <vector>
7#include <utility>
8#include <type_traits>
9
10#include "sanisizer/sanisizer.hpp"
11#ifndef KNNCOLLE_CUSTOM_PARALLEL
12#include "subpar/subpar.hpp"
13#endif
14
15#include "cap_k.hpp"
16
23namespace knncolle {
24
37template<typename Task_, class Run_>
38void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range) {
39#ifndef KNNCOLLE_CUSTOM_PARALLEL
40 // Don't make this nothrow_ = true, as the derived methods could do anything...
41 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
42#else
43 KNNCOLLE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
44#endif
45}
46
56template<typename Index_, typename Distance_>
57using NeighborList = std::vector<std::vector<std::pair<Index_, Distance_> > >;
58
78template<typename Index_, typename Data_, typename Distance_>
80 const Index_ nobs = index.num_observations();
81 k = cap_k(k, nobs);
82 auto output = sanisizer::create<NeighborList<Index_, Distance_> >(nobs);
83
84 parallelize(num_threads, nobs, [&](int, Index_ start, Index_ length) -> void {
85 auto sptr = index.initialize_known();
86 std::vector<Index_> indices;
87 std::vector<Distance_> distances;
88 for (Index_ i = start, end = start + length; i < end; ++i) {
89 sptr->search(i, k, &indices, &distances);
90 const auto actual_k = indices.size();
91 output[i].reserve(actual_k);
92 for (I<decltype(actual_k)> j = 0; j < actual_k; ++j) {
93 output[i].emplace_back(indices[j], distances[j]);
94 }
95 }
96 });
97
98 return output;
99}
100
120template<typename Index_, typename Data_, typename Distance_>
121std::vector<std::vector<Index_> > find_nearest_neighbors_index_only(const Prebuilt<Index_, Data_, Distance_>& index, int k, int num_threads = 1) {
122 const Index_ nobs = index.num_observations();
123 k = cap_k(k, nobs);
124 auto output = sanisizer::create<std::vector<std::vector<Index_> > >(nobs);
125
126 parallelize(num_threads, nobs, [&](int, Index_ start, Index_ length) -> void {
127 auto sptr = index.initialize_known();
128 for (Index_ i = start, end = start + length; i < end; ++i) {
129 sptr->search(i, k, &(output[i]), NULL);
130 }
131 });
132
133 return output;
134}
135
136}
137
138#endif
Interface for prebuilt nearest-neighbor indices.
Cap the number of requested neighbors.
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:29
auto initialize_known() const
Definition Prebuilt.hpp:98
virtual Index_ num_observations() const =0
Collection of KNN algorithms.
Definition Bruteforce.hpp:31
NeighborList< Index_, Distance_ > find_nearest_neighbors(const Prebuilt< Index_, Data_, Distance_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:79
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition find_nearest_neighbors.hpp:38
std::vector< std::vector< Index_ > > find_nearest_neighbors_index_only(const Prebuilt< Index_, Data_, Distance_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:121
int cap_k(int k, Index_ num_observations)
Definition cap_k.hpp:27
std::vector< std::vector< std::pair< Index_, Distance_ > > > NeighborList
Definition find_nearest_neighbors.hpp:57