Info
此问题已关闭。 请重新打开它进行编辑或回答。
Is this parfor fixable?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Gaurav Garg
2020-10-12
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
2 个评论
Walter Roberson
2020-10-12
According to https://www.mathworks.com/help/coder/ug/classification-of-variables-in-parfor-loops.html#btfitu2-19
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.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!