Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Avoid redundant computation in parfor

1 回表示 (過去 30 日間)
Jorey
Jorey 2013 年 4 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi all, I have used parfor for parallel computation. But there appears too redundant computation in the parfor. That is, parfor loops perform iteration for each 'ii' when using the command 'parfor ii = 1:n'. For example, the following iteration:
n = numel(seq);
parfor ii = 1:n
b(ii,:) = seq(indices);
end
But what I want is: if an element in 'seq' vector (says jjth element in 'seq') has been assigned to the matrix 'b', I do not let the code perform the computation for this element again, i.e. when the ii equals to jj. This can be done when using for loop, as:
n = numel(seq);
for ii = 1:n
if numel(find(b==seq(ii))) == 0
b(ii,:) = seq(indices);
end
end
However, an error occurs as "The PARFOR loop cannot run due to the way variable 'b' is used." when I modified the code as the above.
Because my code is very complex, and can waste lots of time if iterating on every element in 'seq'.
How can I avoid this?
Best Regars.
Jorey.

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 4 月 26 日
You could use spmd and labbroadcast to tell each of the labs which elements have been assigned.
When you say
if an element in 'seq' vector (says jjth element in 'seq') has been assigned to the matrix 'b',
then do you want to go by value (as you do in your "for" loop), or do you want to go by index ? Or is it known that the values in seq are unique?
If you want to go by index or if the values are unique, you can improve performance by using a logical array: e.g.,
if ~any(seqdone(indices))
b(ii,:) = seq(indices);
seqdone(indices) = true;
end
except that you need to adjust this for whatever difference there is between "ii" and "indices", and you need to consider what you want done if some of seq(indices) have been processed already but not all of them have.
  3 件のコメント
Walter Roberson
Walter Roberson 2013 年 4 月 27 日
What is the relationship between "ii" and "indices" ? And do you possibly want to "break" once a location is found to assign to, or do you want to do that same assignment for all ii that have not already been assigned ?
Jorey
Jorey 2013 年 4 月 28 日
Hi Roberson, Thanks for your reply. 'ii' is the index of the 'seq'. So if any of the element in 'seq' has been assigned (indicated in the 'indices'), ignore the computation for this element during the loops. I think this statement is similar with the latter meaning in your reply.
Best Regards.

この質問は閉じられています。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by