Possible Optimization bug

Possible Optimization bug

For the following code:

#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
    int const dimension = 2;
    int const number_of_nodes = 20;
    std::vector<double> x(number_of_nodes*dimension);
    for (int n=0; n<number_of_nodes; ++n)
    {
        x[0+dimension*n] = 0.01*n;
        x[1+dimension*n] = 0.02*n;
    }
    for (int i=0; i<40; ++i)
        std::cout << "x[" << i << "]: " << x[i] << std::endl;
    return 0;
}

I get the wrong output with -O3:

x[0]: 0
x[1]: 0
x[2]: 0.01
x[3]: 0.02
x[4]: 0
x[5]: 0
x[6]: 0.03
x[7]: 0.06
x[8]: 0
x[9]: 0

...

I get the correct output with -O2:

x[0]: 0
x[1]: 0
x[2]: 0.01
x[3]: 0.02
x[4]: 0.02
x[5]: 0.04
x[6]: 0.03
x[7]: 0.06
x[8]: 0.04
x[9]: 0.08

I get the same results with versions 15.0.090 and 15.0.3. This does not occur with 14.0.4.

Any help would be appreciated.

 

Thanks.

 

 

 

 

 

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi,
This could be a case of floating point consistency issue and uncertainty.  Can you try "-fp-model=precise" and run your test? This will
ensure value safe optimization and should reproduce to the same exact values always.  You can also read the paper on it at: https://software.intel.com/sites/default/files/managed/9d/20/FP_Consistency_300715.pdf  

In the meantime, I'll look at this issue as well and if an issue will file with the developers, thx.

_Kittur

Hi,

With my compiler (MS VS 2015 and Intel Parallel Studio XE 2016 - Update 2, version 2016.0.063), ​this does not occur and does not depend on "-fp-model" option.

Regards,

Jerzy

@Jerzy, yes I tried with the latest 2016 version and is an issue (of course, using -fp-model=precise takes care of it) but have filed with the team to look into and will keep you updated, thanks.
_Kittur 

Here is what I am getting with latest C++ XE 2016:

C:\WORK\C++>icl /O3 /FAs bug.cpp
Intel(R) C++ Intel(R) 64 Compiler for applications running on IA-32, Version 16.0.0.110 Build 20150815
Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

bug.cpp
Microsoft (R) Incremental Linker Version 12.00.40629.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:bug.exe
bug.obj

C:\WORK\C++>bug
x[0]: 0
x[1]: 0
x[2]: 0.01
x[3]: 0.02
x[4]: 0.02
x[5]: 0.04
x[6]: 0.03
x[7]: 0.06
x[8]: 0.04
x[9]: 0.08

I looked at the assembler code and the initialization loop with 20 iterations is completely unrolled -- compiler has precalculated constants and is just doing MOVSD at runtime. How exactly did you get wrong code? Can you list full compile options?

Hi Igor,
Yes, that's correct you don't have an issue on windows. But, if you try using the latest 2016 version  on Linux* you can reproduce:
%icc -V
Intel(R) C Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 16.0.0.109 Build 20150815

_Kittur

Leave a Comment

Please sign in to add a comment. Not a member? Join today