# Parallel execution of an instructions block

I regularly use Parallelize and ParallelMap, andI've been reading the manual pages for parallel computing instructions but i could not find what I am looking for. Assume I have a sequence of instructions that I know to be uncorrelated:

c=a+b;
f=d+e;
i=g+h;


Is it possible to execute the three instrunctions in parallel on three different Kernels ?

• You may be able to use ParallelEvaluate[yourexpression, kernel], where kernel is a KernelObject returned by the Kernels[] command. You will probably have to DistributeDefinitions to the parallel kernels first. – MarcoB Nov 12 '15 at 1:06

## 2 Answers

You can use ParallelMap with unevaluated list of expressions

parallelize[expr_List] := ParallelMap[# &, Unevaluated /@ Unevaluated@expr]
SetAttributes[parallelize, HoldAll];
{a, b} = {FactorInteger[2^500 - 1], FactorInteger[2^331 - 1]} // parallelize;

LaunchKernels
(* Out:
{"KernelObject"[1, "local"],
"KernelObject"[2, "local"],
"KernelObject"[3, "local"]}
*)

MapThread[
ParallelEvaluate[FactorInteger[#1], #2] &,
{RandomInteger[{10^6, 10^7}, 3], Kernels[]}
]

(*Out:
{
{{2, 1}, {3, 1}, {334157, 1}},
{{2, 3}, {59, 1}, {20759, 1}},
{{5, 1}, {11, 1}, {41, 1}, {1667, 1}}
}
*)

• My fault, but I don't fully get the code (that actually don't run on my Mathematica). I must point it out that I want to execute 3 different instructions... maybe having used 3 sums in the example was misleading – dario Nov 13 '15 at 1:36