public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tim Prince <tprince@myrealbox.com>
To: Lasse Kliemann <lasse-list-gcc-2008@mail.plastictree.net>
Cc: gcc-help@gcc.gnu.org
Subject: Re: 4.3.0 strange problem with computation and optimization
Date: Sat, 15 Mar 2008 01:20:00 -0000	[thread overview]
Message-ID: <47DB242F.9060804@myrealbox.com> (raw)
In-Reply-To: <20080312173125.GE2471@lasse.mail.plastictre.net>

Lasse Kliemann wrote:
> Greetings,
> 
> I have a C++ program that does some linear programming optimization. Out of 
> this linear programming, I get a vector of doubles `x' and a double `opt' 
> that should be the sum of the entries in `x' (`x' is the solution vector and 
> `opt' is the optimal value). As a consistency chek, I compute the sum of the 
> elements of `x' and then compute the difference to `opt'. It should be close 
> to zero. Here is a snippet from the code:
> 
>    double sum = 0;
>    const double *x = model->getColSolution();
>    for (unsigned int j=0; j<m; ++j) sum += x[j];
>    double d=opt-sum;
>    cout << "difference: " << d << endl;
> 
> If I compile that with g++ version 4.3.0 and -O0, -O1, or -Os, everything 
> looks OK; in rare cases the difference is zero, and in most cases it is a 
> very small number.  Now, if I compile that with -O2 or -O3, the difference is 
> *always* printed to be zero, which I presume is wrong.
> 
> Now for the weird part. Consider the following code:
> 
>    double sum = 0;
>    const double *x = model->getColSolution();
>    for (unsigned int j=0; j<m; ++j) sum += x[j];
>    double d=opt-sum;
>    cout << "difference: " << d << endl;
>    cout << "difference: " << d << endl;
> 
> That is the above with the last line repeated. This works as expected with 
> any optimization, i.e., the same (in most cases: non-zero) number is printed 
> twice.
> 
> There are other strange ways to get this fixed, for example insert a 
> statement like
> 
>    cout << endl;
> 
> between the computation of the sum and the output of `d'. 
> 
> I tried to construct a minimal example by writing `x' and `opt' to a file and 
> reading it in by a small program which only does the summing up, takes the 
> difference and prints it out. Unfortunately, the phenomenon does not show up 
> there. Now, I can provide the program where the problem occurred, it's no 
> more than 70 lines, but it must be linked against the CLP library (from the 
> coin-or.org project).
> 
> Hopefully, someone can give me a hint how to track this down further. The 
> profile is quite sharp, I think: -O0, -O1, -O2 work, but -O2 and -O3 don't. 
> Operating system is Linux 2.6.x on a Intel Xeon E5310.

I don't recall if this is a repeat of a previous post.  As you talk about
differences with optimization level, I assume you have not specified
-mfpmath=sse, and your sum variable is implicitly promoted to long double
in some cases.  This issue has been around ever since the 68881 and 80287
were invented, so there are plenty of places to read about it.  I believe
others have informed you that gcc is not about to disable support for old
CPUs, or even optimization for not so old ones, simply because Microsoft
has hidden the issue from you.

      parent reply	other threads:[~2008-03-15  1:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-12 17:26 Lasse Kliemann
2008-03-12 17:34 ` Andrew Haley
2008-03-12 17:35   ` Andrew Haley
2008-03-13 10:31     ` Tom Browder
2008-03-13 10:36       ` Andrew Haley
2008-03-13 10:37       ` Dario Saccavino
2008-03-14 15:12         ` Ian Lance Taylor
2008-03-14 17:09         ` Lasse Kliemann
2008-03-15  1:20 ` Tim Prince [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47DB242F.9060804@myrealbox.com \
    --to=tprince@myrealbox.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=lasse-list-gcc-2008@mail.plastictree.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).