# Simple CUDA sum kernel

**URL:** <https://discourse.julialang.org/t/simple-cuda-sum-kernel/53594>\
**Category:** General Usage\
**Created:** [January 19, 2021, 8:43am UTC](https://discourse.julialang.org/t/simple-cuda-sum-kernel/53594 "2021-01-19T08:43:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 19, 2021, 8:43am UTC](https://discourse.julialang.org/t/simple-cuda-sum-kernel/53594/1 "2021-01-19T08:43:14Z")

</div>

I am trying to write a simple CUDA.jl sum kernel. However I always get:

```julia
ERROR: LoadError: CUDA error: an illegal memory access was encountered (code 700, ERROR_ILLEGAL_AD
DRESS)

```

Any ideas? Here is my code:

```julia
using CUDA

function mysum_kernel!(out, arr)
    tindex = threadIdx().x
    stride = blockDim().x
    acc = 0f0
    for i in tindex:stride:length(arr)
        @inbounds acc += arr[i]
    end
    nshacc = blockDim().x
    shacc = @cuDynamicSharedMem(Float32, nshacc)
    shacc[tindex] = acc
    CUDA.sync_threads()

    if tindex === 1
        ret = 0f0
        for i in eachindex(shacc)
            ret += shacc[i]
        end
        #out[] = ret
    end
    CUDA.sync_threads()
    return nothing
end

arr = CuArray{Float32}(1:13)
out = CUDA.zeros()
CUDA.@sync begin
    @cuda threads=8 blocks=1 mysum_kernel!(out, arr)
end
@show out

```
