How to output variables properly from a parfor loop
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.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. 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. parfor, parallel computing, variable MATLAB Answers — New Questions