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

Implement cong in Julia #50203

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 26 additions & 2 deletions base/partr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,33 @@ const heap_d = UInt32(8)
const heaps = [Vector{taskheap}(undef, 0), Vector{taskheap}(undef, 0)]
const heaps_lock = [SpinLock(), SpinLock()]

cong(max::UInt32) = iszero(max) ? UInt32(0) : jl_rand_ptls(max) + UInt32(1)

function jl_rand_ptls(max::UInt32)
ptls = Base.unsafe_convert(Ptr{UInt64}, Core.getptls())
rngseed = Base.unsafe_load(ptls, 2)
# one-extend unbias back to 64-bits
val, seed = _cong(UInt64(max), rngseed)
Base.unsafe_store!(ptls, seed, 2)
return val % UInt32
end

cong(max::UInt32) = iszero(max) ? UInt32(0) : ccall(:jl_rand_ptls, UInt32, (UInt32,), max) + UInt32(1)

function _cong(max::UInt64, seed::UInt64)
if max == 0
return UInt64(0), seed
end
mask = typemax(UInt64)
max -= 1
mask >>= leading_zeros(max | 1)

local x::UInt64
while true
seed = UInt64(69069) * seed + UInt64(362437)
x = seed & mask
x > max || break
end
return x, seed
end

function multiq_sift_up(heap::taskheap, idx::Int32)
while idx > Int32(1)
Expand Down
1 change: 1 addition & 0 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern int jl_gc_mark_queue_obj_explicit(jl_gc_mark_cache_t *gc_cache,
// parallel task runtime
// ---

// TODO: Delete
JL_DLLEXPORT uint32_t jl_rand_ptls(uint32_t max)
{
jl_ptls_t ptls = jl_current_task->ptls;
Expand Down