Skip to content

Commit

Permalink
Changed the signature of parallel near neighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
jaantollander committed Apr 7, 2021
1 parent 2678037 commit 74bcaff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


## Description
`CellLists.jl` is an algorithm that solves the fixed-radius near neighbors problem. That is, it finds all pairs of points that are within a fixed distance apart from each other. Additionally, I wrote an article [*Searching for Fixed-Radius Near Neighbors with Cell Lists*](https://jaantollander.com/post/searching-for-fixed-radius-near-neighbors-with-cell-lists-algorithm-in-julia-language/), which explores the Cell Lists algorithm and theory behind it more deeply.
`CellLists.jl` is an algorithm that solves the fixed-radius near neighbors problem. That is, it finds all pairs of points that are within a fixed distance apart from each other. Additionally, I wrote an article [**Searching for Fixed-Radius Near Neighbors with Cell Lists Algorithm in Julia Language**](https://jaantollander.com/post/searching-for-fixed-radius-near-neighbors-with-cell-lists-algorithm-in-julia-language/), which explores the Cell Lists algorithm and theory behind it more deeply.

[**Multithreading in Julia Language in Julia Language Applied to Cell Lists Algorithm**](https://jaantollander.com/post/multithreading-in-julia-language-applied-to-cell-lists-algorithm/)


## Installation
Expand All @@ -17,7 +19,7 @@ pkg> add https://github.com/jaantollander/CellLists.jl
```


## Example
## Serial Algorithm
We can use `CellLists.jl` by supplying `n`, `d`-dimensional points, and fixed radius `r` to the `CellList` constructor.

```julia
Expand All @@ -30,7 +32,7 @@ c = CellList(p, r)
By calling the `near_neighbors` function, we obtain a list of index pairs of points that are within `r` distance.

```julia
indices = near_neighbors(c)
indices = near_neighbors(c, p, r)
```

```julia
Expand All @@ -57,3 +59,18 @@ The outputs should be equal as follows:
```

On average, the Cell List algorithm is more efficient than brute force when dimensions `d` is small, the number of points `n` is sufficiently large, and radius `r` is small compared to the bounding box of the points.


## Multithreading
```julia
c = CellLists(p, r, Val(:parallel))
```

```julia
near_neighbors(c, p, r, Val(:parallel))
```


## Benchmarks
- [CellListsBenchmarks.jl](https://github.com/jaantollander/CellListsBenchmarks.jl)
- [cell-lists-benchmarks](https://github.com/jaantollander/cell-lists-benchmarks)
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ CellList(::Array{T, 2}, ::T) where T <: AbstractFloat
near_neighbors(::CellList, ::Array{T, 2}, ::T) where T <: AbstractFloat
```

## Parallel
## Multithreading
```@docs
merge(::CellList{d}, ::CellList{d}) where d
CellList(::Array{T, 2}, ::T, ::Val{:parallel}) where T <: AbstractFloat
p_near_neighbors(::CellList{d}, ::Array{T, 2}, ::T) where d where T <: AbstractFloat
near_neighbors(::CellList{d}, ::Array{T, 2}, ::T, ::Val{:parallel}) where d where T <: AbstractFloat
```
2 changes: 1 addition & 1 deletion src/CellLists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module CellLists

include("serial.jl")
include("parallel.jl")
export CellList, near_neighbors, brute_force!, distance_condition, merge, p_near_neighbors
export CellList, near_neighbors, brute_force!, distance_condition, merge

end # module
2 changes: 1 addition & 1 deletion src/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function brute_force_cell!(pts, cell, is, p, r, data, offsets)
end

"""Parallel near neighbors"""
function p_near_neighbors(c::CellList{d}, p::Array{T, 2}, r::T) where d where T <: AbstractFloat
function near_neighbors(c::CellList{d}, p::Array{T, 2}, r::T, ::Val{:parallel}) where d where T <: AbstractFloat
offsets = neighbors(d)
pts = [Vector{Tuple{Int, Int}}() for _ in 1:nthreads()]
data = collect(c.data)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function test_parallel_near_neighbors(rng::AbstractRNG, ns::Vector{Int}, ds::Vec
p = 2 .* rand(rng, n, d) .- 1.0
c = CellList(p, r)
a = near_neighbors(c, p, r)
b = p_near_neighbors(c, p, r)
b = near_neighbors(c, p, r, Val(:parallel))
@test Set(Set.(b)) == Set(Set.(a))
end
end
Expand All @@ -79,7 +79,7 @@ function test_parallel_near_neighbors_large(rng::AbstractRNG, n::Int, d::Int, r:
p = 2 .* rand(rng, n, d) .- 1.0
c = CellList(p, r)
a = near_neighbors(c, p, r)
b = p_near_neighbors(c, p, r)
b = near_neighbors(c, p, r, Val(:parallel))
@test Set(Set.(b)) == Set(Set.(a))
end

Expand Down

0 comments on commit 74bcaff

Please sign in to comment.