How to output variables properly from a parfor loop
22 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Edric Ellis
2024-4-18
loops = 10000000;
b = [];
parfor ii = 1:loops
var = ii;
if ii == 12345
b = [b, var]; % parfor "reduction" assignment
end
end
b
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!