MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# ode parallel processing help

Asked by JJ on 18 Aug 2012

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?

JJ on 18 Aug 2012

what do you mean?

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."

JJ on 20 Aug 2012

I am not even sure how to script the parfor to get a similar piece of code as above...I tried to follow http://blogs.mathworks.com/seth/2010/10/17/parallel-computing-with-simulink-running-thousands-of-simulations/ but how to use the same index for multiple parameters...I don't really understand!

When I tried changing the first 'for' into a 'parfor', I got the error:

??? Error: File: solvecoupledvdp2c.m Line: 35 Column: 21 The variable my_data1 in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview"

Which I read, and I semi-understand that my_data, which stores data from the parfor index and the for index is not a valid variable inside this for statement.

## Products

No products are associated with this question.

Answer by José-Luis on 20 Aug 2012
Edited by 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);
end
```

Cheers!

José-Luis on 20 Aug 2012

You are completely right. Sorry, there was a typo in my previous post, by

```parfor k = 1:number_of_A1s * number_of_A2s
value_ofA1 = idx(i,1);
value_ofA2 = idx(i,2);
end
```

I meant

```parfor k = 1:number_of_A1s * number_of_A2s
value_ofA1 = idx(k,1);
value_ofA2 = idx(k,2);
end
```

Then it should work

Cheers!

JJ on 21 Aug 2012

But it does not seem to have sped anything up maybe because it has these two for loops at the beginning...

It also says in the warning labels that "idx is indexed but not sliced, which might cause unnecessary communication overhead."

My other thought was to try to create a number_of_B1s by number_of_B2s by 2 matrix. and index just the x and y part, so that each index refers to a 1x2 vector, which can be plugged into the set of differential equations. I guess I will experiment with this...

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);