Parfor Loop Help: Classification of Variables
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Jeff Miller
on 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.
0 Comments
Edric Ellis
on 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 Comments
Jeff Miller
on 23 Feb 2018
At the top of the parfor loop, instead of
w=iValues(idx);
I think Edric meant to write
w=iValues(j);
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!