|
| 1 | +using Random |
| 2 | + |
| 3 | +const n = 256 |
| 4 | + |
| 5 | +function apply_seed(seed) |
| 6 | + if seed === missing |
| 7 | + # should result in different numbers across launches |
| 8 | + Random.seed!() |
| 9 | + # XXX: this currently doesn't work, because of the definition in Base, |
| 10 | + # `seed!(r::MersenneTwister=default_rng())`, which breaks overriding |
| 11 | + # `default_rng` with a non-MersenneTwister RNG. |
| 12 | + elseif seed !== nothing |
| 13 | + # should result in the same numbers |
| 14 | + Random.seed!(seed) |
| 15 | + elseif seed === nothing |
| 16 | + # should result in different numbers across launches, |
| 17 | + # as determined by the seed set during module loading. |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +@testset "rand($T), seed $seed" for T in ( |
| 22 | + Int32, UInt32, Int64, UInt64, Float32, Float64, |
| 23 | +), seed in (nothing, #=missing,=# 1234) |
| 24 | + # different kernel invocations should get different numbers |
| 25 | + @testset "across launches" begin |
| 26 | + function kernel(A::AbstractArray{T}, seed) where {T} |
| 27 | + apply_seed(seed) |
| 28 | + tid = get_global_id(1) |
| 29 | + A[tid] = rand(T) |
| 30 | + return nothing |
| 31 | + end |
| 32 | + |
| 33 | + a = OpenCL.zeros(T, n) |
| 34 | + b = OpenCL.zeros(T, n) |
| 35 | + |
| 36 | + @opencl global_size=n local_size=n kernel(a, seed) |
| 37 | + @opencl global_size=n local_size=n kernel(b, seed) |
| 38 | + |
| 39 | + if seed === nothing || seed === missing |
| 40 | + @test Array(a) != Array(b) |
| 41 | + else |
| 42 | + @test Array(a) == Array(b) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + # multiple calls to rand should get different numbers |
| 47 | + @testset "across calls" begin |
| 48 | + function kernel(A::AbstractArray{T}, B::AbstractArray{T}, seed) where {T} |
| 49 | + apply_seed(seed) |
| 50 | + tid = get_global_id(1) |
| 51 | + A[tid] = rand(T) |
| 52 | + B[tid] = rand(T) |
| 53 | + return nothing |
| 54 | + end |
| 55 | + |
| 56 | + a = OpenCL.zeros(T, n) |
| 57 | + b = OpenCL.zeros(T, n) |
| 58 | + |
| 59 | + @opencl global_size=n local_size=n kernel(a, b, seed) |
| 60 | + |
| 61 | + @test Array(a) != Array(b) |
| 62 | + end |
| 63 | + |
| 64 | + # different threads should get different numbers |
| 65 | + @testset "across threads, dim $active_dim" for active_dim in 1:6 |
| 66 | + function kernel(A::AbstractArray{T}, seed) where {T} |
| 67 | + apply_seed(seed) |
| 68 | + id = get_local_id(1) * get_local_id(2) * get_local_id(3) * |
| 69 | + get_group_id(1) * get_group_id(2) * get_group_id(3) |
| 70 | + if 1 <= id <= length(A) |
| 71 | + A[id] = rand(T) |
| 72 | + end |
| 73 | + return nothing |
| 74 | + end |
| 75 | + |
| 76 | + tx, ty, tz, bx, by, bz = [dim == active_dim ? 3 : 1 for dim in 1:6] |
| 77 | + gx, gy, gz = tx*bx, ty*by, tz*bz |
| 78 | + a = OpenCL.zeros(T, 3) |
| 79 | + |
| 80 | + @opencl local_size=(tx, ty, tz) global_size=(gx, gy, gz) kernel(a, seed) |
| 81 | + |
| 82 | + # NOTE: we don't just generate two numbers and compare them, instead generating a |
| 83 | + # couple more and checking they're not all the same, in order to avoid |
| 84 | + # occasional collisions with lower-precision types (i.e., Float16). |
| 85 | + # TODO: why is the third dimension broken? |
| 86 | + @test length(unique(Array(a))) > 1 broken = active_dim == 3 || active_dim == 6 |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +@testset "basic randn($T), seed $seed" for T in ( |
| 91 | + Float32, Float64, |
| 92 | +), seed in (nothing, #=missing,=# 1234) |
| 93 | + function kernel(A::AbstractArray{T}, seed) where {T} |
| 94 | + apply_seed(seed) |
| 95 | + tid = get_global_id(1) |
| 96 | + A[tid] = randn(T) |
| 97 | + return |
| 98 | + end |
| 99 | + |
| 100 | + a = OpenCL.zeros(T, n) |
| 101 | + b = OpenCL.zeros(T, n) |
| 102 | + |
| 103 | + @opencl global_size=n local_size=n kernel(a, seed) |
| 104 | + @opencl global_size=n local_size=n kernel(b, seed) |
| 105 | + |
| 106 | + if seed === nothing || seed === missing |
| 107 | + @test Array(a) != Array(b) |
| 108 | + else |
| 109 | + @test Array(a) == Array(b) |
| 110 | + end |
| 111 | +end |
| 112 | + |
| 113 | +@testset "basic randexp($T), seed $seed" for T in ( |
| 114 | + Float32, Float64, |
| 115 | +), seed in (nothing, #=missing,=# 1234) |
| 116 | + function kernel(A::AbstractArray{T}, seed) where {T} |
| 117 | + apply_seed(seed) |
| 118 | + tid = get_global_id(1) |
| 119 | + A[tid] = randexp(T) |
| 120 | + return |
| 121 | + end |
| 122 | + |
| 123 | + a = OpenCL.zeros(T, n) |
| 124 | + b = OpenCL.zeros(T, n) |
| 125 | + |
| 126 | + @opencl global_size=n local_size=n kernel(a, seed) |
| 127 | + @opencl global_size=n local_size=n kernel(b, seed) |
| 128 | + |
| 129 | + if seed === nothing || seed === missing |
| 130 | + @test Array(a) != Array(b) |
| 131 | + else |
| 132 | + @test Array(a) == Array(b) |
| 133 | + end |
| 134 | +end |
0 commit comments