Hello everyone,
I am currently transforming some Matlab code into Julia. Everything works fine, but I recognized a big difference in runtime. I don’t have much experience in that regard, so I started reading the guidelines of Julia. Further, I know that Matlab is highly parallelized, which I plan for my Julia code to some extent, too. But first I want my cde to run as fast as possible without parallelization.
In my code, the following function is used repeatedly and it takes rather long time to calculate (around 36 ms, evaluated with @btime)
gsm_cascade(Sc::Array{Array{Complex{Float64},2},2}, S0::Array{Array{Complex{Float64},2},2}, S1::Array{Array{Complex{Float64},2},2}, D::Diagonal{Complex{Float64},Array{Complex{Float64},1}})
S111 = S1[1,1]
S121 = S1[1,2]
S211 = S1[2,1]
S221 = S1[2,2]
S110 = S0[1,1]
S120 = S0[1,2]
S210 = S0[2,1]
S220 = S0[2,2]
I = sparse(1.0LinearAlgebra.I, size(D,1), size(D,1))
U1 = inv(I - S221*D*S110*D))
U2 = inv(I - S110*D*S221*D))
Sc[1,1] = S111 + S121*D*U2*S110*D*S211
Sc[1,2] = S121*D*U2*S120
Sc[2,1] = S210*D*U1*S211
Sc[2,2] = S220 + S210*D*U1*S221*D*S120
return Sc
end
For explanation, I have two Matrices S0 and S1, which are used to get matrix Sc. Since the matrices are constructed by four submatrices, I decided to use an Array of Arrays to represent each of S0, S1 and Sc, in order to keep track of the submatrices.
At first, I thought that taking the inverses is the most time consuming part, but actually the matrix multiplications and additions of
Sc[1,1] = S111 + S121*D*U2*S110*D*S211
Sc[1,2] = S121*D*U2*S120
Sc[2,1] = S210*D*U1*S211
Sc[2,2] = S220 + S210*D*U1*S221*D*S120
are the slow part of the function. Does anybody know how I can accelerate this function? The biggest matrix is S111, which is currently 32 by 32. Or would be using parallelized code the only option here?
Further, I recognized that
S111 = S1[1,1]
does not allocate anything on the heap . Why is that the case? Is it because the matrix S1 is aleady allocated and fed into the function?
Thank you in advance and have a nice day
Jonas