How to solve error while generating random number inside kernel?

Hi,

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


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:

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.

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?

2 Likes

@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:

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

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

1 Like

@Oscar_Smith: Thanks for your reply