How to output variables properly from a parfor loop

24 Ansichten (letzte 30 Tage)
Leon
Leon am 18 Apr. 2024
Kommentiert: Leon am 20 Apr. 2024
Having read over the documentation and Googled, I have an idea of what I'm not allowed to do in a parfor loop, but can't seem to find an answer for how to output variables efficiently. I've come up with work-arounds that feel quite hacky and inefficient. Is there a better way of achieving these two things below?
First case: store one value once:
loops = 10000000;
b = 0; % Will try to store value in b but it will silently fail
c = {}; % Cell array to (scucessfully) store just one value
parfor ii = 1:loops
var = ii; % It would just start at zero
% Lots of calculation here, that would change "var" and output other variables too %
if ii == 12345
disp(var)
b = var; % Doesn't get changed, even though there is no parallelism conflict
c{ii} = var; % Does get set
end
end
b % b is still zero
c = c{end} % Reduce cell array to just one variable. Works
Second case: store a value once every step number of loops:
loops = 10000000;
step = 10000;
a = zeros(loops,1); % Having to create one element for every loop (many more than actually used)
parfor ii = 1:loops
var = ii; % It would just start at zero
% Lots of calculation here, that changes "var" and outputs other variables too %
if mod(ii, step) == 0
a(ii) = var;
end
end
% Remove unused elements. This works, but I had to create a massive array that
% I imagine uses a lot of memory.
a = a(step:step:loops);
Thanks.

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 18 Apr. 2024
For your first case, you could use a parfor "reduction variable". Like this:
loops = 10000000;
b = [];
parfor ii = 1:loops
var = ii;
if ii == 12345
b = [b, var]; % parfor "reduction" assignment
end
end
b
b = 12345
However, note that you cannot "return early" from a parfor loop. It might be worth looking at parfeval instead, and fetchNext.
You can use the same concatenation trick for your second example. My only concern is that since parfor loop iterations are required to be independent, in your example code, you'd be better off modifying the loop to run over only those values for which you are going to store the output. But maybe you don't know which those are ahead of time.
  1 Kommentar
Leon
Leon am 20 Apr. 2024
Thanks. This is what I was looking for. In my actual case, I need to run the loop over all iterations because I am saving a value for every loop, but every now and then I want to save more information.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by