Parfor Loop Help: Classification of Variables

9 Ansichten (letzte 30 Tage)
Brian W.
Brian W. am 23 Feb. 2018
Kommentiert: Jeff Miller am 23 Feb. 2018
I have the below code that I am attempting to run faster utilizing parallel pooling. The code compiles for lower ranges of w, which I have tried. (example: w=10000:10000:1000000)
% code
for w=10000:10000:100000000
tspan=[0:(1/w)*100:(1/w)*1000000];
[t, y]=ode45(@(t,y) ode_sys(t,y,w),tspan,yo);
output(j,:,:)=[t y];
[amp(j,1) loc(j,1)]=max(y(size(t)-30:size(t),1));
[amp(j,2) loc(j,2)]=max(y(size(t)-30:size(t),3));
[amp(j,3) loc(j,3)]=max(y(size(t)-30:size(t),5));
delta(j,1)=t(loc(j,2)+1,1)-t(loc(j,1)+1,1);
delta(j,2)=t(loc(j,3)+1,1)-t(loc(j,1)+1,1);
j=j+1;
end
I've never utilized parallel pooling before but I am trying to adapt my code to run using parfor. I notice that my code should - "Ensure That parfor-Loop Variables Are Consecutive Increasing Integers", so I have adapted my code as such (using the example in "help")
%code
iValues=10000:10000:100000000;
parfor idx = 1:numel(iValues)
w=iValues(idx);
tspan=[0:(1/w)*100:(1/w)*1000000];
[t, y]=ode45(@(t,y) ode_sys(t,y,w),tspan,yo);
output(j,:,:)=[t y];
[amp(j,1) loc(j,1)]=max(y(size(t)-30:size(t),1));
[amp(j,2) loc(j,2)]=max(y(size(t)-30:size(t),3));
[amp(j,3) loc(j,3)]=max(y(size(t)-30:size(t),5));
delta(j,1)=t(loc(j,2)+1,1)-t(loc(j,1)+1,1);
delta(j,2)=t(loc(j,3)+1,1)-t(loc(j,1)+1,1);
j=j+1;
end
I am still getting the following error however: " Error: The variable output in a parfor cannot be classified."
Any help would be greatly appreciated.
  2 Kommentare
KSSV
KSSV am 23 Feb. 2018
What is j inside the loop?
Brian W.
Brian W. am 23 Feb. 2018
j is initially set to 1 and is just used to compile each iteration in the loop

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jeff Miller
Jeff Miller am 23 Feb. 2018
Can't you replace j with idx (and get rid of j=j+1)? The problem is that MATLAB does know which row of amp and loc should receive the output of each iteration. Each processor knows its own value of idx, but it doesn't really know j because the other processors could also be incrementing that.

Edric Ellis
Edric Ellis am 23 Feb. 2018
@Jeff is correct that replacing j with idx is a necessary change. This is necessary to allow the parfor machinery to realise that you are correctly "slicing" the output variables such as amp. Unfortunately, on its own, that change is not sufficient. For a parfor output ("sliced") variable, you need to make a single assignment. In this case, you need to assign whole rows of amp, loc, and delta. So, you need something like this:
iValues=10000:10000:100000000;
parfor j = 1:numel(iValues)
w=iValues(idx);
tspan=[0:(1/w)*100:(1/w)*1000000];
[t, y]=ode45(@(t,y) ode_sys(t,y,w),tspan,yo);
output(j,:,:)=[t y];
%
% Temporary rows to store the pieces of 'amp' and 'loc'
tmpAmp = zeros(1, 3);
tmpLoc = zeros(1, 3);
[tmpAmp(1) tmpLoc(1)]=max(y(size(t)-30:size(t),1));
[tmpAmp(2) tmpLoc(2)]=max(y(size(t)-30:size(t),3));
[tmpAmp(3) tmpLoc(3)]=max(y(size(t)-30:size(t),5));
%
% Correctly sliced assignment into 'amp' and 'loc'
amp(j,:) = tmpAmp;
loc(j,:) = tmpLoc;
%
% Same pattern for 'delta'
tmpDelta = zeros(1, 2);
tmpDelta(1)=t(tmpLoc(2)+1,1)-t(tmpLoc(1)+1,1);
tmpDelta(2)=t(tmpLoc(3)+1,1)-t(tmpLoc(1)+1,1);
delta(j,:) = tmpDelta;
end
It may even be possible to rearrange your computations so that you compute whole rows of the output variables directly, thus avoiding the temporaries.
  2 Kommentare
Brian W.
Brian W. am 23 Feb. 2018
Thank you for your help Edric. I attempted to use your suggestion and now I get the following error -
"An UndefinedFunction error was thrown on the workers for 'idx'. This might be because the file containing 'idx' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Caused by: Undefined function or variable 'idx'."
Jeff Miller
Jeff Miller am 23 Feb. 2018
At the top of the parfor loop, instead of
w=iValues(idx);
I think Edric meant to write
w=iValues(j);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by