# How to successfully put a ParallelDo loop inside a Do loop?

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;

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] = {{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?

• 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. – b3m2a1 Mar 29 '17 at 17:24
• @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. – user35305 Mar 29 '17 at 17:43
• Use SetSharedVariable then. Based on what you say I think that's what you want. – b3m2a1 Mar 29 '17 at 17:47
• @MB1965 Ah ok. So do I just move the ParallelEvaluate completely and just put SetSharedVariable[n[i], M[i]]? – user35305 Mar 29 '17 at 17:58
• 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. – Szabolcs Mar 29 '17 at 20:11