How to adapt my for loop to get it working with parfor? (ODE solver)

2 views (last 30 days)
Hello,
Ive come a long way with programming as a newbie in Matlab, but now Im really stuck. Ive tried to 'slice' the variable dy, but unfortunately it didnt work. I guess the problem lies in the fact that i is dependent on SACn. Ive done this because the ODE solver needs a vector as input, not a matrix. Can someone please help me?
This is a snippet of code:
parfor SACn=0:SACnum-1
i=19+SACn*200:1:66+SACn*200;
dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:);
dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
I tried something like this (didnt work though):
parfor SACn=0:SACnum-1
temp1dy=zeros(1,200);
temp2dy=zeros(1,200);
for i=19:1:66
temp1dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:);
temp2dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
dy(i-18+100+SACn*200,:)=temp1dy;
dy(i-18+150+SACn*200,:)=temp2dy;
end
Additional info: I have vectorized my code in an ODE solver of Matlab, these are all the :) 's you see at the end of all variables such as y(i+18-100,:). Ive done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
Please do not hesitate to ask for more details!

Accepted Answer

Edric Ellis
Edric Ellis on 6 Mar 2015
For dy to be successfully "sliced", you need to index it in precisely the correct form - where one of the subscripts is the loop variable SACn, and the other subscripts follow the other rules described here in the doc.
I think what you need to do is to make dy have an extra dimension - currently you're trying to assign into all the columns, and a range of rows. I would try making dy three-dimensional, and then you should be able to assign into it in the following form:
dy(:, :, SACn) = <...>;
You'll need to unpick this different form outside the parfor loop.
  1 Comment
Sanne
Sanne on 6 Mar 2015
Hi Edric, Thank you so much for taking the time to look at my problem :) Actually during the night I kept trying and I got a little bit further. This is now my code. (Sorry for not posting it immediately I was so tired I fel asleep xD)
*size(y,2) (or all the :)'s further up the code) is the number of parallel solutions for the same dy at the same time point the ODE solver computes, as a result of vectorisation of the code nessecary for the first speedup. As far as I know I dont have control of this size.
*I put back the vector i=19:1:66; instead of the for loop i=19:1:66 and it didnt give me different results (thats how my code was before, to be faster)
dyt1=zeros(SACnum,200,size(y,2));
dyt2=zeros(SACnum,200,size(y,2));
parfor SACn=0:SACnum-1
temp1dy=zeros(200,size(y,2));
temp2dy=zeros(200,size(y,2));
i=19:1:66;
temp1dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:);
temp2dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
dyt1(SACn+1,:,:)=temp1dy;
dyt2(SACn+1,:,:)=temp2dy;
end
for SACn=0:SACnum-1
dy(SACn*200+1:(SACn+1)*200,:)=dyt1(SACn+1,:,:);
dy(SACn*200+1:(SACn+1)*200,:)=dyt2(SACn+1,:,:);
end
My (new) understanding of parfor loops is: you can only assign variables to a matrix with the current loop variable, the outer or nested loop variables are unknown. Also you can only use for the first index: a scaler or all fields, not a range (I guess this is why my first example didnt work) Is this correct?
However, the loop SACn=0:SACnum-1, actually loops over a few additional other 'i' loops in the code, of which now I just put regular loops. Unfortunately then the ODE solver does not give the same results, so Im playing around a little with that.
Also Im wondering if I need this much code (maybe I put too much temps in?) But I guess Ill think about that later, after the whole code works in parfor loop :)
Again, thanks a bunch for looking! And Ill let you know what happens after trying to implement the rest of the code

Sign in to comment.

More Answers (1)

Jan
Jan on 6 Mar 2015
I've done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
This sounds like you use the ODE solver for non-smooth values. This collides with the specifications of the step-size control. Are you using a fixed-step solver?
  2 Comments
Sanne
Sanne on 6 Mar 2015
Hello Jan,
Thanks for thinking with me about my problem! Im using ode15s , I think this one takes variable step size, right?
'This collides with the specifications of the step-size control' Do you mean that the solver might take different step sizes at the same time range for different SACn's because I parallellised? Im not sure how exactly parfor works in an ODE solver, but theoretically I guess it would be possible because I have 200 ODE's per cell. If I then parallellize each 200-dy block per cell for one time step , and then compute the next time step again parallelized, it is independent of each other.
Could you please tell me why it may not work like that or in what direction I have to look if this is so?
Sanne
Sanne on 16 Mar 2015
Hi Jan,
I think I bumped into the problem you mentioned.. The parallelization must concentrate on one time step. Do you know if this is possible?
I guess its too much work to look at the code but maybe you can give a pointer on what to change? Here is a link to the code I have so far:

Sign in to comment.

Categories

Find more on Startup and Shutdown 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!