4
$\begingroup$

Say I have something like this:

myfun[dataA,X1,Y1,Z1,W1];
myfun[dataB,X2,Y2,Z2];
myfun[dataC,X3,Y3,Z3];
(* with many more of the same *)

Each would take about 5 to 10 minuets to run. The function takes many arguments, some have default values.

With lots of these functions, I am currently dividing them into 4 different notebooks, then run them on different kernels, so that I am using 4 cores fully. But I think there is a better way to do this?

I want to know how to group them and run them at the same time? Say with 16 different calls to myfun, can I ask Mathematica, to use all 4 cores? If one core finishes a single run, then move on to the next run? So there are always 4 cores running?

Thanks!

$\endgroup$
  • $\begingroup$ reference.wolfram.com/language/guide/ParallelComputing.html $\endgroup$ – Yves Klett Dec 3 '14 at 10:44
  • $\begingroup$ @YvesKlett Yes, I have been reading them. But I see a lot of them uses a "Table" as an example? But in my case, most of the arguments to myfun are different. Do I have to setup a table in some way? $\endgroup$ – Chen Stats Yu Dec 3 '14 at 10:48
  • $\begingroup$ You could also ParallelMap or ParallelSubmit your jobs. I would suggest you try some of the documentation examples to get an idea. As for the number of cores, parallelization works on kernels. Where each kernel will run is a different story (which works out fine usually,though). Try AbsoluteTiming[ParallelMap[Pause, {1, 1, 1,1}]] ... $\endgroup$ – Yves Klett Dec 3 '14 at 10:51
7
$\begingroup$

It's straightforward for simple cases! Just as @Yves Klett commented:

f[a_, b_: 1] := (Pause[.3]; a + b)

(* List of arguments to be passed for each job *)
jobs = {{0}, {1}, {0, 3}, {2, 2}}

Map[f @@ # &, jobs] // AbsoluteTiming
(* {1.205, {1,2,3,4}} *)

ParallelMap[f @@ # &, jobs] // AbsoluteTiming
(* {0.309, {1,2,3,4}} *)
$\endgroup$
3
$\begingroup$

Aisamu's solution is clean, and is what I'd recommend for a bigger project or if you do this a lot. But for a quick and dirty solution, the following works well:

Parallelize[{expr1, expr2, expr4, ...}]

expr1, expr2, etc. will be evaluated on separate kernels.

Demonstration:

Parallelize[{$KernelID, $KernelID, $KernelID}]
(* {4, 3, 2} *)
$\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.