I'm struggling understanding the syntax for parallel process. I have two Do that are independent and I would like to parallelize them,
Clear["Global`*"]
ClearAll[Subscript]
d = 0.95; n = 10; gridl = 0.001; L = Reverse[Range[1, n - 1]];
P[b_, a_] := Module[{p = a/(a + b + 1)},
While[
p/(1 - d) < (a/(a + b)) (1 + d*R[[b, a + 1]]) +
b/(a + b)*d*R[[b + 1, a]], p = p + gridl;];
SetPrecision[p, 4]]
R = Table[Subscript[r, b, a], {b, 1, n}, {a, 1, n}];
Do[Subscript[r, x, n] = (n/(n + x))/(1 - d);
Subscript[r, n, x] = (x/(n + x))/(1 - d), {x, 1, n}]
Do[
Subscript[r, i, i] = P[i, i]/(1 - d);
list = Reverse[Range[1, i - 1]];
Do[Subscript[r, i, x] = P[i, x]/(1 - d), {x, list}]; (***)
Do[Subscript[r, x, i] = P[x, i]/(1 - d), {x, list}], {i, L}]
I would like the two Do at (***) to go in parallel of each other. They fills the column and row of the table given the initial position in the diagonal and they are independent of each other (the do themself are sequential since they need previous values).
I checked similar questions (sol1, sol2), but I can't make it work. Any help is appreciated.
Thanks!
SetSharedVariable
, but the mechanisms that are used to make this work in the background will make the parallel version slower than the serial one. $\endgroup$Do
loop. Concretely, if you get away from usingSubscript
and sayr[x_, n_] := r[x, n] = (n/(n + x))/(1 - d)
etc., everything may get much simpler for you anyway. $\endgroup$