I couldn't find a better title, so I'll jump straight to the example!
There is this long list of slow jobs to be executed in a parallel way, at the press of a button:
DynamicModule[{dispatch, list, doStuff},
list = Range[8];
dispatch[] := ParallelMap[(Pause[0.5]; Print@{$KernelID, #}) &, list];
Button["Go", dispatch[] , Method -> "Queued"]
]
This works as expected, with every kernel printing it's job and ID. As soon as I start modularizing code, things break:
DynamicModule[{dispatch, list, doStuff},
list = Range[8];
doStuff[a_] := (Pause[0.5]; Print@{$KernelID, a});
dispatch[] := ParallelMap[doStuff, list];
Button["Go", dispatch[] , Method -> "Queued"]
]
This executes sequentially using only the main kernel (ID 0).
I've tried distributing definitions, contexts and even sharing variables, but the closest I've gotten to parallelization was the following:
DynamicModule[{dispatch, list, doStuff, doSlowStuff},
list = Range[8];
doSlowStuff[] := Pause[0.5];
doStuff[a_] := (doSlowStuff[]; Print@{$KernelID, a});
dispatch[] := ParallelMap[doStuff, list];
dispatch[]; (*This call is correctly executed in parallel*)
Button["Go", dispatch[] , Method -> "Queued"]
]
The "inline" dispatch[]
call is correctly executed (using all kernels) even with the added nested definition of doSlowStuff
, but the button's dispatch[]
still executes sequentially on kernel 0.
What am I missing?