I found a bunch of questions regarding similar problems (for example, one ), and the problem with opening closures seems to be all around when working with iPython. In parallel, but I could not get around this problem anywhere. So my problem is this:
I want to solve for zeros a function f( a, b )for multiple values busing ipcluster. fin itself is a complex function. Let me use a stupid example
import scipy.optimize
def f(a,b):
return (a+b)**2 + numpy.sin(a*b)
bs = range( 20 )
for b in bs:
g = lambda a : f(a,b)
root = scipy.optimize.fsolve( g, 0.0 )
print root
ok that is a general idea of what i am doing. The thing is, if I try to create a function that returns the root, it will have to create a closure and pass it to scipy.optimize.fsolve(I think). For example, I tried
def root( b ):
g = lambda a : f( a, b )
return scipy.optimize.fsolve( g, 0.0 )
iPython.Parallel map, . , ?
,
import numpy
import scipy.optimize
from IPython.parallel import Client
def f( a,b):
return (a+b)**2+numpy.cos(a*b)
def h( a,b=1.0):
return (a+b)**2+numpy.cos(a*b)
def root_h( a ):
return scipy.optimize.fsolve( h, 0.0 )
def root(b):
g = lambda a : f(a,b)
return scipy.optimize.fsolve( g, 0.0 )
if __name__=='__main__':
print root( 1.0 )
print root( 2.0 )
c = Client()
dview = c[:]
with dview.sync_imports():
import numpy
import scipy.optimize
dview.push({'h':h})
res = dview.map( root_h, [1.0,2.0] )
for i in res:
print i
dview.push({'f':f})
res = dview.map( root, [1.0,2.0] )
for i in res:
print i
ValueError: Sorry, cannot pickle code objects with closures , - ...
, , , , :).