Parfor indexing with nested loop - what is wrong here?

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

Answers (2)

Marius
Marius on 15 Dec 2015
Edited: Marius on 15 Dec 2015
Okay, I seem to have found a solution - with an initilization for "in", the following does work:
parfor jj = 1:4
in = NaN(1,3);
for kk = 1:3
in(1,kk) = kk;
end
A(jj,:) = in;
end
display(A)
I'd appreciate a comment on this as I do not yet have a full understanding of what parfor does expect and what not.

2 Comments

Your initialization of in before the loop was the problem. Any variable defined before the loop must be either readonly or a sliced variable.
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.

Sign in to comment.

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 Comments

Hello Walter,
thanks for your reply. Of course, the example should have read
parfor jj = 1:4 % "for" works, parfor does not!
for kk = 1:3
in(1,kk) = kk;
end
A(jj,:) = in;
end
display(A)
My apologies - I made a mistake copying my code when posting it here. For a simple for-loop, the above code works, for a parfor loop, it does not.
I am sorry, but could you please elaborate on your suggestion a little more? I can't seem to take the appropriate course of action. Where would I have to put the "clear in" in the code? Thank you very much!
Right after the parfor line add
clear in
parfor jj = 1:4
clear in
for kk = 1:3
in(1,kk) = kk;
end
A(jj,:) = in;
end
display(A)
Thanks for your help, but this does not work either - and I do not really understand the purpose of the "clear in", to be honest. What is the intention here to clear "in"? Is it that if I don't clear "in", the next parallel loop for jj consider "in" as unsliced?
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.
I use Matlab 2015a, 64-bit.
And yes, in the real problem I want to solve, "in" will be initialized before the parfor-loop with NaN() and I would also like to use "A" after the loop. The whole loop's raison d'etre is that I want to perform calculations whose results are "in" and I want to save them to "A" which I would like to refer to after the parfor loop.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Dec 2015

Commented:

on 15 Dec 2015

Community Treasure Hunt

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

Start Hunting!