brian-team / brian2

Brian is a free, open source simulator for spiking neural networks.
http://briansimulator.org
Other
946 stars 221 forks source link

Random number generation on C++ standalone is slow #1483

Closed mstimberg closed 2 months ago

mstimberg commented 1 year ago

I only just now realized that random number generation on C++ standalone is slow compared to Cython/numpy. E.g., with a trivial example like:

G = NeuronGroup(10000, 'x : 1')
G.run_regularly('x = rand()')

the run_regularly code object uses ~2.5s on Cython and numpy, and ~7s on C++ standalone. My hunch was that this is because in Cython/numpy, we create a buffer of random numbers at once using numpy's rand function, whereas in C++ standalone, we call rk_double for each number we need. But it might be more fundamental: numpy switched to a different random number generator a couple of years ago, while in C++ standalone, we are still using the Mersenne-Twister "legacy code"…

Not sure how feasible/easy it is to switch to the same generator in C++ standalone.

This could also be the time to tackle #19, but I don't see any obvious/simple solution.

mstimberg commented 1 year ago

Note to self, try TRNG: https://www.numbercrunch.de/trng/

mstimberg commented 1 year ago

And https://github.com/DEShawResearch/random123

mstimberg commented 1 year ago

Also see: https://github.com/google/jax/blob/main/docs/jep/263-prng.md