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:
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)
PS can you put a for statement in a parfor statment?
No products are associated with this question.
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