ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 22 Nov 2016 19:09:54 -0600Parallel Interface to the Sage interpreterhttp://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/ I am using the `PSage()` parallel interpreter as described here http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/psage.html#sage-interfaces-psage. The interface works fine to evaluate multiple instances of function calls to functions of one or more scalar variables, however, if a function variable is array-like, the interface doesn't seem to work any more. I wonder if any special quoting is necessary for array-like arguments. Here is a minimal example. For reference, I copy the example from the above page, which works just fine, `factor()` has a single scalar variable.
v = [PSage() for _ in range(3)]
w = [x('factor(2^%s-1)'% randint(250,310)) for x in v]
w
[4057 * 8191 * 6740339310641 * 3340762283952395329506327023033,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681]
However, the `rosen()` function with a single array-like/vector argument doesn't seem to work in the parallel interface. (The example below just calculates the same function value three times, but that is not the point here.)
from scipy.optimize import rosen
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
rosen(x0)
848.22000000000003
v = [PSage() for _ in range(3)]
w = [x('rosen(x0)') for x in v]
w
[Sage, Sage, Sage]
Does anyone have any suggestion?Mon, 14 Nov 2016 18:16:14 -0600http://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/Answer by tmonteil for <p>I am using the <code>PSage()</code>parallel interpreter as described here <a href="http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/psage.html#sage-interfaces-psage">http://doc.sagemath.org/html/en/refer...</a>. The interface works fine to evaluate multiple instances of function calls to functions of one or more scalar variables, however, if a function variable is array-like, the interface doesn't seem to work any more. I wonder if any special quoting is necessary for array-like arguments. Here is a minimal example. For reference, I copy the example from the above page, which works just fine, <code>factor()</code> has a single scalar variable.</p>
<pre><code>v = [PSage() for _ in range(3)]
w = [x('factor(2^%s-1)'% randint(250,310)) for x in v]
w
[4057 * 8191 * 6740339310641 * 3340762283952395329506327023033,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681]
</code></pre>
<p>However, the <code>rosen()</code> function with a single array-like/vector argument doesn't seem to work in the parallel interface. (The example below just calculates the same function value three times, but that is not the point here.)</p>
<pre><code>from scipy.optimize import rosen
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
rosen(x0)
848.22000000000003
v = [PSage() for _ in range(3)]
w = [x('rosen(x0)') for x in v]
w
[Sage, Sage, Sage]
</code></pre>
<p>Does anyone have any suggestion?</p>
http://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/?answer=35697#post-id-35697If your parallel computations are calls to the same function with intependent parameters, you shoud have a look at the [@parallel decorator](http://doc.sagemath.org/html/en/reference/parallel/sage/parallel/decorate.html).Tue, 22 Nov 2016 19:09:54 -0600http://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/?answer=35697#post-id-35697Answer by ikol for <p>I am using the <code>PSage()</code>parallel interpreter as described here <a href="http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/psage.html#sage-interfaces-psage">http://doc.sagemath.org/html/en/refer...</a>. The interface works fine to evaluate multiple instances of function calls to functions of one or more scalar variables, however, if a function variable is array-like, the interface doesn't seem to work any more. I wonder if any special quoting is necessary for array-like arguments. Here is a minimal example. For reference, I copy the example from the above page, which works just fine, <code>factor()</code> has a single scalar variable.</p>
<pre><code>v = [PSage() for _ in range(3)]
w = [x('factor(2^%s-1)'% randint(250,310)) for x in v]
w
[4057 * 8191 * 6740339310641 * 3340762283952395329506327023033,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681,
31 * 13367 * 2940521 * 164511353 * 70171342151 *
3655725065508797181674078959681]
</code></pre>
<p>However, the <code>rosen()</code> function with a single array-like/vector argument doesn't seem to work in the parallel interface. (The example below just calculates the same function value three times, but that is not the point here.)</p>
<pre><code>from scipy.optimize import rosen
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
rosen(x0)
848.22000000000003
v = [PSage() for _ in range(3)]
w = [x('rosen(x0)') for x in v]
w
[Sage, Sage, Sage]
</code></pre>
<p>Does anyone have any suggestion?</p>
http://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/?answer=35595#post-id-35595It turns out that the problem has nothing to do with scalar vs. vector variable. The bottom line is that `PSage()` takes a single STRING as parameter enclosed in quotes. The following (somewhat awkward) modification makes the parallel interface work properly.
from scipy.optimize import rosen
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
rosen(x0)
848.22000000000003
v = [PSage() for _ in range(3)]
w = [x(eval('rosen(%s)'% str(x0))) for x in v]
w
[848.22, 848.22, 848.22]
Unfortunately, using `eval()` does not solve the problem. Because the example here is instantaneous to compute, I haven't realized that the calculations were, in fact, executed sequentially and not in parallel. After looking at the `PSage()` code it is clear that the argument must be a single string and the fact that this example "worked" although not in parallel, must be some artifact of using an explicit `eval()`. Mea culpa, HOWEVER, I created a better example and opened a new ticket, because `PSage()` would be an exteremely good tool for this kind of thing.Tue, 15 Nov 2016 10:23:32 -0600http://ask.sagemath.org/question/35579/parallel-interface-to-the-sage-interpreter/?answer=35595#post-id-35595