-
Notifications
You must be signed in to change notification settings - Fork 875
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
Fix for memory leak in pbc_shortest_vectors #2983
Fix for memory leak in pbc_shortest_vectors #2983
Conversation
Great. Thanks! I have a small request. Can you provide a simple benchmark of the difference in speed (if any) between old and new version? This is a rather core part of the code. I would want to make sure that the efficiency is not compromised. |
@@ -178,6 +179,8 @@ def pbc_shortest_vectors(lattice, fcoords1, fcoords2, mask=None, return_d2=False | |||
for l in range(3): | |||
vs[i, j, l] = 1e20 | |||
|
|||
if not (n_pbc == 3): | |||
free(&frac_im[0,0]) |
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.
These changes don't look like they should affect performance. Not sure how fast the free
operation is but considering we're currently leaking memory, some slowdown is definitely preferable.
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.
Just a minor pt - find_pbc_vector is very very performance critical (this is why it is in cython). I am willing to lose < 10% performance for mem, but if it is a 10x diff (this is not difficult to imagine), we need to consider the tradeoff. Basically all operations involving bond distances will slow down. Since the performance loss seems minimal, this is ok.
I've never written a test for |
As request by @shyuep, I wrote this very simple benchmark.
The same benchmark on the same machine with the modifications applied results in:
Judging from this very rough benchmarking, I would assess the impact on runtimes to be typically less than 10%. Regarding additional tests for memory leaks within |
Thanks for the benchmark! |
Thanks. Much appreciated! |
Summary
The
pbc_shortest_vectors
function leaks all memory allocated to store the fractional coordinates of surrounding periodic sites. As a result, memory consumption keeps creeping up when (heavily) calling the function, see this memory profile of a script of mine which made me aware of the problem. The suggested modifications completely eliminate the issue, curtailing the memory consumption of the same script.With the suggested minimal modifications, additional memory is only allocated (and freed accordingly) when needed - that is, when periodic boundary conditions are applied selectively.
Before
After