eggplantbren / DNest4

Diffusive Nested Sampling
MIT License
62 stars 22 forks source link

Faster python #29

Closed SpaceOdyssey closed 6 years ago

SpaceOdyssey commented 6 years ago

Updates:

Notes:

eggplantbren commented 6 years ago

Thanks again. I've merged it. Any chance there's a way to remove the wrap and randh from the straightline class? It's a bit annoying. I guess the difficulty is that numba would have trouble with it if it called the cythonized randh and wrap? On Tue, Jul 10, 2018 at 12:52 PM SpaceOdyssey notifications@github.com wrote:

Updates:

Removed numpy.random dependency in utils.pyx. dnest.randh is now on par with the Numba version. Created callable cython functions: dnest.rand, dnest.randn, dnest.randt2. These should be faster than the numpy.random versions to generate random variates. The ability to create arrays is not allowed. Notes: We should consider putting the random variate generation functions in a dnest.rng module, but this will affect some peoples current codes.


You can view, comment on, or merge this pull request online at:

https://github.com/eggplantbren/DNest4/pull/29

Commit Summary

Sped up randh by about 1/3rd using cython implementation of box-muller randn. Added randn, rand as callable functions. Constructed randt2 function. Making randt2 callable

File Changes

M python/dnest4/init.py (4) M python/dnest4/utils.pyx (38)

Patch Links:

https://github.com/eggplantbren/DNest4/pull/29.patch https://github.com/eggplantbren/DNest4/pull/29.diff

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

-- Dr Brendon J. Brewer Department of Statistics, The University of Auckland, New Zealand Ph: +64 27 500 1336 Web: https://www.stat.auckland.ac.nz/~brewer/

SpaceOdyssey commented 6 years ago

No worries. Yeah, Numba doesn't understand cython. So you lose a considerable amount of time. You also have to use @jit on the individual functions rather than @jitclass. I just got a working version but it's about 2 minutes slower than the pure Numba version. All the time appears to be lost in the perturb function. Still runs in about 1/2 the time the original version did though. So I can do a pull request if you prefer to get rid of randh/wrap within straightline.py.

I have an idea regarding cythonising a generic perturb function that could do it in the backend. Similar to the way PyDNest does it using scipy.stats (but scipy.stats is very slow). Define the prior within the class using a list of distributions, then perturb can be done in the backend. If it's cythonised it should run at similar speeds to the Numba version. Then we won't have to deal with the mixed Numba/Cython which is not as stable. Should be better for the general user as well. Might write something up and send it to you.