Dear Community members:
I need advice for speeding up the following operations.
I have three arrays,
Fockmatrix, fcmatrix, densitymatrix
densitymatrix and fcmatrix are already given, and they are passed as arguments to the function. None of these arrays are sparse. Within this function, I need to use densitymatrix and fcmatrix to construct the Fockmatrix. Fockmatrix is a 3-D array, with size (a,a,b), fcmatrix is an array of size (a,a,b,b), densitymatrix is an array of size (a,a,b).
Besides, a is typically from 50-100, b is typically a few thousand
The relation between these three arrays is that for any ja and jb
Fockmatrix[ja,jb,:]=fcmatrix[ja,jb,:,:]*densitymatrix[ja,jb,:]
There are special properties for these arrays. fcmatrix is a real Float64 array, besides, it is symmetric when exchanging the first two indices or the last two indices. Besides,
desitymatrix[:,:,i] is an Hermitian matrix
So it is guranteed that
Fockmatrix[:,:,i] is an Hermitian matrix.
Below is how I am currently doing these construction.
function test_func(density_matrix::Array{ComplexF64},fcmatrix::Array{Float64})
a=50
b=1000
Fock_matrix=zeros(ComplexF64,a,a,b)
Threads.@threads for ja in 1:a
for jb in 1:ja-1
Fock_matrix[ja,jb,:]+=fcmatrix[ja,jb,:,:]*density_matrix[ja,jb,:]
end
end
Threads.@threads for ja in 1:b
Fock_matrix[:,:,ja]+=Fock_matrix[:,:,ja]'
end
Threads.@threads for ja in 1:a
Fock_matrix[ja,ja,:]+=fcmatrix[ja,ja,:,:]*density_matrix[ja,ja,:]
end
end
I am running these codes on a high-performance server, so there are multiple cores available, which is why I am using the Threads.@threads here. I am looking for advice on how to make this construction faster. Any improvements are highly appreciated!
Thanks