# How to make \`Colon()\` stable in CUDA kernel

**URL:** <https://discourse.julialang.org/t/how-to-make-colon-stable-in-cuda-kernel/102934>\
**Category:** GPU\
**Tags:** question, cuda\
**Created:** [August 18, 2023, 8:20am UTC](https://discourse.julialang.org/t/how-to-make-colon-stable-in-cuda-kernel/102934 "2023-08-18T08:20:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![huiyuxie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huiyuxie/32/214265_2.png) [@huiyuxie](https://discourse.julialang.org/u/huiyuxie)\
**Post date:** [August 18, 2023, 8:20am UTC](https://discourse.julialang.org/t/how-to-make-colon-stable-in-cuda-kernel/102934/1 "2023-08-18T08:20:26Z")

</div>

Hi! I’m wondering is there any way to make `Colon()` stable when calling the CUDA kernel. For example (I’ve simplified the sample case here as the practical one is complex),

```plaintext
# Helper function
function view_helper(u, arg1, arg2, arg3)

    idx1 = (arg1 == 1) ? size(u, 2) * isequal(arg2, 1) : Colon()
    idx2 = (arg1 == 2) ? size(u, 3) * isequal(arg2, 1) : Colon()
    idx3 = (arg1 == 3) ? size(u, 4) * isequal(arg2, 1) : Colon()

    return view(u, :, idx1, idx2, idx3, arg3)
end

# CUDA kernel
function kernel!()

    i = (blockIdx().x - 1) * blockDim().x + threadIdx().x
    j = (blockIdx().y - 1) * blockDim().y + threadIdx().y
    k = (blockIdx().z - 1) * blockDim().z + threadIdx().z

    if (...)
        # Compute `arg1`, `arg2`, and `arg3`
        new_u = view_helper(u, arg1, arg2, arg3)
        # Use `new_u` to do something
    end

    return nothing
end

```

I defined a helper function here to avoid using `if/else` condition too much in the kernel (as it will affect the performance). But the kernel could not run with success and if I changed `Colon()` to some fixed number then it could run successfully. So I guess it is the issue about the stability of `Colon()` when calling the CUDA kernel. Has anyone else ever had the same issue as me? And how can I solve this problem? Thanks!

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 18, 2023, 11:21am UTC](https://discourse.julialang.org/t/how-to-make-colon-stable-in-cuda-kernel/102934/2 "2023-08-18T11:21:04Z")

</div>

In this line you are using a runtime lookup to decide the _type_ of `idx1` at runtime. You cant do that on the GPU - the type needs to be fixed at compile time.

```julia
idx1 = (arg1 == 1) ? size(u, 2) * isequal(arg2, 1) : Colon()

```

So instead of `Colon`, you could return a `UnitRange` from both parts of the conditional, explicitly passing the whole axis range rather than `Colon`. Then the type remains constant, just the runtime values change.
