Hi,
I am trying to do some calculation which involves large array (float64 arrays with length 2^30). The array is large but I think still acceptable. However, when I tried to run the codes within some loops, the memory allocation increases quickly and it easily leads to out of memory error. I wonder if there is any way to reduce the memory allocation. A sample code is below
using LinearAlgebra
using StaticArrays
using StatsBase
using Statistics
using Random
using JLD
using Tullio
using Plots
using Printf
function recursion_node(sigmas,taus,theta1,theta2)
M_SS_S =@SVector[(5/32+3*cos(2*theta1+2*theta2)/32),0,0,9*(1-cos(2*theta1+2*theta2))/32]
M_TT_S =@SVector[15/32+9*cos(2*theta1+2*theta2)/32,0,0,3*(1-cos(2*theta1+2*theta2))/32]
M_ST_T=@SVector[0,3/32+cos(2*theta1)/16+cos(2*theta2)/16+cos(2*theta1-2*theta2)/32,3/32-cos(2*theta1)/16-cos(2*theta2)/16+cos(2*theta1-2*theta2)/32,(1-cos(2*theta1-2*theta2))/8]
M_TS_T=@SVector[0,3/32+cos(2*theta1)/16+cos(2*theta2)/16+cos(2*theta1-2*theta2)/32,3/32-cos(2*theta1)/16-cos(2*theta2)/16+cos(2*theta1-2*theta2)/32,(1-cos(2*theta1-2*theta2))/8]
M_TT_T=@SVector[0,3/16+cos(2*theta1)/8+cos(2*theta2)/8+cos(2*theta1-2*theta2)/16,3/16-cos(2*theta1)/8-cos(2*theta2)/8+cos(2*theta1-2*theta2)/16,0]
sigmas_new = [ M_SS_S[i] * kron(sigmas, sigmas)+ M_TT_S[i] * kron(taus, taus) for i in 1:4 ]
sigmas_new = vcat(sigmas_new...)
taus_new = [ M_ST_T[i] * kron(sigmas, taus) + M_TS_T[i] * kron(taus, sigmas) + M_TT_T[i] * kron(taus, taus) for i in 1:4 ]
taus_new = vcat(taus_new...)
return sigmas_new,taus_new
end
function dynamics(theta1,theta2)
sigmas = [1/4]
taus =[3/4]
for i in 1:4
sigmas, taus = recursion_node(sigmas, taus,theta1,theta2)
ind = sigmas+taus .> 1e-16
sigmas = sigmas[ind]
taus = taus[ind]
sigmas = sigmas./(sigmas.+taus)
taus = taus./(sigmas.+taus)
end
return sigmas,taus
end
function main()
pool = zeros(99*2,2)
t = 1
for i in 0:1
for j in 1:1
theta1 = i>j ? i*pi/(2*99) : pi-j*pi/(2*99)
theta2 = i>j ? j*pi/(2*99) : pi*i/(2*99)
sigma,tau = dynamics(theta1,theta2)
order = sum(sigma.^2+tau.^2)/sum(sigma+tau)
purity = sum(sigma.^2+tau.^2/3)/sum(sigma+tau)
pool[t,1] = order
pool[t,2] = purity
t= t+1
end
end
save("/xxx.xx","pool",pool) #my save path
return #pool
end
@time main();
For instance, when I try to run the code above with i
goes from 0 to 1 and j
is just 1, the memory allocation is
94.263461 seconds (6.74 k allocations: 134.412 GiB, 8.81% gc time, 0.03% compilation time)
Is there a way to reduce the memory allocation? Thanks