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

Provide full seed and counter to underlying generator #15

Open
joaander opened this issue Dec 6, 2023 · 4 comments
Open

Provide full seed and counter to underlying generator #15

joaander opened this issue Dec 6, 2023 · 4 comments

Comments

@joaander
Copy link

joaander commented Dec 6, 2023

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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

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

No branches or pull requests

2 participants