# Shared variable impacting speed? [duplicate]

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.

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

• 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. – Szabolcs Jul 17 '14 at 18:23
• @Ziofil You may be interested in the "Possible Issues" section of ParallelSubmit, which shows some of the overheads costs (through examples). – Michael E2 Jul 17 '14 at 18:53