Skip to content

Commit

Permalink
Threading improvements (#493)
Browse files Browse the repository at this point in the history
* better safety in nested threading

* SpinLock brings back the speeeed

* NEWS entry

(cherry picked from commit 624805c)
  • Loading branch information
palday committed Mar 26, 2021
1 parent d211f9c commit 72aacc6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ MixedModels v3.5.0 Release Notes
* The Progressbar for `parametricbootstrap` and `replicate` is not displayed
when in a non-interactive (i.e. logging) context. The progressbar can also
be manually disabled with `hide_progress=true`.[#495]
* Threading in `parametricbootstrap` now uses a `SpinLock` instead of a `ReentrantLock`.
This improves performance, but care should be taken when nesting spin locks. [#493]
* Single-threaded use of `paramatricbootstrap` now works when nested within a larger
multi-threaded context (e.g. `Threads.@threads for`). (Multi-threaded `parametricbootstrap`
worked and continues to work within a nested threading context.) [#493]

MixedModels v3.4.1 Release Notes
========================
Expand Down Expand Up @@ -166,4 +171,5 @@ Package dependencies
[#486]: https://github.com/JuliaStats/MixedModels.jl/issues/486
[#489]: https://github.com/JuliaStats/MixedModels.jl/issues/489
[#490]: https://github.com/JuliaStats/MixedModels.jl/issues/490
[#493]: https://github.com/JuliaStats/MixedModels.jl/issues/493
[#495]: https://github.com/JuliaStats/MixedModels.jl/issues/495
24 changes: 15 additions & 9 deletions src/bootstrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ families with a dispersion parameter.
`hide_progress` can be used to disable the progress bar. Note that the progress
bar is automatically disabled for non-interactive (i.e. logging) contexts.
Note that `use_threads=true` may not offer a performance boost and may even
decrease peformance if multithreaded linear algebra (BLAS) routines are available.
In this case, threads at the level of the linear algebra may already occupy all
processors/processor cores. There are plans to provide better support in coordinating
Julia- and BLAS-level threads in the future.
!!! note
Note that `use_threads=true` may not offer a performance boost and may even
decrease peformance if multithreaded linear algebra (BLAS) routines are available.
In this case, threads at the level of the linear algebra may already occupy all
processors/processor cores. There are plans to provide better support in coordinating
Julia- and BLAS-level threads in the future.
!!! warning
The PRNG shared between threads is locked using [`Threads.SpinLock`](@ref), which
should not be used recursively. Do not wrap `parametricbootstrap` in an outer `SpinLock`.
"""
function parametricbootstrap(
rng::AbstractRNG,
Expand Down Expand Up @@ -88,11 +93,12 @@ function parametricbootstrap(
# we use locks to guarantee thread-safety, but there might be better ways to do this for some RNGs
# see https://docs.julialang.org/en/v1.3/manual/parallel-computing/#Side-effects-and-mutable-function-arguments-1
# see https://docs.julialang.org/en/v1/stdlib/Future/index.html
rnglock = ReentrantLock()
rnglock = Threads.SpinLock()
samp = replicate(n; use_threads=use_threads, hide_progress=hide_progress) do
mod = m_threads[Threads.threadid()]
local βsc = βsc_threads[Threads.threadid()]
local θsc = θsc_threads[Threads.threadid()]
tidx = use_threads ? Threads.threadid() : 1
mod = m_threads[tidx]
local βsc = βsc_threads[tidx]
local θsc = θsc_threads[tidx]
lock(rnglock)
mod = simulate!(rng, mod, β = β, σ = σ, θ = θ)
unlock(rnglock)
Expand Down
2 changes: 1 addition & 1 deletion test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ end
str = String(take!(io))
@test !isempty(findall("PC1", str))
@test !isempty(findall("load: yes", str))
end
end

0 comments on commit 72aacc6

Please sign in to comment.