-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
function get_global_thread_index() | ||
block_id = (gridDim().x * (blockIdx().y - 1)) + (blockIdx().x - 1) | ||
|
||
thread_id = (block_id * (blockDim().x * blockDim().y)) + | ||
((threadIdx().y - 1) * blockDim().x) + | ||
(threadIdx().x - 1) | ||
|
||
return thread_id + 1 | ||
end | ||
|
||
struct ThreadLocalRNGState | ||
val::UInt32 | ||
end | ||
|
||
function xorshift(x::UInt32)::UInt32 | ||
x = xor(x, x << 13) | ||
x = xor(x, x >> 17) | ||
x = xor(x, x << 5) | ||
return x | ||
end | ||
|
||
function generate_next_state(state::ThreadLocalRNGState) | ||
new_val = xorshift(state.val) | ||
return ThreadLocalRNGState(new_val) | ||
end | ||
|
||
@eval @inline global_random_state() = | ||
Base.llvmcall( | ||
$("""@global_random_state = weak externally_initialized global i$(WORD_SIZE) 0 | ||
define i$(WORD_SIZE) @entry() #0 { | ||
%ptr = load i$(WORD_SIZE), i$(WORD_SIZE)* @global_random_state, align 8 | ||
ret i$(WORD_SIZE) %ptr | ||
} | ||
attributes #0 = { alwaysinline } | ||
""", "entry"), Ptr{Cvoid}, Tuple{}) | ||
|
||
function get_random_state_ptr() | ||
random_state_address = global_random_state() | ||
random_state_ptr = LLVMPtr{ThreadLocalRNGState, AS.Generic}(random_state_address) | ||
# FIXME: we should wrap this pointer in a CuDeviceVector but this leads to some very weird assertion errors | ||
end | ||
|
||
function seed_rng!(seed=nothing) | ||
if isnothing(seed) | ||
seed = clock(UInt32) | ||
end | ||
|
||
# make sure the seed is different for each thread | ||
index = get_global_thread_index() | ||
seed += (UInt32(index) + UInt32(10)) ^ 5 | ||
|
||
random_state_ptr = get_random_state_ptr() | ||
align = Base.datatype_alignment(ThreadLocalRNGState) | ||
unsafe_store!(random_state_ptr, ThreadLocalRNGState(seed), index, Val(align)) | ||
end | ||
|
||
function rand() | ||
random_state_ptr = get_random_state_ptr() | ||
index = get_global_thread_index() | ||
|
||
align = Base.datatype_alignment(ThreadLocalRNGState) | ||
state = unsafe_load(random_state_ptr, index, Val(align)) | ||
new_state = generate_next_state(state) | ||
unsafe_store!(random_state_ptr, new_state, index, Val(align)) | ||
|
||
res = (new_state.val >> 9) | reinterpret(UInt32, 1f0) | ||
return reinterpret(Float32, res) - 1.0f0 | ||
end | ||
|
||
function init_random_state!(kernel, len) | ||
required_size = sizeof(ThreadLocalRNGState) * len | ||
|
||
if isnothing(kernel.random_state) | ||
kernel.random_state = Mem.alloc(Mem.Device, required_size) | ||
elseif sizeof(kernel.random_state) < required_size | ||
Mem.free(kernel.random_state, async=true) | ||
kernel.random_state = Mem.alloc(Mem.Device, required_size) | ||
end | ||
|
||
random_state_ptr = CuGlobal{Ptr{Cvoid}}(kernel.mod, "global_random_state") | ||
random_state_ptr[] = reinterpret(Ptr{Cvoid}, convert(CuPtr{Cvoid}, kernel.random_state)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters