# An optimization problem on GPU works in Julia v1.8, but does not work in v1.9

**URL:** <https://discourse.julialang.org/t/an-optimization-problem-on-gpu-works-in-julia-v1-8-but-does-not-work-in-v1-9/100036>\
**Category:** GPU\
**Tags:** cuda, optim\
**Created:** [June 8, 2023, 6:12am UTC](https://discourse.julialang.org/t/an-optimization-problem-on-gpu-works-in-julia-v1-8-but-does-not-work-in-v1-9/100036 "2023-06-08T06:12:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DennisFang](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@DennisFang](https://discourse.julialang.org/u/DennisFang)\
**Post date:** [June 8, 2023, 6:12am UTC](https://discourse.julialang.org/t/an-optimization-problem-on-gpu-works-in-julia-v1-8-but-does-not-work-in-v1-9/100036/1 "2023-06-08T06:12:05Z")

</div>

Hello,

I’m doing an optimization problem and trying to work on CUDA. I can run my program in Julia v1.8.2 and v1.8.5. However, I get some errors when running on the GPU in Julia v1.9.0 and v1.9.1.

Here is a minimum example:

```julia

using CUDA, Distributions, Optim

x = rand(truncated(Normal(0, 1); lower=0.0), 100)

logL(x, p1, p2) = logpdf(truncated(Normal(p1, exp(p2)); lower=0.0), x)

obj(x, p1, p2) = -mean(logL.(CuArray(x), p1, p2))

init = ones(2)

H = TwiceDifferentiable(vars -> obj(x, vars[1], vars[2]), init)

opt = optimize(H, init, Optim.Options(iterations = 10, show_trace=true))

```

And here are errors when running the last line of the above example in Julia v1.9.0 and v1.9.1:

```julia

ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#broadcast_kernel#26")(::CUDA.CuKernelContext, ::CuDeviceVector{Float64, 1}, ::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Tuple{Base.OneTo{Int64}}, typeof(logL), Tuple{Base.Broadcast.Extruded{CuDeviceVector{Float64, 1}, Tuple{Bool}, Tuple{Int64}}, Float64, Float64}}, ::Int64) resulted in invalid LLVM IR

Reason: unsupported dynamic function invocation (call to var"#setprecision#25"(kws::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}, ::typeof(setprecision), f::Function, ::Type{T}, prec::Integer) where T @ Base.MPFR mpfr.jl:969)

```

The above errors point out `Reason: unsupported dynamic function`. I’m not sure whether it is a bug of Julia v1.9.x or not. How can I solve the problem?

---

<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:** [June 8, 2023, 3:45pm UTC](https://discourse.julialang.org/t/an-optimization-problem-on-gpu-works-in-julia-v1-8-but-does-not-work-in-v1-9/100036/2 "2023-06-08T15:45:26Z")

</div>

Not necessarily a bug, but possibly an upstream change that introduced GPU-incompatible code. You should have included the backtrace to `setprecision`, but seeing MPFR in there, you’re attempting to use BigFloat which are GPU incompatible (as they rely on a binary C library).

---

<div class="post-metadata">

**Author:** ![DennisFang](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@DennisFang](https://discourse.julialang.org/u/DennisFang)\
**Post date:** [June 9, 2023, 4:19am UTC](https://discourse.julialang.org/t/an-optimization-problem-on-gpu-works-in-julia-v1-8-but-does-not-work-in-v1-9/100036/3 "2023-06-09T04:19:47Z")

</div>

I try to check the type problem via ` @device_code_warntype`, `Cthulhu.jl` and its macro `@descend_code_typed`. They show all variables are stable in work.

After that, I guess the problem may arise from the broadcast operator, so I merge `logL` and `obj` into a function as follows:

```julia
obj(x, p1, p2) = -mean(logpdf.(truncated(Normal(p1, exp(p2)); lower=0.0), CuArray(x)))

```

In this way, Julia v1.9.0 and v1.9.1 can work.

Although combining functions can avoid the above problem, I wonder whether there exists any way that splitting functions works fine, or if this is a problem because the way of broadcast identification is changed.
