Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Results not repeatable for sum() function on fixed / deterministic vector

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: Christopher

Date: 5 Apr, 2010 16:38:03

Message: 1 of 9

Hi,

I've run into an interesting problem with the following code:

x1 = (0 : 95999) / 40e6;
x2 = x1 - mean(x1);
sum(x2)
sum(x2)

As I repeat the sum(x2) command and print out the result, I get slightly different answers, not all of which are zero. I can understand that the result doesn't come out to be exactly zero due to precision issues, but I would like to get the same answer every time. Any ideas what is going on here?

I thought may it was due to the use of threading, if different numbers of threads would interchange the order of operations and hence the final result. But setting maxNumCompThreads to 1 still gives an answer that varies.

Here's some output from the 'ver' command:
MATLAB Version 7.8.0.347 (R2009a)
Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)

Thanks in advance,
--Chris

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: us

Date: 5 Apr, 2010 16:47:04

Message: 2 of 9

"Christopher " <camejia@remove.this.alum.mit.edu> wrote in message <hpd3lb$8mc$1@fred.mathworks.com>...
> Hi,
>
> I've run into an interesting problem with the following code:
>
> x1 = (0 : 95999) / 40e6;
> x2 = x1 - mean(x1);
> sum(x2)
> sum(x2)
>
> As I repeat the sum(x2) command and print out the result, I get slightly different answers, not all of which are zero. I can understand that the result doesn't come out to be exactly zero due to precision issues, but I would like to get the same answer every time. Any ideas what is going on here?
>
> I thought may it was due to the use of threading, if different numbers of threads would interchange the order of operations and hence the final result. But setting maxNumCompThreads to 1 still gives an answer that varies.
>
> Here's some output from the 'ver' command:
> MATLAB Version 7.8.0.347 (R2009a)
> Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)
>
> Thanks in advance,
> --Chris

quite interesting...
here
     ic2/2*2.6ghz/2gb/winxp.sp3.32/r2010a.32
we get

     x1=(0:95999)/40e6;
     x2=x1-mean(x1);
     nt=100000;
     xr=nan(nt,1);
for i=1:nt
     xr(i,1)=sum(x2);
end
     r=all((xr-xr(1))==0)
% r = 1

us

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: Bruno Luong

Date: 5 Apr, 2010 16:54:05

Message: 3 of 9

"Christopher " <camejia@remove.this.alum.mit.edu> wrote in message <hpd3lb$8mc$1@fred.mathworks.com>...
> Hi,
>
> I've run into an interesting problem with the following code:
>
> x1 = (0 : 95999) / 40e6;
> x2 = x1 - mean(x1);
> sum(x2)
> sum(x2)
>
> As I repeat the sum(x2) command and print out the result, I get slightly different answers, not all of which are zero. I can understand that the result doesn't come out to be exactly zero due to precision issues, but I would like to get the same answer every time. Any ideas what is going on here?
>
> I thought may it was due to the use of threading, if different numbers of threads would interchange the order of operations and hence the final result. But setting maxNumCompThreads to 1 still gives an answer that varies.
>
> Here's some output from the 'ver' command:
> MATLAB Version 7.8.0.347 (R2009a)
> Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)
>

I believe you point to an almost death horse. Take a look of this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/260828
http://www.mathworks.com/matlabcentral/newsreader/view_thread/261541

There is a serious issue with 2009A.

If you want predictable sum result, take a look at Jan's XSum
http://www.mathworks.com/matlabcentral/fileexchange/26800-xsum (I have my own FEX for this very reason).

Bruno

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: us

Date: 5 Apr, 2010 16:59:06

Message: 4 of 9

"Bruno Luong"
> There is a serious issue with 2009A.

bruno
correct me if i'm wrong - but:
seems to be solved in r2010a...

is there a link to a bug-report(?)...

us

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: Bruno Luong

Date: 5 Apr, 2010 17:09:05

Message: 5 of 9

"us " <us@neurol.unizh.ch> wrote in message <hpd4sq$r62$1@fred.mathworks.com>...
seems to be solved in r2010a...
>
> is there a link to a bug-report(?)...

Here we go us: http://www.mathworks.com/support/bugreports/532399

Bruno

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: James Tursa

Date: 5 Apr, 2010 18:42:06

Message: 6 of 9

"us " <us@neurol.unizh.ch> wrote in message <hpd468$gr4$1@fred.mathworks.com>...
>
> for i=1:nt
> xr(i,1)=sum(x2);
> end
> r=all((xr-xr(1))==0)
> % r = 1

You need to be very careful with tests like this in MATLAB. Sometimes the JIT accelerator will recognize that certain calculations don't change inside a loop and move that calculation outside the loop, effectively torpedoing your intended test. You may need to wrap such function tests inside another function to coax JIT into not optimizing it out of the loop. I know this happens with matrix multiply operations as I had a heck of a time (and still do) designing meaningful timing tests for my mtimesx FEX submission.

James Tursa

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: us

Date: 5 Apr, 2010 19:00:08

Message: 7 of 9

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hpdatu$351$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hpd468$gr4$1@fred.mathworks.com>...
> >
> > for i=1:nt
> > xr(i,1)=sum(x2);
> > end
> > r=all((xr-xr(1))==0)
> > % r = 1
>
> You need to be very careful with tests like this in MATLAB. Sometimes the JIT accelerator will recognize that certain calculations don't change inside a loop and move that calculation outside the loop, effectively torpedoing your intended test. You may need to wrap such function tests inside another function to coax JIT into not optimizing it out of the loop. I know this happens with matrix multiply operations as I had a heck of a time (and still do) designing meaningful timing tests for my mtimesx FEX submission.
>
> James Tursa

james
quite(!) interesting and never heard/thought of it...
for the example shown, it would mean that the jit-engine recognizes that

     sum(x2)

is always the same and computes it only once BEFORE the loop starts(?)...
is this documented(?)...

urs

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: us

Date: 5 Apr, 2010 19:02:05

Message: 8 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hpd5fg$6r4$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hpd4sq$r62$1@fred.mathworks.com>...
> seems to be solved in r2010a...
> >
> > is there a link to a bug-report(?)...
>
> Here we go us: http://www.mathworks.com/support/bugreports/532399
>
> Bruno

thanks for the pointer, bruno
urs

Subject: Results not repeatable for sum() function on fixed / deterministic vector

From: James Tursa

Date: 5 Apr, 2010 21:01:21

Message: 9 of 9

"us " <us@neurol.unizh.ch> wrote in message <hpdbvo$ik8$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hpdatu$351$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <hpd468$gr4$1@fred.mathworks.com>...
> > >
> > > for i=1:nt
> > > xr(i,1)=sum(x2);
> > > end
> > > r=all((xr-xr(1))==0)
> > > % r = 1
> >
> > You need to be very careful with tests like this in MATLAB. Sometimes the JIT accelerator will recognize that certain calculations don't change inside a loop and move that calculation outside the loop, effectively torpedoing your intended test. You may need to wrap such function tests inside another function to coax JIT into not optimizing it out of the loop. I know this happens with matrix multiply operations as I had a heck of a time (and still do) designing meaningful timing tests for my mtimesx FEX submission.
> >
> > James Tursa
>
> james
> quite(!) interesting and never heard/thought of it...
> for the example shown, it would mean that the jit-engine recognizes that
>
> sum(x2)
>
> is always the same and computes it only once BEFORE the loop starts(?)...
> is this documented(?)...
>
> urs

Maybe. An optimizer certainly *could* do that, but I don't know the JIT details for what it does specifically with regards to loops and function calls. I don't know that the JIT details are documented anywhere. I think MATLAB regards this as proprietary and subject to change from release to release and they don't want the community to write code that depends too much on a particular JIT feature, so they don't publish very many details. For the sum() function in particular, I don't know if the JIT will move it out of the loop ... I would have to run some tests. I just know for sure that the mtimes operation *is* effectively moved out of a loop under certain circumstances. The way I got around this in my mtimesx timing tests (drove me nuts until I tracked the cause down btw) was to change one of the operand elements inside the loop so the JIT couldn't move the operation outside the loop.

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us