Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocations in the sum over a product of besselj(::Int64, ::Float32) #472

Open
yakovbraver opened this issue May 1, 2024 · 0 comments
Open

Comments

@yakovbraver
Copy link

Consider summing a product of three besselj(n, x)s when the type of n is Int64 (the default Int on a 64-bit machine) and type of x is Float32:

import SpecialFunctions.besselj as J
function jtest()
    n = 1
    x = 1.0f0
    sum(J(n+q, x) * J(n+q, x) * J(n+q, x) for q in 1:2)
end

This code allocates:

@time jtest() # 0.000004 seconds (8 allocations: 128 bytes)

The type of the final result is inferred as Union{Float32, Float64} instead of Float32:

Arguments
  #self#::Core.Const(jtest)
Locals
  #19::var"#19#20"{Float32, Int64}
  x::Float32
  n::Int64
Body::Union{Float32, Float64}
1 ─       (n = 1)
│         (x = 1.0f0)
│   %3  = Main.:(var"#19#20")::Core.Const(var"#19#20")
│   %4  = Core.typeof(x::Core.Const(1.0f0))::Core.Const(Float32)
│   %5  = Core.typeof(n::Core.Const(1))::Core.Const(Int64)
│   %6  = Core.apply_type(%3, %4, %5)::Core.Const(var"#19#20"{Float32, Int64})
│   %7  = x::Core.Const(1.0f0)
│         (#19 = %new(%6, %7, n::Core.Const(1)))%9  = #19::Core.Const(var"#19#20"{Float32, Int64}(1.0f0, 1))%10 = (1:2)::Core.Const(1:2)
│   %11 = Base.Generator(%9, %10)::Core.Const(Base.Generator{UnitRange{Int64}, var"#19#20"{Float32, Int64}}(var"#19#20"{Float32, Int64}(1.0f0, 1), 1:2))
│   %12 = Main.sum(%11)::Union{Float32, Float64}
└──       return %12

One workaround is to cast the first argument to Int32, i.e. change J(n+q, x) to J(Int32(n+q), x).
When summing products of only two Bessels, the problem does not appear.

BTW, besselj from Bessels.jl does not suffer from this issue 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant