Skip to content

Commit 33ccb17

Browse files
parallelizing knn and inrange searches
1 parent 1de39f5 commit 33ccb17

File tree

7 files changed

+14
-13
lines changed

7 files changed

+14
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "NearestNeighbors"
22
uuid = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
3-
version = "0.4.9"
3+
version = "0.4.10"
44

55
[deps]
66
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"

src/NearestNeighbors.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Distances: Metric, result_type, eval_reduce, eval_end, eval_op, eval_star
55

66
using StaticArrays
77
import Base.show
8+
using Base.Threads: @threads
89

910
export NNTree, BruteTree, KDTree, BallTree, DataFreeTree
1011
export knn, nn, inrange # TODOs? , allpairs, distmat, npairs

src/inrange.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function inrange(tree::NNTree,
1515

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

18-
for i in 1:length(points)
18+
@threads for i in 1:length(points)
1919
inrange_point!(tree, points[i], radius, sortres, idxs[i])
2020
end
2121
return idxs

src/knn.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ end
1111
Performs a lookup of the `k` nearest neigbours to the `points` from the data
1212
in the `tree`. If `sortres = true` the result is sorted such that the results are
1313
in the order of increasing distance to the point. `skip` is an optional predicate
14-
to determine if a point that would be returned should be skipped based on its
14+
to determine if a point that would be returned should be skipped based on its
1515
index.
1616
"""
1717
function knn(tree::NNTree{V}, points::Vector{T}, k::Int, sortres=false, skip::F=always_false) where {V, T <: AbstractVector, F<:Function}
1818
check_input(tree, points)
1919
check_k(tree, k)
2020
n_points = length(points)
21-
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
22-
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
23-
for i in 1:n_points
21+
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
22+
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
23+
@threads for i in 1:n_points
2424
knn_point!(tree, points[i], sortres, dists[i], idxs[i], skip)
2525
end
2626
return idxs, dists

test/test_inrange.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Does not test leafsize
22
@testset "inrange" begin
3-
@testset "metric" for metric in [Euclidean()]
4-
@testset "tree type" for TreeType in trees_with_brute
3+
@testset "metric $Metric" for metric in [Euclidean()]
4+
@testset "tree type $TreeType" for TreeType in trees_with_brute
55
function test(data)
66
tree = TreeType(data, metric; leafsize=2)
77
dosort = true

test/test_knn.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import Distances.evaluate
44

55
@testset "knn" begin
6-
@testset "metric" for metric in [metrics; WeightedEuclidean(ones(2))]
7-
@testset "tree type" for TreeType in trees_with_brute
6+
@testset "metric $metric" for metric in [metrics; WeightedEuclidean(ones(2))]
7+
@testset "tree type $TreeType" for TreeType in trees_with_brute
88
function test(data)
99
tree = TreeType(data, metric; leafsize=2)
1010

test/test_monkey.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import NearestNeighbors.MinkowskiMetric
33
# some edge case has been missed in the real tests
44

55

6-
@testset "metric" for metric in fullmetrics
7-
@testset "tree type" for TreeType in trees_with_brute
8-
@testset "type" for T in (Float32, Float64)
6+
@testset "metric $metric" for metric in fullmetrics
7+
@testset "tree type $TreeType" for TreeType in trees_with_brute
8+
@testset "element type $T" for T in (Float32, Float64)
99
@testset "knn monkey" begin
1010
# Checks that we find existing point in the tree
1111
# and that it is the closest

0 commit comments

Comments
 (0)