0
$\begingroup$

I am trying to find 5 minimas for some function. I can obtain the results if I implement a simple Do function. I tried to speed it up by using ParallelDo and then my code returns spurious results. Here is an example:

With Do Loop

mytable = {{0, 0, 0, 0, 0, 0, 10^7}, {0, 0, 0, 0, 0, 0, 10^6}, {0, 0, 0, 0, 0, 0, 
    10^10}, {0, 0, 0, 0, 0, 0, 10^8}, {0, 0, 0, 0, 0, 0,10^9}};
ele[a_, b_, c_, d_, e_, f_] := (c/(a + b)) Exp[(d/e)^(-1/f)];

Do[mytable = SortBy[mytable, Last]; If[ele[a, b, c, d, e, f] < Last[Last[mytable]],
    mytable[[-1, All]] = {a, b, c, d, e, f, ele[a, b, c, d, e, f]}], 
    {a,1, 2, 0.5}, {b, 2, 2.5, 0.05}, {c, 0.1, 1.1, 0.5}, {d, 1, 2, 0.5},
    {e, 1, 2, 0.5}, {f, 1, 2, 0.5}]

SortBy[mytable, Last]

I get the desired output:

{{2., 2.5, 0.1, 2., 1., 1., 0.0366383}, {2., 2.45, 0.1, 2., 1., 1.,0.0370499}, {2., 2.4, 0.1, 2., 1., 1., 0.0374709}, {2., 2.35, 0.1,2., 1., 1., 0.0379016}, {2., 2.3, 0.1, 2., 1., 1., 0.0383424}}

But when I simply change Do to ParallelDo, it gives me the original mytable values without any change.

{{0, 0, 0, 0, 0, 0, 1000000}, {0, 0, 0, 0, 0, 0, 10000000}, {0, 0, 0, 0, 0, 0, 100000000}, {0, 0, 0, 0, 0, 0, 1000000000}, {0, 0, 0, 0, 0, 0, 10000000000}}

Can anyone tell me if this is due to independence problem or is there a better way to Parallelize my code?

$\endgroup$
2
  • 1
    $\begingroup$ This is essentially a duplicate. See this answer and the one linked from it. Very briefly: if you need to parallelize, don't use procedural programming. Stick to functional constructs with no side effects. SetSharedFunction and SetSharedVariable will kill performance if they are called with every iteration. $\endgroup$
    – Szabolcs
    Feb 24, 2014 at 18:43
  • $\begingroup$ Thanks, I see your point, I am trying to make a function for my loop. Since I am relatively a newbie its taking me bit of time to make the new function work. $\endgroup$ Feb 24, 2014 at 18:58

0

Browse other questions tagged or ask your own question.