# CUDAnative dynamic allocation

**URL:** https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435
**Category:** GPU
**Tags:** question, cudanative
**Created:** [March 3, 2020, 12:12am UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435 "2020-03-03T00:12:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Benton](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Michael\_Benton](https://discourse.julialang.org/u/Michael_Benton)
#### Post date: [March 3, 2020, 12:12am UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/1 "2020-03-03T00:12:26Z")

</div>

I need to be able to dynamically allocate in kernel.

`val = MArray{Tuple{4}, Float32}(undef)`

is a way to get static allocation with StaticArrays.jl inside a CUDA kernel

I figured something like `Array{Float32}(undef, 4)` would work in a kernel but doesn’t, and I haven’t found a way to get the syntax from anything I’ve seen on C cuda.

Is this functionality available outside of calls to `@cuDynamicSharedMem` (which I’ve found require very careful syncing to avoid mistakes, and risk not having enough available memory) ?

---

<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 3, 2020, 6:55am UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/2 "2020-03-03T06:55:51Z")

</div>

> [@Michael\_Benton](#):
>
> I figured something like `Array{Float32}(undef, 4)` would work in a kernel but doesn’t, and I haven’t found a way to get the syntax from anything I’ve seen on C cuda.

`Array` is the Julia CPU implementation of the `AbstractArray` interface and will never work on the GPU. You can use StaticArrays, but you need to take care it remains stack allocated and doesn’t actually allocate on the heap:Asince we don’t have garbage collection on the GPU, heap memory will never be freed and you’ll quickly run out of dynamic memory. Using shared memory is a good alternative, but has different semantics wrt. parallelism.

---

<div class="post-metadata">

### Author: ![Michael\_Benton](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Michael\_Benton](https://discourse.julialang.org/u/Michael_Benton)
#### Post date: [March 3, 2020, 4:34pm UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/3 "2020-03-03T16:34:17Z")

</div>

StaticArrays has a dynamic array option? I can’t make any sense out of what you said, I’m trying to do:

```julia
using StaticArrays, CUDAnative

function myKern(n)
val = MArray{Tuple{n}, Float32}(undef)
return nothing
end

@cuda threads=1 blocks=1 myKern(2)

```

where memory size is not known at compile

---

<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 4, 2020, 7:34am UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/4 "2020-03-04T07:34:38Z")

</div>

> [@Michael\_Benton](#):
>
> StaticArrays has a dynamic array option?

No, but if you let a StaticArray escape, or pass it around in ways the compiler can’t reason about, it will heap allocate and result in calls to `malloc`. That kind of dynamic memory is very limited and untracked.

> [@Michael\_Benton](#):
>
> I’m trying to do:
> 
> ```julia
> using StaticArrays, CUDAnative
> 
> function myKern(n)
> val = MArray{Tuple{n}, Float32}(undef)
> return nothing
> end
> 
> @cuda threads=1 blocks=1 myKern(2)
> 
> ```
> 
> where memory size is not known at compile

That won’t work like that. At the very least, you will need to specialize on `n` (i.e. pass it as a `Val`). But I have limited experience working with StaticArrays on the GPU, maybe @vchuravy knows of another way to, essentially, emit a variable-size alloca here without forcing respecialization.

---

<div class="post-metadata">

### Author: ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)
#### Post date: [March 4, 2020, 5:39pm UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/5 "2020-03-04T17:39:08Z")

</div>

Yeah the only way to make that work is with statically known sizes:

```julia
using StaticArrays, CUDAnative

function myKern(::Val{n}) where n
val = MArray{Tuple{n}, Float32}(undef)
return nothing
end

@cuda threads=1 blocks=1 myKern(Val(2))

```

Should work.

---

<div class="post-metadata">

### Author: ![Michael\_Benton](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Michael\_Benton](https://discourse.julialang.org/u/Michael_Benton)
#### Post date: [March 4, 2020, 6:29pm UTC](https://discourse.julialang.org/t/cudanative-dynamic-allocation/35435/6 "2020-03-04T18:29:52Z")

</div>

While that isn’t dynamic, I realize that I can make my thing work with this, thank you for sharing the syntax too.
