Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Avoiding loops
Date: Wed, 21 Jan 2009 15:45:03 +0000 (UTC)
Organization: Univ of Newcastle upon Tyne
Lines: 113
Message-ID: <gl7ftv$h2k$1@fred.mathworks.com>
References: <gkkcva$4ka$1@fred.mathworks.com> <gl2lsh$1cv$1@fred.mathworks.com> <gl4g4c$mmh$1@fred.mathworks.com> <gl4nrp$omn$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1232552703 17492 172.30.248.37 (21 Jan 2009 15:45:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 21 Jan 2009 15:45:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1511480
Xref: news.mathworks.com comp.soft-sys.matlab:512946


"Doug Hull" <hull@mathworks.SPAMPROOFcom> wrote in message <gl4nrp$omn$1@fred.mathworks.com>...
> 
> > Dear Doug,
> > thanks very much for your help and correct my msprint,
> > now this code works more efficiently.
> > But, still I would like to reduce the time of my code,
> > do you think using (parfor) instead of (for) in one loop  with the
> > computer parallel toolbox can reduce time in this code?
> > Thanks in advance,
> > Jose.
> 
> Jose,
> 
> I would caution against premature optimization.  Get your algorithm working and then see if it needs optimization.  People very often can work at optimizing code without knowing if it really needs it.
> 
> That being said, your code is a candidate for "embarrassingly parallel".  Essentially, you are doing a set number of iterations, 2,000,000 (nv * nx).   Each of these iterations are completely independent of one another.  Thus, you could do them in any order, or all at the same time (if you had 2,000,000 MATLAB sessions)
> 
> Basically, the more processors you throw at this, the faster it will go.
> 
> Here is a job time estimator that I made a few years ago.  It should still be a reasonable guess at how long a job will take on a cluster.
> 
> http://www.mathworks.com/matlabcentral/fileexchange/13075
> 
> -Doug

Hello Doug, thanks for your reply.
Now, I installed parallel computing toolbox to check it, but something is wrong
in my approach to get better time parallelizing it:

%code without parallelization

clc
clear all
d = 1;
nv=1000;
nx=2000;
x=rand(nx,2); % data 2D
sigma=cov(x);
bbb = inv(sigma); %%%%% MOVED
y=zeros(1,nx);

tic
 for j=1:nv % Loop in free parameter mu
 mu= [rand(1,1) rand(1,1)];

 for i=1:nx % Loop to calculate the likelihood function (lf)
 ir=round(1+(nx-1)*rand(1,1));
 aaa = (x(ir,:)-mu);

 ccc = (x(ir,:)-mu)';
 y(i)=aaa*bbb*ccc;
 end

 lf=0.5*sum(y)+nx*0.5*log((det(sigma)))+nx*0.5*d*log(2.0*pi); % likelihood
 p1(j)=lf;
 end
 p=exp(-sum(p1(j))/nv)
 
 toc

22.60 sec.



%code using parallelization

Now, i start matlabpool and suply (parfor) by (for)  in the second loop of nx:


Starting matlabpool using the parallel configuration 'local'.
Waiting for parallel job to start...
Connected to a matlabpool session with 4 labs.

clc
clear all
d = 1;
nv=1000;
nx=2000;
x=rand(nx,2); % data 2D
sigma=cov(x);
bbb = inv(sigma); %%%%% MOVED
y=zeros(1,nx);

tic
 for j=1:nv % Loop in free parameter mu
 mu= [rand(1,1) rand(1,1)];

 parfor (i=1:nx) % ONLY MODIFIED IT IN MY CODE.
 ir=round(1+(nx-1)*rand(1,1));
 aaa = (x(ir,:)-mu);

 ccc = (x(ir,:)-mu)';
 y(i)=aaa*bbb*ccc;
 end

 lf=0.5*sum(y)+nx*0.5*log((det(sigma)))+nx*0.5*d*log(2.0*pi); % likelihood
 p1(j)=lf;
 end
 p=exp(-sum(p1(j))/nv)
 
 toc

101.39 sec

The time is more slower paralelizing it, 
Please Doug, do you know what I am doing wrong?

My laptop is a centrino duo ...i.e with two processors.

Thanks in advance.

Jose.