CUDA Error in constant CuArray with conditional usage

Hi everyone, I am confronted with an error concerning a constant CuArray. Any help would be greatly appreciated.

Quick context:
I’m one of the developers of the GNSSSignals package, adapting it to do signal processing on the GPU. Receving GPS signals requires keeping a matrix of binary codes in the memory at all times. Currently:

MWE:
Trying to implement conditional CUDA usage according to the documentation.

using CUDA, GNSSSignals

const use_gpu = CUDA.functional() # true if CUDA available

function read_in_codes(filename, num_prns, code_length)
    # reading the int8 encoded binary codes under filename as
    # code_length x num_prns matrix
    code_int8 = open(filename) do file_stream
    read!(file_stream, Array{Int8}(undef, code_length, num_prns)
    end
    # return either an array or a CuArray according to the use_gpu flag
    use_gpu ? CuArray{ComplexF32}(code_int8) : Int16.(code_int8)
end

# change nothing
function extend_front_and_back(codes)
    [codes[end, :]'; codes; codes[1,:]'; codes[2,:]']
end

# change nothing
const GPS_CA_CODES = extend_front_and_back(read_in_codes(
    joinpath(dirname(pathof(GNSSSignals)), "..", "data", "codes_gps_l1.bin"),
    37,
    1023
))

Remark: I need the codes to be of ComplexF32 type due to performance reasons in calling of CUBLAS functions in other packages.

Error stacktrace:
When compiling the dev package on this branch with the above mentioned implementation:

julia> using GNSSSignals
[ Info: Precompiling GNSSSignals [52c80523-2a4e-5c38-8979-05588f836870]

julia> GNSSSignals.GPS_CA_CODES
1026×37 CUDA.CuArray{Float32,2}:
Error showing value of type CUDA.CuArray{Float32,2}:
ERROR: CUDA error: invalid argument (code 1, ERROR_INVALID_VALUE)
Stacktrace:
 [1] throw_api_error(::CUDA.cudaError_enum) at /home/coz/.julia/packages/CUDA/d6WNR/lib/cudadrv/error.jl:103
 [2] macro expansion at /home/coz/.julia/packages/CUDA/d6WNR/lib/cudadrv/error.jl:110 [inlined]
 [3] cuMemcpyDtoH_v2(::Ptr{Float32}, ::CUDA.CuPtr{Float32}, ::Int64) at /home/coz/.julia/packages/CUDA/d6WNR/lib/utils/call.jl:93
 [4] #unsafe_copyto!#6 at /home/coz/.julia/packages/CUDA/d6WNR/lib/cudadrv/memory.jl:395 [inlined]
 [5] unsafe_copyto! at /home/coz/.julia/packages/CUDA/d6WNR/lib/cudadrv/memory.jl:388 [inlined]
 [6] unsafe_copyto! at /home/coz/.julia/packages/CUDA/d6WNR/src/array.jl:314 [inlined]
 [7] copyto!(::Array{Float32,2}, ::Int64, ::CUDA.CuArray{Float32,2}, ::Int64, ::Int64) at /home/coz/.julia/packages/CUDA/d6WNR/src/array.jl:289
 [8] copyto! at /home/coz/.julia/packages/GPUArrays/uaFZh/src/host/abstractarray.jl:104 [inlined]
 [9] copyto_axcheck! at ./abstractarray.jl:946 [inlined]
 [10] Array at ./array.jl:562 [inlined]
 [11] Array at ./boot.jl:430 [inlined]
 [12] convert at ./array.jl:554 [inlined]
 [13] adapt_storage at /home/coz/.julia/packages/CUDA/d6WNR/src/array.jl:267 [inlined]
 [14] adapt_structure at /home/coz/.julia/packages/Adapt/8kQMV/src/Adapt.jl:42 [inlined]
 [15] adapt at /home/coz/.julia/packages/Adapt/8kQMV/src/Adapt.jl:40 [inlined]
 [16] convert_to_cpu at /home/coz/.julia/packages/GPUArrays/uaFZh/src/host/abstractarray.jl:49 [inlined]
 [17] print_array(::IOContext{REPL.Terminals.TTYTerminal}, ::CUDA.CuArray{Float32,2}) at /home/coz/.julia/packages/GPUArrays/uaFZh/src/host/abstractarray.jl:54
 [18] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::CUDA.CuArray{Float32,2}) at ./arrayshow.jl:358
 [19] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:214
 [20] display(::REPL.REPLDisplay, ::Any) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:218
 [21] display(::Any) at ./multimedia.jl:328
 [22] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at ./c.jl:0
 [23] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:223
 [24] (::REPL.var"#do_respond#54"{Bool,Bool,VSCodeServer.var"#36#37"{REPL.LineEditREPL,REPL.LineEdit.Prompt},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:822
 [25] #invokelatest#1 at ./essentials.jl:710 [inlined]
 [26] invokelatest at ./essentials.jl:709 [inlined]
 [27] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/LineEdit.jl:2355
 [28] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildworker/worker/package_linuxaarch64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:1143
 [29] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at ./task.jl:356

No errors when running MWE in REPL :

...

julia> const GPS_CA_CODES = extend_front_and_back(read_in_codes(
           joinpath(dirname(pathof(GNSSSignals)), "..", "data", "codes_gps_l1.bin"),
           37,
           1023
       ))
1026×37 CuArray{Complex{Float32},2}:
 -1.0-0.0im  -1.0-0.0im  -1.0-0.0im  -1.0-0.0im  -1.0-0.0im   1.0-0.0im  …   1.0-0.0im   1.0-0.0im  -1.0-0.0im  -1.0-0.0im   1.0-0.0im

...

julia> typeof(GPS_CA_CODES)
CuArray{Complex{Float32},2}

System info:
Julia v1.5,
CUDA.jl v1.3.0 with CUDA 10.0
Linux (aarch64) on Nvidia Jetson AGX Xavier

Additional information:
Tried without the conditional usage, error persists.
Tried to declare GPS_CA_CODES without const in front of it, error persists.

You cannot precompile a CuArray, they are session dependent. Change it to a Ref or something and create it during __init__ (or at another time, but at run-time).

1 Like

Thanks for the answer