I have two Do
loops. The outer loop iterate over the inner loop. The inner loop contains a set of step by step instructions which each next step depends on the previous one so the inner loop must not be parallelized. However, the parallelization of the outer loop is desirable.
How can I just parallelize the outer loop only?
Does the code below is parallelized in both loops? If the code below is just parallelized on the outer loop only, then the other problem is that without using SetSharedVariable[a, b]
I can't get the values of a
and b
and if I use it it runs much more slowly than the unparallelized version.
SetSharedVariable[a, b]
AbsoluteTiming[
ParallelDo[
{
a = Table[i + j, {i, 1.0, 100.0}, {j, 1.0, 100.0}];
Do[
{
b = a.a;
c = If[Tr[b] > 2000, b = a, Break[]];
}
, {iterator1, 1, 500}];
}, {iterator2, 1, 500}]]
Edit
Consider the code below :
a = Table[Null, {i, 1, 1000}];
b = Table[Null, {i, 1, 10}];
SetSharedVariable[a, b]
ParallelDo[Do[a[[iterator1]] = RandomReal[], {iterator1, 1, 1000}];
b[[iterator2]] = a;, {iterator2, 1, 10}]
Now, a
and b
are outside of the loops and they are different for each iteration of outer loop. Indeed, b
gets bigger and bigger with each outer loop iteration. Without SetSharedVariable
after execution the code, a
and b
aren't accessible and if I use it, it is incredibly more slower than usual. How can I have a
and b
without losing speed?
Block
,Module
, orWith
? $\endgroup$