RNGPool.jl

A pool of random number generators for multi-threaded applications
Author awllee
Popularity
0 Stars
Updated Last
3 Years Ago
Started In
July 2018

RNGPool.jl

CI codecov

This package provides a simple interface for thread-specific random number generators (RNGs).

Currently, Threefry4x RNGs are used, as implemented in RandomNumbers.jl.

At runtime, a Vector of Threads.nthreads() RNGs is initialized when the package is loaded.

Calling getRNG() will return the RNG associated to the thread on which it is called.

Calling setRNGs(v::Int64) sets the RNGs so that output is reproducible.

Example usage:

using RNGPool

# on return, each element of out is the average of many Uniform(0,1) pseudo-random variates
function foo!(out::Vector{Float64}, N::Int64)
  nt = Threads.nthreads()
  M::Int64 = div(N, nt)
  Threads.@threads for i in 1:nt
    rng::RNG = getRNG()
    v::Float64 = 0.0
    for j in 1:M
      v += rand(rng)
    end
    out[i] = v / M
  end
end

nt = Threads.nthreads()
out = Vector{Float64}(undef, nt)

setRNGs(1)

N0 = 2^25
foo!(out, N0)

Big Crush Output with 16 threads:

$ julia -O3 test/bigCrush.jl
Testing circular RNG with 16 threads
20297.402768 seconds (3.13 M allocations: 169.970 MiB, 0.00% gc time)

Big Crush p-values:

0.35024617233125155
0.8835532763583632
0.6636497877293779
0.018903942941190053
0.5169888091502591
0.48459348174551337
0.7209339139632519
0.4953826520232639
0.9024402475518911
0.5
0.8651926099525509
0.9561154784011432
0.8376321650399638
0.6202341415528604
1.799269319133724e-5
0.8859876568914304
0.22492814670245764
0.3927173570756824
0.4520844575203478
0.8986304417260517
0.038014481966689814
(0.21568858056919513, 0.7389715767238474, 0.9076375138182249, 0.5966883110300443)
(0.2431181574370577, 0.4871669809334896, 0.3231521924042511, 0.9215267021031692)
(0.9712727839965605, 0.33191575846120747, 0.8321392860558294, 0.576450918419529)
(0.8079223429884855, 0.29902676017610325, 0.274672277693068, 0.44596300957381135)
0.6406462673625273
0.5723208292533664
0.8517476719247175
0.6738959447126625
0.3204715210910115
0.033925233014120204
0.8307482626649948
0.5202223295545486
0.0730899510030465
0.9884621513805903
0.5504918111758579
0.07592255377356572
0.8106929224255082
0.9299569648871202
0.7816015502299919
0.6981399845286156
0.11180741408148176
0.5692101750490258
0.6676474222753446
0.8746415962396613
(0.5115042482198283, 0.4466981107162864)
(0.8441064470045998, 0.450098214225219)
(0.5011236928371351, 0.846882944835641)
(0.6052083611219846, 0.11989197241692676)
0.8706050732302925
0.24992576811155562
0.7599705315327361
0.6666798516789317
0.5865513095444427
0.18220660197484762
0.5861954650088912
0.5164889795925813
0.06519595125937305
0.8299875918826629
0.5325072810883595
0.36774868494116086
0.7265016150707939
0.7021869190099859
0.5366819723353544
0.8444332494141247
0.882916216172351
0.49723779059378237
0.2548676909227585
0.2604060378981502
0.6764645024999533
0.9948541696689198
0.5924658062980863
0.42134891840784894
(0.3413158471198531, 0.34416968317789554, 0.2394729304126597, 0.6245575765098257, 0.09818457402629566)
(0.6650635533444043, 0.24782138623974148, 0.743574533545167, 0.27541332372772076, 0.6128708321231922)
(0.9821876686695014, 0.0849037720959841, 0.33329997993956706, 0.2298794954495592, 0.5169204798676033)
(0.5885470974265907, 0.9040681527000856, 0.3897441111991409, 0.22866923396646388, 0.9013466337980678)
(0.21546156684571005, 0.2389096235593432, 0.5139772080346743, 0.6849063061748559, 0.704148823149277)
(0.8876023613895775, 0.5401482944911973, 0.07260663105257459, 0.697180881377814, 0.04297879789302261)
(0.2735042082218486, 0.9303041521766279)
(0.8966399623254027, 0.39194501288887884)
0.1560730722289243
0.22665875528141483
0.9668336806166385
0.6597833962007846
(0.12961526864821815, 0.5)
(0.4598726205050785, 0.6878130593504515)
0.9881687651315975
0.36703506587905493
0.24463319618891602
0.7648482035840278
0.37514384022957814
0.4620117955709462
0.4624403232337113
0.19667608525470504
0.6965966537122446
0.33501810021141654
0.4305745819908624
0.3792369085087328
0.2140086341676749
(0.9753379212091818, 0.1648700050329902)
(0.3586029601856259, 0.36252843463374806)
0.6775217740167053
0.467673260137216
0.925942247194567
0.4705768063135961

Smallest p-value = 1.799269319133724e-5