Hi,
I am very new to Julia and try to use multi-threading to make my code faster. If I do not use linear algebra, my code using “Thread.@threads” for the “for” loop speeds up as expected. However, when I use linear algebra in the loop, it just does not give any speed increase. (Indeed, it gets worse.) The two test codes (w or w/o linear algebra) are as follows:
Code w/o linear algebra
function sincall(x)
return(sin(x))
end
function test()
nMax = 1e2
mMax = 1e6
ax = 1.0
ay = 0.0
Threads.@threads for n = 1:nMax
tmp = 0
for m = 1:mMax
tmp = tmp + sincall(2*pi*(m*ax+m*ay))
end
end
end
test()
This gives the results as:
> time julia -t 1 test.jl
real 0m2.634s
user 0m3.085s
sys 0m4.083s
> time julia -t 4 test.jl
real 0m0.969s
user 0m3.550s
sys 0m4.114s
On the other hand, code w linear algebra:
using LinearAlgebra
BLAS.set_num_threads(1)
function test()
nMax = 1e2
mMax = 1e5
Threads.@threads for n = 1:nMax
tmp = 0
for m = 1:mMax
a = rand(10,10);
tmp = tmp + sum(a*a)
end
end
end
test()
This gives the results as:
> time julia -t 1 test2.jl
real 0m6.948s
user 0m7.345s
sys 0m4.198s
> time julia -t 4 test2.jl
real 0m19.462s
user 0m38.935s
sys 0m37.127s
Maybe I am doing something completely wrong. Does anybody have any clue to solve this issue?
Julia version is 1.8.5, running on Debian/Bookworm (on Ryzen 3970X).
Many thanks,
tj