Conversation
Note that LSAN can be checked for actual binding code.
|
Tick the box to add this pull request to the merge queue (same as
|
|
This is great! Some high level comments and questions:
|
|
Thanks for reviewing! 1. Binding codea) minimal binding exampleHere's a branch spun out of this this extra commit showing a rather minimal test of what the binding code looks like: ./test_cooperative_intrusive_install_consume.sh[ 92%] Building CXX object CMakeFiles/min_ext.dir/min_ext.cpp.o
[100%] Linking CXX shared module min_ext.cpython-313-x86_64-linux-gnu.so
[100%] Built target min_ext
+ echo '=== Running consumer smoke test'
=== Running consumer smoke test
+ PYTHONPATH=/tmp/symengine-cooperative-intrusive-consume/consumer
+ python /work/symengine/bin/cooperative_intrusive_consume/smoke.py
cooperative_intrusive python smoke: OK -> x + y
+ echo '=== SUCCESS: cooperative_intrusive install-consume test passed'
=== SUCCESS: cooperative_intrusive install-consume test passed
+ rm -rf /tmp/symengine-cooperative-intrusive-consumeb) getting everything rightTear down of the process is still a little tricky if we want to avoid warnings from nanobind's refcount checks on exit and also LeakSanitizer, but this prototype (LLM written, beware) python wrapper seems to clear both in my testing: 2. Performance & 4. multiple RCP implementations.I've built SymEngine in release mode for all three RCP implementations and ran the pre-existing benchmarks, and two new benchmarks in this commit: bjodah@3d7ba4c The results (again I must warn up-front that this is LLM written results file) are here: So to me it looks like performance is very competitive (and that is in line with what I've read prior to trying this out, nanobind's author is very performance-aware). And as for RCP implementations, I think we could retire TeuchosRCP (our vendored version isn't even thread-safe, there's been upstream fixes since but last time I tried to update our in-tree teuchos subset I ran into a myriad of issues and gave up on it). But maybe the discussion and potentially the removal of Teuchos RCP is better had in a separate issue/PR? 3. LeaksI think subclassing symbols is the one issue we couldn't work around, but I suspect there could be other ways to create cycles (but that's just a feeling, I don't have any concrete examples). |
|
Thanks for your thorough explanation. I have one follow up question. In the setting of a software package eco-system will it be possible to have symengine and symengine.py separate packages or will they have to be built together? I.e. will symengine.py always have to contain a special version of symengine? Perhaps this is not even currently possible. |
|
So symengine.py builds just fine against this branch as is (if we don't register any hooks for incref/decref this RCP behaves like our existing ones). If we want symeninge.py to take advantage of the cooperative reference counting, we would need to patch |
The goal of this PR is to offer building SymEngine with a variant of intrusive counting which is suitable for sharing ownership with an external runtime that relies of reference counting (Python, Swift, Perl, PHP).
I've been working on the implementation on and off, and recently decided to see if LLM models are strong enough yet to help with this kind of task (my assessment is "yes, definitely" see e.g. this issue for recent issues found even in the intrusive_ptr implementation).
If we were to e.g. use this pointer type, along with nanobind for our python bindings, we would solve the current leaking associated with the current state of subclassing Symbol in symengine.py:
https://github.com/symengine/symengine.py/blob/88ad3236502ddafc771e823c5c4a2645a69c344a/symengine/lib/symengine_wrapper.in.pyx#L1283
To verify this thesis I've used LLMs extensively to implement proof-of-concept wrappers for Python (nanobind), Swift, Perl and PHP. The (AI-generated, beware) findings are presented here:
https://github.com/bjodah/superproj-symengine-cooperative-intrusive/blob/main/docs/upstream/PR-SUPPLEMENTARY.md#23-side-by-side
(I've used sanitizers extensively in that sister repo, see e.g. this file, if interested)
Let me know what you think!
P.S. I also have some potential benchmarks we could add in this PR, but in the interest of keeping the PR small I've not yet done so.