"valid indices for 'a' are restricted in PARFOR loops" for unindexed struct?
55 views (last 30 days)
Show older comments
For the following code:
parfor k = 1:2
a.b = k;
end
I'm getting the error "valid indices for 'a' are restricted in PARFOR loops"
But there is no indexing here. Could someone explain what I'm missing?
In case it's important, the code above comprises the entire script.
You cannot create a structure in a parfor-loop using dot notation assignment.
It's not clear why, though.
Edit 2: Mathworks support has submitted an enhancement request to update the error message to something clearer in a future release.
MATLAB Version: 9.1.0.441655 (R2016b)
0 Comments
Answers (1)
OCDER
on 16 Jul 2018
Edited: OCDER
on 16 Jul 2018
parfor k = 1:2
a(k).b = k;
end
The reason a.b = k does NOT work is because when the parfor finishes, and there are, say N workers creating 1000 parallel versions of a.b = k, which one is going to be the final a.b? If 1000's of parallel universes converge into 1 universe, which universe will we be the "real" universe? Error error.
But, a(k).b = k works because each worker creates its own separate variable (or isolated universe), a(1).b, a(2).b, ..., a(N).b. No conflict issues.
In the example, this works too because temp is defined inside the parfor, which tell matlab "this is a temporary variable that's created in the parallel world - do not bring it to the real world"
parfor i = 1:4
temp = struct(); %What's created in the parallel world stays in the parallel world
temp.myfield1 = rand();
temp.myfield2 = i;
end
7 Comments
OCDER
on 16 Jul 2018
More examples for clarity (or confusion).
parfor k = 1:10 %<=== look for the iterator, 'k'
a(k) % sliced, I see a direct use of 'k'
a(k).b % sliced, I see a direct use of 'k' in the 1st level of a struct
a.b % NOT sliced. No use of 'k' anywhere
a.b(k) % Tricky. Not sliced at the 1st lv of struct 'a', but is sliced at 2nd lv 'a.b'.
% Matlab doesn't know if you want to save 'a' or treat as temporary var
a(k).b(k) % Tricky. Sliced variable! Sliced at 1st lv of struct a.
end
Hmm, yeah, the explanation isn't obvious as to why a.b = k is not just assumed to be a temporary variable. My guess is that structures are normally assumed to behave like objects that persistent across iteration, but parfor makes this assumption invalid. To prevent confusion, users have to be VERY CLEAR they know this structure will delete after each iteration unless it's sliced.
for j = 1:10
a.b = j; %structure "a" is created when j == 1, and it persists from here on out.
%a.b = 10 when loop finishes. No ambiguity.
end
parfor j = 1:10
a.b = j; %structure a is created 10 times at random order! a.b = 10, a.b = 2, ... a.b = 5.
%How do we assemble this back to a single "a" structure?
%Should this even be a single structure as an output?
%Was this a temporary variable?
%Let's just make this throw an error. Unclear how to return "a".
end
See Also
Categories
Find more on Loops and Conditional Statements 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!