msu-sparta / OpenRAND

Reproducible random number generation for parallel computations
https://msu-sparta.github.io/OpenRAND/
MIT License
24 stars 2 forks source link

Provide full seed and counter to underlying generator #15

Open joaander opened 7 months ago

joaander commented 7 months ago

Would it be possible to add an optional API to OpenRAND that exposes the full number of bits of the counter state?

I currently use Philox4x32 random123 in HOOMD-blue: https://github.com/glotzerlab/hoomd-blue. There are portions of the code where I use the full 128-bit counter that Philox4x32 provides. If OpenRAND had an API for 128 bit counter initialization, I would consider migrating HOOMD-blue from random123 to OpenRAND. This could also be an opportunity to test OpenRAND with HIP.

You can see HOOMD's use of Philox4x32 in this code: https://github.com/glotzerlab/hoomd-blue/blob/trunk-patch/hoomd/RandomNumbers.h

Shihab-Shahriar commented 6 months ago

Thanks @joaander for your interest in OpenRAND.

I've given this some thought. and I think it's possible without breaking the current API, although the solution might not look the most elegant.

Please note: fully exposing the 128-bit Philox counter via the constructor isn't feasible, but we can manage 96 bits. Each generator instance maintains its own internal counter, which means users won't need to manually increment the counter after generating each number. This enables usage like:

RNG rng(1, 0, global_seed);
float sum = 0;
for(int i=0; i<32; i++)
    sum += rng.rand<float>();        // range [0,1)

Instead of this:

RNG rng(1, 0, global_seed);
float sum = 0;
for(int i=0; i<32; i++){
    sum += rng.rand<float>();        // range [0,1)
    rng.counters[0]++;
}
joaander commented 6 months ago

Understood, and thanks for considering my request. I use the same type of internal counter setup in my own thin wrapper around Philox - so OpenRAND will work really well.

I can work with 96 bits in the constructor. There are a few places where HOOMD currently uses 112 bits, but I will be deprecating and removing that code soon. Everywhere else uses 96 or fewer bits.

Shihab-Shahriar commented 6 months ago

Hi @joaander, I have pushed the relevant code to OpenRAND. An exxample use of current Philox API would be:

openrand::Philox rng(seed_64, ctr0_32, ctr1_32, ctr2_32);
float sum = 0;
for(int i=0; i<32; i++)
    sum += rng.rand<float>();        // range [0,1)

Let me know what you think.

joaander commented 6 months ago

Thanks! I will test this in HOOMD when I get a chance and let you know how it goes.