Usage of sturctures inside parfor!!
68 views (last 30 days)
Show older comments
Hi,
I have a code which I am using to simulate a model 'n' number of times using parallel computation toolbox. Some of the inputs to the model are in the form of "structures(a.b)" and " multiple structures(a.b(c))". I am loading the input through workspace. The code simulates without any errors in normal for loop. But when I change the for to parfor loop the code analyser shows an error that " Valid indices are restricted for this input inside the parfor loop" for structures and "Field reference for multiple structure elements that is followed by more reference blocks is an error." for multiple structures. I can change the name of structure and represent it as a variable but the problem is the same variable is used at different places when I integrate all my models. Hence I cant do that??
Is it possible to represent structures and multiple structures inside parfor loop??
For example:
structures = a.b;
multiple structures = a.b(c);
2 Comments
Edric Ellis
on 22 Apr 2014
Please could you post a simple self-contained example of something you'd like to run but doesn't work in PARFOR?
Answers (1)
Edric Ellis
on 22 Apr 2014
A simpler reproduction of the problem is shown below:
parfor idx = 1:2
a.x = 1;
a.y = 2;
end
The problem here is that technically the iterations of the loop cannot be proven to be order-independent (even though we can clearly see that they are). The reasons for this are a little obscure, and are to do with how structure dot-referencing works - basically, MATLAB treats "a.x" as a indexing request into the variable "a". So, in the loop above, you're indexing into "a" twice, but not "slicing" it - and in a FOR loop, the value of "a.y" would be available at the start of the next iteration of the loop, thus making it technically not order-independent.
In this case, the fix is simply to ensure that you assign a complete new value to "a" each time round the loop - then MATLAB will be able to see that "a" is a temporary variable that needs to live only for the duration of the iteration. Like so:
parfor idx = 1:2
a = [];
a.x = 1;
a.y = 2;
end
Also, note that the ASSIGNIN you posted is incorrect. You probably need
assignin('base', 'a', a)
4 Comments
See Also
Categories
Find more on Parallel for-Loops (parfor) 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!