I am trying to keep track of a calculation for visualization purposes ( I want to show to the user that calculation is in progress) . For that I use ProgressIndicator. However inside the paralleldo keeping track of the variable for the indicator makes the calculation much slower with respect to no-indicator. I wrote a "toy code" that mimics what I am doing. In this toy-code I am simply multiplying two arrays over and over again. Is there a way to make d run only in the main kernel and I simply multiply the with number of kernels, this will give me a rough idea.
myDummyF[nd_, np_] := Module[{mat, mat2, aI},
ns = 200;
mat = SetAccuracy[RandomReal[{-100, 100}, ns], nd];
mat2 = SetAccuracy[RandomReal[{-200, 200}, ns], nd];
d = 0;
aI = ConstantArray[0, np];
SetSharedVariable[aI, d];
Time = AbsoluteTiming[
If[nd <= 16,
Monitor[Do[
If[iC == 2, Print["Do Loop"]];
aI[[iC]] = SetAccuracy[mat.(mat2/iC^0.5), nd], {iC, np}],
ProgressIndicator[Dynamic[iC/np]]],
Monitor[ParallelDo[d++;
If[iC == 2, Print[" ParallelDo Loop"]];
aI[[iC]] = SetAccuracy[mat.(mat2/iC^0.5), nd];
, {iC, np}],
ProgressIndicator[Dynamic[d/np]]]]];
Print["Do loop time = ", Time[[1]], ", np = ", np, ", nd = ", nd];
aInum = Apply[Plus, (aI/np)];
Print["Total = ", aInum];
Return[aInum]];
np = 1000;
nd = 30;
myDummyF[nd, np];
For do loop (not the parallel one) there is not performance issue. I know the that the performance hit coming from the overhead of carrying d. Is there another way of doing this? I don't need to keep track exactly just an idea would be fine.
Below is the same code without process indicator:
myDummyF[nd_, np_] := Module[{mat, mat2, aI},
ns = 200;
mat = SetAccuracy[RandomReal[{-100, 100}, ns], nd];
mat2 = SetAccuracy[RandomReal[{-200, 200}, ns], nd];
aI = ConstantArray[0, np];
SetSharedVariable[aI];
Time = AbsoluteTiming[
If[nd <= 16,
Do[
If[iC == 2, Print["---------------------"]];
If[iC == 2, Print["Do Loop", " nd= ", nd, " np= ", np]];
aI[[iC]] = SetAccuracy[mat.(mat2/iC^0.5), nd], {iC, np}],
ParallelDo[
If[iC == 2, Print["---------------------"]];
If[iC == 2,
Print[" ParallelDo Loop", " nd= ", nd, " np= ", np]];
aI[[iC]] = SetAccuracy[mat.(mat2/iC^0.5), nd], {iC, np}]]];
Print["Do loop time = ", Time[[1]]];
aInum = Apply[Plus, (aI/np)];
Print["Total = ", aInum];
Print["---------------------"];
Return[aInum]];
np = 1000;
nd = 30;
myDummyF[nd, np];
ProgressIndicator[ Appearance -> "Indeterminate"]
before it is finished. p.s. are you sure every part of this code is essential to reproduce the problem? $\endgroup$