ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 09 Sep 2012 23:38:09 +0200Symbolic calculations in Parallel inside Sage?https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/Hello all,
It appears to me that Python has a number of tools to work with multi cores/nodes for parallel calculations: [multiprocessing](http://docs.python.org/library/multiprocessing.html), [MPI for Python](http://code.google.com/p/mpi4py/), [parallel Python](http://www.parallelpython.com/), [Celery](http://www.celeryproject.org/), [Scoop](http://https://code.google.com/p/scoop/) and etc. (If I am right!). However, right now I am living inside Sage and see the Python world from its sight, and from Sage I see [this question](http://ask.sagemath.org/question/14/what-are-my-best-options-for-taking-advantage-of) asked in the present forum two years before, wherein W.Stein explains that "**The Sage roadmap, such as it is, doesn't address parallel computing**" and "**Check out the multiprocessing module in Python but watch out -- sometimes you really have to understand how certain things in Sage works to safely use it**". I am almost new to Sage and don't know really what's going on inside it, and also I have a number (I mean really great number) of functions over which some heavy symbolic calculations (integrations and etc.) are to be applied, however, the calculations on each function is independent of the others, so that I was thinking about now that Sage has both Python and Maxima included in one place maybe I can handle independent tasks of the symbolic world using the paralleling facilities of the Python world?
Is it possible or you may suggest [SymPy](http://sympy.org/en/index.html) instead? I already have a [code](http://ask.sagemath.org/question/1661/how-to-speed-up-a-code-containing-several-symbolic) inside Sage so prefer to continue living inside Sage but I just doubt if it is as compatible with Python as might be SymPy (?), also I am not sure if SymPy (which has not reached its first stable release yet) would be as strong in calculations as is Maxima in Sage ?
Any guide would be appreciated,
Best Regards
Sat, 08 Sep 2012 05:32:08 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/Answer by Volker Braun for <p>Hello all,</p>
<p>It appears to me that Python has a number of tools to work with multi cores/nodes for parallel calculations: <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing</a>, <a href="http://code.google.com/p/mpi4py/">MPI for Python</a>, <a href="http://www.parallelpython.com/">parallel Python</a>, <a href="http://www.celeryproject.org/">Celery</a>, <a href="http://https://code.google.com/p/scoop/">Scoop</a> and etc. (If I am right!). However, right now I am living inside Sage and see the Python world from its sight, and from Sage I see <a href="http://ask.sagemath.org/question/14/what-are-my-best-options-for-taking-advantage-of">this question</a> asked in the present forum two years before, wherein W.Stein explains that "<strong>The Sage roadmap, such as it is, doesn't address parallel computing</strong>" and "<strong>Check out the multiprocessing module in Python but watch out -- sometimes you really have to understand how certain things in Sage works to safely use it</strong>". I am almost new to Sage and don't know really what's going on inside it, and also I have a number (I mean really great number) of functions over which some heavy symbolic calculations (integrations and etc.) are to be applied, however, the calculations on each function is independent of the others, so that I was thinking about now that Sage has both Python and Maxima included in one place maybe I can handle independent tasks of the symbolic world using the paralleling facilities of the Python world?</p>
<p>Is it possible or you may suggest <a href="http://sympy.org/en/index.html">SymPy</a> instead? I already have a <a href="http://ask.sagemath.org/question/1661/how-to-speed-up-a-code-containing-several-symbolic">code</a> inside Sage so prefer to continue living inside Sage but I just doubt if it is as compatible with Python as might be SymPy (?), also I am not sure if SymPy (which has not reached its first stable release yet) would be as strong in calculations as is Maxima in Sage ?</p>
<p>Any guide would be appreciated,</p>
<p>Best Regards</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?answer=14015#post-id-14015If your problem is trivially parallel (independent tasks) then you can just use the [@parallel decorator](http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel):
sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.Sat, 08 Sep 2012 06:46:08 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?answer=14015#post-id-14015Comment by owari for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19083#post-id-19083And have you suggestion on which method is best to do so? ThanksSun, 09 Sep 2012 23:38:09 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19083#post-id-19083Comment by Volker Braun for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19093#post-id-19093Whenever you are dealing with trivial parallelism then you will end up using map/reduce. Having to control which cpu core / cluster node works on a given task is totally opposite to what these frameworks are trying to achieve. Maybe you should ask your actual question instead of an abstract treatise of the pros and cons of different approaches.Sat, 08 Sep 2012 14:39:01 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19093#post-id-19093Comment by Volker Braun for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19089#post-id-19089Your outer loop just isn't parallelizable, each iteration step depends on the previous. You can only hope to do some steps of the inner loop in parallel. The maximum gain is limited by the fixed number of tasks in the inner loop. Sun, 09 Sep 2012 07:07:38 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19089#post-id-19089Comment by owari for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19090#post-id-19090I don't think it be easy to manage parallel processing for computing also each heavy symbolic integration, existing inside some series within each function-update during each step of the iteration loop, but if it is then it may help reducing the time consumption by the code very considerably! [My real code, albeit in its simplest and smallest version with 12 functions being iterated for, is the one I linked to above in my question, if it may help to see also its details.]Sat, 08 Sep 2012 17:16:34 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19090#post-id-19090Comment by owari for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19091#post-id-19091Ok, suppose I have an iteration loop during each iteration a number of functions being updated within some expressions containing a number of symbolic differentiation and integration. These updating processes are not coupled, so that inside each step of the loop there are plenty of functions that stand in a queue to be updated, one after another, each being considerably time consuming, whereas each of them can be updated exploiting one cpu core/node cluster so that the whole updating step occurs in a time of the order of one function update time. This was, if the number of functions to be updated inside the loop is small such a parallelization will be useless but if they are many then it will be a big advantage to use a parallel algorithm.Sat, 08 Sep 2012 17:05:48 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19091#post-id-19091Comment by owari for <p>If your problem is trivially parallel (independent tasks) then you can just use the <a href="http://www.sagemath.org/doc/reference/sage/parallel/decorate.html#sage.parallel.decorate.parallel">@parallel decorator</a>:</p>
<pre><code>sage: @parallel
....: def f(x):
....: return x^2
....:
sage: list(f(range(10)))
[(((0,), {}), 0), (((1,), {}), 1), (((2,), {}), 4), (((3,), {}), 9), (((4,), {}), 16), (((5,), {}), 25), (((6,), {}), 36), (((7,), {}), 49), (((8,), {}), 64), (((9,), {}), 81)]
</code></pre>
<p>This will use all cores of your computer to work in parallel. Of course you can use any of the other Python tools you mentioned to distribute your work tasks.</p>
https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19095#post-id-19095Thanks, if I am right the parallel decorator only allows a regular function to be called with a list of inputs (instead of the argument originally defined for it), whose values will be computed in parallel, and this is not something that I desire, however, that you added "you can use any of the other Python tools you mentioned to distribute your work tasks" is what I had hope to hear! But now Is there any comparison made between all those parallel processing methods in Python? Which one is easier to be implemented (with minor modification of the code) with enough control (but not too much control like usable only for experts) on managing which calculations to be sent to which cpu (on a PC or a cluster), also which method is in overall efficient? Is there any document about it? Thanks againSat, 08 Sep 2012 10:42:02 +0200https://ask.sagemath.org/question/9304/symbolic-calculations-in-parallel-inside-sage/?comment=19095#post-id-19095