2
$\begingroup$

Suppose I have the following functions:

f1:=(a=RandomInteger[{1,24}]; b=RandomInteger[{1,24}]; Return[a+b])
f2:=If[f1==2,Print["snake eyes"]; Return[true], Print["nada"]; Return[false]]
  1. How can I parallelize these functions so that each kernel continuously executes f1 until any one kernel obtains "snake eyes" (at which point the process should terminate)?
  2. Relating to the first question, once parallelized is there a more efficient way of writing or executing this resulting code?
$\endgroup$
  • $\begingroup$ For a discussion of interkernel communication, the primitives, and the issues that arise, I think this question is quite informative (the question AND the answer, both). $\endgroup$ – Igor Rivin Sep 22 '14 at 0:17
1
$\begingroup$

All function you need it's DistributeDefinitions and ParallelTry

f1 := RandomInteger[{1, 24}] + RandomInteger[{1, 24}];
func[x_] := While[True,
   If[f1 === 2,
    Print["snake eyes"];
    Return[True]
    ,(*else*)
    Print["nada"]
    ]
   ];

ParallelTry[func, Range[$KernelCount]]
$\endgroup$

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.