Parfor: Converting broadcast variables into temporary variables or sliced variables

1 次查看(过去 30 天)
Is there a difference in efficiency if I just assign a broadcast varible to a temporary variable inside a parfor loop vs re-writing it as a sliced variable that is indexed the same as the main index of the parfor loop? Matlab does not categorize it as a broadcast variable either way, but I'm not sure if the temporary variable method still avoids sending more data than is necesary to workers.

采纳的回答

Matt J
Matt J 2021-5-18
编辑:Matt J 2021-5-18
Temporary variables are not sent to the workers. They are created on the workers (and are destroyed there).
  4 个评论
Brandon
Brandon 2021-5-18
Thanks. Follow up question: Suppose that the example above is modifed slightly so that the temporary variable is always assigned a constant and the broadcast variable is never used:
parfor ih=1:10
if ih>0
a=0;
else
a=x;
end
store{ih} = f(a); % some operations on a
end
Does the broadcast variable, x, still get sent to every worker?
Walter Roberson
Walter Roberson 2021-5-18
Yes, the broadcast variable will (probably) be sent. MATLAB does some static flow analysis, but you cannot count on it being advanced enough to be able to reason that ih>0 will always be true.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by