|
"James " <jfaghm@googlemail.com> writes:
> I've been trying to use a parfor on this piece of code but it never
> executes the if-statements. When the lopp ends, I can't find the net1,
> net2,... variables in the workspace. [...]
Your code essentially comes down to this:
parfor ii=1:2
if ii == 1
x1 = 1;
else
x2 = 2;
end
end
and you observe that you don't get x1 and x2 in your workspace after the
loop ends. This is expected - the statements are executed, but x1 and x2
are not output variables of the loop.
For a variable to be an output from a PARFOR loop, it must either be
"sliced" or a "reduction". Sliced basically means that it's an array
where one dimension is indexed using only the loop variable, and the
other indexing expressions are constant. An example of a reduction is
where a sum is being accumulated in the loop. Like so:
x = zeros(N);
y = 0;
parfor ii=1:N
x(ii, :) = rand(1, N); % x is "sliced"
y = y + max(x(ii,:)); % y is a "reduction"
end
You can slice have sliced cell arrays as outputs from a PARFOR loop,
like so:
x = cell(1, N);
parfor ii=1:N
if ii < 3
x{ii} = rand(3);
else
x{ii} = rand(ii);
end
end
Cheers,
Edric.
|