# Using ParallelDo

Following code is the simplified example of what I am trying to do.

R={f[0, 1]-> 1/6, f[0, 2]-> 1/6, f[0, 3]-> 1/6, f[0, 4]-> 1/6 , f[0, 5]-> 1/6, f[0, 6]-> 1/6};
DC:= DeleteCases[DeleteCases[{1, 2, 3, 4, 5, 6},DSC[[k]][]], DSC[[k]][]]
DSC:={{-524690.,2,1},{-556686.,2,3},{-556686.,4,1}}
XXXX :=Reap[K=1;For[k = K, k<4, k++, If[(DSC[[k]][]/.R)<0, R={f[1,DSC[[k]][]]-> f[0,DSC[[k]][]]+f[0,DSC[[k]][]],f[1,DSC[[k]][]]-> 0,f[1,DC[]]-> f[0,DC[]],f[1,DC[]]-> f[0,DC[]],f[1,DC[]]-> f[0,DC[]],
f[1,DC[]]-> f[0,DC[]]}/.R;Sow[Sort[R]];Break[]]]]
Last[Last[Reap[Do[Sow[ XXXX],{phi,0,Pi/4,Pi/4},{theta,0,ArcCot[Cos[phi]], ArcCot[Cos[phi]]}]]]]


In the last line I am using "Do" operation to implement the code for different values of theta & phi (To make it simple, I have removed the terms dependent on phi and theta, so all theta-phi combinations to give same o/p). I am getting the expected results with "Do".

Since I want to do this analysis for large numbers of phi-theta combinations, want to use ParallelDo instead, which is not working at all due to some reason. Will appreciate any help.

thanks

This is quite convoluted code and it is far from clear what it is supposed to accomplish, in particular since nothing depends on phi and theta. Parallelization is at the moment your least problem.

What I can say with quite a certainty is that your second use of Reap and Sow is mostly bogus; better use Table for that.

Also the way you use SetDelayed (:=) is causing me mental pain. Really, I love Pascal, because it was my first programming language 20 years ago. But this is Mathematica: assignments ought to be made with Set (=).

Moreover, I would discourage using global variables when parallelizing. I'd rather suggest to make variable dependencies explicit by using function constructs and handing variables over as arguments.

This is my refactorization of your code

XXXX[phi_, theta_, rule_, DSC_] :=
Table[
If[
(DSC[[k, 1]] /. rule) < 0,
With[{dc = Complement[Range, DSC[[k, {2, 3}]]]},
Sort[{
f[1, DSC[[k, 3]]] -> f[0, DSC[[k, 3]]] + f[0, DSC[[k, 2]]],
f[1, DSC[[k, 2]]] -> 0,
f[1, dc[]] -> f[0, dc[]],
f[1, dc[]] -> f[0, dc[]],
f[1, dc[]] -> f[0, dc[]],
f[1, dc[]] -> f[0, dc[]]
} /. rule]
],
Nothing
]
, {k, 1, 3}
];

rule = {f[0, 1] -> 1/6, f[0, 2] -> 1/6, f[0, 3] -> 1/6,
f[0, 4] -> 1/6, f[0, 5] -> 1/6, f[0, 6] -> 1/6};
DSC = {{-524690., 2, 1}, {-556686., 2, 3}, {-556686., 4, 1}};

Flatten[
ParallelTable[
XXXX[phi, theta, rule, DSC],
{phi, 0, Pi/4, Pi/4},
{theta, 0, ArcCot[Cos[phi]], ArcCot[Cos[phi]]}
],
1]


Note that the unparallelized version of the ParallelTable is several times faster,

• Thanks, it worked. As you suggested, time seems to be an issue. Surprisingly, for a Table of 4 elements (2X2) the operation time is about 10 minutes. Hence I expected 16 (4X4) element operation to take about 40 min, but it takes about 1.30 h. Is it normal ?? In that case what is the best way to improve the speed of Table/Do operations ?? – user49535 Apr 30 '18 at 9:50
• Maybe the time complexity in the interior of the Table is higher than linear? That would be less an issue of the Table but of the algorithm used... – Henrik Schumacher Apr 30 '18 at 10:03