Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^13: PDL and srand puzzle - MCE v1.892, v1.893 updates

by marioroy (Prior)
on Jun 08, 2024 at 05:55 UTC ( [id://11159830]=note: print w/replies, xml ) Need Help??


in reply to Re^12: PDL and srand puzzle
in thread PDL and srand puzzle

From etj: Could you take a look at the srand code and see if anything is obviously wrong? Also, are you able to see if the duplicates are in groups i.e. sequences?
From Rob: We need to be looking at how those values were derived.

I'm hoping for the PDL development team to chime in. The testing was out of curiosity. Running non-threads here, PDL generates approximately one billion unique. For threads (uncomment use threads at the top of the script), greater than 92% unique. Better than Math::Random::random_normal/random_uniform.

Calling PDL::srandom has no effect for predictability, before spawning workers. I commented out the line in my last post. It requires calling CORE::srand instead.

Care is needed whether to call a PDL function or class method.

PDL::srandom(42); # not PDL->srandom(42) $r = PDL->random(); # not PDL::random();

I completed validation on the MCE side. Spawning child processes (non-threads), nearly one billion unique > 99.99995% for PDL->random, Math::Prime::Util::drand(), and Math::Random::MT::Auto::rand(). 80% ~ 82% unique for Math::Random::random_uniform() and Math::Random::random_normal().

Running threads, Math::Random::random_normal and random_uniform take beyond 2 minutes to output one billion lines and score less than 20% unique.

The relevant code in MCE 1.891 can be found here lines 644-662 and here lines 2036-2071. MCE::Child and MCE::Hobo have similar code.

See also, Random Numbers Overview by danaj.

Update:

It turns out that I can seed the generator inside threads for CORE, Math::Prime::Util, and Math::Random::MT::Auto and have predictable results, matching non-threads. I released MCE v1.892/v1.893, removing the check whether spinning threads. v1.893 preserves calling CORE::srand(N) for older Perl, non-threads.

# Sets the seed of the base generator uniquely between workers. # The new seed is computed using the current seed and ID value. # One may set the seed at the application level for predictable # results (non-thread workers only). Ditto for Math::Prime::Util, # Math::Random, Math::Random::MT::Auto, and PDL. # # MCE 1.892, 2024-06-08 # Removed check if spawning threads i.e. use_threads. # Predictable output matches non-threads for CORE, # Math::Prime::Util, and Math::Random::MT::Auto. # https://perlmonks.org/?node_id=11159834 { my $_wid = $_args[1]; my $_seed = abs($self->{_seed} - ($_wid * 100000)) % 2147483560; CORE::srand($_seed) if (!$self->{use_threads} || $] ge '5.020000'); + # drand48 Math::Prime::Util::srand($_seed) if $INC{'Math/Prime/Util.pm'}; if (!$self->{use_threads}) { PDL::srand($_seed) if $INC{'PDL.pm'} && PDL->can('srand'); # PDL + 2.062 ~ 2.089 PDL::srandom($_seed) if $INC{'PDL.pm'} && PDL->can('srandom'); # + PDL 2.089_01+ } } if (!$self->{use_threads} && $INC{'Math/Random.pm'}) { my ($_wid, $_cur_seed) = ($_args[1], Math::Random::random_get_seed( +)); my $_new_seed = ($_cur_seed < 1073741781) ? $_cur_seed + (($_wid * 100000) % 1073741780) : $_cur_seed - (($_wid * 100000) % 1073741780); Math::Random::random_set_seed($_new_seed, $_new_seed); } if ($INC{'Math/Random/MT/Auto.pm'}) { my ($_wid, $_cur_seed) = ( $_args[1], Math::Random::MT::Auto::get_seed()->[0] ); my $_new_seed = ($_cur_seed < 1073741781) ? $_cur_seed + (($_wid * 100000) % 1073741780) : $_cur_seed - (($_wid * 100000) % 1073741780); Math::Random::MT::Auto::set_seed($_new_seed); }

Replies are listed 'Best First'.
Re^14: PDL and srand puzzle - MCE v1.892, v1.893 updates
by etj (Priest) on Jun 09, 2024 at 14:15 UTC
    I'm hoping for the PDL development team to chime in.
    I honestly wonder who you think that is.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11159830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2025-02-09 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (96 votes). Check out past polls.