Hello, I am trying to optimize a code for simulating fluids that is currently allocating massive amounts of memory. I’m focusing on some specific functions that are simple but need to be called repeatedly (thousands of times), so they should not allocate anything. I’m finding that my functions, which take arrays and scalars and do basic operations, are performing small allocations for each scalar argument provided, and again for the result. This is a minimal example:
function f(a, b1, b2, b3, c)
return a[1]*b1 + b2*(c[2,1]*a[2]^3) + b3*a[3]
end
a = rand(4)
b1 = 1.0
b2 = 2.0
b3 = 3.0
c = rand(3,3)
@btime f($a, $b1, $b2, $b3, $c)
I expect this to not allocate memory at any point. However, I get:
47.861 ns (4 allocations: 64 bytes)
Why do I get 4 allocations? Are these allocations really a concern, or should I focus my attention elsewhere in the code?
Just in case, this is the actual function I’m trying to optimize, which comes from Symbolics.build_function:
Aabc_functions333 = function (ˍ₋arg1, ˍ₋arg2, χ_1, χ_2, μ, ν, ψ_1, ψ_2, ˍ₋arg9, ˍ₋arg10, ˍ₋arg11, ˍ₋arg12)
#= /home/pablo/.julia/packages/Symbolics/eUt8E/src/build_function.jl:143 =# @inbounds begin
#= /home/pablo/.julia/packages/Symbolics/eUt8E/src/build_function.jl:143 =#
begin
(+)((+)((+)((+)((+)((+)((/)((*)((*)((*)(-6, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ˍ₋arg1[4]), χ_1), (^)(μ, 4)), (/)((*)((*)((*)(12.0, ˍ₋arg1[4]), ˍ₋arg2[16]), χ_2), (^)(μ, 4))), (/)((*)((*)((*)(-96.0, (+)((*)((*)(-0.5, ˍ₋arg12[16]), ν), (*)((*)(2, ˍ₋arg9[4]), ˍ₋arg1[4]))), ˍ₋arg1[4]), χ_2), (^)(μ, 5))), (/)((*)((*)((*)((*)(480.0, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ν), ˍ₋arg1[4]), χ_2), (^)(μ, 6))), (/)((*)((*)(24.0, (+)((*)(0.5, (+)((*)((*)(2, ˍ₋arg12[16]), ˍ₋arg9[4]), (*)((*)(2, ˍ₋arg1[4]), ˍ₋arg2[16]))), (*)((*)(-0.5, ˍ₋arg12[16]), ˍ₋arg9[4]))), χ_2), (^)(μ, 4))), (/)((+)((*)((*)((*)(-96.0, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ˍ₋arg9[4]), χ_2), (*)((*)((*)((*)(-72.0, ˍ₋arg12[16]), ν), ˍ₋arg1[4]), χ_2)), (^)(μ, 5))), (/)((*)((*)((*)(1.5, ˍ₋arg12[16]), ˍ₋arg1[4]), χ_1), (^)(μ, 3)))
end
end
end
and this is what I’m doing to benchmark it:
χ_1 = 1.0
χ_2 = 2.0
μ = 1.0
ν = 0.5
ψ_1 = 0.3
ψ_2 = 0.7
ξᵃ = rand(4)
lᵃ = rand(4)
ηᵃ = rand(4)
ξᵃᵇ = rand(4, 4)
Mᵃᵇ = rand(4, 4)
gᵃᵇ = rand(4, 4)
@btime Aabc_functions333(
$ξᵃ, $ξᵃᵇ, $χ_1, $χ_2, $μ, $ν, $ψ_1, $ψ_2,
$lᵃ, $ηᵃ, $Mᵃᵇ, $gᵃᵇ
)
and I get:
69.661 ns (7 allocations: 112 bytes)