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

set random seed for batch job #227

Open
mheldman opened this issue May 24, 2022 · 6 comments
Open

set random seed for batch job #227

mheldman opened this issue May 24, 2022 · 6 comments

Comments

@mheldman
Copy link

Hi,

I am using Readdy for a reversible A + B <-> C simulation. I want to compute some statistics to high accuracy and so I am running a lot of trials, which would best be done as a batch job. Is there a way to set the seed for the random number generator for each job?

Sincerely,

Max Heldman

@clonker
Copy link
Member

clonker commented May 25, 2022

Hi Max,

unfortunately it is currently not possible to set the seed manually, but it would a nice feature to have indeed. Part of the problem is that random generators are not thread-safe (because they are stateful) so we instantiate new ones per thread which are all randomly seeded, otherwise you'd get artifacts. I could imagine setting the seed for the singlecpu (single-threaded) implementation, though. That one is much better suited for sampling anyways.
Right now I don't have the capacity to implement this but I'll put it on my to-do list. Or you can have a stab at implementing it under my guidance.

Cheers,
Moritz

@mheldman
Copy link
Author

mheldman commented May 25, 2022

Hi Moritz,

Yes, I am running in single cpu mode- I'm using fully reversible reaction dynamics which it seems does not have a multithread implementation.

I'm happy to try to implement it myself with guidance. I would have tried already but I am not that comfortable with C++ so the layout of the source code is a little complicated for me. What I'm imagining is adding an option to the initialization of a simulation to set the seed, and then a function to get the seed as well (along with the corresponding python interface).

Max

@clonker
Copy link
Member

clonker commented May 25, 2022

The first step would be implementing an option to the KernelConfiguration of the single cpu kernel (see here). You can take inspiration on how to implement that based on the other kernels and their configurations. A random generator (as in here) should be saved in the SCPUStateModel. We can worry about the places where the generator has to be used and the python binding afterwards. :)

@mheldman
Copy link
Author

mheldman commented May 28, 2022

Ok, I think for now I am going to use a workaround by setting the seed in an environment variable and modifying the randomlySeededGenerator function to read its value:

template<typename Generator = std::mt19937>
Generator randomlySeededGenerator() {
    std::random_device r;
    std::random_device::result_type env_seed;
    char *seed_char = std::getenv("READDY_RNG_SEED");
    if(!seed_char) {
      env_seed = r();
    } else {
      std::string seed_str(seed_char);
      env_seed = std::stoi(seed_str);
    }
    std::random_device::result_type threadId = std::hash<std::thread::id>()(std::this_thread::get_id());
    std::random_device::result_type clck = clock();
    std::seed_seq seed{threadId, env_seed, r(), clck, r(), r(), r(), r()};
    return Generator(seed);
}

By the way, I think readdy is not compatible with the newest version of catch2 right now. In version 3 they changed the name of their header file. For the conda build script to work I had to set catch2=2 in meta.yaml.

Max

@clonker
Copy link
Member

clonker commented May 28, 2022

Hi, the way you pasted it here it is (unless im missing something) still not deterministic. Only if the std::seed_seq is completely initialized from your environment variable you will have quasireproducible results. Thanks for letting me know about the new catch2 version, will fix asap. :)

@mheldman
Copy link
Author

Yep, it's not deterministic as written but as you suggest it can be modified to be so (maybe replace the r() and clck with zeros).

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