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

fix test_threads (hopefully) #298

Merged
merged 1 commit into from
Feb 8, 2024
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
25 changes: 15 additions & 10 deletions test/test_threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
println("Testing Progress() with Threads.@threads across $threads threads")
(Threads.nthreads() == 1) && @info "Threads.nthreads() == 1, so Threads.@threads test is suboptimal"
n = 20 #per thread
threadsUsed = Int[]
threadsUsed = fill(false, threads)
vals = ones(n*threads)
p = ProgressMeter.Progress(n*threads)
p.threads_used = 1:threads # short-circuit the function `is_threading` because it is racy (#232)
Threads.@threads for i = 1:(n*threads)
!in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid())
threadsUsed[Threads.threadid()] = true
vals[i] = 0
sleep(0.1)
ProgressMeter.next!(p)
end
@test !any(vals .== 1) #Check that all elements have been iterated
@test length(threadsUsed) == threads #Ensure that all threads are used
@test all(threadsUsed) #Ensure that all threads are used


println("Testing ProgressUnknown() with Threads.@threads across $threads threads")
trigger = 100.0
prog = ProgressMeter.ProgressUnknown(desc="Attempts at exceeding trigger:")
prog.threads_used = 1:threads
vals = Float64[]
threadsUsed = Int[]
threadsUsed = fill(false, threads)
lk = ReentrantLock()
Threads.@threads for _ in 1:1000
!in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid())
threadsUsed[Threads.threadid()] = true
valssum = lock(lk) do
push!(vals, rand())
return sum(vals)
Expand All @@ -46,10 +48,11 @@
println("Testing ProgressThresh() with Threads.@threads across $threads threads")
thresh = 1.0
prog = ProgressMeter.ProgressThresh(thresh; desc="Minimizing:")
prog.threads_used = 1:threads
vals = fill(300.0, 1)
threadsUsed = Int[]
threadsUsed = fill(false, threads)
Threads.@threads for _ in 1:100000
!in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid())
threadsUsed[Threads.threadid()] = true
valssum = lock(lk) do
push!(vals, -rand())
return sum(vals)
Expand All @@ -72,20 +75,22 @@
println("Testing Progress() with Threads.@spawn across $threads threads")
n = 20 #per thread
tasks = Vector{Task}(undef, threads)
threadsUsed = Int[]
# threadsUsed = fill(false, threads)
vals = ones(n*threads)
p = ProgressMeter.Progress(n*threads)
p.threads_used = 1:threads

for t in 1:threads
tasks[t] = Threads.@spawn for i in 1:n
!in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid())
# threadsUsed[Threads.threadid()] = true
vals[(n*(t-1)) + i] = 0
sleep(0.05 + (rand()*0.1))
ProgressMeter.next!(p)
end
end
wait.(tasks)
@test !any(vals .== 1) #Check that all elements have been iterated
#@test length(threadsUsed) == threads #Ensure that all threads are used (unreliable for @spawn)
# @test all(threadsUsed) #Ensure that all threads are used (unreliable for @spawn)
else
@info "Threads.nthreads() == 1, so Threads.@spawn tests cannot be meaningfully tested"
end
Expand Down
Loading