1
$\begingroup$

Suppose I want to calculate a vector or an array (say, for simplicity a 4-by-1 vector), but each element contains independent numerically evaluated functions $f_1,f_2,f_3,f_4$ and each takes long time. Is there a way to parallelize computation of $f_i$?

The documentation and Stackexchange I searched so far (maybe I missed some) seems to be catering for a situation where we have a "master function" $F[i]$ whose entries are parallelized by using ParallelTable. Does that mean the only way (perhaps valid anyway) to do this is to simply define $F[i\_]:= f_i$? What is the "best practice" for this?

Update: the functions $f_i$ are a priori unrelated, but they depend on the same set of variable, so I was trying to Parallelize this inside a Module. I am also trying to avoid defining too many functions which is why if I can help it I want to avoid defining $F$ (because this will occur many times in the notebook).

$\endgroup$
1
  • 3
    $\begingroup$ For a start, you could try Parallelize[{f1, f2, f3, f4}, Method -> "CoarsestGrained"]. ParallelSubmit may also be helpful. For more relevant answers, please provide an example of your functions, or even some made up sample code that represents your problem. $\endgroup$
    – MarcoB
    Jan 14, 2022 at 22:20

2 Answers 2

3
$\begingroup$

Here is an example using ParallelSubmit:

f1[x_] = x;
f2[x_] = x^2;
f3[x_] = x^3;
f4[x_] = x^4;
WaitAll[{
  ParallelSubmit[f1[2]],
  ParallelSubmit[f2[2]],
  ParallelSubmit[f3[2]],
  ParallelSubmit[f4[2]]
  }]

(* {2, 4, 8, 16} *)
$\endgroup$
2
$\begingroup$
ParallelMap[Construct[#, x] &, {f1, f2, f3, f4}]
(*    {f1[x], f2[x], f3[x], f4[x]}    *)
$\endgroup$
3
  • 1
    $\begingroup$ You need to add a Method -> "FinestGrained" to ensure one calc per kernel at a time. $\endgroup$
    – Edmund
    Jan 15, 2022 at 17:19
  • $\begingroup$ @Edmund that's not necessary. Try f[x_] := Module[{}, Pause[1]; x^2] and then ParallelMap[Construct[#, x] &, {f, f, f, f}] // AbsoluteTiming, which on my computer takes about 1 second, meaning that the execution was one task per kernel. It looks like the default method can deal with this situation just fine. $\endgroup$
    – Roman
    Jan 15, 2022 at 17:29
  • 1
    $\begingroup$ Add the option will guarantee it to be the case. Automatic behavior can change. $\endgroup$
    – Edmund
    Jan 15, 2022 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.