Reputation: 388
I am facing a weird situation with Visual C++ compiler optimization.
In a rather mid-size c++ code, compromising 10 static libraries, if the code is compiled with optimization being On (either /O1, /O2, or /Ox), the run of the program produces correct output.
With /Od though, the program produces wrong output.
I know this question is very general, but I appreciate any clue what the cause can be, or to what it can be associated.
p.s. the code is doing FEM numerics, so much of floating-point operations. p.s. the code is in Visual Studio 2010.
EDIT: sample output of the code:
the difference in r (residual) is considerable
I. without optimization:
Solver. time: 0.12, iteration: 1
Solver.
------------------------------------
determining values:
dg. 0
limit of dg. 0.897278
dr. 7675.3
limit of dr. 45.3704
r. 7675.3
limit of r. 453.704
dx. 0.122164
limit of dx. 8.97278e-005
------------------------------------
II. with optimization:
Solver. time: 0.12, iteration: 1
Solver.
------------------------------------
determining values:
dg. 0
limit of dg. 0.897278
dr. 5894.53
limit of dr. 45.3704
r. 5894.53
limit of r. 453.704
dx. 0.122164
limit of dx. 8.97278e-005
------------------------------------
p.s. I can not provide sample of code itself, bcs it really is not known where in the code the problem is stemming from.
Upvotes: 0
Views: 327
Reputation: 388
The bug was revealed, it was uninitialized variable which looked like as initialized! I had defined a (double) constant kInfinity value in numerics library. Other libraries (fem, etc., which very much relied on the numerics library) took this value on the onset of the program from the numerics library, for using this value later on through the run of the program. The thing was that they took this value before it got initialized in the numerics library in the first place (this was like a race situation.)
Now comes the interesting part:
without optimization, the compiler set the uninitialized variables to 'zero' (quite well-known behaviour.) If such value is used in a "linear elastic material model" as "yield stress", that means non-physical material, meaning the code fails to produce interesting output.
with optimization, the compiler set the uninitialized variables to 'random' (in my case very big) number. If such value is used in a "linear elastic material model" as "yield stress", that perfectly means a linear elastic material (quite physical.)
That explains why with optimization my code worked, but not without.
Thanky for all the replies and sharing thoughts.
Upvotes: 1
Reputation: 1817
It is difficult to say, but optimization could for example remove some arithmetic expressions and put "optimized" ones. By modifying the arithmetic expressions containing operations like addition, multiplication and so on, the results are influenced. You can have overflow, precision loss, ...
Upvotes: 0