Skip to content
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

Simplify the PRNG code and usage #148

Closed
wants to merge 6 commits into from
Closed

Simplify the PRNG code and usage #148

wants to merge 6 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Dec 7, 2014

The patch replaces the concept of magic boosters by a set a well chosen PRNG seeds, allowing 30% quicker magics init; a simpler PRNG design particularly easy to seed; and for skill handicap play, does away with RNG sequence skipping.

Passed STC:
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 17668 W: 3628 L: 3500 D: 10540

and LTC:
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 73703 W: 12281 L: 12245 D: 49177

Ernesto Gatti and others added 6 commits November 27, 2014 19:45
This patch replaces RKISS by a simpler and faster PRNG, xorshift64* proposed
by S. Vigna (2014). It is extremely simple, has a large enough period for
Stockfish's needs (2^64), requires no warming-up (allowing such code to be
removed), and offers slightly better randomness than MT19937.

Paper: http://xorshift.di.unimi.it/
Reference source code (public domain):
http://xorshift.di.unimi.it/xorshift64star.c

The patch also simplifies how init_magics() searches for magics:

- Old logic: seed the PRNG always with the same seed,
  then use optimized bit rotations to tailor the RNG sequence per rank.

- New logic: seed the PRNG with an optimized seed per rank.

This has two advantages:
1. Less code and less computation to perform during magics search (not ROTL).
2. More choices for random sequence tuning. The old logic only let us choose
from 4096 bit rotation pairs. With the new one, we can look for the best seeds
among 2^64 values. Indeed, the set of seeds[][] provided in the patch reduces
the effort needed to find the magics:

64-bit SF:
Old logic -> 5,783,789 rand64() calls needed to find the magics
New logic -> 4,420,086 calls

32-bit SF:
Old logic -> 2,175,518 calls
New logic -> 1,895,955 calls

In the 64-bit case, init_magics() take 25 ms less to complete (Intel Core i5).

Finally, when playing with strength handicap, non-determinism is achieved
by setting the seed of the static RNG only once. Afterwards, there is no need
to skip output values.

The bench only changes because the Zobrist keys are now different (since they
are random numbers straight out of the PRNG).

New bench: 7573215
With these seeds, magics are found even faster.
On Intel Core i5, time spent in init_magics is now 185 ms,
vs 245 ms with former logic (RKISS).
This time, the seed has been carefully chosen so that the
resulting Zobrist keys are particularly well-behaved:

1. All triplets of XORed keys are unique, implying that it
   would take at least 7 keys to find a 64-bit collision
   (test suggested by ceebo)

2. All pairs of XORed keys are unique modulo 2^32

3. The cardinality of { (key1 ^ key2) >> 48 } is as close
   as possible to the maximum (65536)

Point 2 aims at ensuring a good distribution among the bits
that determine an TT entry's cluster, likewise point 3
among the bits that form the TT entry's key16 inside a
cluster.

Details:

     Bitset   card(key1^key2)
     ------   ---------------
RKISS
     key16     64894   = 99.020% of theoretical maximum
     low18    180117   = 99.293%
     low32    305362   = 99.997%

Xorshift64*, old seed
     key16     64918   = 99.057%
     low18    179994   = 99.225%
     low32    305350   = 99.993%

Xorshift64*, new seed
     key16     65027   = 99.223%
     low18    181118   = 99.845%
     low32    305371   = 100.000%
Class is small enough that it doesn't deserve its own file.
Suggested my Marco.
@glinscott
Copy link
Contributor

Nice! Changes look good to me.

@mcostalba
Copy link

As the author of (part of) the code this patch replaces I am very happy it
passed LTC because this solution is much nicer than the original.

s
On Sunday, December 7, 2014, rnysto [email protected] wrote:

The patch replaces the concept of magic boosters by a set a well chosen
PRNG seeds, allowing 30% quicker magics init; a simpler PRNG design
particularly easy to seed; and for skill handicap play, does away with RNG
sequence skipping.

Passed STC:
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 17668 W: 3628 L: 3500 D: 10540

and LTC:
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]

Total: 73703 W: 12281 L: 12245 D: 49177

You can merge this Pull Request by running

git pull https://github.com/rnysto/Stockfish simpleprng

Or view, comment on, or merge it at:

#148
Commit Summary

  • Simpler PRNG and faster magics search
  • Better seeds for finding magics
  • Change PRNG seed for Zobrist keys
  • Merge branch 'master' into simpleprng
  • Merge branch 'master' into simpleprng
  • Move PRNG into misc.h

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#148.

@zamar
Copy link

zamar commented Dec 7, 2014

Approved. RKiss was an overkill for Stockfish, so I'm happy that we can switch to something more simple.

@glinscott glinscott closed this in 1588642 Dec 8, 2014
niklasf pushed a commit to niklasf/Stockfish that referenced this pull request Jan 5, 2017
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.

3 participants