Parfor: Converting broadcast variables into temporary variables or sliced variables

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.

 Réponse acceptée

Matt J
Matt J le 18 Mai 2021
Modifié(e) : Matt J le 18 Mai 2021
Temporary variables are not sent to the workers. They are created on the workers (and are destroyed there).

4 commentaires

As a highly simplified example, suppose that I have a broadcast variable x=1, with the following parfor loop:
parfor ih=1:10
if mod(ih,2)==0
a=x;
else
a=0;
end
store{ih} = f(a); % some operations on a
end
I get that a is deleted after the parfor loop, but I'm trying to understand if x would get sent all all the workers for each iteration of ih, or only the iterations for when ih is an even number.
Matt J
Matt J le 18 Mai 2021
Modifié(e) : Matt J le 18 Mai 2021
PARFOR broadcasts variables to the workers before any iterations begin. So, in your example, a copy of x will be sent to each worker once and only once. As the worker loops through the sub-batch of iterations assigned to it, it will draw upon x when needed, just like in an ordinary for-loop.
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?
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by