Usage of sturctures inside parfor!!

44 views (last 30 days)
Abhishek M
Abhishek M on 21 Apr 2014
Commented: Abhishek M on 5 May 2014
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
Abhishek M
Abhishek M on 22 Apr 2014
Hi Edric,
Please find the below code.
model = 'a_demo_model'; %%%My model %%%
numCases=100;
parfor j=1:numCases
S=load('a_demo_WS');%%%Loading of input to workspace through %%%"demo_WS.mat"
ip=S.ip; %%%Normal input %%%
a.b=S.a.b; %%%Normal Structure input %%%
a.d(fg)=S.a.d(fg); %%%Multiple Structure input %%%
load_system(model)
assignin('base','ip',ip)
assignin('base','a.b',a.b)
assignin('base','c.d(fg)',c.d(fg))
simout(j)= sim(model,'SimulationMode','normal');
end
I am running 'a_demo_model' using this code. But I have 2 more models to be integrated. All the 3 models which i have not yet integrated uses the same input 'b(normal structure)' and 'd(fg)(multiple structure)'. But parfor is throwing these( " 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. ") errors if I define these inputs as the syntax specified in the above code.
I hope I am clear with this to you or else we can discuss more for further clarifications.

Sign in to comment.

Answers (1)

Edric Ellis
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
Abhishek M
Abhishek M on 5 May 2014
Hi Edric, my mistake sorry for mentioning previously, my syntax of structures and multiple structures doesn't work in normal for loop also...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!