CovertLab / arrow

Stochastic simulations in python
MIT License
3 stars 1 forks source link

Serialize PRNG state #47

Closed U8NWXD closed 3 years ago

U8NWXD commented 3 years ago

When serializing a StochasticSimulation, we should also serialize the state of the Mersenne Twister pseudo-random number generator so that a simulation can be serialized mid-simulation without losing any state.

U8NWXD commented 3 years ago

When I run the tests locally on master, test_flagella fails.

U8NWXD commented 3 years ago

It's been a while since I've written C code, so please check the C code in this PR extra carefully

U8NWXD commented 3 years ago

@1fish2 Thanks for your comments! I've addressed them. Could you take another look?

1fish2 commented 3 years ago

Sorry! I wrote this snippet wrong:

copy_c_array(
    &state, mersenne.TWISTER_SIZE, sizeof(MTState),
    np.NPY_UINT32)

It should be more like:

copy_c_array(
    &state, sizeof(mersenne.MTState) / sizeof(uint32_t), sizeof(uint32_t),
    np.NPY_UINT32)
U8NWXD commented 3 years ago

@1fish2 thanks! I've made the changes you requested. I decided to not copy the whole MTState struct into a single numpy array though since doing so doesn't make the code that much simpler but adds some unnecessary conceptual overhead. Please let me know if you have any other comments!

U8NWXD commented 3 years ago

@eagmon deferred offline to @1fish2 for review

1fish2 commented 3 years ago

It's great!

And it reminds me how awful C is, not to speak of C++. We'll never have secure computing with so much C/C++ code in the world. Hmm, maybe the seed of a scary pumpkin idea...

Sometime I'd like to hear about the Vivarium parallelization strategy.