<br><br><div class="gmail_quote">On Thu, Nov 18, 2010 at 11:43 AM, Skipper Seabold <span dir="ltr"><<a href="mailto:jsseabold@gmail.com">jsseabold@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Brian,<br>
<br>
Thanks for having a look. I think I figured out that it is an issue<br>
of overhead and the parallel version wins as I scale it up.<br>
<div class="im"><br>
On Tue, Nov 16, 2010 at 5:16 PM, Brian Granger <<a href="mailto:ellisonbg@gmail.com">ellisonbg@gmail.com</a>> wrote:<br>
> Skipper,<br>
> I looked through your code and have a few questions and thoughts:<br>
> * What data are you sending back and forth between the client and the<br>
> engines. It looks like that is going on here:<br>
<br>
</div>These are different starting values for the optimizer. In this case<br>
is 4 different float64 arrays of shape (2,)<br>
<div class="im"><br>
> mec.push(dict(start_params=start_params1), targets=0)<br>
> mec.push(dict(start_params=start_params2), targets=1)<br>
> mec.push(dict(start_params=start_params3), targets=2)<br>
> mec.push(dict(start_params=start_params4), targets=3)<br>
> mec.execute("""<br>
> params = start_params<br>
> ret = optimize.fmin_tnc(obj, params, approx_grad=True,<br>
> eta=eta, maxfun=3000, args=(Y,X),<br>
> messages=0)""")<br>
> ret1 = mec.pull('ret', targets=0)[0]<br>
> ret2 = mec.pull('ret', targets=1)[0]<br>
> ret3 = mec.pull('ret', targets=2)[0]<br>
> ret4 = mec.pull('ret', targets=3)[0]<br>
<br>
</div>ret is a tuple of len 3 returned by the tnc optimizer. It contains a<br>
shape (2,) array, a float, and an int.<br>
<div class="im"><br>
> and:<br>
> mec.push(dict(np=np, optimize=optimize, Y=Y,X=X,eta=eta,dot=dot))<br>
><br>
<br>
</div>I'm wondering if pushing the whole numpy and scipy.optimize namespace<br>
could cause a performance hit.<br>
<br></blockquote><div><br></div><div>Yes, I would use the MultiEngineClient to run "import numpy as np; from scipy import optimize" on all the engine rather than pushing the entire namespaces.</div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Y and X are arrays of shape (100,1) and (100,2) respectively.<br>
<div class="im"><br>
> One way of getting a sense of the data movement overhead is to run the code<br>
> with just the data movement and no computation.<br>
> * Can you time the parallel and serial code to get a better sense of where<br>
> it is taking a long time.<br>
> * Can you estimate the amount of time spend in the part of the code you are<br>
> parallelizing. Or, how much time is spent in the part of the code you<br>
> didn't parallelize.<br>
> Let's start there..<br>
> Cheers,<br>
> Brian<br>
><br>
<br>
</div>Comes back later... It seems that it is indeed a matter of overhead.<br>
I've attached a standalone script if anyone is interested that times<br>
only the relevant parts that I want to speed up. As I make the size<br>
of the arrays bigger, the parallel versions starts to win handily.<br>
<br></blockquote><div><br></div><div>OK great.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
In [18]: run parallel_optimize.py<br>
<snip><br>
T = 100<br>
0.105112075806 seconds for parallel optimization<br>
0.0321190357208 seconds for serial optimization<br>
<br>
In [19]: run parallel_optimize.py<br>
<snip><br>
T = 1000<br>
0.117481946945 seconds for parallel optimization<br>
0.062824010849 seconds for serial optimization<br>
<br>
In [20]: run parallel_optimize.py<br>
<snip><br>
T = 10000<br>
0.262063980103 seconds for parallel optimization<br>
0.464597940445 seconds for serial optimization<br>
<br>
In [21]: run parallel_optimize.py<br>
<snip><br>
T = 100000<br>
1.49375987053 seconds for parallel optimization<br>
4.25988888741 seconds for serial optimization<br>
<br>
In my case, T will be 10, 50, and 500. Since what I'm really doing is<br>
running this code as monte carlo simulations many times. What I could<br>
do is leave the individual fitting in serial and break the many times<br>
over my cores I suppose. Surely that will give a performance boost.<br>
We'll see.<br>
<br></blockquote><div><br></div><div>Yes, that is a good alternative. But I would definitely avoid pushing all of numpy and scipy.optimize.</div><div><br></div><div>Cheers,</div><div><br></div><div>Brian</div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Skipper<br>
<br>
PS. It seems that the new syntax for ipcluster is<br>
<br>
ipcluster start -n #<br>
<br>
with the most recent git cloned. Should the nightly build docs reflect this?<br>
<div><div></div><div class="h5"><br>
><br>
><br>
> On Sun, Nov 14, 2010 at 9:07 PM, Skipper Seabold <<a href="mailto:jsseabold@gmail.com">jsseabold@gmail.com</a>><br>
> wrote:<br>
>><br>
>> I have a class that does some optimizations multiple times using<br>
>> different starting values in order to check for convergence problems<br>
>> due to some "bad" data. My code was taking about an hour, then I<br>
>> added the multiple calls to scipy.optimize, and it went up to about 4<br>
>> hours. I am new to this, but I tried to parallelize this code,<br>
>> thinking it could speed things up. It looks like it's much actually<br>
>> slower. Is there anything I can do to optimize this or remove some<br>
>> overhead? A test case is inlined and attached.<br>
>><br>
>><br>
>> In [3]: timeit mod.fit()<br>
>> 10 loops, best of 3: 39.2 ms per loop<br>
>><br>
>> In [4]: timeit pmod.fit()<br>
>> 1 loops, best of 3: 255 ms per loop<br>
>><br>
>><br>
>> import numpy as np<br>
>> from numpy import log, dot, array<br>
>> from scipy import optimize<br>
>> from IPython.kernel import client<br>
>><br>
>><br>
>> def pobj(params, Y,X):<br>
>> """<br>
>> Normal log-likelihood<br>
>> """<br>
>> nobs2 = len(X)/2.<br>
>> resid = Y - dot(X,params[:,None])<br>
>> return -(- nobs2*np.log(2*np.pi)-nobs2*np.log(1/(2*nobs2) *\<br>
>> dot(resid.T, resid)) - nobs2)<br>
>><br>
>><br>
>> class PModel(object):<br>
>> def __init__(self, Y,X):<br>
>> self.X = X<br>
>> self.Y = Y<br>
>><br>
>> def fit(self, start_params=[0,0], eta=1e-8):<br>
>> start_params = np.asarray(start_params)<br>
>> Y = self.Y<br>
>> X = self.X<br>
>><br>
>> start_params1 = start_params.copy()<br>
>> start_params2 = start_params + 5<br>
>> start_params3 = start_params - 5<br>
>> start_params4 = np.random.randint(-10,10,size=2)<br>
>> mec.push(dict(start_params=start_params1), targets=0)<br>
>> mec.push(dict(start_params=start_params2), targets=1)<br>
>> mec.push(dict(start_params=start_params3), targets=2)<br>
>> mec.push(dict(start_params=start_params4), targets=3)<br>
>> mec.execute("""<br>
>> params = start_params<br>
>> ret = optimize.fmin_tnc(obj, params, approx_grad=True,<br>
>> eta=eta, maxfun=3000, args=(Y,X),<br>
>> messages=0)""")<br>
>> ret1 = mec.pull('ret', targets=0)[0]<br>
>> ret2 = mec.pull('ret', targets=1)[0]<br>
>> ret3 = mec.pull('ret', targets=2)[0]<br>
>> ret4 = mec.pull('ret', targets=3)[0]<br>
>> self.results = ret1<br>
>> # check results<br>
>> try:<br>
>> np.testing.assert_almost_equal(ret1[0], ret2[0], 4)<br>
>> np.testing.assert_almost_equal(ret1[0], ret3[0], 4)<br>
>> np.testing.assert_almost_equal(ret1[0], ret4[0], 4)<br>
>> self.converged = str(ret1[-1])+":<br>
>> "+optimize.tnc.RCSTRINGS[ret1[-1]]<br>
>> except:<br>
>> self.converged = "9: Results sensitive to starting values"<br>
>><br>
>> class Model(object):<br>
>> def __init__(self, Y,X):<br>
>> self.X = X<br>
>> self.Y = Y<br>
>><br>
>> def obj(self, params, Y, X):<br>
>> """<br>
>> Normal log-likelihood<br>
>> """<br>
>> X = self.X<br>
>> Y = self.Y<br>
>> nobs2 = len(X)/2.<br>
>> resid = Y - np.dot(X,params[:,None])<br>
>> return - nobs2*np.log(2*np.pi)-nobs2*np.log(1/(2*nobs2) *\<br>
>> np.dot(resid.T,resid)) - nobs2<br>
>><br>
>> def fit(self, start_params=[0,0], eta=1e-8):<br>
>> start_params = np.asarray(start_params)<br>
>> obj = self.obj<br>
>> Y = self.Y<br>
>> X = self.X<br>
>><br>
>> start_params1 = start_params.copy()<br>
>> start_params2 = start_params + 5<br>
>> start_params3 = start_params - 5<br>
>> start_params4 = np.random.randint(-10,10,size=2)<br>
>> ret1 = optimize.fmin_tnc(obj, start_params1, approx_grad=True,<br>
>> eta=eta, maxfun=3000, args=(Y,X), messages=0)<br>
>> ret2 = optimize.fmin_tnc(obj, start_params2, approx_grad=True,<br>
>> eta=eta, maxfun=3000, args=(Y,X), messages=0)<br>
>> ret3 = optimize.fmin_tnc(obj, start_params3, approx_grad=True,<br>
>> eta=eta, maxfun=3000, args=(Y,X), messages=0)<br>
>> ret4 = optimize.fmin_tnc(obj, start_params4, approx_grad=True,<br>
>> eta=eta, maxfun=3000, args=(Y,X), messages=0)<br>
>><br>
>> self.results = ret1<br>
>> # check results<br>
>> try:<br>
>> np.testing.assert_almost_equal(ret1[0], ret2[0], 4)<br>
>> np.testing.assert_almost_equal(ret1[0], ret3[0], 4)<br>
>> np.testing.assert_almost_equal(ret1[0], ret4[0], 4)<br>
>> self.converged = str(ret1[-1])+":<br>
>> "+optimize.tnc.RCSTRINGS[ret1[-1]]<br>
>> except:<br>
>> self.converged = "9: Results sensitive to starting values"<br>
>><br>
>><br>
>> if __name__ == "__main__":<br>
>> np.random.seed(12345)<br>
>> X = np.random.uniform(0,10,size=(10000,2))<br>
>> beta = np.array([3.5,-3.5])<br>
>> Y = np.dot(X,beta[:,None]) + np.random.normal(size=(10000,1))<br>
>><br>
>> eta = 1e-8<br>
>><br>
>> mec = client.MultiEngineClient()<br>
>> mec.push_function(dict(obj=pobj))<br>
>> mec.push(dict(np=np, optimize=optimize, Y=Y,X=X,eta=eta,dot=dot))<br>
>> pmod = PModel(Y,X)<br>
>> pmod.fit()<br>
>><br>
>> mod = Model(Y,X)<br>
>> mod.fit()<br>
>><br>
>> _______________________________________________<br>
>> IPython-User mailing list<br>
>> <a href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
>> <a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
>><br>
><br>
><br>
><br>
> --<br>
> Brian E. Granger, Ph.D.<br>
> Assistant Professor of Physics<br>
> Cal Poly State University, San Luis Obispo<br>
> <a href="mailto:bgranger@calpoly.edu">bgranger@calpoly.edu</a><br>
> <a href="mailto:ellisonbg@gmail.com">ellisonbg@gmail.com</a><br>
><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Brian E. Granger, Ph.D.<br>Assistant Professor of Physics<br>Cal Poly State University, San Luis Obispo<br><a href="mailto:bgranger@calpoly.edu">bgranger@calpoly.edu</a><br>
<a href="mailto:ellisonbg@gmail.com">ellisonbg@gmail.com</a><br>