# Is it possible to use @static to distinguish CPU or GPU call in CUDA?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-use-static-to-distinguish-cpu-or-gpu-call-in-cuda/52366>\
**Category:** GPU\
**Tags:** question\
**Created:** [December 25, 2020, 11:18am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-static-to-distinguish-cpu-or-gpu-call-in-cuda/52366 "2020-12-25T11:18:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [December 25, 2020, 11:18am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-static-to-distinguish-cpu-or-gpu-call-in-cuda/52366/1 "2020-12-25T11:18:43Z")

</div>

As we all know, some codes can not run on GPU. But there will be some alternative way to solve it.

For example, `sqrt` can not run on GPU due to a `_fpow` error. But `CUDA.sqrt` is an alternative solution. In this condition, 2 methods will be written like the following code.

```julia
my_sqrt(x) = sqrt(x)
my_sqrt_cuda(x) = CUDA.sqrt(x)

```

When some upper code call this method, it have to duplicated to adapt the duplicated `my_sqrt`.

In C++, `#if` could be used to treat this problem. In julia, a `@static` macro also could be used to solve operating system difference.

Can I detect an expression to distinguish the device (CPU or GPU) the code is running on? If so, the code should be like:

```julia
my_sqrt(x) = @static magic_expression ? CUDA.sqrt(x) : sqrt(x)

```

And this method could be call on either CPU or GPU condition.

* * *

Update: I found that `sqrt` has been fixed in CUDA.jl. So maybe power or something else is a better sample.

---

<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:** [December 25, 2020, 5:45pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-static-to-distinguish-cpu-or-gpu-call-in-cuda/52366/2 "2020-12-25T17:45:54Z")

</div>

I guess, the correct way of writing generic CPU/GPU code is using [Cassette.jl](https://github.com/JuliaLabs/Cassette.jl). Take a look how it is done in, e.g. [KernelAbstractions.jl](https://github.com/JuliaGPU/KernelAbstractions.jl):  
[https://github.com/JuliaGPU/KernelAbstractions.jl/blob/master/src/backends/cuda.jl#L240](https://github.com/JuliaGPU/KernelAbstractions.jl/blob/master/src/backends/cuda.jl#L240)

---

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [December 26, 2020, 7:37am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-static-to-distinguish-cpu-or-gpu-call-in-cuda/52366/3 "2020-12-26T07:37:38Z")

</div>

> [@fedoroff](#):
>
> I guess, the correct way of writing generic CPU/GPU code is using [Cassette.jl](https://github.com/JuliaLabs/Cassette.jl). Take a look how it is done in, e.g. [KernelAbstractions.jl](https://github.com/JuliaGPU/KernelAbstractions.jl):

That’s so cool!
