Parfor Error Handling results in falsely reported error in each loop

조회 수: 1 (최근 30일)
Thomas Förster
Thomas Förster 2017년 9월 8일
답변: Thomas Förster 2017년 9월 9일
Hello, I have a problem regarding parfor loops. I try to solve an array of pdeobjects, that all have a different partly different geometry. Since for some of those geometries the pdenonlin solver doesnt find a solution ( -> Step size to small, to many iterations) I tried using an error handle:
Torque=zeros(1,(length(sCalc.angle)-1));
Error=zeros(1,(length(sCalc.angle)-1));
parfor i=1:1:(length(sCalc.angle)-1)
try
results = solvepde(sMotor(i));
Torque(i)=getTorque2(results);
result(i)=results;
Error(i)=0;
catch
Error(i)=1;
end
end
This works for a normal for:
Error 0 0 0 0 1 0
But when using parfor, even though it doesnt fail I get an Error for each loop:
Error 1 1 1 1 1 1
What am I doing wrong?
  댓글 수: 1
OCDER
OCDER 2017년 9월 8일
Not sure because we don't know what these are: sCalc, sMotor, solvepde, and getTorque2. But instead of catching your Error in a matrix, try to display the error message in the catch statement. That may reveal the error source.
Torque=zeros(1,(length(sCalc.angle)-1));
Error=zeros(1,(length(sCalc.angle)-1));
parfor i=1:1:(length(sCalc.angle)-1)
try
results = solvepde(sMotor(i));
Torque(i)=getTorque2(results);
result(i)=results;
Error(i)=0;
catch ME
disp(ME.message); %Will show the error info
Error(i)=1;
end
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Thomas Förster
Thomas Förster 2017년 9월 9일
Well, rookie mistake on my part, doing it like this it works. Would recommend to not use pde arrays in the way that I use them, since they need a lot of memory!
Works like this:
Torque=zeros(1,(length(sCalc.angle)-1));
Error=zeros(1,(length(sCalc.angle)-1));
result(length(sCalc.angle)-1)=pde.StationaryResults;
parfor i=1:1:(length(sCalc.angle)-1)
try
result(i) = solvepde(sMotor(i));
Torque(i)=getTorque2(result(i));
Error(i)=0;
catch
Error(i)=1;
end
end

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by