public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* 4.3.0 strange problem with computation and optimization
@ 2008-03-12 17:26 Lasse Kliemann
  2008-03-12 17:34 ` Andrew Haley
  2008-03-15  1:20 ` Tim Prince
  0 siblings, 2 replies; 9+ messages in thread
From: Lasse Kliemann @ 2008-03-12 17:26 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2184 bytes --]

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.

Thank you!
Lasse

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2008-03-15  1:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-12 17:26 4.3.0 strange problem with computation and optimization 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 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).