Hi, I am trying to use multi-threading to parallelise a simple loop. The actual code is quite complex and I have given a simplified example.
The multithreaded version runs slower than the single thread version. I have probably not used the @threads
macro correctly (and I am not sure whether I need to introduce any locks as outputs are stored to arrays).
How can I improve performance of the multi-threaded version? Is this the right way of using multi-threading?
using LinearAlgebra, CSV, DataFrames, BenchmarkTools
function generate_data(m)
Values = rand(20.0:140.0, m)
return Values
end
function summation(Values)
A= cumsum(Values, dims =1)
return A
end
function some_thing(Values)
B = sum(Values, dims =1)
return B
end
function run_singlethread(m,n)
V_id = Array{Float64,2}(undef, m,n)
A_id = Array{Float64,2}(undef, m,n)
B_id = Vector{Float64}(undef,n)
for i in 1:n
Values = generate_data(m)
A = summation(Values)
B = some_thing(Values)
V_id[:,i] = Values
A_id[:,i] = A
B_id[i] = B[1]
end
df1 = DataFrame(V_id)
df2 = DataFrame(A_id)
df3 = DataFrame(ID=1:n,some_thing = B_id)
CSV.write("DataFrame1.csv",df1)
CSV.write("DataFrame2.csv",df2)
CSV.write("DataFrame3.csv",df3)
return A_id, V_id, B_id
end
function multithread_run(m,n)
V_id = Array{Float64,2}(undef, m,n)
A_id = Array{Float64,2}(undef, m,n)
B_id = Vector{Float64}(undef,n)
Threads.@threads for i in 1:n
Values = generate_data(m)
A = summation(Values)
B = some_thing(Values)
V_id[:,i] = Values
A_id[:,i] = A
B_id[i] = B[1]
end
df1 = DataFrame(V_id)
df2 = DataFrame(A_id)
df3 = DataFrame(ID=1:n,some_thing = B_id)
CSV.write("DataFrame1mthreads.csv",df1)
CSV.write("DataFrame2mthreads.csv",df2)
CSV.write("DataFrame3mthreads.csv",df3)
return A_id, V_id, B_id
end
Run-time code
@btime run_singlethread(3,10000) #48.490 ms
@btime multithread_run(3,10000) #49.847 ms