Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: FOR loops performance - 32bit vs 64 bit Matlab
Date: Mon, 4 Aug 2008 10:33:54 -0400
Organization: The MathWorks, Inc.
Lines: 76
Message-ID: <g7740j$ii2$1@fred.mathworks.com>
References: <g717k3$kb3$1@news.dialog.net.pl>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1217860435 19010 144.212.105.187 (4 Aug 2008 14:33:55 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 4 Aug 2008 14:33:55 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
Xref: news.mathworks.com comp.soft-sys.matlab:483514




"uC" <bla.bla@uc.uc> wrote in message 
news:g717k3$kb3$1@news.dialog.net.pl...
> Hi all,
>
> Yesterday I have written tha post entitled "density matrix conversion - 
> vectorization" where I needed some help regarding vectorization to improve 
> the speed. It turned out that in fact I don't need to vectorize it and 
> performance problem is somewhere else.
>
> There are three loops:
>
> for n=1:col_no
>        for m=1:row_no
>            for k=1:D(m, n)
>                [SOME SIMPLE ARITHMETIC OPERATIONS]
>            end
>            if rand() < D(m, n)-floor(D(m, n))
>               [SOME SIMPLE ARITHMETIC OPERATIONS]
>            end
>        end
>    end
> end
>
> On 32 bit system (XP Pro) and Matlab (2006a) it takes ~17 seconds to 
> finish these operations but on 64 bit (Win 2003 Serv. End. Ed.) and 64 bit 
> Matlab (2007b) it takes 370 secons!!!
>
> Now the most interesting... when I substitute exact numbers for "col_no" 
> and "row_no" (6700 and 7100 in tested case) 64 bit Matlab needs only 8 
> seconds to finish, when on 32 bit one timings remains almost untouched!
>
> 32 bit machine is dual core Athlon64x2 2GHz with 2 GB RAM
> 64 bit machine is dual socket dual core Opteron 2218 2.6 GHz with 8 GB RAM
>
> Does anyone have an idea if it is a Matlab bug or anything else?

You need to post the rest of the code, including what col_no, row_no, D, and 
the two instances of "[SOME SIMPLE ARITHMETIC OPERATIONS]" are.

Do you grow a matrix inside "[SOME SIMPLE ARITHMETIC OPERATIONS]", like 
this:

d = zeros(1, 0);
for k = 1:10
    d(k) = k;
end

If so, preallocating that matrix would prevent MATLAB from having to 
reallocate memory for that matrix each and every iteration through the loop.

Secondly, since you're using RAND, make sure you initialize the state to the 
same value before each of your calculations.  If during one execution of 
your program you encounter a long run of small random numbers, but the 
second you encounter a run of large random numbers, the second "[SOME SIMPLE 
ARITHMETIC OPERATIONS]" block will execute a different number of times.

Third, if possible generate matrices of random numbers and index into them 
instead of calling the RAND function many times, each time generating a 
scalar.  You will need more memory for that approach, but by reducing the 
number of times you call RAND, you'll likely save on function call overhead. 
[There is some, though usually it's small -- but if you call the function 
thousands or millions of times, it can add up.]

Fourth, after you "substitute exact numbers" for the limits of your FOR 
loops, do you make sure to run the function/script again with a clean 
slate -- i.e. "clear all", "clear functions", etc?  If not, the improvement 
you see may be caused because you're no longer growing your matrix inside 
the loops (it's already reached its largest size during the first run 
through.)

-- 
Steve Lord
slord@mathworks.com