5
$\begingroup$

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 ?

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ – MarcoB Nov 12 '15 at 1:06
4
$\begingroup$

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;
$\endgroup$
3
$\begingroup$
LaunchKernels[3]
(* 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}}
} 
*)
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$ – dario Nov 13 '15 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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