How to make variable visible to parfor?
2 views (last 30 days)
Show older comments
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
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.
Answers (1)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!