How to make variable visible to parfor?

2 views (last 30 days)
B.V. Vijay
B.V. Vijay on 7 Feb 2012
Edited: Alessio Nava on 3 Oct 2013
I have been having trouble getting the following code to give me the correct result from the parfor in it. Briefly, the code is supposed to use input parameter structure PMTS as input to many other functions called inside the parfor loop, and return a single double value at the end, FI, that is assigned to the I-th element of the UNQF vector.
function RSLT = MANAGENAPART(PMTS)
function PSTR = RETURNPARSTR(PMTS)
POPL = PMTS.POPL;
UNIQ = PMTS.UNIQ;
GDTA = PMTS.GDTA;
NVBS = PMTS.NVBS;
SHPE = PMTS.SHPE;
PSTR.POPL = POPL;
PSTR.UNIQ = UNIQ;
PSTR.GDTA = GDTA;
PSTR.NVBS = NVBS;
PSTR.SHPE = SHPE;
PAZ = size(POPL);
PSTR.MBRS=PAZ(1);
PSTR.SLEN=PAZ(2);
end
AA = @ADDTOARCHIVE;
CB = @COMBINEBSISV;
GS = @GNRTESAFESHP;
DS = @DECODSHPESTG;
NF = @CALCNASFITNS;
RP = @RETURNPARSTR;
PA = RP(PMTS);
M = PA.MBRS;
UNQF = zeros(M,1);
parfor (I=1:M)
PAR =RP(PMTS);
UNIQ=PAR.UNIQ;
POPL=PAR.POPL;
GDTA=PAR.GDTA;
NVBS=PAR.NVBS;
SHPE=PAR.SHPE;
SLEN=PAR.SLEN;
ISUQ=UNIQ(I);
CD=GDTA.CD;
GD=GDTA;
SH=SHPE;
P =POPL;
S =SLEN;
if ISUQ
G=P(I,1:S);
PAR.STRG=G;
DC=DS(PAR);
CX = DC.DX;
CY = DC.DY;
CZ = DC.DZ;
SH.CFTX=CX;
SH.CFTY=CY;
SH.CFTZ=CZ;
LC =CB(SH);
SH.LINC=LC;
SH.PFCD=CD;
[~]=GS(SH);
FI =NF(GD);
UNQF(I)=FI;
end
end
PATS.POPL=POPL;
PATS.FTSV=UNQF;
PATS.UNIQ=UNIQ;
DONE =AA(PATS);
RSLT.FUNQ=UNQF;
RSLT.OKAY=DONE;
end
My work is presently on a cluster of 4 labs. I suspect the PMTS parameter is not visible inside the parfor loop, but as a parfor loop is non- debuggable, I am not sure of the same. However when I run the above code with a 'local' configuration (that is only 1 worker, the host itself), the parfor result seems to work correctly! Can anyone help?
  3 Comments
B.V. Vijay
B.V. Vijay on 7 Feb 2012
Thanks, Ellis. Actually why I suspect that PMTS is not visible inside the parlor loop is because the code gives the same (exactly the same) result for all cases of I from 1 thru M, when it should be giving M different results. I have not yet tried the internal function yet and I will try that out soon.
Konrad Malkowski
Konrad Malkowski on 15 Feb 2012
PMTS is a broadcast variable in this instance, so every worker and every iteration of the PARFOR receives an exactly the same copy of PMTS. Is that what you intended?
There also appears to be an issue with POPL and UNIQ variables. They are declared as "temporary" inside of the PARFOR, and are used to initialize PATS outside of PARFOR. This will work in a for loop, but not in a PARFOR. You need to remember that everything inside of PARFOR exists in a different MATLAB workspace, than the code outside of PARFOR.

Sign in to comment.

Answers (1)

B.V. Vijay
B.V. Vijay on 17 Feb 2012
Ok, the above code works as it is, correctly. Thanks to you, Ellis and Malkowski, for your observations. I managed to track the bug to another section of my code outside the above, and had that corrected a couple of days back.
Ellis- when I tried moving the function RETURNPARSTR to the end of the code as you suggested, the MATLAB editor immediately produced lots of warnings (variables getting underlined). As these warnings had never appeared in the code form as seen above, I didn't change the code structure from the above.
Malkowski- regarding the POPL and UNIQ variables, I verified (using the debugger with a breakpoint on the PATS.POPL=POPL; statement) that their values were the same as those from replacing the parfor (I=1:M) by a for I=1:M loop.
Thanks, again. Nevertheless hope we can see some more documentation in future on these issues with the parfor function.

Community Treasure Hunt

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

Start Hunting!