# GPU sum closure throwing an error

**URL:** <https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658>\
**Category:** GPU\
**Tags:** kernelabstractions\
**Created:** [March 27, 2023, 12:48pm UTC](https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658 "2023-03-27T12:48:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [March 27, 2023, 12:48pm UTC](https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658/1 "2023-03-27T12:48:04Z")

</div>

I’m trying to streamline some code using KernelAbstractions and it’s _almost_ working perfectly. I’m getting hung up when I try to write a sum closure like this within the kernel

```julia
using KernelAbstractions
using CUDA
u = CUDA.rand(Float32,5,5,2)
div = CuArray{Float32}(undef,4,4)
δ(j) = (j==1) ? CartesianIndex((1,0)) : CartesianIndex((0,1))
@kernel function k_divergence(div, u)
    I = @index(Global, Cartesian)
    div[I] = sum(u[I+δ(j),j]-u[I,j] for j in eachindex(axes(div)))
end
divergence!(div,u) = k_divergence(CUDABackend(), 64)(div, u, ndrange=size(div))
divergence!(div,u)

```

This throws an `InvalidIRError` “Reason: unsupported dynamic function invocation (call to mapreduce\_empty)”. I tried use Cthulhu to track down the error but everything looks “blue” other than

```julia
(::Core.var"#Any##kw")(::Any, obj::KernelAbstractions.Kernel{CUDABackend}, args...) in CUDA.CUDAKernels at C:\Users\gweymouth\.julia\packages\CUDA\N71Iw\src\CUDAKernels.jl:102
┌ Warning: couldn't retrieve source of (::Core.var"#Any##kw")(::Any, obj::KernelAbstractions.Kernel{CUDABackend}, args...) in CUDA.CUDAKernels at C:\Users\gweymouth\.julia\packages\CUDA\N71Iw\src\CUDAKernels.jl:102
└ @ TypedSyntax C:\Users\gweymouth\.julia\packages\TypedSyntax\HNHOE\src\node.jl:31

```

If I expand out the sum into a for loop it works fine, but I am sure a base function like `sum` is supported, so I’m just doing something wrong.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [March 27, 2023, 12:53pm UTC](https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658/2 "2023-03-27T12:53:25Z")

</div>

This kernel works

```julia
@kernel function k_divergence(div, u)
    I = @index(Global, Cartesian)
    div[I] = sum(j -> u[I+δ(j),j]-u[I,j], eachindex(axes(div)), init=zero(eltype(div)))
end

```

Does KA not support the other form of `sum`?

---

<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:** [March 28, 2023, 6:22am UTC](https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658/3 "2023-03-28T06:22:11Z")

</div>

KA.jl is not involved in the compilation of `sum`. You’re just expecting this `sum` invocation to get compiled (by Julia & GPUCompiler) down to something that’s GPU compatible, which is generally not something you can rely on. Kernels are meant to be used with scalar code, not vectorized functions.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [March 28, 2023, 6:57am UTC](https://discourse.julialang.org/t/gpu-sum-closure-throwing-an-error/96658/4 "2023-03-28T06:57:36Z")

</div>

Ok - that’s good to know.
