# Kernel Compilation error- KernelError: recursion is currently not supported

**URL:** https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853
**Category:** GPU
**Created:** [November 8, 2019, 9:25am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853 "2019-11-08T09:25:31Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Batel\_Str](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batel_str/32/11200_2.png) [@Batel\_Str](https://discourse.julialang.org/u/Batel_Str)
#### Post date: [November 8, 2019, 9:25am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/1 "2019-11-08T09:25:31Z")

</div>

Hi  
i wrote this kernel function:

function gpu\_kernel\_init\_format(format\_img)  
gpu\_lab\_image =CuArrays.fill(128, (10,10,3))

```
i = (blockIdx().x - 1) * blockDim().x + threadIdx().x
j = (blockIdx().y - 1) * blockDim().y + threadIdx().y
if i <= 10 && j<= 10 
    format_img[i, j] = [(gpu_lab_image[i][j][0]), (gpu_lab_image[i][j][1]), (gpu_lab_image[i][j][2]), i, j]
end
return nothing

```

end

Call kernel:

gpu\_format = CuArrays.fill(0, (10,10,5))

@device\_code\_warntype @cuda blocks=(2,2) threads=(16,16) gpu\_kernel\_init\_format(gpu\_format)

i added the @device\_code\_warntype and still coudn’t find the problem:  
GPU compilation of gpu\_kernel\_init\_format(CuDeviceArray{Int64,3,CUDAnative.AS.Global}) failed  
 **KernelError: recursion is currently not supported**

what should i fix?  
Thanks

---

<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: [November 8, 2019, 9:30am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/2 "2019-11-08T09:30:46Z")

</div>

Please format your code using triple backticks. See [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

You are allocating a `CuArray` within your kernel, this is unsupported. `CuArray` is a host-side array, you can only pass it to a device kernel.

---

<div class="post-metadata">

### Author: ![Batel\_Str](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batel_str/32/11200_2.png) [@Batel\_Str](https://discourse.julialang.org/u/Batel_Str)
#### Post date: [November 8, 2019, 9:42am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/3 "2019-11-08T09:42:28Z")

</div>

Hi, Thanks for answering!

i took out allocation and pass it as additional argument and got that:

```julia
Reason: unsupported call through a literal pointer (call to jl_alloc_array_1d)

```

what should i do now?

i am new in julia ,especially in julia GPU, do you have any recommendation for  
beginners tutorial? i have seen some examples on documentation and also enrolled to courses in julia academy but i would like to practice more.

---

<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: [November 8, 2019, 9:44am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/4 "2019-11-08T09:44:09Z")

</div>

You’re still allocating some array from within a kernel. GPU kernels are restricted and cannot just call into any Julia code. If you’re not familiar with GPU computing, I’d recommend using the broadcast abstraction of CuArrays.jl. CUDAnative.jl can be used to create custom kernels, which is a little tricky as you experience here. Have a look at this tutorial: [https://juliagpu.gitlab.io/CUDA.jl/tutorials/introduction/](https://juliagpu.gitlab.io/CUDA.jl/tutorials/introduction/)

---

<div class="post-metadata">

### Author: ![Batel\_Str](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batel_str/32/11200_2.png) [@Batel\_Str](https://discourse.julialang.org/u/Batel_Str)
#### Post date: [November 8, 2019, 9:56am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/5 "2019-11-08T09:56:10Z")

</div>

Thanks!

That’s my new kernel:

```julia
function gpu_kernel_init_format(format_img ,gpu_lab_image)

    i = (blockIdx().x - 1) * blockDim().x + threadIdx().x
    j = (blockIdx().y - 1) * blockDim().y + threadIdx().y
    if i <= 10 && j<= 10 
        format_img[i, j] = [(gpu_lab_image[i][j][0]), (gpu_lab_image[i][j][1]), (gpu_lab_image[i][j][2]), i, j]
    end
    return nothing
end

```

there is no allocation inside it, so should i fix it again?

---

<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: [November 8, 2019, 9:58am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/6 "2019-11-08T09:58:27Z")

</div>

`format_img[i, j] = [(gpu_lab_image[i][j][0]), (gpu_lab_image[i][j][1]), (gpu_lab_image[i][j][2]), i, j]`

That allocates an array, right? ~~`format_img` is also undefined.~~ How would you expect this to work?

---

<div class="post-metadata">

### Author: ![Batel\_Str](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batel_str/32/11200_2.png) [@Batel\_Str](https://discourse.julialang.org/u/Batel_Str)
#### Post date: [November 8, 2019, 10:06am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/7 "2019-11-08T10:06:31Z")

</div>

Sorry about my lack of understanding but i allocated format\_img in advance and pass it as  
argument.  
i just want to copy some values into format\_img, not to allocate.  
that’s my code before calling to kernel:

```julia
gpu_format = CuArrays.fill(0, (10,10,5))
gpu_lab_image =CuArrays.fill(128, (10,10,3))

@device_code_warntype @cuda blocks=(2,2) threads=(16,16) gpu_kernel_init_format(gpu_format,gpu_lab_image)

```

---

<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: [November 8, 2019, 10:13am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/8 "2019-11-08T10:13:52Z")

</div>

> [@Batel\_Str](#):
>
> format\_img[i, j] = [(gpu\_lab\_image[i][j][0]), (gpu\_lab\_image[i][j][1]), (gpu\_lab\_image[i][j][2]), i, j]

But this code wouldn’t even work on the CPU?

```julia
julia> format = fill(0, (10,10,5));
julia> lab_image = fill(128, (10,10,3))
julia> i = 1; j = 2
julia> format[i, j] = [(lab_image[i][j][0]), (lab_image[i][j][1]), (lab_image[i][j][2]), i, j]
ERROR: BoundsError

```

Before even considering GPU execution: there’s plenty wrong with this, you can’t do `format[i,j]` but need to slice, `lab_image[i][j][0]` is 0 indexed, it should also be `lab_image[i,j,1]`, you want to do element-wise assignment, etc. Please make sure your code works first before trying to port it to the GPU, a pretty unfriendly environment where errors are much harder to debug. And again, you probably don’t need custom kernels at all, try working with array abstractions (which you can first develop on Array, and then port to CuArray).

---

<div class="post-metadata">

### Author: ![Batel\_Str](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batel_str/32/11200_2.png) [@Batel\_Str](https://discourse.julialang.org/u/Batel_Str)
#### Post date: [November 8, 2019, 10:36am UTC](https://discourse.julialang.org/t/kernel-compilation-error-kernelerror-recursion-is-currently-not-supported/30853/9 "2019-11-08T10:36:37Z")

</div>

Ok, Thank you!
