Skip to content

Commit

Permalink
fix: eliminate threadid()
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Oct 16, 2024
1 parent b12efba commit 41e414a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Stuffing"
uuid = "4175e07e-e5b7-423e-8796-3ea7f6d48281"
authors = ["guoyongzhi <[email protected]>"]
version = "0.10.1"
version = "0.10.2"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
25 changes: 15 additions & 10 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,21 @@ function filttrain!(qtrees, inpool, outpool, nearlevel2; optimiser,
colist = Vector{QTrees.CoItem}()
sl1 = Threads.SpinLock()
sl2 = Threads.SpinLock()
Threads.@threads for (i1, i2) in inpool |> shuffle!
que = queue[Threads.threadid()]
cp = QTrees._collision_randbfs(qtrees[i1], qtrees[i2], empty!(que))
if cp[1] >= nearlevel2
if outpool !== nothing
@Base.lock sl1 push!(outpool, (i1, i2))
end
if cp[1] > 0
@Base.lock sl2 push!(colist, (i1, i2) => cp)
nc1 += 1
shuffle!(inpool)
nchunks = min(length(queue), max(1, length(inpool)÷4))
Threads.@threads for ichunk in 1:nchunks
que = @inbounds queue[ichunk]
for ind in ichunk : nchunks : length(inpool)
i1, i2 = inpool[ind]
cp = QTrees._collision_randbfs(qtrees[i1], qtrees[i2], empty!(que))
if cp[1] >= nearlevel2
if outpool !== nothing
@Base.lock sl1 push!(outpool, (i1, i2))
end
if cp[1] > 0
@Base.lock sl2 push!(colist, (i1, i2) => cp)
nc1 += 1
end
end
end
end
Expand Down
38 changes: 23 additions & 15 deletions src/qtree_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,42 @@ function collision(Q1::AbstractStackedQTree, Q2::AbstractStackedQTree)
end
const CoItem = Pair{Tuple{Int,Int}, Index}
const AbstractThreadQueue = AbstractVector{<:AbstractVector{Index}}
thread_queue() = [Vector{Tuple{Int,Int,Int}}() for i = 1:Threads.nthreads()]
thread_queue() = [Vector{Tuple{Int,Int,Int}}() for i = 1:2Threads.nthreads()]
# assume inkernelbounds(qtree, at) is true
function _totalcollisions_native(qtrees::AbstractVector, copairs;
colist=Vector{CoItem}(),
queue::AbstractThreadQueue=thread_queue(),
at=(length(qtrees[1]), 1, 1))
sl = Threads.SpinLock()
Threads.@threads for (i1, i2) in copairs
que = @inbounds queue[Threads.threadid()]
empty!(que)
push!(que, at)
cp = @inbounds _collision_randbfs(qtrees[i1], qtrees[i2], que)
if cp[1] >= 0
@Base.lock sl push!(colist, (i1, i2) => cp)
nchunks = min(length(queue), max(1, length(copairs)÷4))
Threads.@threads for ichunk in 1:nchunks
que = @inbounds queue[ichunk]
for ind in ichunk : nchunks : length(copairs)
i1, i2 = copairs[ind]
empty!(que)
push!(que, at)
cp = @inbounds _collision_randbfs(qtrees[i1], qtrees[i2], que)
if cp[1] >= 0
@Base.lock sl push!(colist, (i1, i2) => cp)
end
end
end
colist
end
function _totalcollisions_native(qtrees::AbstractVector, coitems::Vector{CoItem};
colist=Vector{CoItem}(), queue::AbstractThreadQueue=thread_queue())
sl = Threads.SpinLock()
Threads.@threads for ((i1, i2), at) in coitems
que = @inbounds queue[Threads.threadid()]
empty!(que)
push!(que, at)
cp = @inbounds _collision_randbfs(qtrees[i1], qtrees[i2], que)
if cp[1] >= 0
@Base.lock sl push!(colist, (i1, i2) => cp)
nchunks = min(length(queue), max(1, length(coitems)÷4))
Threads.@threads for ichunk in 1:nchunks
que = @inbounds queue[ichunk]
for ind in ichunk : nchunks : length(coitems)
(i1, i2), at = coitems[ind]
empty!(que)
push!(que, at)
cp = @inbounds _collision_randbfs(qtrees[i1], qtrees[i2], que)
if cp[1] >= 0
@Base.lock sl push!(colist, (i1, i2) => cp)
end
end
end
colist
Expand Down

0 comments on commit 41e414a

Please sign in to comment.