# How to solve error while generating random number inside kernel?

**URL:** <https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289>\
**Category:** General Usage\
**Tags:** gpu, cuda\
**Created:** [November 17, 2020, 10:53am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289 "2020-11-17T10:53:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 17, 2020, 10:53am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289/1 "2020-11-17T10:53:37Z")

</div>

Hi,

I am a newbie to CUDA and I am trying to generate random number inside a gpu kernel using the following commands,

```julia

using CUDA
using Random

function kernel(cu_status)
	idx = threadIdx().x
    unif = CUDA.rand(1)
    if unif < 0.5
        cu_status[idx] = true
    end
    return nothing
end

itr = 100
status = fill(false,itr)
cu_status = CuArray(status)
@cuda threads=itr kernel(cu_status)

```

But I am getting the following error:

```julia
ERROR: GPU compilation of kernel kernel(CuDeviceArray{Bool,1,1}) failed
KernelError: kernel returns a value of type `Union{}`

Make sure your kernel function ends in `return`, `return nothing` or `nothing`.
If the returned value is of type `Union{}`, your Julia code probably throws an exception.
Inspect the code with `@device_code_warntype` for more details.

```

---

<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 17, 2020, 11:54am UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289/2 "2020-11-17T11:54:26Z")

</div>

`rand` is not supported in kernels, it’s a heavyweight API that allocates memory, accesses CPU structures, etc. Did you follow the advice the error gave you?

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 17, 2020, 12:40pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289/3 "2020-11-17T12:40:18Z")

</div>

@maleadt: Thanks for your reply. I tried to see warning like below  
`@device_code_warntype @cuda threads=itr kernel(cu_status)`  
and I got as following:

```julia
PTX CompilerJob of kernel kernel(CuDeviceArray{Bool,1,1}) for sm_50

Variables
  #self#::Core.Compiler.Const(kernel, false)
  cu_status::CuDeviceArray{Bool,1,1}
  idx::Int64
  unif::CuArray{Float32,1}

Body::Union{}
1 ─ %1 = Main.threadIdx()::NamedTuple{(:x, :y, :z),Tuple{Int64,Int64,Int64}}
│ (idx = Base.getproperty(%1, :x))
│ %3 = CUDA.rand::Core.Compiler.Const(CUDA.rand, false)
│ (unif = (%3)(1))
│ (unif < 0.5)
│ Core.Compiler.Const(:(%5), false)
│ Core.Compiler.Const(true, false)
│ Core.Compiler.Const(:(Base.setindex!(cu_status, %7, idx)), false)
└── Core.Compiler.Const(:(return Main.nothing), false)
ERROR: GPU compilation of kernel kernel(CuDeviceArray{Bool,1,1}) failed
KernelError: kernel returns a value of type `Union{}`

Make sure your kernel function ends in `return`, `return nothing` or `nothing`.
If the returned value is of type `Union{}`, your Julia code probably throws an exception.
Inspect the code with `@device_code_warntype` for more details.

```

But I don’t know how to interpret this.

Is any other option to generate random numbers in kernels?

Thanks and Regards,  
Manu

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 17, 2020, 1:10pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289/4 "2020-11-17T13:10:31Z")

</div>

If you know how many you need, you could pass an array of random numbers into the function.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [November 17, 2020, 1:25pm UTC](https://discourse.julialang.org/t/how-to-solve-error-while-generating-random-number-inside-kernel/50289/5 "2020-11-17T13:25:04Z")

</div>

@Oscar_Smith: Thanks for your reply
