I am testing out some optimization algorithms. I have written the code with a Do loop inside a Do loop. The inner loop is the algorithm itself and the order the steps are executed is very important. The outer loop simply generates runs of the algorithm and the order is not important. In order to save time I thought I'd parallelize the outer loop. However, it doesn't return the correct number of iterations.
Here is a simple code with the same idea.
d = 5;
SetSharedVariable[re];
re = {};
ParallelDo[
x = RandomReal[];
y = x;
ser = {};
Do[y = y + 1; ser = {ser, y}, {d}];
re = Append[re, Partition[Flatten[ser], d]]
,
{d}
];
re
While the values in "re" are correct, "re" only has length 2, but it clearly should be 5 and it works correctly if the "Parallel" part is removed. Why is this?
The length of the result depends on the variable "d". If we table the length of "re" returned for values of "d" from 1 to 20, we get values
{1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 3, 4, 4, 4, 4, 6, 6, 6, 5}.
ParallelTable
here? $\endgroup$