# Error handling in CUDA kernels

**URL:** <https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692>\
**Category:** GPU\
**Tags:** gpu, cuda\
**Created:** [April 19, 2022, 2:33pm UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692 "2022-04-19T14:33:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [April 19, 2022, 2:33pm UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/1 "2022-04-19T14:33:13Z")

</div>

In my CUDA kernel I check for a specific condition and would like to terminate the execution if the condition is fulfilled. What is the proper way to do so? How can I throw an error from inside a CUDA kernel?

---

<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:** [April 19, 2022, 6:40pm UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/2 "2022-04-19T18:40:27Z")

</div>

You can just call `error("")` but the user will only see the specific error when they run with `julia -g2`

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [April 20, 2022, 6:32am UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/3 "2022-04-20T06:32:27Z")

</div>

Thank you. It works.

In case of normal (non debug run) the thrown error is accompanied by continuously repeating message

```julia
ERROR: a exception was thrown during kernel execution.
       Run Julia on debug level 2 for device stack traces.

```

I guess there is a typo in this message: should be “an exception” instead of “a exception”.

---

<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:** [April 26, 2022, 8:00am UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/4 "2022-04-26T08:00:11Z")

</div>

The proper solution is to restructure your control flow such that you can do an early `return`. One alternative is to use the `exit` PTX instruction using LLVM.jl’s `@asmcall`, but not every GPU supports that instruction, and we’ve encountered miscompilations in the presence of such control flow.

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [April 26, 2022, 8:17am UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/5 "2022-04-26T08:17:40Z")

</div>

I can do the early return. But how I can signal that the return was triggered by an error? In normal functions I can return some variable as an error code, but CUDA kernels do not allow to return anything.

---

<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:** [April 26, 2022, 8:30am UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/6 "2022-04-26T08:30:35Z")

</div>

> [@fedoroff](#):
>
> But how I can signal that the return was triggered by an error?

You can allocate a global flag and write to it from your kernel. This can be a single-element CuArray you pass as an argument, or something fancier (e.g., look for `exception_flag` in CUDA.jl, that’s a global flag allocated in CPU memory mapped into GPU address space so that we can more easily read the value without synchronizing the GPU).

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [April 26, 2022, 8:37am UTC](https://discourse.julialang.org/t/error-handling-in-cuda-kernels/79692/7 "2022-04-26T08:37:52Z")

</div>

Thank you. I will try.
