Info

This question is closed. Reopen it to edit or answer.

Is this parfor fixable?

1 view (last 30 days)
Jacob Stevens
Jacob Stevens on 9 Oct 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
aid = 100;
% matrices to fill
iass = NaN(mma,aid,2,3,aid+run);
% dummy to make parfor work
s = aid+1;
% part A: recalculating paths for those already alive
parfor (i = 2:aid,6)
wag = wages(1:aid+1-i);
r = rint(1:aid+1-i);
gamma = CoSurv(i,:,1);
iass(:,:,:,:,s-i) = recalc_paths(aid, pars, i-1);
end
With error "unable classify variable iass". It /should/ be properly sliced - each loop sets a "row" of iass, completely independently of all the other loops (but which does depend on the loop number i). Is there a way to fix this that I'm missing? I introduced the dummy "s" because it gave me a different error when I inserted aid+1 directly into the index, but clearly that wasn't the silver bullet I thought it would be.

Answers (1)

Gaurav Garg
Gaurav Garg on 12 Oct 2020
Hi Jacob,
Apparently, you cannot subtract the loop index (within parfor loop) from any other scalar/vector quantity.
As a workaround in your case, you can assign the final values returned by the function "recalc_paths" in a temporary variable indexed by i and then write another for loop (parfor won't work here, again due to the same reason/error of classify variable) to assign the values to iass(:,:,:,:,s-i).
Pseudo-Code:
% Declaring temp as a matrix of suitbale size and type
parfor (i = 2:aid,6)
% Finding gamma and other values
temp(i) = recalc_paths(aid, pars, i-1);
end
for (i = 2:aid,6)
iass(:,:,:,:,s-i) = temp(i);
end
To know more about the error, you can refer to the doc here.
  2 Comments
Walter Roberson
Walter Roberson on 12 Oct 2020
Form of Indexing. Within the list of indices for a sliced variable, one index is of the form i, i+k, i-k, k+i, or k-i.
  • i is the loop variable.
  • k is a constant or a simple (nonindexed) variable.
  • Every other index is a constant, a simple variable, colon, or end.
In the user's case, s is a "simple (nonindexed) variable", and k-i form is being used.
The user is using R2019b, but the rules for subtraction where the same then; https://www.mathworks.com/help/releases/R2019b/coder/ug/classification-of-variables-in-parfor-loops.html
Therefor, the operation is documented as being permitted.
Jacob Stevens
Jacob Stevens on 14 Oct 2020
Thank you both - this is indeed the solution I went for, although I used iass(:,:,:,2:end) = temp(:,:,:,end:-1:2) instead of the proposed for loop. But as Walter says, the document does indeed suggest this kind of indexing is allowed, so it seems strange that this workaround is necessary.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!