Are there methods to run NestWhile in parallel mode?
Regards,
Zhao
Are there methods to run NestWhile in parallel mode?
Regards,
Zhao
I do not think this is possible - how could parallelize a function where each iteration uses the result from the previous iteration?
So how do we take advantage of parallel computing in this scenario?
How would you propose that might be done?
(What I’m asking, in slightly more detail, is that questions lacking algorithmic answers be avoided. Absent a clear suggestion in the initial post as to how parallelism might be attained in principle, this is simply not a suitable question for the forum.)
How would you propose that might be done?
I don’t know. I ask, only because I have a time-consuming calculation based on NestWhile, so I want to take advantage of parallelism to improve the efficiency.
Can the computation performed by the function passed to NestWhile be parallelized?
See the following example code snippet, which is based on the method here:
In[19]:= gMatrix =
SparseArray[{{1, 3} -> 1, {2, 1} -> 1, {3, 2} ->
1, {4, 4} -> -(1/2), {4, 5} -> -(1/2), {4, 6} -> -(1/2), {4,
7} -> -(1/2), {5, 4} -> 1/2, {5, 5} -> -(1/2), {5, 6} ->
1/2, {5, 7} -> -(1/2), {6, 4} ->
1/2, {6, 5} -> -(1/2), {6, 6} -> -(1/2), {6, 7} -> 1/2, {7, 4} ->
1/2, {7, 5} ->
1/2, {7, 6} -> -(1/2), {7, 7} -> -(1/2), {_, _} -> 0}, 7];
hMatrix =
SparseArray[{{1, 5} ->
1, {2, 2} -> -(1/2), {2, 3} -> -(1/2), {2, 6} -> 1/2, {2, 7} ->
1/2, {3, 2} -> 1/2, {3, 3} -> -(1/2), {3, 6} -> -(1/2), {3, 7} ->
1/2, {4, 1} -> 1, {5, 4} -> 1, {6, 2} -> -(1/2), {6, 3} ->
1/2, {6, 6} -> -(1/2), {6, 7} ->
1/2, {7, 2} -> -(1/2), {7, 3} -> -(1/2), {7, 6} -> -(1/2), {7,
7} -> -(1/2), {_, _} -> 0}, 7];
kMatrix =
SparseArray[{{1, 4} -> -1, {2, 2} -> -(1/2), {2, 3} -> -(1/2), {2,
6} -> -(1/2), {2, 7} -> 1/2, {3, 2} ->
1/2, {3, 3} -> -(1/2), {3, 6} -> -(1/2), {3, 7} -> -(1/2), {4,
5} -> -1, {5, 1} -> 1, {6, 2} -> 1/2, {6, 3} ->
1/2, {6, 6} -> -(1/2), {6, 7} -> 1/2, {7, 2} -> -(1/2), {7, 3} ->
1/2, {7, 6} -> -(1/2), {7, 7} -> -(1/2), {_, _} -> 0}, 7];
gen= {gMatrix, hMatrix, kMatrix};
count = 0; (matrices = NestWhile[(Print[count++];
Union[#~Join~Flatten[Outer[Dot, gen, #, 1], 1]]) &,
{IdentityMatrix[7]}, Length[#2] != Length[#1] &, 2, 99]) // Length // Timing
During evaluation of In[19]:= 0
During evaluation of In[19]:= 1
During evaluation of In[19]:= 2
During evaluation of In[19]:= 3
During evaluation of In[19]:= 4
During evaluation of In[19]:= 5
During evaluation of In[19]:= 6
During evaluation of In[19]:= 7
During evaluation of In[19]:= 8
During evaluation of In[19]:= 9
During evaluation of In[19]:= 10
During evaluation of In[19]:= 11
During evaluation of In[19]:= 12
During evaluation of In[19]:= 13
Out[23]= {2.27465, 6048}