LoginSignup
1
1

More than 3 years have passed since last update.

JuliaGPU で乱数を生成する

Last updated at Posted at 2019-07-11

環境

最新の CuArrays を用いる。
curand は deprecated なので使わない。

Julia Version 1.1.1
CuArrays v2.0.0 #master

]add CuArrays#master

配列の形で乱数を生成する

cuRAND を呼び出す

Out of place

using CuArrays

rnd = CuArrays.rand(10)

In-place

using Random: rand!
using CuArrays

rnd = CuArray{Float32}(undef, 10)
rand!(rnd)

カーネル内で乱数を生成する

Combining the LCG (Linear Congruential Generator) and Tausworthe into an Improved Generator

using GPUArrays, CuArrays

function kernel(state, u, randstates)
    i = @linearidx u state
    r = GPUArrays.gpu_rand(eltype(u), state, randstates)
    @inbounds u[i] = 1.0f0 + r
    return
end

u = CuArray{Float32}(undef, 10)
rng = GPUArrays.global_rng(u)

gpu_call(kernel, u, (u, rng.state))

https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/random.jl
https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch37.html

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1