Skip to content

Commit

Permalink
Merge pull request #21 from JuliaSIMD/passfuncs
Browse files Browse the repository at this point in the history
allow passing functions
  • Loading branch information
chriselrod authored Oct 9, 2022
2 parents 46d8e5a + f787764 commit 408cc72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VectorizedRNG"
uuid = "33b4df10-0173-11e9-2a0c-851a7edac40e"
authors = ["Chris Elrod <[email protected]>"]
version = "0.2.19"
version = "0.2.20"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
36 changes: 23 additions & 13 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
@inline _vload(ptr::VectorizationBase.AbstractStridedPointer, args::Vararg{Any,K}) where {K} = vload(ptr, args...)
@inline _vload(x::Number, args::Vararg{Any,K}) where {K} = x

function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, α, β, γ) where {F,P,T}
function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, α, β, γ, g::G=identity) where {F,P,T,G}
state = getstate(rng, Val{2}(), pick_vector_width(UInt64))
GC.@preserve x begin
ptrx = zero_pointer(x); ptrβ = zero_pointer(β); ptrγ = zero_pointer(γ);
Expand All @@ -179,8 +179,8 @@ function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, α,
z₁, z₂ = data(zvu2)
x₁ = vload(ptrx, (n,)); β₁ = _vload(ptrβ, (n,)); γ₁ = _vload(ptrγ, (n,));
x₂ = vload(ptrx, (vadd(W, n),)); β₂ = _vload(ptrβ, (vadd(W, n),)); γ₂ = _vload(ptrγ, (vadd(W, n),));
vstore!(ptrx, α * x₁ + z₁ * γ₁ + β₁, (n,));
vstore!(ptrx, α * x₂ + z₁ * γ₂ + β₂, (vadd(W, n),));
vstore!(ptrx, g(α * x₁ + z₁ * γ₁ + β₁), (n,));
vstore!(ptrx, g(α * x₂ + z₁ * γ₂ + β₂), (vadd(W, n),));
n = vadd(W2, n)
end
m = VectorizationBase.mask(W, N)
Expand All @@ -189,19 +189,19 @@ function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, α,
z₁, z₂ = data(zvu2)
x₁ = vload(ptrx, (n,)); β₁ = _vload(ptrβ, (n,)); γ₁ = _vload(ptrγ, (n,));
x₂ = vload(ptrx, (vadd(W, n),), m); β₂ = _vload(ptrβ, (vadd(W, n),), m); γ₂ = _vload(ptrγ, (vadd(W, n),), m);
vstore!(ptrx, α * x₁ + z₁ * γ₁ + β₁, (n,));
vstore!(ptrx, α * x₂ + z₂ * γ₂ + β₂, (vadd(W, n),), m);
vstore!(ptrx, g(α * x₁ + z₁ * γ₁ + β₁,), (n,));
vstore!(ptrx, g(α * x₂ + z₂ * γ₂ + β₂,), (vadd(W, n),), m);
elseif scalar_less(n, N)
state, zvu1 = f(state, Val{1}(), T)
(z₁,) = data(zvu1)
x₁ = vload(ptrx, (n,), m); β₁ = _vload(ptrβ, (n,), m); γ₁ = _vload(ptrγ, (n,), m);
vstore!(ptrx, α * x₁ + z₁ * γ₁ + β₁, (n,), m);
vstore!(ptrx, g(α * x₁ + z₁ * γ₁ + β₁), (n,), m);
end
storestate!(rng, state)
end # GC preserve
x
end
function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, ::StaticInt{0}, β, γ) where {F,P,T}
function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, ::StaticInt{0}, β, γ,g::G=identity) where {F,P,T,G}
state = getstate(rng, Val{2}(), pick_vector_width(UInt64))
GC.@preserve x begin
ptrx = zero_pointer(x); ptrβ = zero_pointer(β); ptrγ = zero_pointer(γ);
Expand All @@ -213,8 +213,8 @@ function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, ::St
(z₁,z₂) = data(zvu2)
β₁ = _vload(ptrβ, (n,)); γ₁ = _vload(ptrγ, (n,));
β₂ = _vload(ptrβ, (vadd(W, n),)); γ₂ = _vload(ptrγ, (vadd(W, n),));
vstore!(ptrx, z₁ * γ₁ + β₁, (n,));
vstore!(ptrx, z₂ * γ₂ + β₂, (vadd(W, n),));
vstore!(ptrx, g(z₁ * γ₁ + β₁), (n,));
vstore!(ptrx, g(z₂ * γ₂ + β₂), (vadd(W, n),));
n = vadd(W2, n)
end
m = VectorizationBase.mask(W, N)
Expand All @@ -223,13 +223,13 @@ function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, ::St
(z₁,z₂) = data(zvu2)
β₁ = _vload(ptrβ, (n,)); γ₁ = _vload(ptrγ, (n,));
β₂ = _vload(ptrβ, (vadd(W, n),), m); γ₂ = _vload(ptrγ, (vadd(W, n),), m);
vstore!(ptrx, z₁ * γ₁ + β₁, (n,));
vstore!(ptrx, z₂ * γ₂ + β₂, (vadd(W, n),), m);
vstore!(ptrx, g(z₁ * γ₁ + β₁), (n,));
vstore!(ptrx, g(z₂ * γ₂ + β₂), (vadd(W, n),), m);
elseif scalar_less(n, N)
state, zvu1 = f(state, Val{1}(), T)
(z₁,) = data(zvu1)
β₁ = _vload(ptrβ, (n,), m); γ₁ = _vload(ptrγ, (n,), m);
vstore!(ptrx, z₁ * γ₁ + β₁, (n,), m);
vstore!(ptrx, g(z₁ * γ₁ + β₁), (n,), m);
end
storestate!(rng, state)
end # GC preserve
Expand All @@ -238,11 +238,21 @@ function random_sample_u2!(f::F, rng::AbstractVRNG{P}, x::AbstractArray{T}, ::St
x
end


function Random.rand!(
f::F,
rng::AbstractVRNG, x::AbstractArray{T}, α::Number = StaticInt{0}(), β = StaticInt{0}(), γ = StaticInt{1}()
) where {T <: Union{Float32,Float64},F}
random_sample_u2!(random_uniform, rng, x, α, β, γ, f)
end

function Random.rand!(
rng::AbstractVRNG, x::AbstractArray{T}, α::Number = StaticInt{0}(), β = StaticInt{0}(), γ = StaticInt{1}()
) where {T <: Union{Float32,Float64}}
random_sample_u2!(random_uniform, rng, x, α, β, γ)
random_sample_u2!(random_uniform, rng, x, α, β, γ, identity)
end


function Random.randn!(
rng::AbstractVRNG, x::AbstractArray{T}, α::Number = StaticInt{0}(), β = StaticInt{0}(), γ = StaticInt{1}()
) where {T<:Union{Float32,Float64}}
Expand Down

2 comments on commit 408cc72

@chriselrod
Copy link
Member Author

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/69783

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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.20 -m "<description of version>" 408cc72b3c8d6683be1240ef1c2aa7a07c83670a
git push origin v0.2.20

Please sign in to comment.