Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native RNG fixes for very large arrays #2561

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function Random.rand!(rng::RNG, A::AnyCuArray)

# grid-stride loop
threadId = threadIdx().x
window = blockDim().x * gridDim().x
offset = (blockIdx().x - 1) * blockDim().x
window = widemul(blockDim().x, gridDim().x)
offset = widemul(blockIdx().x - 1i32, blockDim().x)
while offset < length(A)
i = threadId + offset
if i <= length(A)
Expand Down Expand Up @@ -96,8 +96,8 @@ function Random.randn!(rng::RNG, A::AnyCuArray{<:Union{AbstractFloat,Complex{<:A

# grid-stride loop
threadId = threadIdx().x
window = (blockDim().x - 1) * gridDim().x
offset = (blockIdx().x - 1) * blockDim().x
window = widemul(blockDim().x, gridDim().x)
offset = widemul(blockIdx().x - 1i32, blockDim().x)
while offset < length(A)
i = threadId + offset
j = threadId + offset + window
Expand Down Expand Up @@ -129,8 +129,8 @@ function Random.randn!(rng::RNG, A::AnyCuArray{<:Union{AbstractFloat,Complex{<:A

# grid-stride loop
threadId = threadIdx().x
window = (blockDim().x - 1) * gridDim().x
offset = (blockIdx().x - 1) * blockDim().x
window = widemul(blockDim().x, gridDim().x)
offset = widemul(blockIdx().x - 1i32, blockDim().x)
while offset < length(A)
i = threadId + offset
if i <= length(A)
Expand All @@ -150,11 +150,11 @@ function Random.randn!(rng::RNG, A::AnyCuArray{<:Union{AbstractFloat,Complex{<:A
return
end

kernel = @cuda launch=false name="rand!" kernel(A, rng.seed, rng.counter)
config = launch_configuration(kernel.fun; max_threads=64)
threads = max(32, min(config.threads, length(A)÷2))
blocks = min(config.blocks, cld(cld(length(A), 2), threads))
kernel(A, rng.seed, rng.counter; threads, blocks)
# see note in `rand!` about the launch configuration
threads = 32
blocks = cld(cld(length(A), 2), threads)

@cuda threads=threads blocks=blocks name="randn!" kernel(A, rng.seed, rng.counter)

new_counter = Int64(rng.counter) + length(A)
overflow, remainder = fldmod(new_counter, typemax(UInt32))
Expand Down
7 changes: 7 additions & 0 deletions test/base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,10 @@ end
end
end

@testset "counter overflow" begin
rng = CUDA.RNG()
# we may not be able to allocate over 4GB on the GPU, so use unified memory
c = CuArray{Float16, 5, CUDA.UnifiedMemory}(undef, 64, 32, 512, 32, 64)
rand!(rng, c)
randn!(rng, c)
end