How to output variables properly from a parfor loop

조회 수: 24 (최근 30일)
Leon
Leon 2024년 4월 18일
댓글: Leon 2024년 4월 20일
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.

채택된 답변

Edric Ellis
Edric Ellis 2024년 4월 18일
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
Leon
Leon 2024년 4월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by