-
-
Notifications
You must be signed in to change notification settings - Fork 398
Pickle serialization of Solution objects #692
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
Conversation
|
As I mentioned in the thread on #570, implementation of YAML-based serialization is planned, which will work for objects regardless of how they were created and how they have been manipulated. With the introduction of the YAML format, deprecation and removal of the CTI and XML formats is also planned, so I wouldn't recommend putting any significant effort into features based around these formats. Beyond that, I'm not sure whether there's a reason to keep this PR open. |
|
Agreed. I simply wanted to supply an example for pickling (which was surprisingly simple as the XML tree was available - 40ish lines of code!). As I wrote
I’d still like to keep this PR open until the core of the code can be switched over to YAML. Ultimately, I hope that this feature can be implemented. See also #696, which i believe is a necessary step in that direction. |
6e94366 to
ab1ee88
Compare
ab1ee88 to
12df33a
Compare
12df33a to
e7116fa
Compare
e7116fa to
b3cebac
Compare
45f1a81 to
49a99af
Compare
49a99af to
2647667
Compare
|
Linking #984, which will create the basis for a permanent implementation. |
2647667 to
013e03c
Compare
3b56eb4 to
147f49a
Compare
|
This would be a wonderful addition. I ran into the problem of not being able to do this when I first wanted to use multiprocessing to speed up calculations for optimization. I know that others have had the same issue based on the Cantera google group. The current method of using Cantera in a multiprocessing application is rather unintuitive and finicky. This PR would greatly simplify this task for other users. |
|
Kicking this back to draft - the implementation depends on the ability to create a PS: I probably won't work on this unless I hear back from one of the @Cantera/committers, i.e. comments are explicitly welcome. |
|
I think it should be possible to instantiate an object of the I just tried using this in a modified version of the I'm glad the implementation needed here for standalone |
@speth Could you clarify? None of these are currently instantiable without arguments. E.g. (using As far as I can tell, all of these are limitations of the implementation of the Python interface, and not necessarily about the underlying C++ structure. The crux here is that half of the time, the required |
|
Sorry for the confusion. What I meant was setting the Probably the easiest way to do this would be to add this as a case for |
|
@speth ... interesting indeed. Could you post the code you used? (Edit: I may have overanalyzed the 'serialized' here. If it's just accessing the restored object I can probably take it from here) |
|
Yes, that's all I did: gas0 = ct.Solution('gri30.yaml')
with open('test.pkl', 'wb') as out:
pickle.dump(gas0, out)
with open('test.pkl', 'rb') as infile:
gas1 = pickle.load(infile)
print(gas1.thermo_model)
print(gas1.kinetics_model)
print(gas1.transport_model) # segfaults here |
ddcef88 to
2be4f40
Compare
|
@speth ... fixed. The bug was hiding in plain sight ... one of the derived class constructor that Python calls ( I also updated the unit test to ensure that the model is properly set (including |
speth
left a comment
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.
Thanks, @ischoegl. I'm glad to see this working based on YAML serialization. I had just a few comments on the implementation.
c91d0d2 to
7e3a516
Compare
Also, catch edge cases that provide insufficient information for phase instantiation.
7e3a516 to
4758541
Compare
4758541 to
e3a7e28
Compare
|
@speth … thank you for the careful review! I believe I took care of everything. I ended up disabling |
speth
left a comment
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.
Thanks for the updates. This looks good to me.
Pickle support for
Solutionobjects would facilitate multiprocessing applications, where each thread/process is working with a separate copy of a Solution object. The approach is comparable to constructing the object from an input file plus setting phase and state vector.Closes Cantera/enhancements#10
The PR illustrates a rudimentary implementation (XML/CTI support only), which should not be considered for adoption (at least not in its present form).… now replaced by a permanent solution using YAML serializationIt allows for the following scenario (obviously, multiprocessing applications are more relevant):
Recreating the object in a separate session via
yields:
The current implementation for pickling/unpickling makes use of the static XML tree that is retained after creation of aSolutionfrom CTI/XML files. A more useful (but significantly more involved) implementation would generate YAML strings dynamically.Changes proposed in this pull request:
N/A: at least at this point, the PR serves for illustration purposes onlyKnown issues:
The current implementation will fail if theSolutionobject is changed after instantiation (e.g. species are added, etc.)Unpickling requires that an empty constructor, i.e.... fixed_SolutionBase()is allowed. While this works fine forpickle.load, an attempt forgas = Solution()results in a segmentation fault.