symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

pickle Lambdify only works with backend='llvm' and not with backend='lambda

Open raphaelquast opened this issue 6 years ago • 5 comments

I've just noticed that pickling of symengine Lambdify functions fails with the default 'lambda' backend, but works fine with the 'llvm' backend... is there any particular reason for that? (or in other words... will I run into problems when I use the 'llvm' backend?)

maybe the fixes of #213 need to be repeated?

import symengine as seng
import pickle
x, y = seng.var("x, y")
f1 = seng.Lambdify([x, y], x+y, backend='llvm')
f2 = seng.Lambdify([x, y], x+y, backend='lambda')

_ = pickle.dumps(f1)
>>> works perfect

_ = pickle.dumps(f2)
>>>TypeError: self.lambda_double,self.lambda_double_complex cannot be converted to a Python object for pickling

raphaelquast avatar Aug 08 '19 12:08 raphaelquast

The problem is bigger than that. We would need to serialize the function objects. C++, which (unbelievably) still lacks proper reflection makes serialization a hassle (essentially forcing projects to do their own code generation, see e.g. Qt's moc, google's protobuf etc.). xref: https://github.com/symengine/symengine/issues/1394

EDIT: On that note, I've been looking into using libclang to parse the ast of a project of mine and generate serialization/deserialization functions during the build process (I've seen some talks on the subject, some also use annotations, but I haven't found a well established and maintained tool for this)

bjodah avatar Aug 08 '19 13:08 bjodah

OK, thanks for the quick reply! ...i already thought that the actual issue might lie a bit deeper...

will I run into any (expected) problems if I use the 'llvm' backend instead?

raphaelquast avatar Aug 08 '19 13:08 raphaelquast

@raphaelquast, the pickle file for llvm backend is platform dependent. You won't be able to save in linux and load in macos

isuruf avatar Aug 08 '19 13:08 isuruf

As long as you pickle and unpickle on a machines with the same instruction set there shouldn't be any issues (barring any unknown bugs of course). (and OS as @isuruf points out)

bjodah avatar Aug 08 '19 13:08 bjodah

Perfect, thanks! (the platform-dependency is fortunately only a minor issue for me)

raphaelquast avatar Aug 08 '19 13:08 raphaelquast