cuRAND
cuRAND.jl generates random numbers on the GPU, with NVIDIA's cuRAND library or with kernels written in Julia. It offers them at three levels:
- Random's in-place functions
rand!andrandn!fillCuArrays, and Random'srand,randn,rand!,randn!andseed!work with the generators below:LibraryRNG, backed by cuRAND, andNativeRNG, which calls the device-siderandin a kernel. cuRAND.randandcuRAND.randn, also available asCUDA.randandCUDA.randn, generateCuArrays with the current task's generators, as dorand_lognandrand_poissonfor distributions that Random does not offer.cuRAND.seed!(CUDA.seed!) seeds those generators. These are different functions thanRandom.randetc., which work on the CPU, so call them qualified.- The generated bindings in
libcurand.jlfollow NVIDIA's C API, and change with the CUDA toolkit. ALibraryRNGconverts to thecurandGenerator_tthey take.
using CUDA, cuRAND, RandomA = cuRAND.rand(Float32, 1024) # uniform on (0, 1]B = cuRAND.randn(1024; stddev=2) # normal, Float32 by defaultrandn!(B) # in placerng = cuRAND.LibraryRNG()Random.seed!(rng, 42)C = rand(rng, Float64, 16) # a CuArray, using this generatorEach task has its own generators, one per device: library_rng for the types cuRAND supports, gpuarrays_rng for the others, and native_rng.
Public
cuRAND.CURANDError — Type
CURANDError(code::curandStatus_t)The exception thrown when a cuRAND call fails. code is the status that cuRAND returned.
cuRAND.LibraryRNG — Type
cuRAND.LibraryRNG(typ=CURAND_RNG_PSEUDO_DEFAULT; stream=stream())A random number generator backed by the cuRAND library, using cuRAND's pseudo-random generator typ (a curandRngType, e.g. CURAND_RNG_PSEUDO_PHILOX4_32_10). Use it with Random's rand, randn, rand! and randn!, and with rand_logn, rand_poisson and their in-place versions. It generates uniformly distributed Float32, Float64 and UInt32 numbers, normally and log-normally distributed Float32 and Float64 numbers, and Poisson-distributed UInt32 numbers, into CuArrays. Other arrays, such as an Array, are filled by generating into a temporary CuArray and copying it over. It runs on the stream of the task that uses it.
A new generator uses cuRAND's default seed. Random.seed!(rng[, seed]; offset=0) seeds it, randomly when no seed is given, and skips offset numbers ahead in the sequence. A LibraryRNG converts to a curandGenerator_t, for use with cuRAND's C API, and is destroyed when it is garbage collected.
cuRAND.NativeRNG — Type
cuRAND.NativeRNG()A random number generator that launches a CUDA kernel which calls the device-side rand()/randn() on CUDACore's Philox2x32 generator.
This RNG exists for testing, RNGTest statistical validation, and perf comparison against alternative designs (cuRAND library, GPUArrays.RNG, PhiloxRNG.jl). For production use prefer the GPUArrays RNG.
See also: CUDACore.Philox2x32, CUDA.RNG.
cuRAND.functional — Method
cuRAND.functional() -> BoolWhether cuRAND was loaded and initialized successfully.
cuRAND.gpuarrays_rng — Method
cuRAND.gpuarrays_rng() -> GPUArrays.RNGThe GPUArrays.jl random number generator of the current task, for its active context. cuRAND.rand and randn, and rand! and randn! without a generator, use it for the element types that cuRAND does not support. CUDA.default_rng() returns it. It is seeded randomly when a task first uses it, and by cuRAND.seed!.
cuRAND.library_rng — Method
cuRAND.library_rng() -> LibraryRNGThe cuRAND generator of the current task, for its active context. cuRAND.rand, randn, rand_logn, rand_poisson and the in-place rand!, randn!, rand_logn! and rand_poisson! use it for the element types cuRAND supports. It is seeded randomly when a task first uses it, and by cuRAND.seed!. The generator is borrowed: seed it or generate numbers with it, but don't destroy it or change its settings.
cuRAND.native_rng — Method
cuRAND.native_rng() -> NativeRNGThe NativeRNG of the current task, for its active context. It is seeded by cuRAND.seed!.
cuRAND.rand — Function
cuRAND.rand([T=Float32,] dims...) -> CuArray{T}
cuRAND.rand(T=Float32) -> TGenerate a CuArray of uniformly distributed random numbers, or a single number, on the GPU. Float32, Float64 and UInt32 numbers come from the task's cuRAND generator (library_rng), other types from its GPUArrays generator (gpuarrays_rng). CUDA.rand is this function. A single number is read back from the GPU by indexing, which needs CUDA.@allowscalar outside interactive sessions.
This is not Random.rand, which generates numbers on the CPU, but it takes the same arguments without the generator. To use a specific generator, call Random.rand with a LibraryRNG or NativeRNG.
cuRAND.rand_logn — Function
cuRAND.rand_logn([rng::LibraryRNG,] [T=Float32,] dims...; mean=0, stddev=1) -> CuArray{T}
cuRAND.rand_logn([rng::LibraryRNG,] T=Float32; mean=0, stddev=1) -> TGenerate a CuArray of log-normally distributed random numbers, or a single number, with cuRAND: exp(x) for normally distributed x with mean mean and standard deviation stddev. T is Float32 or Float64. Without rng, the task's library_rng is used. See also rand_logn!. A single number is read back from the GPU by indexing, which needs CUDA.@allowscalar outside interactive sessions.
cuRAND.rand_logn! — Method
cuRAND.rand_logn!([rng::LibraryRNG,] A::CuArray{<:Union{Float32,Float64}}; mean=0, stddev=1) -> AFill A with log-normally distributed random numbers: exp(x) for normally distributed x with mean mean and standard deviation stddev. Without rng, the task's library_rng is used. With rng, A can also be another array, such as an Array. Wraps curandGenerateLogNormal(Double).
cuRAND.rand_poisson — Function
cuRAND.rand_poisson([rng::LibraryRNG,] [T=UInt32,] dims...; lambda=1) -> CuArray{T}
cuRAND.rand_poisson([rng::LibraryRNG,] T=UInt32; lambda=1) -> TGenerate a CuArray of Poisson-distributed random numbers with mean lambda, or a single number, with cuRAND. T must be UInt32. Without rng, the task's library_rng is used. See also rand_poisson!. A single number is read back from the GPU by indexing, which needs CUDA.@allowscalar outside interactive sessions.
cuRAND.rand_poisson! — Method
cuRAND.rand_poisson!([rng::LibraryRNG,] A::CuArray{UInt32}; lambda=1) -> AFill A with Poisson-distributed random numbers with mean lambda. Without rng, the task's library_rng is used. With rng, A can also be another array, such as an Array. Wraps curandGeneratePoisson.
cuRAND.randn — Function
cuRAND.randn([T=Float32,] dims...; mean=0, stddev=1) -> CuArray{T}
cuRAND.randn(T=Float32; mean=0, stddev=1) -> TGenerate a CuArray of normally distributed random numbers with mean mean and standard deviation stddev, or a single number, on the GPU. Float32 and Float64 numbers come from the task's cuRAND generator (library_rng), other types from its GPUArrays generator (gpuarrays_rng). CUDA.randn is this function. A single number is read back from the GPU by indexing, which needs CUDA.@allowscalar outside interactive sessions.
This is not Random.randn, which generates numbers on the CPU, but it takes the same arguments without the generator. To use a specific generator, call Random.randn with a LibraryRNG or NativeRNG.
cuRAND.seed! — Function
cuRAND.seed!([seed])Seed the random number generators of the current task with seed, or a random seed: the ones that cuRAND.rand and its relatives use (library_rng and gpuarrays_rng), and native_rng. CUDA.seed! is this function.
This is not Random.seed!, which seeds the CPU's generator. To seed a single generator, call Random.seed! on it.
cuRAND.version — Method
cuRAND.version() -> VersionNumberThe version of the cuRAND library in use. This is the library's own version, which differs from that of the CUDA toolkit it ships with.