Parallel Interface to the Sage interpreter 2.0

asked 2016-11-22 19:33:33 -0500

ikol gravatar image

updated 2016-11-22 19:38:55 -0500

The parallel Sage interface PSage()http://doc.sagemath.org/html/en/refer... works fine with the given example, but I have trouble with a more complex case, which would be a typical application of this very useful feature. The following code works exactly as advertised:

>>> v = [ PSage() for _ in range(5)]
>>> w = [x('factor(2**%s-1)'% randint(250,310)) for x in v]
>>> print w
[127 * 13367 * 164511353 * 17137716527 * 51954390877748655744256192963206220919272895548843817842228913,
 ,
 <<currently executing code>>,
 3 * 5^2 * 11 * 31 * 41 * 53 * 131 * 157 * 521 * 1613 * 2731 * 8191 * 51481 * 409891 * 7623851 * 34110701 * 108140989558681 * 145295143558111,
 ]
[127 * 13367 * 164511353 * 17137716527 * 51954390877748655744256192963206220919272895548843817842228913,
 7 * 73 * 16183 * 34039 * 1437967 * 2147483647 * 833732508401263 * 658812288653553079 * 2034439836951867299888617,
 <<currently executing code>>,
 3 * 5^2 * 11 * 31 * 41 * 53 * 131 * 157 * 521 * 1613 * 2731 * 8191 * 51481 * 409891 * 7623851 * 34110701 * 108140989558681 * 145295143558111,
 ]
[127 * 13367 * 164511353 * 17137716527 * 51954390877748655744256192963206220919272895548843817842228913,
 7 * 73 * 16183 * 34039 * 1437967 * 2147483647 * 833732508401263 * 658812288653553079 * 2034439836951867299888617,
 <<currently executing code>>,
 3 * 5^2 * 11 * 31 * 41 * 53 * 131 * 157 * 521 * 1613 * 2731 * 8191 * 51481 * 409891 * 7623851 * 34110701 * 108140989558681 * 145295143558111,
 7 * 78903841 * 28753302853087 * 618970019642690137449562111 * 24124332437713924084267316537353]
[127 * 13367 * 164511353 * 17137716527 * 51954390877748655744256192963206220919272895548843817842228913,
 7 * 73 * 16183 * 34039 * 1437967 * 2147483647 * 833732508401263 * 658812288653553079 * 2034439836951867299888617,
 131071 * 12761663 * 179058312604392742511009 * 3320934994356628805321733520790947608989420068445023,
 3 * 5^2 * 11 * 31 * 41 * 53 * 131 * 157 * 521 * 1613 * 2731 * 8191 * 51481 * 409891 * 7623851 * 34110701 * 108140989558681 * 145295143558111,
 7 * 78903841 * 28753302853087 * 618970019642690137449562111 * 24124332437713924084267316537353]

Printing w repeatedly shows the progress of the five factorizations running in parallel (monitoring it looking at top is showing 5 sage/python jobs running simultaneously). The following example is global optimization, starting from 5 different starting points using the differential evolution algorithm available in SciPy. The setup is more complex but still, only a single command string is passed to PSage().

>>> v = [ PSage() for _ in range(5)]
>>> w = [x('from scipy.optimize import rosen, differential_evolution; differential_evolution(rosen, [(0,2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2)])') for x in v]
>>> print w
[Sage, Sage, Sage, Sage, Sage]

Apparently, something is wrong here, it doesn't work. But why? Let's see what happens with the serial Sage interpreter Sage().

>>> s = Sage()
>>> t = s('from scipy.optimize import rosen, differential_evolution;differential_evolution(rosen,[(0,2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2)])')
>>> print t
Sage

Looks like the same problem. However, Sage() can be made to work using the eval() method.

>>> s = Sage()
>>> t = s.eval('from scipy.optimize import rosen, differential_evolution;differential_evolution(rosen,[(0,2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2)])')
>>> print t
fun: 1.2785524204717224e-18

PSage() also has an eval() method, which AFAIK uses Sage().eval() internally, but unfortunately, it doesn't work.

>>> v = [ PSage() for _ in range(5)]
>>> w = [x.eval('from scipy.optimize import rosen, differential_evolution; differential_evolution(rosen, [(0,2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2), (0, 2)])') for x in v]
>>> print w
['<<currently executing code>>', '<<currently
executing code>>', '<<currently executing code>>',
'<<currently executing code>>', '<<currently executing
code>>']

Based on what I see looking at top the five optimization jobs do run in parallel, but even after they finish, no matter how many times I print w all I get is <<currently executing code>>.

The bottom line is that PSage() is either not working at all without using the eval() method, or, it seems to be working with the eval() method, but somehow is stuck in a bad internal state and never producing the output. Any comment is highly appreciated, it would be great to get this to work consistently.

edit retag flag offensive close merge delete