Add proper OpenMP support
alexgenin opened this issue · 1 comments
Right now the support for OpenMP has a lot of overhead, to the extent that it is counterproductive to enable it when we precompute probabilities. This is potentially due to
- the xoshiro RNG which is not thread safe, and thus requires producing a matrix of random numbers in advance on a single thread
- the fact that updating the state change matrix needs blocking
This should be fixed. This needs some thinking also to not make the code full of cryptic #pragmas.
So right now I fixed the xoshiro issues, this is easy. We just store n integers for the RNG, where n is the number of threads, so each threads gets its own xoshiro rng.
The updating of the matrix though is more tricky, we need to guarantee that all threads work on cells which are at least 2 cells apart, so that they will never update clash with each other. We need two cells apart at least, because when a cell switches state, we update its neighbors. So if two cells switch at the same time, and they are less than two cells apart, we may end up with two threads updating their common neighbor cell at the same time. This need for synchronisation seems to divide by four the speedup per thread (i.e. speedup ~ 0.25 * nthread), which is not too great.