@cuDynamicSharedMem : allocating beforehand?


#1

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

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:

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

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


#2

Changing to @cuStaticSharedMem fixed all errors.


#3

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.