# Parallelize elements of a vector or array

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).

• 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. Jan 14 at 22:20

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} *)

ParallelMap[Construct[#, x] &, {f1, f2, f3, f4}]
(*    {f1[x], f2[x], f3[x], f4[x]}    *)

• You need to add a Method -> "FinestGrained" to ensure one calc per kernel at a time. Jan 15 at 17:19
• @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. Jan 15 at 17:29
• Add the option will guarantee it to be the case. Automatic behavior can change. Jan 15 at 17:37