How to fix indexing?
4 views (last 30 days)
Show older comments
Hi! I am trying to run a parfor loop on matlab, but the parfor doesn't want to run because the indexing of a variable is not working out.
Here is the code
parfor ii = 1:1:2*nsim
%p is a temporary variable
p = pvals(2);
if ii> nsim
p = pvals(2);
kk = k(ii-nsim);
else
kk = k(end);
end
disp(ii)
[MuTmpA,MuTmpB,VarTmpA,VarTmpB,CovTmpAB,Scatter_tmp]
= ModelCalc(kk,s_1,s_2,N,T,q,p,f,n_plt,pop_sample, plt_step);
MuA(:,ii) =
MuTmpA;
MuB(:,ii) = MuTmpB;
VarA(:,ii) = VarTmpA;
VarB(:,ii)
= VarTmpB;
CovAB(:,ii) = CovTmpAB;
*Scatter_final(:,2*ii-1:2*ii)=
Scatter_tmp* ;
end
The issue is with the Scatter_final variable the asterik line. The following message appears:
Explanation For MATLAB to execute parfor loops efficiently, the amount of data sent to the MATLAB workers must be minimal. One of the ways MATLAB achieves this is by restricting the way variables can be indexed in parfor iterations. The indicated variable is indexed in a way that is incompatible with parfor.
Suggested Action Fix the indexing. For a description of the indexing restrictions, see “Sliced Variables†in the Parallel Computing Toolbox documentation.
Thanks so much for reading my question!
0 Comments
Accepted Answer
Edric Ellis
on 17 Jun 2016
Outputs from a parfor loop must be sliced or reductions. In this case, you want a "sliced" output. The linked doc page will show you the full details, but in short - you need to use the loop variable directly as a subscript. In this case, you can make a simple modification to create a 3-D output and then reshape it back on the client.
n = 4;
% Original FOR loop something like this:
for idx = 1:2*n
tmp = repmat(idx, 3, 2);
out(:, 2*idx-1:2*idx) = tmp;
end
% Fixed up PARFOR loop uses 3-D 'pOut'
parfor idx = 1:2*n
tmp = repmat(idx, 3, 2);
pOut(:, :, idx) = tmp;
end
% RESHAPE 'pOut' to match 'out', and ASSERT it's correct
pOut = reshape(pOut, [], 4*n);
assert(isequal(out, pOut))
0 Comments
More Answers (0)
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!