-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize circumsphere and triangulation.py #245
Conversation
To speed up circumsphere, I directly imported the relevant functions, and I also removed the fast_det calls, as these end up defaulting to numpy's linalg.det anyways. I also avoid array allocations by getting rid of np.delete() calls. I made a few more tweaks, particularly in dealing with applying the (-1) multiplier and the constant factor a. I also added general optimizations by directly importing all numpy functions - for hot loops, this means that Python doesn't have to look up the function repeatedly.
It appears that for vectors with large values, it is possible to induce an integer overflow in dot(x, x).
Add unit test for test_circumsphere.
Add black formatted code.
Update with black formatted code.
pyupgrade fix.
Thanks a lot for doing these optimizations! You can easily fix all the style issues by running I've made #246 to make this more obvious in the future. |
I don't see any usages of numpy's sqrt for vectors, so I just switched the import over to the math library.
Should we use |
Forgot to run pre-commit.
While most of the names that we import have no collision with built-ins, Is making all names available in the module namespace an important optimization? |
As a quick demo:
A 20-30% performance boost from direct imports is pretty nice to have, though this may vary for specific functions and their overhead. |
Use numpy's abs only for vectors.
The issue with @ is that it doesn't work if the argument is a regular list (I think a lot of calls to fast norm aren't numpy arrays). Also, it seems to be slower, at least from my testing:
|
Azure seems to be throwing this error which is causing the linting tests to fail:
|
That failure is because of psf/black#1207. I imagine that will be fixed soon. |
Corrected documentation, optimized a boundary check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, and thank you for adding an extra test!
Optimize cirumsphere and triangulation.py
I merged this manually (so that we could get a rebase but also an explicit merge commit, which GitHub does not allow, for some reason), but it seems that GitHub did not register this as closing this PR. @philippeitis your changes are merged in, but I will have to "close" this PR. |
@philippeitis Let me know how you'd like me to credit you in the AUTHORS file (your GH profile does not have your real name). |
@jbweston Happy to see these changes merged in. If you'd like to credit me in the AUTHORS file, my name is Philippe Solodov. |
To speed up circumsphere, I directly called numpy's determinant function, as fast_det defaults to this case for >= 4 dimensions, which is always the case for circumsphere. I also directly took the norm, as this again defaults to sqrt(np.dot(vec * vec)) in fast_det for 4 dimensions. I also removed np.delete in favour of masked indices, which means that numpy doesn't need to allocate new arrays for each determinant.
For circumsphere, the speedup is about 33% on a four dimensional sphere (1.35x faster):
(50000 runs on arbitrary coordinates)
For a five dimensional sphere, the difference is more obvious, at around 50% (2x faster):
(50000 runs on arbitrary coordinates)
I also added general optimizations by directly importing all numpy functions - for hot loops, this means that Python doesn't have to look up the function repeatedly, which can save a fair amount of time.