How using a script of variables in a parfor loop ?

Hello,
I have gathered the declaration of variables in one and the same script, to simply reduce the number of lines in the main function. The script of variables therefore contains:
Var1 = 2;
Var2 = 3;
....
The loop just reads the script.
parfor i = 1:5
scriptOfVariables
Var3 = Var1 + Var2
end
It seems that parfor does not accept it and I have to write the set of variables directly in the loop.
Any idea to avoid overloading the loop?

2 Comments

why not save/load the variables in/as mat/struct? Though not sure about overhead issues (should be fine if variables are few).
Thank you for your answer !
Because I don't want then, during the many variable calls, to have to use dot notation each time, which makes the code more cumbersome.

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 13 Jan 2022
Edited: Matt J on 13 Jan 2022
Does scriptOfVariables use the loop counter i in any way? If not, you should move it outside the loop,
scriptOfVariables;
parfor i = 1:5
Var3 = Var1 + Var2;
end
Otherwise, you should be using a function instead
parfor i = 1:5
[Var1,Var2]=mfunction(i);
Var3 = Var1 + Var2;
end

4 Comments

Thank you for your answer !
No, scriptOfVariables does not use the loop counter i in any way. But, in the script there is the declaration of structure type variables.
Var1 = 2;
Var2 = 3;
struct.var(1) = 5;
struct.var(2) = 7;
....
New values are then assigned to them in the parfor loop.
parfor i = 1:5
scriptOfVariables
Var3 = Var1 + Var2
struct.var(1).type = 6;
....
end
The concern is that if I take the script out of the loop, Matlab shows me the error below :
Error: Unable to classify the variable 'struct' in the body of the parfor-loop.
For more information, see Parallel for Loops in MATLAB, "Solve Variable
Classification Issues in parfor-Loops".
From what I understand, Matlab cannot recognize structures declared outside the loop and used inside the loop. Reason why I want to insert the script inside the loop..
About your second suggestion, it's simply because of the large number of variables :)
"Unable to classify the variable 'struct' in the body of the parfor-loop."
Do not define variables named struct.
From what I understand, Matlab cannot recognize structures declared outside the loop and used inside the loop.
No, that's not the problem. The problem is that you are not allowed to assign to variables declared outside the loop unless it is a sliced variable. However, you can return the struct from a function:
parfor i = 1:5
[S,Var1,Var2]=declareTemp();
Var3 = Var1 + Var2
struct.var(1).type = 6;
....
end
function [S,Var1,Var2]=declareTemp()
Var1 = 2;
Var2 = 3;
struct.var(1) = 5;
struct.var(2) = 7;
end
Thanks again for your answer.
Actually, I simplified the example.
The reality is that the struct has multiple fields (S.var, S.coco, S.moo, etc.). So if I don't provide the structure as input parameter of the function you propose, these other fields (other than .var) will all be cleared.
[S,Var1,Var2]=declareTemp(S);
What is surprising is that parfor refuses I provide "S" as input to the declareTemp function, but it accepts the other variables (of type dooble) even though they are also declared in the script before the parfor loop
[S,Var1,Var2]=declareTemp(Var1,Var2);

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!