Skip to content

Commit 24fddaf

Browse files
committed
Resolve requested changes:
- Add `@inbounds` in `push_cell_atomic!` - Improved type dispatch for `supported_update_strategies` - Clarified and cleaned up cell list initialization and emptying, - General code cleanup.
1 parent 07420df commit 24fddaf

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

src/cell_lists/cell_lists.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ end
1212

1313
# We need the prod() because FullGridCellList's size is a tuple of cells per dimension whereas
1414
# SpatialHashingCellList's size is an Integer for the number of cells in total.
15-
function construct_backend(::Type{<:AbstractCellList}, ::Type{Vector{Vector{T}}},
15+
function construct_backend(::Type{Vector{Vector{T}}},
1616
max_outer_length,
1717
max_inner_length) where {T}
1818
return [T[] for _ in 1:max_outer_length]
@@ -39,7 +39,15 @@ function construct_backend(cell_list::Type{<:AbstractCellList},
3939
max_inner_length)
4040
end
4141

42+
function max_points_per_cell(cells::DynamicVectorOfVectors)
43+
return size(cells.backend, 1)
44+
end
45+
46+
# Fallback when backend is a `Vector{Vector{T}}`. Only used for copying the cell list.
47+
function max_points_per_cell(cells)
48+
return 100
49+
end
50+
4251
include("dictionary.jl")
4352
include("full_grid.jl")
4453
include("spatial_hashing.jl")
45-
include("cell_lists_util.jl")

src/cell_lists/full_grid.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,6 @@ function copy_cell_list(cell_list::FullGridCellList, search_radius, periodic_box
192192
max_points_per_cell = max_points_per_cell(cell_list.cells))
193193
end
194194

195-
function max_points_per_cell(cells::DynamicVectorOfVectors)
196-
return size(cells.backend, 1)
197-
end
198-
199-
# Fallback when backend is a `Vector{Vector{T}}`. Only used for copying the cell list.
200-
function max_points_per_cell(cells)
201-
return 100
202-
end
203-
204195
@inline function check_cell_bounds(cell_list::FullGridCellList{<:DynamicVectorOfVectors{<:Any,
205196
<:Array}},
206197
cell::Tuple)

src/cell_lists/spatial_hashing.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ end
4040

4141
@inline Base.ndims(::SpatialHashingCellList{NDIMS}) where {NDIMS} = NDIMS
4242

43-
function supported_update_strategies(::SpatialHashingCellList{<:DynamicVectorOfVectors})
43+
function supported_update_strategies(::SpatialHashingCellList{NDIMS, CL, CI, CF}) where {NDIMS,
44+
CL <:
45+
DynamicVectorOfVectors,
46+
CI,
47+
CF}
4448
return (ParallelUpdate, SerialUpdate)
4549
end
46-
4750
function supported_update_strategies(::SpatialHashingCellList)
48-
return (SerialUpdate)
51+
return (SerialUpdate;)
4952
end
5053

5154
function SpatialHashingCellList{NDIMS}(list_size,
@@ -60,15 +63,15 @@ function SpatialHashingCellList{NDIMS}(list_size,
6063
end
6164

6265
function Base.empty!(cell_list::SpatialHashingCellList)
63-
(; list_size) = cell_list
66+
(; cells) = cell_list
6467
NDIMS = ndims(cell_list)
6568

6669
# `Base.empty!.(cells)`, but for all backends
67-
for i in eachindex(cell_list.cells)
68-
emptyat!(cell_list.cells, i)
70+
@threaded default_backend(cells) for i in eachindex(cells)
71+
emptyat!(cells, i)
6972
end
7073

71-
cell_list.coords .= [ntuple(_ -> typemin(Int), NDIMS) for _ in 1:list_size]
74+
fill!(cell_list.coords, ntuple(_->typemin(Int), NDIMS))
7275
cell_list.collisions .= false
7376
return cell_list
7477
end
@@ -102,15 +105,15 @@ function push_cell_atomic!(cell_list::SpatialHashingCellList, cell, point)
102105
@boundscheck check_cell_bounds(cell_list, hash_key)
103106
@inbounds pushat_atomic!(cells, hash_key, point)
104107

105-
cell_coord = coords[hash_key]
108+
cell_coord = @inbounds coords[hash_key]
106109
if cell_coord == ntuple(_ -> typemin(Int), NDIMS)
110+
# Throws `bitcast: value not a primitive type`-error
111+
# @inbounds Atomix.@atomic coords[hash_key] = cell
107112
# If this cell is not used yet, set cell coordinates
108-
# Atomix.@atomic coords[hash_key] = cell
109-
coords[hash_key] = cell
113+
@inbounds coords[hash_key] = cell
110114
elseif cell_coord != cell
111115
# If it is already used by a different cell, mark as collision
112-
# Atomix.@atomic collisions[hash_key] = true
113-
collisions[hash_key] = true
116+
@inbounds Atomix.@atomic collisions[hash_key] = true
114117
end
115118
end
116119

@@ -125,8 +128,6 @@ function copy_cell_list(cell_list::SpatialHashingCellList, search_radius,
125128
(; list_size) = cell_list
126129
NDIMS = ndims(cell_list)
127130

128-
# Here I'm using max_points_per_cell which is defined in src/cell_lists/full_grid.jl
129-
# Think about putting it somewhere all cell list can access it or copying it here
130131
return SpatialHashingCellList{NDIMS}(list_size, typeof(cell_list.cells),
131132
max_points_per_cell(cell_list.cells))
132133
end

src/nhs_grid.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ end
379379

380380
# Fully parallel incremental update with atomic push.
381381
# TODO `cell_list.cells.lengths` and `cell_list.cells.backend` are hardcoded
382-
# for `FullGridCellList` and `SpatialHashingCellList`, which are currently
383-
# the only implementations supporting this update strategy.
382+
# for `FullGridCellList`, which is currently the only implementation
383+
# supporting this update strategy.
384384
function update_grid!(neighborhood_search::GridNeighborhoodSearch{<:Any,
385385
ParallelIncrementalUpdate},
386386
y::AbstractMatrix; parallelization_backend = default_backend(y),

test/cell_lists/spatial_hashing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
points1 = nhs.cell_list[cell1]
5353
points2 = nhs.cell_list[cell2]
5454

55-
@test points1 == points2 == [1, 2]
55+
@test sort(points1) == sort(points2) == [1, 2]
5656
@test cell1_hash == cell2_hash
5757
end
5858

0 commit comments

Comments
 (0)