Skip to content

Commit

Permalink
Merge pull request #6 from sunoru/upgrade-with-1.0
Browse files Browse the repository at this point in the history
Upgrade with 1.0
  • Loading branch information
sunoru authored Sep 7, 2019
2 parents 86f0035 + 44ef564 commit ca139dd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/VSL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ module VSL
include("errors.jl")
include("brngs.jl")
include("methods.jl")

import Random: AbstractRNG

abstract type VSLDistribution <: AbstractRNG end
include("common.jl")

include("continuous_distributions.jl")
include("discrete_distributions.jl")
Expand Down
10 changes: 5 additions & 5 deletions src/brngs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
VSL_BRNG_PHILOX4X32X10 = 0x01000000
)

struct BasicRandomNumberGenerator
mutable struct BasicRandomNumberGenerator
brng_type::BRNGType
stream_state::Vector{Ptr{Cvoid}}
end
Expand All @@ -29,8 +29,8 @@ function BasicRandomNumberGenerator(brng_type::BRNGType, seed::UInt)
if status VSL_STATUS_OK
throw(VSL_ERROR(status))
end
brng = new(brng_type, stream_state)
finalizer(brng, brng_finalizer)
brng = BasicRandomNumberGenerator(brng_type, stream_state)
finalizer(brng_finalizer, brng)
brng
end
function BasicRandomNumberGenerator(brng_type::BRNGType, seed::Vector{Cuint})
Expand All @@ -40,8 +40,8 @@ function BasicRandomNumberGenerator(brng_type::BRNGType, seed::Vector{Cuint})
if status VSL_STATUS_OK
throw(VSL_ERROR(status))
end
brng = new(brng_type, stream_state)
finalizer(brng, brng_finalizer)
brng = BasicRandomNumberGenerator(brng_type, stream_state)
finalizer(brng_finalizer, brng)
brng
end
BasicRandomNumberGenerator(brng_type::BRNGType, seed::Integer) =
Expand Down
14 changes: 14 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Random: AbstractRNG, rand, rand!

abstract type VSLDistribution{T} <: AbstractRNG end

rand(d::VSLDistribution{T}, dims::Integer...) where T = rand(d, T, Dims(dims))

function rand(d::VSLDistribution{T}, ::Type{X}) where {T, X}
error("Only $(T) type is supported for $d.")
end

function rand!(d::VSLDistribution{T}, A::AbstractArray{K}, ::Type{X}=T) where {T, K, X}
# T !== K || T !== X
error("Only $(T) type is supported for $d.")
end
16 changes: 6 additions & 10 deletions src/continuous_distributions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Random: rand, rand!

abstract type VSLContinuousDistribution <: VSLDistribution end
abstract type VSLContinuousDistribution{T} <: VSLDistribution{T} end

macro vsl_distribution_continuous(name, methods, tmp, properties...)
t2 = :(Union{})
Expand All @@ -9,7 +9,7 @@ macro vsl_distribution_continuous(name, methods, tmp, properties...)
push!(t2.args, method)
end
code = quote
mutable struct $name{T1<:Union{Cfloat, Cdouble}, T2<:$t2} <: VSLContinuousDistribution
mutable struct $name{T1<:Union{Cfloat, Cdouble}, T2<:$t2} <: VSLContinuousDistribution{T1}
brng::BasicRandomNumberGenerator
ii::Int
end
Expand All @@ -23,7 +23,7 @@ macro vsl_distribution_continuous(name, methods, tmp, properties...)

push!(code.args, :(
function $name(brng::BasicRandomNumberGenerator, $(properties...), method::Type{T2}) where {T1<:Union{Cfloat, Cdouble}, T2<:$t2}
tmp = $(tmp.args[2].args[1] == :Vector ? :(Vector{T1}(BUFFER_LENGTH)) : :(Matrix{T1}(dimen, BUFFER_LENGTH)))
tmp = $(tmp.args[2].args[1] == :Vector ? :(zeros(T1, BUFFER_LENGTH)) : :(zeros(T1, dimen, BUFFER_LENGTH)))
$name(brng, BUFFER_LENGTH, tmp, $([property.args[1] for property in properties]...), method)
end
))
Expand Down Expand Up @@ -58,25 +58,21 @@ macro register_rand_functions_continuous(name, methods, method_constants, types,
)
push!(
code.args,
:(function rand!(d::$name{$ttype, $method}, A::Array{$ttype})
:(function rand!(d::$name{$ttype, $method}, A::AbstractArray{$ttype}, ::Type{$ttype}=$ttype)
n = length(A)
t = BUFFER_LENGTH - d.ii
if n <= t
copy!(A, 1, d.tmp, d.ii + 1, n)
copyto!(A, 1, d.tmp, d.ii + 1, n)
d.ii = d.ii + n
return A
end
copy!(A, 1, d.tmp, d.ii + 1, t)
copyto!(A, 1, d.tmp, d.ii + 1, t)
d.ii = BUFFER_LENGTH
ccall(($function_name, libmkl), Cint, (Int, Ptr{Cvoid}, Int, Ptr{$ttype}, $(argtypes...)),
$constant, d.brng.stream_state[1], n - t, view(A, t+1:n), $(arguments.args...))
A
end)
)
push!(
code.args,
:(rand(d::$name{$ttype, $method}, dims::Dims) = rand!(d, Array{$ttype}(dims)))
)
end
end
esc(code)
Expand Down
12 changes: 6 additions & 6 deletions src/discrete_distributions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Random: rand, rand!

abstract type VSLDiscreteDistribution <: VSLDistribution end
abstract type VSLDiscreteDistribution{T} <: VSLDistribution{T} end

macro vsl_distribution_discrete(name, methods, rtype, properties...)
t = :(Union{})
Expand All @@ -9,7 +9,7 @@ macro vsl_distribution_discrete(name, methods, rtype, properties...)
push!(t.args, method)
end
code = quote
mutable struct $name{T<:$t} <: VSLDiscreteDistribution
mutable struct $name{T<:$t} <: VSLDiscreteDistribution{$rtype}
brng::BasicRandomNumberGenerator
ii::Int
tmp::Vector{$rtype}
Expand All @@ -23,7 +23,7 @@ macro vsl_distribution_discrete(name, methods, rtype, properties...)

push!(code.args, :(
$name(brng::BasicRandomNumberGenerator, $(properties...), method::Type{T}) where {T<:$t} =
$name(brng, BUFFER_LENGTH, Vector{$rtype}(BUFFER_LENGTH), $([property.args[1] for property in properties]...), method)
$name(brng, BUFFER_LENGTH, zeros($rtype, BUFFER_LENGTH), $([property.args[1] for property in properties]...), method)
))

push!(code.args, :(
Expand Down Expand Up @@ -53,15 +53,15 @@ macro register_rand_functions_discrete(typename, name, methods, method_constants
)
push!(
code.args,
:(function rand!(d::$typename{$method}, A::Array{$rtype})
:(function rand!(d::$typename{$method}, A::Array{$rtype}, ::Type{$rtype}=$rtype)
n = length(A)
t = BUFFER_LENGTH - d.ii
if n <= t
copy!(A, 1, d.tmp, d.ii + 1, n)
copyto!(A, 1, d.tmp, d.ii + 1, n)
d.ii = d.ii + n
return A
end
copy!(A, 1, d.tmp, d.ii + 1, t)
copyto!(A, 1, d.tmp, d.ii + 1, t)
d.ii = BUFFER_LENGTH
ccall(($function_name, libmkl), Cint, (Int, Ptr{Cvoid}, Int, Ptr{$rtype}, $(types.args...)),
$constant, d.brng.stream_state[1], n - t, view(A, t+1:n), $(arguments.args...))
Expand Down
5 changes: 4 additions & 1 deletion test/common.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using VSL
import Test: @test
import Random: rand, rand!
import Test: @test, @test_throws
import Printf: @printf

brng_types = [
Expand All @@ -17,3 +18,5 @@ macro test_rand(expr)
@printf "%.6f\n" values
end
end

test_folder = joinpath(dirname(pathof(VSL)), "..", "test")
7 changes: 5 additions & 2 deletions test/test_continuous_distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ brng_type = VSL_BRNG_MT19937
@info("Testing continuous distributions with $brng_type...")
brng = BasicRandomNumberGenerator(brng_type, 200701281)

stdout_ = STDOUT
stdout_ = stdout
pwd_ = pwd()
cd(joinpath(Pkg.dir("VSL"), "test/"))
cd(test_folder)
mkpath("./actual")
outfile = open("./actual/continuous.out", "w")
redirect_stdout(outfile)
Expand All @@ -13,6 +13,9 @@ t_uniform = Uniform(brng, 1.0, 5.0, VSL_RNG_METHOD_STD)
@test_rand(rand(t_uniform))
@test_rand(rand(t_uniform, 2, 3))

@test_throws ErrorException rand(t_uniform, UInt64)
@test_throws ErrorException rand!(t_uniform, zeros(UInt64, 5))

t_uniform_s = Uniform(brng, 1.0f0, 5.0f0, VSL_RNG_METHOD_STD)
@test_rand(rand(t_uniform_s))
@test_rand(rand(t_uniform_s, 2, 3))
Expand Down
7 changes: 5 additions & 2 deletions test/test_discrete_distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ brng_type = VSL_BRNG_ARS5
@info("Testing discrete distributions with $brng_type...")
brng = BasicRandomNumberGenerator(brng_type, 200701281)

stdout_ = STDOUT
stdout_ = stdout
pwd_ = pwd()
cd(joinpath(Pkg.dir("VSL"), "test/"))
cd(test_folder)
mkpath("./actual")
outfile = open("./actual/discrete.out", "w")
redirect_stdout(outfile)
Expand All @@ -13,6 +13,9 @@ t_uniform = UniformDiscrete(brng, Cint(1), Cint(10))
@test_rand(rand(t_uniform))
@test_rand(rand(t_uniform, 2, 3))

@test_throws ErrorException rand(t_uniform, Float64)
@test_throws ErrorException rand!(t_uniform, zeros(Float64, 5))

t_uniform_bits = UniformBits(brng)
@test_rand(rand(t_uniform_bits))
@test_rand(rand(t_uniform_bits, 2, 3))
Expand Down

3 comments on commit ca139dd

@sunoru
Copy link
Member Author

@sunoru sunoru commented on ca139dd Sep 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunoru
Copy link
Member Author

@sunoru sunoru commented on ca139dd Sep 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/3363

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" ca139dd7c92ec2c700889b1754cf99c040b2a119
git push origin v0.2.0

Also, note the warning: Version 0.2.0 skips over 0.1.0
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.