Skip to content

Commit 9ee7f0c

Browse files
committed
Merge branch 'SpatialHashingThreadSafe' into SpatialHashingBackend
2 parents ce48889 + 9eefdc2 commit 9ee7f0c

File tree

3 files changed

+36
-69
lines changed

3 files changed

+36
-69
lines changed

src/cell_lists/spatial_hashing.jl

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ function push_cell_atomic!(cell_list::SpatialHashingCellList, cell, point)
104104

105105
cell_coord = @inbounds coords[hash_key]
106106
if cell_coord == ntuple(_ -> typemin(Int), Val(NDIMS))
107-
# Throws `bitcast: value not a primitive type`-error
108107
@inbounds Atomix.@atomic coords[hash_key] = cell_coord_hash
109108
# If this cell is not used yet, set cell coordinates
110-
# @inbounds coords[hash_key] = cell_coord_hash
111109
elseif cell_coord != cell_coord_hash
112110
# If it is already used by a different cell, mark as collision
113111
@inbounds Atomix.@atomic collisions[hash_key] = true
@@ -163,46 +161,16 @@ end
163161
check_cell_bounds(cell_list, spatial_hash(cell, cell_list.list_size))
164162
end
165163

164+
# Compute a compact 128-bit hash by reinterpreting each coordinate as a UInt32
165+
# and bit-shifting them into a UInt128 slot.
166166
function coordinates_hash(cell_coordinate)
167-
# Check the dimensionality of the coordinate since we can not stuff more the 3 UInt32 in a UInt128
168-
@assert length(cell_coordinate) <= 4
169-
170-
function coords2uint(hash::UInt128, coord::Int, n::Int)
171-
ua = reinterpret(UInt32, Int32(coord))
172-
return (UInt128(ua) << (n * 32)) | hash
173-
end
167+
# size check
168+
@assert length(cell_coordinate) <= 3
174169

175170
hash = UInt128(0)
176171
for (i, coord) in enumerate(cell_coordinate)
177-
hash = coords2uint(hash, coord, i-1)
172+
ucoord = reinterpret(UInt32, Int32(coord))
173+
hash = (UInt128(ucoord) << ((i-1) * 32)) | hash
178174
end
179175
return hash
180-
end
181-
182-
# function coordinates_hash_10(cell_coordinate)
183-
# shift10 = 0
184-
# hash = Int128(0)
185-
186-
# function shift_by_10(x, n::Int)
187-
# @assert n >= 0
188-
# x = Int128(x)
189-
# for _ in 1:n
190-
# # multiply by 10 with binary shift operations
191-
# x = (x << 3) + (x << 1)
192-
# end
193-
# return x
194-
# end
195-
196-
# for i in reverse(1:length(cell_coordinate))
197-
# coord = cell_coordinate[i]
198-
199-
# # shift coord `shift` many times by 10 and add up
200-
# hash = hash + shift_by_10(coord, shift10)
201-
202-
# # compute the shift for the next iteration
203-
# shift10 += length(string(abs(coord)))
204-
205-
# end
206-
207-
# return Int(hash)
208-
# end
176+
end

src/gpu.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#
99
# `Adapt.@adapt_structure` automatically generates the `adapt` function for our custom types.
1010
Adapt.@adapt_structure FullGridCellList
11-
Adapt.@adapt_structure SpatialHashingCellList
1211
Adapt.@adapt_structure DynamicVectorOfVectors
1312

1413
# `adapt(CuArray, ::SVector)::SVector`, but `adapt(Array, ::SVector)::Vector`.

test/cell_lists/spatial_hashing.jl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,38 @@
3232
@test neighbors == [1]
3333
end
3434

35-
# @testset "Collision Handling With Non-Empty Cells" begin
36-
# # Cell (-1, 0) with point 1 has a hash collision with cell (-2, -1) with point 2
37-
# coordinates = [[-0.05 -0.15]; [0.05 -0.05]]
38-
# NDIMS = size(coordinates, 1)
39-
# n_points = size(coordinates, 2)
40-
# search_radius = 0.1 + 10 * eps()
41-
# point_index = 1
35+
@testset "Collision Handling With Non-Empty Cells" begin
36+
# Cell (-1, 0) with point 1 has a hash collision with cell (-2, -1) with point 2
37+
coordinates = [[-0.05 -0.15]; [0.05 -0.05]]
38+
NDIMS = size(coordinates, 1)
39+
n_points = size(coordinates, 2)
40+
search_radius = 0.1 + 10 * eps()
41+
point_index = 1
4242

43-
# nhs = GridNeighborhoodSearch{2}(; search_radius, n_points,
44-
# cell_list = SpatialHashingCellList{NDIMS}(n_points))
45-
# initialize_grid!(nhs, coordinates)
43+
nhs = GridNeighborhoodSearch{2}(; search_radius, n_points,
44+
cell_list = SpatialHashingCellList{NDIMS}(n_points))
45+
initialize_grid!(nhs, coordinates)
4646

47-
# @testset "Test For Collision" begin
48-
# cell1 = (-1, 0)
49-
# cell2 = (-2, -1)
50-
# cell1_hash = PointNeighbors.spatial_hash(cell1, n_points)
51-
# cell2_hash = PointNeighbors.spatial_hash(cell2, n_points)
52-
# points1 = nhs.cell_list[cell1]
53-
# points2 = nhs.cell_list[cell2]
47+
@testset "Test For Collision" begin
48+
cell1 = (-1, 0)
49+
cell2 = (-2, -1)
50+
cell1_hash = PointNeighbors.spatial_hash(cell1, n_points)
51+
cell2_hash = PointNeighbors.spatial_hash(cell2, n_points)
52+
points1 = nhs.cell_list[cell1]
53+
points2 = nhs.cell_list[cell2]
5454

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

59-
# neighbors = [Int[] for _ in axes(coordinates, 2)]
60-
# foreach_point_neighbor(coordinates, coordinates, nhs,
61-
# points = axes(coordinates, 2)) do point, neighbor, pos_diff,
62-
# distance
63-
# push!(neighbors[point], neighbor)
64-
# end
59+
neighbors = [Int[] for _ in axes(coordinates, 2)]
60+
foreach_point_neighbor(coordinates, coordinates, nhs,
61+
points = axes(coordinates, 2)) do point, neighbor, pos_diff,
62+
distance
63+
push!(neighbors[point], neighbor)
64+
end
6565

66-
# @test neighbors[1] == [1]
67-
# @test neighbors[2] == [2]
68-
# end
66+
@test neighbors[1] == [1]
67+
@test neighbors[2] == [2]
68+
end
6969
end

0 commit comments

Comments
 (0)