ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 21 Feb 2020 14:31:46 -0600Parallel computation for different functionshttps://ask.sagemath.org/question/50013/parallel-computation-for-different-functions/For a single function with a list of inputs, the @parallel decoration can be used to do parallel computation. I am wandering whether it is possible to do parallel computation for different functions.
A simple is example is to calculate the difference f-g of two independent functions f and g. How can I ask SageMath to simultaneously compute the values of f and g, and then calculate the difference?
My time measurements suggest that when calculating the difference f-g, SageMath actually calculates f and g one after another, and then take the difference.
I can think of a naive approach using the @parallel decoration. I can create a function whose input variable is the name of the functions I want to compute simultaneously. Then I use a list of function names as input to get a generator of outputs. This may work if the final result doesn't depend on the order of the outputs, but does not work if the order matters, for example when taking the difference.
In general, suppose I have a flow chart for the computation, in other words, I already know which jobs can be parallelized and how the information flows to the next stage. Then what is the best way to implement the flow chart?Fri, 21 Feb 2020 01:37:16 -0600https://ask.sagemath.org/question/50013/parallel-computation-for-different-functions/Answer by vdelecroix for <p>For a single function with a list of inputs, the @parallel decoration can be used to do parallel computation. I am wandering whether it is possible to do parallel computation for different functions.</p>
<p>A simple is example is to calculate the difference f-g of two independent functions f and g. How can I ask SageMath to simultaneously compute the values of f and g, and then calculate the difference?</p>
<p>My time measurements suggest that when calculating the difference f-g, SageMath actually calculates f and g one after another, and then take the difference.</p>
<p>I can think of a naive approach using the @parallel decoration. I can create a function whose input variable is the name of the functions I want to compute simultaneously. Then I use a list of function names as input to get a generator of outputs. This may work if the final result doesn't depend on the order of the outputs, but does not work if the order matters, for example when taking the difference.</p>
<p>In general, suppose I have a flow chart for the computation, in other words, I already know which jobs can be parallelized and how the information flows to the next stage. Then what is the best way to implement the flow chart?</p>
https://ask.sagemath.org/question/50013/parallel-computation-for-different-functions/?answer=50019#post-id-50019The `@parallel` decorator is based on the [multiprocessing Python library](https://docs.python.org/3/library/multiprocessing.html). It is nothing advanced and only allows you to perform embarassingly parallel computations (ie no dependence between each individual computation).
If you want more advanced parallelization, a flow chart is not enough as it does not take into account the cost of a given computation. Let us imagine that you have 2 processors for the following tasks
- A1, A2, A3 that takes respectively 1min, 1min, 3min
- B that takes 1min and depends on A1, A2, A3
Then, doing it blindly you will start the computation with A1, A2 simultaneously. They will roughly finish at the same time and then A3 will run alone for 3 min.
Even with the assumption that you can give a rough approximation of the time of each task, the scheduling problem is algorithmically non-trivial (ie unlikely to be solved on large instances). I don't know of any Python implementation of something in that direction.Fri, 21 Feb 2020 14:31:46 -0600https://ask.sagemath.org/question/50013/parallel-computation-for-different-functions/?answer=50019#post-id-50019