Skip to content

Multi threaded parallelism

zhaotianjing edited this page Mar 22, 2022 · 7 revisions

Using multi-threaded parallelism for mega trait

The number of execution threads is controlled by using the -t/--threads command-line argument (requires at least Julia 1.5). But if you're using Juno via IDE, the Juno will load all threads automatically.

For example, to start Julia with 8 julia threads:

julia --threads 8

You can check the number of julia threads by Threads.nthreads()

The number of BLAS threads LinearAlgebra.BLAS.get_num_threads() will be equal to the number of julia threads. So users don't need to set BLAS threads once you start Julia with multiple Julia threads.

In this post, setting BLAS threads as 1 may slightly improve the speed, and 1 BLAS thread is a little bit special. Users can set by LinearAlgebra.BLAS.set_num_threads(1) in your Julia code.

Threads.@threads

From Julia documentation on Threads.@threads:

"A macro to parallelize a for loop to run with multiple threads. Splits the iteration space among multiple tasks and runs those tasks on threads according to a scheduling policy. A barrier is placed at the end of the loop which waits for all tasks to finish execution."

Since a barrier is placed at the end of the loop, our implementation for megaBayesABC!() should be correct. And below is a simple test on my laptop with 6 threads:

t=ones(1000)

Threads.@threads for i in 1:1000
    t[i]=9
end

sum(t.==9).==1000 #true
Clone this wiki locally