I've used Julia for some time, but still know little about it, especially for the paralell computing. I want to obtain a new array with the existing data, as the array is very large, I want to do it in a parallel way, and the code is written as follows:
ψ1 = Array{Complex}(undef, D)
ψ = rand(Complex{Float64},D)
Threads.@threads for k = 1:D
ψ1[k] = @views sum(ψ[GetBasis(k - 1, N)])
end
I run it with julia -t 4, But it turn out to be very slow, compared to the non parallel code as follows,
ψ1 = [@views sum(ψ[GetBasis(k - 1, N)]) for k=1:D]
I have no idea about why this happens, and GetBasis() is just a function to generate a Array, Array{Int64,1}(N).
I would like to ask how I could improve the first code, or, is there some way I can modify the second code to also run it in a parallel way? As the array can be very large, and I want to find a way to speed it up...
Thanks a lot, and look forward to your replies!
The complete code can be found as follows
function GetBasis(n, N)
xxN = collect(0:N-1)
BI = BitFlip.(n,xxN).+1
end
function BitFlip(n, i)
n = n ⊻ (1 << i)
end
N=24
D=2^N
ψ1 = Array{Complex}(undef, D)
ψ = rand(Complex{Float64},D)
Threads.@threads for k = 1:D
ψ1[k] = @views sum(ψ[GetBasis(k - 1, N)])
end
ψ2 = [@views sum(ψ[GetBasis(k - 1, N)]) for k=1:D]