-
Notifications
You must be signed in to change notification settings - Fork 19
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 pointers #322
base: main
Are you sure you want to change the base?
Fix pointers #322
Conversation
Before we merge it, let me understand in more detail what is happening.
So where comes the problem ? Basically my idea was that responsibility over memory allocation/deallocation should be on python side (since it is more clever, it has garbage collector), and C++ only "borrows" pointers to memory allocated in python, and since C++ is dumb I wanted to dessing it that C++ can be totally oblivious to the lifetime of the pointed memory (only python should care about that). The only problem I see when python deallocate memory which is still used in C++, but that we can handle by keeping the variable around (in scope) until C++ calculation is finished. If we initialize new arrays in python which creates new numpy arrays, we must not forget to update the pointers on C++ side (there are function setPointer() in the interface for exactly that purpose, but maybe these are not dano meticulously enough) Another problem which cannot be solved this way, si to run multiple instances of the simulator in python, which is obviously not possible on C++ side as the library has only one global state. But I guess this is not the problem now. |
The problem is that you don't necessarily pass new pointers in the next computation for all of the old pointers. Specifically there was an issue where the pointer for the energy array was set in a previous computation, but the next computation does not want to compute the energy, so it does not allocate a new numpy array for it. However, the pointer for the energy array from the previous computation is still there, so the C++ code tries to compute it anyways since it does not know that the pointer is not valid anymore. For the force field pointer this may not be an issue since all of the current routines always use it, but I set it to delete it anyways just in case. |
Yes, this is just a quick fix for the current problem. The more fundamental solution would be to keep a separate pointer for each new array on the C++ side instead of the global ones. But that's for later. |
OK, I understand now. Sorry, I did not read the code in detail, just scan over it, and I'm a bit confused. But that is normal, these memory management issues python/C++ ownership are always messy. |
OK, I looked at the code once more.
|
My contribution here isn't necessary. I let you guys continue moving forward. |
It's because the cleanup function is only called when the garbage collector runs, which is not guaranteed to happen before the next calculation starts, so it's called explicitly. I guess you could also call the |
Fixes #321
The force field arrays were left as dangling pointers when running multiple computational steps in a row, which manifested in #316. This fixes the issue by setting some cleanup code to run on garbage collection. It's not very pretty since you also have to manually run the garbage collector to make sure that the pointers are deleted in time, but it works.