I am using a shared variable as a progress indicator ( via SetSharedVariable[counter]
and then Dynamic[counter]
), to keep track of a parallel computation. However, there seems to be a massive overhead just because I ask ParallelTable to increment the counter:
AbsoluteTiming[ParallelTable[(counter++; myCompiledFunction),{i,1000}];]
(* {6.580984, Null} *)
But the same exact computation without the counter is about 50 times faster:
AbsoluteTiming[ParallelTable[myCompiledFunction,{i,1000}];]
(* {0.129618, Null} *)
Does anybody know why, and if there is a solution? Note: this does not happen if I use Table
, but then I cannot take advantage of all my cores.
SetSharedFunction
andSetSharedVariable
force the function or variable to be evaluated by the main kernel (not subkernels). Every single timecounter++
is evaluated, the subkernel will pause and wait for the main kernel to finish this evaluation. In your example this means that bidirectional communication between the main kernel and subkernels will be forced 1000 times, even if you useMethod -> "CoarsestGrained"
. (And this commununication is costly: one should always try to minimize it.) Just don't useSetSharedVariable
if performance is important. $\endgroup$ParallelSubmit
, which shows some of the overheads costs (through examples). $\endgroup$