Parallel computing and fmincon

Hi, my problem relies in running a huge amount (10000) of indipendent simulations. Each of them implies the use of fmincon. To make it run faster I decided to use the parallel computation toolbox as follows:
matlabpool
parfor ii = 1:numel(k1)
[z(:,ii),exitflag(1,ii),output(ii)]=ParallelOpt(Aeq,Ex,Ey,nx,sll,k1(1,ii),k2(1,ii),Z0(:,ii),imax,jmax);
end
matlabpool close
where ParallelOpt is
function [z,exitflag, output]=ParallelOpt(Aeq,Ex1,Ey1,nx1,sll1,c1,c2,x0,imax1,jmax1)
global nx
global sll
global Ex
global Ey
global imax
global jmax
nx=nx1;
sll=sll1;
Ex=Ex1;
Ey=Ey1;
imax=imax1;
jmax=jmax1;
Beq=zeros(3,1);
Beq(2,1)=Beq(2,1)+c1;
Beq(3,1)=Beq(3,1)+c2;
options=optimset('LargeScale','off','GradObj','on','GradConstr','on','Display','iter','MaxIter',100000, 'Algorithm','active-set' );
[z,fval,exitflag,output]=fmincon(@funRealEx,x0,[],[],Aeq,Beq,[],[],@nonlcon,options);
end
I know that you cannot use global variables in a parfor, but I needed them to pass some parameters to nonlcon and funRealEx functions. So to go around the problem I have embedded all the global variables into a function ParallelOpt, making them global just in the worker that processes the iteration of the parfor corresponding to an execution of fmincon.
In some cases the code runs without any problems but in case of longer simulations a recurrent error occurs:
What is really strange is that running more times the same code it occurrs in different iterations of the parfor.. and it is like the worker does not have access to some data.
I need parallel computation otherwise it takes forever, meaning weeks.
Any advice is wellcome..many thanks in advance. Anna

Answers (1)

Hi,
I don't think this is the source of your problems, but nevertheless: you don't need global variables. Let's say your function funRealEx needs the variables nx, Ex, Ey, then do the following: define your function funRealEx as
function y = funRealEx(x, nx, Ex, Ey)
and call the optimizer with
[z,fval,exitflag,output]=fmincon(@(x) funRealEx(x, nx, Ex, Ey), ...);
This way you don't need global variables at all.
Titus

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Asked:

on 4 Apr 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!