1
$\begingroup$

I'm trying to set up a ParallelDo loop inside of a Do loop, however I'm running into issues. Here is a very simplified toy model code of the final code that I wish to implement:

LaunchKernels[16];

ParallelEvaluate[

n[i_] = {};

M[i_] = {};

Do[ 
   AppendTo[n[i],{0,0}];
   AppendTo[M[i],{0,0}],
   {i,1,5}
  ]

];

Do[
   ParallelDo[
    J1[i] = {i, j}; 
    J2[i] = {i + 1, j + 1}; 
    AppendTo[M[i], J1[i]]; 
    AppendTo[n[i], J2[i]],
    {i, 1, 5}
   ],
  {j, 0, 5}
]

The problem seems to be that Mathematica appends to the initial conditions to a given n[i] (or M[i]) as many times as there are parallel kernels. For example,

Join@@ParallelEvaluate[n[1]] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, 
                               {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 
                               {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 
                               {0, 0}, {0, 0}, {2, 1}, {2, 2}, {2, 3},
                               {2, 4}, {2, 5}, {2, 6}}

And so Mathematica duplicates the initial conditions that it AppendsTo a particular n[i] (or M[i]) 16 times over.

Why is this happening and what can I do to resolve it?

$\endgroup$
  • $\begingroup$ are you trying to set it up so that n, M, J1 and J2 are synchronized? If so see SetSharedVariable. This might provide a slow-down though. Better to initialize each chunk separately, I think so that it's right in the end. Oh, or are you trying to get the code in the ParallelDo to happen on all kernels? Then just use ParallelEvaluate and `Do. $\endgroup$ – b3m2a1 Mar 29 '17 at 17:24
  • $\begingroup$ @MB1965 The problem is that I want the inner most Do loop to be parallelised, i.e. like you say, I want the computations in the ParallelDo to be distributed among the kernels. The outer one needs to be a "normal" Do loop since the results might depend on previous calculations. $\endgroup$ – user35305 Mar 29 '17 at 17:43
  • $\begingroup$ Use SetSharedVariable then. Based on what you say I think that's what you want. $\endgroup$ – b3m2a1 Mar 29 '17 at 17:47
  • $\begingroup$ @MB1965 Ah ok. So do I just move the ParallelEvaluate completely and just put SetSharedVariable[n[i], M[i]]? $\endgroup$ – user35305 Mar 29 '17 at 17:58
  • 2
    $\begingroup$ This is not the right approach to parallelization. Please read here and here. Do + AppendTo is generally a bad approach in Mathematica, particularly so with parallelization. Use Table instead. $\endgroup$ – Szabolcs Mar 29 '17 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.