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:


(* {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.


marked as duplicate by Szabolcs, RunnyKine, m_goldberg, Öskå, Mr.Wizard Jul 17 '14 at 23:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ SetSharedFunction and SetSharedVariable force the function or variable to be evaluated by the main kernel (not subkernels). Every single time counter++ 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 use Method -> "CoarsestGrained". (And this commununication is costly: one should always try to minimize it.) Just don't use SetSharedVariable if performance is important. $\endgroup$ – Szabolcs Jul 17 '14 at 18:23
  • $\begingroup$ Understood, thanks. $\endgroup$ – Ziofil Jul 17 '14 at 18:33
  • $\begingroup$ Related: Monitor doesn't work with ParallelTable. (The OP may have gotten the approach from this post.) $\endgroup$ – Michael E2 Jul 17 '14 at 18:40
  • 2
    $\begingroup$ @Ziofil You may be interested in the "Possible Issues" section of ParallelSubmit, which shows some of the overheads costs (through examples). $\endgroup$ – Michael E2 Jul 17 '14 at 18:53

Browse other questions tagged or ask your own question.