# Future.randjump peculiar speed

**URL:** <https://discourse.julialang.org/t/future-randjump-peculiar-speed/34862>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [February 19, 2020, 4:40pm UTC](https://discourse.julialang.org/t/future-randjump-peculiar-speed/34862 "2020-02-19T16:40:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [February 19, 2020, 4:40pm UTC](https://discourse.julialang.org/t/future-randjump-peculiar-speed/34862/1 "2020-02-19T16:40:30Z")

</div>

At my company we typically break up the simulations into predefined fixed “chunks”, and assign a seed to each chunk this way the dynamic scheduler in the threading is repeatable no matter the number of threads.

I have been using `Future.randjump` to generate a new seed/state for each chunk. After experimenting a bit, it seems that the `randjump` is pretty slow for the default jump `big(10)^20`. I think this is supposed to be predefined.I thought by using this jump amount `randjump` would be faster.

In fact, I noticed no change in runtime for generating, 10 seeds, if I used the default of `big(10)^20` or not.

Any thoughts on how to speed up `randjump` or or why the default isn’t faster? In my “real world” code, I am calling `randjump` in separate threads to speed things up, but it is actually slower than my simulation.

```nohighlight
using Future, Random

seed = 1234
nchunks = 10
jumpamt = big(10)^20

a = fill(MersenneTwister(),nchunks)

m = MersenneTwister(seed)
a[1] = m

for i = 2:nchunks
 @inbounds a[i] = Future.randjump(m,jumpamt)
end 

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 21, 2020, 3:05am UTC](https://discourse.julialang.org/t/future-randjump-peculiar-speed/34862/2 "2020-02-21T03:05:07Z")

</div>

Looking at `calc_jump` (called via `randjump`)

> <https://github.com/JuliaLang/julia/blob/8009429a7b236dd6aacd5929f9f09706fa2a2ec3/stdlib/Random/src/DSFMT.jl#L175-L184>

it looks like any `steps` is cached. So I guess you don’t see the difference if you use the same `steps` more than two times.

To be fair, the computation cached by `calc_jump` does seem to be much faster for `big(10)^20` than other values, compared to the rest of what `Future.randjump` does:

```julia
julia> @btime Future.randjump(m, big(10)^20) setup=(m = MersenneTwister(0));
  14.303 ms (17 allocations: 22.77 KiB)

julia> @btime Random.DSFMT.GF2X(Random.DSFMT.JPOLY1e20);
  17.986 μs (5 allocations: 7.52 KiB)

julia> @btime Random.DSFMT.powxmod(big(10)^20 + 1, Random.DSFMT.CharPoly());
  92.881 ms (17266 allocations: 12.34 MiB)

```

> [@stephenll](#):
>
> calling `randjump` in separate threads to speed things up

By the way, I just realized that `calc_jump` uses a bare `Dict` to cache the jump polynomials:

> <https://github.com/JuliaLang/julia/blob/8009429a7b236dd6aacd5929f9f09706fa2a2ec3/stdlib/Random/src/DSFMT.jl#L156-L157>

So, using `randjump` from different threads seems to be a bit dangerous, unless you call `randjump` first in the main thread.

> [@stephenll](#):
>
> ```julia
> for i = 2:nchunks
> @inbounds a[i] = Future.randjump(m,jumpamt)
> end
> 
> ```

Just a nitpick, but I guess you meant to write `a[i-1]` instead of `m`?
