# InvalidIRError with CuArray broadcast when complex values are involved in Bessel functions

**URL:** <https://discourse.julialang.org/t/invalidirerror-with-cuarray-broadcast-when-complex-values-are-involved-in-bessel-functions/91312>\
**Category:** GPU\
**Tags:** specialfunctions\
**Created:** [December 6, 2022, 4:07pm UTC](https://discourse.julialang.org/t/invalidirerror-with-cuarray-broadcast-when-complex-values-are-involved-in-bessel-functions/91312 "2022-12-06T16:07:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [December 6, 2022, 4:07pm UTC](https://discourse.julialang.org/t/invalidirerror-with-cuarray-broadcast-when-complex-values-are-involved-in-bessel-functions/91312/1 "2022-12-06T16:07:31Z")

</div>

Hi, I’m receiving runtime errors from julia when playing with the following simple code

```julia
> 
> julia> using CUDA, SpecialFunctions
> 
> julia> function cutest(x::Float64,f::Float64)
> c=besselj0(f);
> return x+c
> end
> cutest (generic function with 1 method)
> 
> julia> x=CuVector(rand(10));
> 
> julia> y=cutest.(x,1.0);
> 
> julia> y
> 10-element CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}:
> 1.2543758351300334
> 1.6969145783025747
> 0.772833790043381
> 1.5466838680623587
> 0.9814961041987019
> 1.3892472218597938
> 1.5766825893406144
> 1.3715270039175633
> 1.070043997005175
> 1.4994660984390684
> 
> julia> function cutest(x::Float64,f::ComplexF64)
> c=besselj0(f);
> return x+c
> end
> cutest (generic function with 2 methods)
> 
> julia> y=cutest.(x,1.0+0.0im);
> ERROR: InvalidIRError: compiling kernel #broadcast_kernel#17(CUDA.CuKernelContext, CuDeviceVector{ComplexF64, 1}, Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Tuple{Base.OneTo{Int64}}, typeof(cutest), Tuple{Base.Broadcast.Extruded{CuDeviceVector{Float64, 1}, Tuple{Bool}, Tuple{Int64}}, ComplexF64}}, Int64) resulted in invalid LLVM IR
> Reason: unsupported call to the Julia runtime (call to jl_lazy_load_and_lookup)

```

It looks like the `besselj0` function is giving some trouble when argument is complex. If I replace it with, e.g., `exp`, there is no error. How do I avoid this error? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [December 6, 2022, 10:01pm UTC](https://discourse.julialang.org/t/invalidirerror-with-cuarray-broadcast-when-complex-values-are-involved-in-bessel-functions/91312/2 "2022-12-06T22:01:25Z")

</div>

That function is only implemented for Float32/Float64: [CUDA.jl/special\_math.jl at 49902d8812a7796da313c39e4e9fe6d84a7309e9 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/blob/49902d8812a7796da313c39e4e9fe6d84a7309e9/src/device/intrinsics/special_math.jl#L32-L33). I’m not familiar with the Bessel function; if it’s possible to express the operation on ComplexF32/F64 in terms of these that would make for a great addition to CUDA.jl.

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [December 8, 2022, 12:47am UTC](https://discourse.julialang.org/t/invalidirerror-with-cuarray-broadcast-when-complex-values-are-involved-in-bessel-functions/91312/3 "2022-12-08T00:47:51Z")

</div>

Ah, I did not realize that all elementary functions were implemented separately for the GPU, not really calling the package I’m using.
