-
Notifications
You must be signed in to change notification settings - Fork 121
Replace polyprism Cython code with pure numpy #368
Conversation
Currently trying to optimize gz. Tried to replace two arctan2 calls with a single one using an identity. Doesn't work because the identity isn't always valid. Had success replacing a log subtraction with a division for moderate speed ups. Here is the benchmark result comparing to
|
Simplify the tests of `polyprism` vs `prism` and add regression tests against saved results, like was done for `sphere` in #364. This allows us to test more complex polygonal prisms (an ellipse for example) and catch regressions that might affect both `polyprism` and `prism` at the same time. Test coverage decreases because the numpy alternative implementation is not tested anymore. This will become the main version in #368 so it's not a big deal right now.
Could get a decent speed by replacing log subtraction with a single log. Couldn't get the arctan identities to work for some reason.
No speedups for gzz but got the same speed as master on the benchmark.
|
Combine the 4 log calls into one.
Combined 4
|
Couldn't optimize because collapsing the logs leads to a decreased precision (larger difference with the prism code). Not a problem because it's already pretty fast and the same as the Cython speed.
Same optimization used for kernelxx (collapse logs)
Used same optimization for |
Didn't optimize the logs for the same reason as kernelxz
They all use the kernel functions so got a bit of a speedup, specially in tf because of that.
Finished optimizations (see results of benchmark in PR description). |
Need to take a better look at the kernel functions.
Add them to the API list for polyprism.
@birocoles a bit of a heads-up on this PR. I've simplified some of your original code. Don't know if @leobvital is using the code for Fatiando or implemented his own, but this is much simpler to read now. The tests are also improved and I've learned how to use pytest better. |
Very good @leouieda ! @leobvital is using the Fatiando code but I think that these changes will not affect his work. |
Replacing the Cython
gravmag.polyprism
forward modeling codewith simpler pure Python + numpy versions (following #364).
Using some optimizations and simplifications, the resulting code is just as fast or
even faster than the Cython version.
The main optimization is combining logarithms, e.g.
log(a/b) - log(c/d)
withlog((a*d)/(b*c))
.Using the following IPython script to benchmark against
master
:These are the times for the
master
branch on my laptop:And after the conversion and optimization:
Checklist:
doc/install.rst
,environment.yml
,ci/requirements-conda.txt
andci/requirements-pip.txt
.cd doc; make
locally)