Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Possible Optimization bug

Jae_C_
Beginner
433 Views

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 << 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.

 

 

 

 

 

0 Kudos
5 Replies
KitturGanesh
Employee
433 Views

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

0 Kudos
Jerzy_P_
Beginner
433 Views

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

0 Kudos
KitturGanesh
Employee
433 Views

@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 

0 Kudos
levicki
Valued Contributor I
433 Views

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?

0 Kudos
KitturGanesh
Employee
433 Views

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

0 Kudos
Reply