Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelizing knn and inrange searches #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "NearestNeighbors"
uuid = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
version = "0.4.9"
version = "0.4.10"

[deps]
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
Expand Down
1 change: 1 addition & 0 deletions src/NearestNeighbors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Distances: Metric, result_type, eval_reduce, eval_end, eval_op, eval_star

using StaticArrays
import Base.show
using Base.Threads: @threads

export NNTree, BruteTree, KDTree, BallTree, DataFreeTree
export knn, nn, inrange # TODOs? , allpairs, distmat, npairs
Expand Down
2 changes: 1 addition & 1 deletion src/inrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function inrange(tree::NNTree,

idxs = [Vector{Int}() for _ in 1:length(points)]

for i in 1:length(points)
@threads for i in 1:length(points)
inrange_point!(tree, points[i], radius, sortres, idxs[i])
end
return idxs
Expand Down
8 changes: 4 additions & 4 deletions src/knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ end
Performs a lookup of the `k` nearest neigbours to the `points` from the data
in the `tree`. If `sortres = true` the result is sorted such that the results are
in the order of increasing distance to the point. `skip` is an optional predicate
to determine if a point that would be returned should be skipped based on its
to determine if a point that would be returned should be skipped based on its
index.
"""
function knn(tree::NNTree{V}, points::Vector{T}, k::Int, sortres=false, skip::F=always_false) where {V, T <: AbstractVector, F<:Function}
check_input(tree, points)
check_k(tree, k)
n_points = length(points)
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
for i in 1:n_points
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
@threads for i in 1:n_points
knn_point!(tree, points[i], sortres, dists[i], idxs[i], skip)
end
return idxs, dists
Expand Down
4 changes: 2 additions & 2 deletions test/test_inrange.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Does not test leafsize
@testset "inrange" begin
@testset "metric" for metric in [Euclidean()]
@testset "tree type" for TreeType in trees_with_brute
@testset "metric $Metric" for metric in [Euclidean()]
@testset "tree type $TreeType" for TreeType in trees_with_brute
function test(data)
tree = TreeType(data, metric; leafsize=2)
dosort = true
Expand Down
4 changes: 2 additions & 2 deletions test/test_knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Distances.evaluate

@testset "knn" begin
@testset "metric" for metric in [metrics; WeightedEuclidean(ones(2))]
@testset "tree type" for TreeType in trees_with_brute
@testset "metric $metric" for metric in [metrics; WeightedEuclidean(ones(2))]
@testset "tree type $TreeType" for TreeType in trees_with_brute
function test(data)
tree = TreeType(data, metric; leafsize=2)

Expand Down
6 changes: 3 additions & 3 deletions test/test_monkey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import NearestNeighbors.MinkowskiMetric
# some edge case has been missed in the real tests


@testset "metric" for metric in fullmetrics
@testset "tree type" for TreeType in trees_with_brute
@testset "type" for T in (Float32, Float64)
@testset "metric $metric" for metric in fullmetrics
@testset "tree type $TreeType" for TreeType in trees_with_brute
@testset "element type $T" for T in (Float32, Float64)
@testset "knn monkey" begin
# Checks that we find existing point in the tree
# and that it is the closest
Expand Down