I am working on a problem where we have to evaluate a function at different values of a parameter called t
, each evaluation is time consuming and involves a lot of matrix-multiplication and inversion. The rough outline of the function is given below. The key point is that it reads a few data files and initializes some parameter dependent matrices.
function G(t::Float64,M::Matrix{ComplexF64},Mp::Matrix{ComplexF64})
sysize = size(M)[1]
S = zeros(ComplexF64,sysize,sysize)
Sp = zeros(ComplexF64,sysize,sysize)
Mp = Mp * 10^-6
M = M * 10^-6
for i in range(1,sysize)
S[i,i] = M[i,i]*sinh(t)
Sp[i,i] = Mp[i,i]*sinh(im*t)
end
g = det(S*Sp*inv(S+Sp))*tr(inv(Sp*S+Sp^2))
sleep(0.25)
## The point here is ----- that finding G(t) is time consuming because of matrix calculations
return g
end
Now I need to find the value of the function for various values of t
given M
and Mp
are constant matrices defined outside the function. For accomplishing this I have defined another function as below
function calc()
t = range(start=-5,stop=5,length=100)
y = zeros(Float64,100)
for i in range(1,100)
y[i] = real(G(t[i],M,Mp))
end
end
My Aim - To parallelize the code using Distributed.jl. I know that one entry of the array y
would not depend on the other entry. Thus I would like to let different workers simultaneously fill different cells of the array.
Initial Try - I tried to break up the for loop in calc()
into 10 parts. Evaluating one part by one worker. I declared y
as an @everywhere
variable. But it did not work out as all the workers seem to be operating on their own copy of y
. Is there any way to synchronize the array y
among all workers after each worker has filled their portion of the array ?
I am extremely new to parallel computing. I have tried to achieve the same using Threads.jl , the code ran successfully but the speed-up was not significant enough, besides it would not provide me with multi-cpu parallelization which is my end goal. Any pointers towards what can be done is greatly appreciated.