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

xoshiro265** uniform random number generator #1769

Merged
merged 38 commits into from
Sep 28, 2023

Conversation

ralfkonrad
Copy link
Contributor

Over the last couple of years the family of xoshiro / xoroshiro pseudorandom number generators has become popular and is being adopted in a couple of programming languages as the new default prng or at least as one of its optional ones. The main reason for this family is its performance, it is faster than the Mersenne twister.

Out of this family I have implemented xoshiro265** here which has a of period 2^256-1.

On my machine this implementation is approx. twice as fast in returning a double and still 20% faster for the BoxMullerGaussian.next() compared to Mersenne Twister.

Windows benchmark

--------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations
--------------------------------------------------------------------------------------
xoshiro256StarStar.nextInt64();                  0.734 ns        0.732 ns    896000000
mersenneTwister.nextInt32();                      1.45 ns         1.44 ns    497777778

xoshiro256StarStar.next();                       0.734 ns        0.732 ns    896000000
mersenneTwister.next();                           1.46 ns         1.44 ns    497777778

xoshiro256StarStarBoxMullerGaussian.next();       9.06 ns         8.79 ns     64000000
mersenneTwisterBoxMullerGaussian.next();          11.6 ns         11.5 ns     64000000

xoshiro256StarStarCLGaussian.next();              19.7 ns         19.7 ns     37333333
mersenneTwisterCLGaussian.next();                 18.0 ns         18.0 ns     40727273

Ubuntu wsl benchmark

--------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations
--------------------------------------------------------------------------------------
xoshiro256StarStar.nextInt64();                  0.677 ns        0.677 ns    999294783
mersenneTwister.nextInt32();                      1.48 ns         1.48 ns    464873186

xoshiro256StarStar.next();                       0.676 ns        0.676 ns   1000000000
mersenneTwister.next();                           1.47 ns         1.47 ns    467262894

xoshiro256StarStarBoxMullerGaussian.next();       9.03 ns         9.03 ns     76846821
mersenneTwisterBoxMullerGaussian.next();          11.1 ns         11.1 ns     63022694

xoshiro256StarStarCLGaussian.next();              8.15 ns         8.15 ns     84570989
mersenneTwisterCLGaussian.next();                 13.9 ns         13.9 ns     50685338

ralfkonrad and others added 27 commits August 6, 2023 16:17
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@ralfkonrad
Copy link
Contributor Author

As a sidenote:

I'm testing the implementation against the reference implementation in https://prng.di.unimi.it/xoshiro256starstar.c by loading this c file unchanged in the Xoshiro256StarStarTest source file.

extern "C" {
#include "xoshiro256starstar.c"
}

A little bit odd perhaps?!

@coveralls
Copy link

coveralls commented Aug 16, 2023

Coverage Status

coverage: 71.913% (+0.01%) from 71.902% when pulling 73d8568 on ralfkonrad:xoshiro_random_number_generator into ac9d996 on lballabio:master.

Copy link
Contributor

@pcaspers pcaspers left a comment

Choose a reason for hiding this comment

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

Great stuff! Would it be safer to use uint64_t instead of unsigned long long?

@ralfkonrad
Copy link
Contributor Author

Would it be safer to use uint64_t instead of unsigned long long?

Haven't thought about it. 🤔

According to https://en.cppreference.com/w/cpp/language/types currently unsigned long long is defined as "at least 64" bits in the standard and all implementations (seem to) use 64 bits. But to be on the safe side probably uint64_t is the better choice.

ql/math/randomnumbers/xoshiro256starstaruniformrng.cpp Outdated Show resolved Hide resolved
ql/math/randomnumbers/xoshiro256starstaruniformrng.cpp Outdated Show resolved Hide resolved
test-suite/xoshiro256starstar.c Outdated Show resolved Hide resolved
test-suite/xoshiro256starstar.cpp Outdated Show resolved Hide resolved
@ralfkonrad ralfkonrad marked this pull request as draft August 17, 2023 20:06
@ralfkonrad ralfkonrad force-pushed the xoshiro_random_number_generator branch from 0d490e9 to c47b1cb Compare August 17, 2023 21:10
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lballabio Little bit unsure about the SplitMix64 class here. Do we want it to be perhaps only an inner nested class in Xoshiro256StarStarUniformRng?

Not sure if it is a fully prng? Do we want to use its functionality outside of Xoshiro256StarStarUniformRng later on (separate class) or just for initialize xoshiro++ (inner nested)?

Copy link
Owner

Choose a reason for hiding this comment

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

I think it's ok here.

Copy link
Contributor Author

@ralfkonrad ralfkonrad Aug 19, 2023

Choose a reason for hiding this comment

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

Okay, fine with me. But then it also needs to be tested.

Copy link
Owner

Choose a reason for hiding this comment

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

True. I wouldn't bother. It can be an inner class for the time being, or it can even be in an anonymous namespace in the cpp file.

s1_ = splitMix64.next();
s2_ = splitMix64.next();
s3_ = splitMix64.next();
} while (s0_ == 0 && s1_ == 0 && s2_ == 0 && s3_ == 0);
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this is ever going to happen.

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 fully agree, it's very, very unlikely.

I have "stolen" the idea from c#. Random number generator guys are probably extra paranoid careful. 😄

Personally I would keep it here as a reminder what theoretically might go wrong. But I leave it up to you.

https://github.com/dotnet/runtime/blob/a5ff66c825fab95898c84a57ff305bece82f352d/src/libraries/System.Private.CoreLib/src/System/Random.Xoshiro256StarStarImpl.cs#L33-L45

Copy link
Owner

Choose a reason for hiding this comment

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

I'd take it out. If the splitmix rng gives you 4 zeroes in a row, it's probably going to keep giving you zeroes.

@lballabio
Copy link
Owner

@ralfkonrad should we finalize this? I can do the last changes if you don't mind.

@ralfkonrad
Copy link
Contributor Author

ralfkonrad commented Sep 28, 2023

@lballabio Sorry, busy times plus Corona virus prevented making progress here. Do I have this weekend to finish it?

Or do you want to ensure to get it into next release? Then please go ahead.

@lballabio
Copy link
Owner

Sorry to hear that—are you ok now? In any case, don't use your weekend for this, I'll do the changes :)

@lballabio lballabio marked this pull request as ready for review September 28, 2023 09:31
@lballabio lballabio added this to the Release 1.32 milestone Sep 28, 2023
@lballabio lballabio merged commit 75accb5 into lballabio:master Sep 28, 2023
@ralfkonrad ralfkonrad deleted the xoshiro_random_number_generator branch September 30, 2023 13:36
@ralfkonrad
Copy link
Contributor Author

Thanks @lballabio for picking this up. I would have probably over-engineered it in the approach I had in mind... 😃

I'm okay, but it took a while. Needed a lot of rest besides the regular work.

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