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

Extract function to fix NHS on macOS ARM #137

Merged
merged 4 commits into from
May 10, 2023
Merged
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
32 changes: 21 additions & 11 deletions src/sph/neighborhood_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,7 @@ function update!(neighborhood_search::SpatialHashingSearch, coords_fun)
# Find all cells containing particles that now belong to another cell.
# `collect` the keyset to be able to loop over it with `@threaded`.
@threaded for cell in collect(keys(hashtable))
for particle in hashtable[cell]
if cell_coords_(particle) != cell
# Mark this cell and continue with the next one.
#
# `cell_buffer` is preallocated,
# but only the entries 1:i are used for this thread.
i = cell_buffer_indices[Threads.threadid()] += 1
cell_buffer[i, Threads.threadid()] = cell
break
end
end
mark_changed_cell!(neighborhood_search, cell, coords_fun)
end

# This is needed to prevent lagging on macOS ARM.
Expand Down Expand Up @@ -171,6 +161,26 @@ function update!(neighborhood_search::SpatialHashingSearch, coords_fun)
return neighborhood_search
end

# Use this function barrier and unpack inside to avoid passing closures to Polyester.jl
# with @batch (@threaded).
# Otherwise, @threaded does not work here with Julia ARM on macOS.
# See https://github.com/JuliaSIMD/Polyester.jl/issues/88.
@inline function mark_changed_cell!(neighborhood_search, cell, coords_fun)
@unpack hashtable, cell_buffer, cell_buffer_indices = neighborhood_search

for particle in hashtable[cell]
if cell_coords(coords_fun(particle), neighborhood_search) != cell
# Mark this cell and continue with the next one.
#
# `cell_buffer` is preallocated,
# but only the entries 1:i are used for this thread.
i = cell_buffer_indices[Threads.threadid()] += 1
cell_buffer[i, Threads.threadid()] = cell
break
end
end
end

@inline function eachneighbor(coords, neighborhood_search::SpatialHashingSearch{2})
cell = cell_coords(coords, neighborhood_search)
x, y = cell
Expand Down