# Create static vector of variable lenght in gpu kernel

**URL:** <https://discourse.julialang.org/t/create-static-vector-of-variable-lenght-in-gpu-kernel/87863>\
**Category:** GPU\
**Tags:** question, package\
**Created:** [September 27, 2022, 8:41am UTC](https://discourse.julialang.org/t/create-static-vector-of-variable-lenght-in-gpu-kernel/87863 "2022-09-27T08:41:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)\
**Post date:** [September 27, 2022, 8:41am UTC](https://discourse.julialang.org/t/create-static-vector-of-variable-lenght-in-gpu-kernel/87863/1 "2022-09-27T08:41:48Z")

</div>

Are there any ways to create a static vector of variable length for each thread in a gpu kernel?  
I tried this but it didn’t work.

```julia
function dosomething(n)
    vec = @MVector zeros(n)
end

@cuda threads=5 dosomething(n)

```

In my application, I need to create few local small arrays (the size is determined by the argument of the kernel function call) for some intermediate calculation. Are there any alternative ways to do this?

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [September 27, 2022, 9:55am UTC](https://discourse.julialang.org/t/create-static-vector-of-variable-lenght-in-gpu-kernel/87863/2 "2022-09-27T09:55:24Z")

</div>

Try passing it as a [value type](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22):

```julia
using CUDA, StaticArrays

function dosomething(::Val{N}) where N
    vec = @MVector zeros(N)
    return
end

@cuda threads=5 dosomething(Val(3))

```

---

<div class="post-metadata">

**Author:** ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)\
**Post date:** [September 27, 2022, 8:59pm UTC](https://discourse.julialang.org/t/create-static-vector-of-variable-lenght-in-gpu-kernel/87863/3 "2022-09-27T20:59:10Z")

</div>

It works. Thanks a lot!
