Skip to content

Performance fixes for rand #156

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

Merged
merged 5 commits into from
Sep 24, 2022
Merged

Performance fixes for rand #156

merged 5 commits into from
Sep 24, 2022

Conversation

nsajko
Copy link
Contributor

@nsajko nsajko commented Sep 22, 2022

Some small fixes.

The current code is `zero(DoubleFloat(T))`, which seems like a typo of
`DoubleFloat(zero(T))`.

Even though this branch is reached only rarely/never, this change
should help Julia generate better code.
@codecov
Copy link

codecov bot commented Sep 22, 2022

Codecov Report

Base: 48.75% // Head: 48.78% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (0ab751e) compared to base (ac31d99).
Patch coverage: 25.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #156      +/-   ##
==========================================
+ Coverage   48.75%   48.78%   +0.02%     
==========================================
  Files          63       63              
  Lines        3425     3425              
==========================================
+ Hits         1670     1671       +1     
+ Misses       1755     1754       -1     
Impacted Files Coverage Δ
src/extras/random.jl 13.04% <25.00%> (+1.44%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

`s===0` is equivalent to `isa(s,Int)&iszero(s)` which doesn't seem
like the intended behavior.

Also changed `||` to `|` because these comparisons don't seem to be
costly enough to warrant the branch.
@@ -1,8 +1,9 @@
function rand(rng::AbstractRNG, ::Random.SamplerTrivial{Random.CloseOpen01{DoubleFloat{T}}}) where {T<:IEEEFloat}
hi, lo = rand(rng, T, 2)
hi = rand(rng, T)
lo = rand(rng, T)
Copy link
Member

Choose a reason for hiding this comment

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

This is unfortunate because vectorized random numbers are faster to generate. Do we need a better idiom in Julia for generating a small fixed amount of random numbers?

Choose a reason for hiding this comment

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

Maybe this won't be needed once the compiler get the full power of escape analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess that StaticArrays hits the same problem with their rand functions. I tried taking a look at the code, but all the generated functions made me nauseous: https://github.com/JuliaArrays/StaticArrays.jl/blob/master/src/arraymath.jl#L38

On the other hand, maybe the only real fix would be for Julia to provide a contiguous stack-allocated array type, because I don't think that tuple operations can be vectorized because they don't necessarily have contiguous layout in memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did some measurements now, and it turns out that separate rand() calls seem to be faster than rand!(array), at least for tiny lengths like 2 and 4. For length two:

using Random, BenchmarkTools

f(::Val{n}) where {n} = ntuple(i -> rand(), Val{n}())

# warm up
rand!(zeros(2))
f(Val(2))

@benchmark rand!(a) setup = (a = zeros(2);)  # Time  (median):     5.691 ns
@benchmark rand!(a) setup = (a = zeros(2); Random.seed!(1234))  # Time  (median):     5.851 ns

@benchmark f(Val(2))  # Time  (median):     4.949 ns
@benchmark f(Val(2)) setup = (Random.seed!(1234);)  # Time  (median):     4.950 ns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The above measurements are with nightly Julia.

@JeffreySarnoff
Copy link
Member

Thank you. This is great.

@JeffreySarnoff JeffreySarnoff merged commit eb0723f into JuliaMath:master Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants