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

[WIP] Reversible rng #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NiLangCore = "575d3204-02a4-11ea-3f62-238caa8bf11e"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
FixedPointNumbers = "0.6, 0.7, 0.8"
Expand All @@ -25,9 +26,8 @@ julia = "1.3,1.4"
[extras]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random", "Statistics", "Distributions", "ForwardDiff"]
test = ["Test", "Statistics", "Distributions", "ForwardDiff"]
41 changes: 41 additions & 0 deletions src/stdlib/rng.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Random

export LcgRNG

struct LcgRNG{T<:Integer} <: AbstractRNG
a::T
c::T
m::T
x::T
end
LcgRNG(; seed=42) = LcgRNG(
UInt(6364136223846793005),
UInt(1442695040888963407),
UInt(1)<<UInt(63),
UInt(seed),
)

# https://stackoverflow.com/questions/2911432/reversible-pseudo-random-sequence-generator
# https://github.com/bobbaluba/rlcg/blob/master/include/rlcg.hpp
@i function Base.rand(rng::LcgRNG{T}) where T
xnew ← zero(T)
@routine begin
anc ← zero(T)
anc += rng.a * rng.x
anc += rng.c
end
xnew += mod(anc, rng.m)
~@routine

# uncompute x
@routine begin
@zeros T x_c anc1 ainv
ainv += gcdx(rng.a, rng.m) |> tget(2)
x_c += xnew - rng.c
anc1 += ainv * x_c
end
rng.x -= mod(anc1, rng.m)
~@routine
SWAP(rng.x, xnew)
xnew → zero(T)
end
1 change: 1 addition & 0 deletions src/stdlib/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ include("nnlib.jl")
include("sparse.jl")
include("mapreduce.jl")
include("sorting.jl")
include("rng.jl")
10 changes: 10 additions & 0 deletions test/stdlib/rng.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Test, NiLang, Random

@testset "LcgRNG" begin
rng = LcgRNG(; seed=42)
@test rng isa LcgRNG{UInt64}
@test rng.x == 42
@instr rand(rng)
@test rng.x != 42
@test check_inv(rand, (rng,))
end
1 change: 1 addition & 0 deletions test/stdlib/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include("statistics.jl")
include("nnlib.jl")
include("sparse.jl")
include("mapreduce.jl")
include("rng.jl")