PPS if you could comment also on how to replace more than two nested for statements with parfor that would also be great!--Amelia
ode parallel processing help
9 views (last 30 days)
Show older comments
Hi everyone! I was wondering if anyone could help me out. I am trying to map parameter space for a set of coupled oscillators. I can map it, but for one run it sometimes takes the whole day. I have reading about parfor that can be used for parallel processing of iterations to speed up the processing time.
My code is set up such that it stores a phase difference in a multidimensional array for each combination of parameter values, and passes through many parameters (eventually there will be nine independent parameters) and a large range of values for each parameter. Unfortunately I am not understanding the logic of the parfor statement...it gives me an error when I try to store things in my array. I understand that you cannot nest parfors; however, I guess without nested for statements, I can't really understand how the indexing works.
Also I have Matlab 2007b (7b), when I think that parfor changed in some way. However, even if you dont know matlab 2007 that is fine. I have access to 2012 at work.
Here is example of my SLOW code:
A1_vector=(0:0.1:10); number_of_A1s=length(A1_vector);
B1_vector=(-10:0.1:10); number_of_B1s=length(B1_vector);
my_data1=zeros(number_of_A1s, number_of_B1s);
for i=1:1:number_of_A1s
for j=1:1:number_of_B1s
tspan=0:pi/96:30;
x0=2*pi*(rand(1,number_of_oscillators));
A=A1_vector(i);
B=B1_vector(j);
[t,s]=ode15s(@(t,a)coupledvdp2c(t,a,A,B),tspan,x0);
if std(s(:,1)-s(:,2))>0.1;
my_data1(i,j)=nan;
else
my_data1(i,j)=mod(mean(s(:,2)-s(:,1)),2*pi);
end
end
end
pcolor(my_data1)
Thanks! Amelia
PS can you put a for statement in a parfor statment?
5 Comments
José-Luis
on 18 Aug 2012
"Unfortunately I am not understanding the logic of the parfor statement...it gives me an error when I try to store things in my array."
Answers (1)
José-Luis
on 20 Aug 2012
Edited: José-Luis
on 20 Aug 2012
PARFOR loops can not be nested. So rewrite your code to avoid the double for loop. For that you can create a matrix of indices:
idx = NaN * ones(number_of_A1s * number_of_A2s,2);
counter = 0;
for k = 1:1:number_of_A1s
for l=1:1:number_of_B1s
counter = counter + 1;
idx(counter,1) = A1_vector(k);
idx(counter,2) = A2_vector(k);
end
end
There is a better way to do the above, but i'm soon on company time... And then you can do your simulations in a single parfor:
parfor k = 1:number_of_A1s * number_of_A2s
value_ofA1 = idx(i,1);
value_ofA2 = idx(i,2);
%do your stuff here...
end
Cheers!
4 Comments
José-Luis
on 21 Aug 2012
Edited: José-Luis
on 21 Aug 2012
It is very strange that the first loops ar so slow, especially if preallocated. Anyway, if you don't want the two for loops in the beginning:
A1_vector=(0:0.1:10)'; nA1s=length(A1_vector);
A2_vector=(-10:0.1:10)'; nA2s=length(A2_vector);
idxVec = NaN * ones(nA1s * nA2s,2);
idxVec(:,1) = cell2mat(arrayfun(@(x) repmat(x,nA2s,1) , A1_vector , 'uniformoutput',0));
idxVec(:,2) = repmat(A2_vector,nA1s,1);
But note that you don't really have to create that matrix of indices. You just need to create A1_vector and B1_vector. Once you are inside your parfor loop, then you can use ind2sub:
parfor k = 1:number_of_A1s * number_of_A2s
[v b] = ind2sub([number_of_A1s,number_of_A2s],k);
value_ofA1 = A1_vector(v);
value_ofA2 = A2_vector(b);
%do your stuff here...
end
You can test it out for yourself to see which one is faster. I would place my money on creating the vector of indices beforehand...
Cheers!
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!