Random numbers in [0.f0,1.f0]

Hi again,
is there a simple way to have the same as this

rand([0.f0,1.f0],10)

on the GPU? I need to generate random bits (in that case they’re Float32 but anyway), and trying the obvious thing refuses to work

CuArrays.rand([0.f0,1.f0],10)

MethodError: no method matching rand(::Array{Float32,1}, ::Int64)
You may have intended to import Base.rand
Closest candidates are:
  rand(!Matched::Union{Type{Float32}, Type{Float64}}, ::Integer, !Matched::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:122
  rand(!Matched::Type, ::Integer, !Matched::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:147
  rand(!Matched::Integer, ::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:157

Stacktrace:
 [1] ##core#449() at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:297
 [2] ##sample#450(::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:303
 [3] #_run#16(::Bool, ::String, ::Base.Iterators.Pairs{Symbol,Integer,NTuple{4,Symbol},NamedTuple{(:samples, :evals, :gctrial, :gcsample),Tuple{Int64,Int64,Bool,Bool}}}, ::typeof(BenchmarkTools._run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:331
 [4] (::getfield(BenchmarkTools, Symbol("#kw##_run")))(::NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}, ::typeof(BenchmarkTools._run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at ./none:0
 [5] (::getfield(Base, Symbol("#inner#2")){Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}},typeof(BenchmarkTools._run),Tuple{BenchmarkTools.Benchmark{Symbol("##benchmark#448")},BenchmarkTools.Parameters}})() at ./essentials.jl:793
 [6] #invokelatest#1 at ./essentials.jl:794 [inlined]
 [7] #invokelatest at ./none:0 [inlined]
 [8] #run_result#37 at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:32 [inlined]
 [9] #run_result at ./none:0 [inlined]
 [10] #run#39(::Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}}, ::typeof(run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:46
 [11] #run at ./none:0 [inlined] (repeats 2 times)
 [12] #warmup#42 at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:79 [inlined]
 [13] warmup(::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:79
 [14] top-level scope at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:390
 [15] top-level scope at In[93]:1

I can do

CuArray(rand([0.f0,1.f0],10))

but that takes more time than generating the numbers on the GPU :confused:

Thanks a lot,

Ferran.

you can try to do something like this:

but as you noted bits are bytes anyways, so maybe you can just use a if < 0.5 or something.

Not all methods of rand are implemented for the GPU in CuArrays. If you have a need for a specific method, you can always file an issue (or better yet, have a look at how to implement it or extend the current functionality :slightly_smiling_face:).

1 Like