# Declaration of CUDA variables in module results in undefined reference

**URL:** <https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765>\
**Category:** General Usage\
**Tags:** cuda, memory-allocation, potential-bug\
**Created:** [March 29, 2023, 9:31am UTC](https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765 "2023-03-29T09:31:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcasbol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcasbol/32/18756_2.png) [@dcasbol](https://discourse.julialang.org/u/dcasbol)\
**Post date:** [March 29, 2023, 9:31am UTC](https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765/1 "2023-03-29T09:31:59Z")

</div>

Hi everyone,

I am working on a project that makes intensive use of GPU computation and we wanted to have some module constants that were already placed on the GPU memory, but apparently the memory is freed after the module has been defined and accessing that constant results in an undefined reference error (if you access from CPU) or non-determined data (when accessed from a GPU kernel).

I tried to replicate it in the REPL but it doesn’t happen there:

```julia
module CUDAtest
using CUDA
const A = Vector{Float32}([1f0, 2f0, 3f0])
const B = CuArray(A)
end

```

In this case I can access both `CUDAtest.A` and `CUDAtest.B` without issues, but when I try to access constants in my other module I can only have the one located in CPU. About the constant located in GPU, I can query its type, size and length (which indicates that the pointer is there), but trying to print it throws the following error:

```julia
julia> Common.CU_BLUE
5-element CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
Error showing value of type CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:
ERROR: UndefRefError: access to undefined reference

```

With CPU arrays it works all right, so do you know if this is somewhat an expected behaviour or maybe it is a bug?

Thanks in advance,  
David

---

<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 29, 2023, 1:40pm UTC](https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765/2 "2023-03-29T13:40:38Z")

</div>

CPU arrays can get serialized in the precompilation image, but with GPU arrays a new context is created when the process starts. So you essentially cannot have global arrays in a module, you’ll need to initialize them at the start of your application.

---

<div class="post-metadata">

**Author:** ![dcasbol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcasbol/32/18756_2.png) [@dcasbol](https://discourse.julialang.org/u/dcasbol)\
**Post date:** [March 30, 2023, 8:03am UTC](https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765/3 "2023-03-30T08:03:56Z")

</div>

Thanks for your answer. It makes sense, but it is somewhat inconsistent with the general behaviour, and also deceptive from the user’s POV. I would rather have an error/warning than this. Another option might be serializing a CPU version of the array and initializing it automatically upon the loading of the module, although that will require significantly more effort.

---

<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 30, 2023, 6:53pm UTC](https://discourse.julialang.org/t/declaration-of-cuda-variables-in-module-results-in-undefined-reference/96765/4 "2023-03-30T18:53:41Z")

</div>

> [@dcasbol](#):
>
> I would rather have an error/warning than this.

The problem is that there’s AFAIK no methods that are being called to (de)serialize these objects, so there’s no clear-cut point where to throw an error. I haven’t taken a proper look at the issue though, so if you have any thoughts feel free to open an issue with details, or a PR if you have the time.
