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

R[0]={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[1][[k]][[3]]], DSC[1][[k]][[2]]]
XXXX :=Reap[K=1;For[k = K, k<4, k++, If[(DSC[1][[k]][[1]]/.R[0])<0, R[1]={f[1,DSC[1][[k]][[3]]]-> f[0,DSC[1][[k]][[3]]]+f[0,DSC[1][[k]][[2]]],f[1,DSC[1][[k]][[2]]]-> 0,f[1,DC[[1]]]-> f[0,DC[[1]]],f[1,DC[[2]]]-> f[0,DC[[2]]],f[1,DC[[3]]]-> f[0,DC[[3]]],
    f[1,DC[[4]]]-> f[0,DC[[4]]]}/.R[0];Sow[Sort[R[1]]];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.



1 Answer 1


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_] :=
    (DSC[[k, 1]] /. rule) < 0,
    With[{dc = Complement[Range[6], DSC[[k, {2, 3}]]]},
        f[1, DSC[[k, 3]]] -> f[0, DSC[[k, 3]]] + f[0, DSC[[k, 2]]],
        f[1, DSC[[k, 2]]] -> 0,
        f[1, dc[[1]]] -> f[0, dc[[1]]],
        f[1, dc[[2]]] -> f[0, dc[[2]]],
        f[1, dc[[3]]] -> f[0, dc[[3]]],
        f[1, dc[[4]]] -> f[0, dc[[4]]]
        } /. rule]
   , {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}};

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

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

  • $\begingroup$ 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 ?? $\endgroup$
    – user49535
    Apr 30, 2018 at 9:50
  • $\begingroup$ 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... $\endgroup$ Apr 30, 2018 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.