# @cuDynamicSharedMem : allocating beforehand?

**URL:** <https://discourse.julialang.org/t/cudynamicsharedmem-allocating-beforehand/8101>\
**Category:** GPU\
**Created:** [January 2, 2018, 5:18am UTC](https://discourse.julialang.org/t/cudynamicsharedmem-allocating-beforehand/8101 "2018-01-02T05:18:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drjoke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drjoke/32/23562_2.png) [@drjoke](https://discourse.julialang.org/u/drjoke)\
**Post date:** [January 2, 2018, 5:18am UTC](https://discourse.julialang.org/t/cudynamicsharedmem-allocating-beforehand/8101/1 "2018-01-02T05:18:41Z")

</div>

I am getting: ERROR: LoadError: CUDA error: an illegal memory access was encountered (code #700, ERROR\_ILLEGAL\_ADDRESS) on the following

```julia
using CUDAdrv, CUDAnative

function kernel(x)
    i = threadIdx().x
    shared = @cuDynamicSharedMem(Int64,1)
    if i == 1
        shared[1] = 255
    end
    sync_threads()
    x[i] = shared[1]
    return nothing
end

d_x = CuArray{Int64,1}(10)
@cuda (1, 10) kernel(d_x)
x = Array(d_x)
println(x)

```

The error probably occurs as soon as I try

`shared[1] = 255`

In the source code CUDAnative.jl/src/device/intrinsics/memory\_shared.jl it mentions:

```julia
Dynamic shared memory also needs to be allocated beforehand, when calling the kernel.

```

Yet, I cannot find an example on how to do this.

---

<div class="post-metadata">

**Author:** ![drjoke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drjoke/32/23562_2.png) [@drjoke](https://discourse.julialang.org/u/drjoke)\
**Post date:** [January 2, 2018, 6:00am UTC](https://discourse.julialang.org/t/cudynamicsharedmem-allocating-beforehand/8101/2 "2018-01-02T06:00:20Z")

</div>

Changing to @cuStaticSharedMem fixed all errors.

---

<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:** [January 2, 2018, 8:41am UTC](https://discourse.julialang.org/t/cudynamicsharedmem-allocating-beforehand/8101/3 "2018-01-02T08:41:11Z")

</div>

This is by design: dynamic shared memory in CUDAnative.jl is identical to shared memory in CUDA, ie. you need to specify how many bytes to allocate at the launch site: `@cuda (blocks, threads[, shmem[, stream]]) kernel(args)`. If you use static shared memory you specify the number of elements, and the amount of memory can be deduced.
