Parfor indexing with nested loop - what is wrong here?
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I try to generate a vector by a for-loop which is then needed inside a parfor loop. A minimal example reads like this:
parfor jj = 1:4
for kk = 1:3
in(kk,1) = kk;
end
A(jj) = in
end
display(A)
Matlab, however, complains about not being able to run "due to the way the variable 'in' is used".
The kk-loop has to be placed inside the parfor-loop.
I am a bit surprised because 'in' is completely independent from the parfor-index jj, or did I miss something here?
Thanks in advance and best
0 个评论
回答(2 个)
Marius
2015-12-15
编辑:Marius
2015-12-15
2 个评论
Walter Roberson
2015-12-15
Your initialization of in before the loop was the problem. Any variable defined before the loop must be either readonly or a sliced variable.
Edric Ellis
2015-12-15
By fully assigning to in before you use it inside the parfor loop, you're telling parfor that in is a loop temporary variable. Before you did that, the parfor analysis thought that you might be using in in an order-dependent way. This is because when you were assigning to in(1,1), the rest of in could be considered to still be there from the previous loop iteration. You and I can see that you weren't in fact using in in an order-dependent way, but the parfor analysis couldn't prove it.
Walter Roberson
2015-12-14
in is the wrong size to store in A(jj)
If you defined in before the parfor loop then if it does not happen to be a column vector of length 3 then parfor would consider it to be an attempt to write only part of in. You might get further by tossing in an explicit "clear in"
5 个评论
Walter Roberson
2015-12-15
Which matlab version are you using?
The clear is there to deal with the possibility that you already have a variable named in of a different size. If the variable exists before the parfor then your loop would be seen as using it as both input and output. Do you use the variable after the loop? That would cause problems.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!