If you are passing objects into or out of a parfor
-loop,
the objects must properly facilitate being saved and loaded. For more
information, see Save and Load Process.
You can send handle objects as inputs to the body of a parfor
-loop,
but any changes made to handle objects on the workers during loop
iterations are not automatically propagated back to the client. That
is, changes made inside the loop are not automatically reflected after
the loop.
To get the changes back to the client after the loop, explicitly
assign the modified handle objects to output variables of the parfor
-loop.
In the following example, maps
is a sliced input/output
variable:
maps = {containers.Map(),containers.Map(),containers.Map()}; parfor ii = 1:numel(maps) mymap = maps{ii}; % input slice assigned to local copy for jj = 1:1000 mymap(num2str(jj)) = rand; end maps{ii} = mymap; % modified local copy assigned to output slice end
You cannot directly call a function handle with the loop index
as an input argument, because this cannot be distinguished from a
sliced input variable. If you need to call a function handle with
the loop index variable as an argument, use feval
.
For example, suppose you had a for
-loop that
performs:
B = @sin; for ii = 1:100 A(ii) = B(ii); end
A corresponding parfor
-loop does not allow B
to
reference a function handle. So you can work around the problem with feval
:
B = @sin; parfor ii = 1:100 A(ii) = feval(B,ii); end