How to make variable visible to parfor?
이전 댓글 표시
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
Edric Ellis
2012년 2월 7일
What makes you thing PMTS is not 'visible'? Do you get an error? Wrong answer? Any chance you could reduce your code down to something which demonstrates the problem, but without the external dependencies? Does it make any difference if you change RETURNPARSTR to be an internal function (i.e. defined after then end of MANAGENAPART)?
B.V. Vijay
2012년 2월 7일
Konrad Malkowski
2012년 2월 15일
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.
답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 Functional Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!