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

Bug in loop optimization?

sergey_82
Beginner
537 Views
Hello!

I use Intel C++ compiler 64 bit Professional Build 20090827 Package ID: l_cproc_p_11.1.056 running on Linux x86_64.

I wrote the following program:

#include
#include

const int N = 10;

class LUTest {
public:
double a;
//
LUTest()
{
srand(25);
for(int i=0; i for(int j=0; j a = rand() % N;
}
void LU()
{
for(int i=0; i for(int j=i+1; j a /= a;
a[i+1] -= a[i+1]*a;
// std::cout << a[i+1];
}
for(int j=i+2; j for(int k=i+1; k a -= a*a;
}
}
void save()
{
std::ofstream f;
//
f.open("test.out");
for(int i=0; i for(int j=0; j f << a << std::endl;
f.close();
}
};

int main()
{
LUTest test;
//
test.LU();
test.save();
//
return 0;
}

Now, if I compile it with -O0 or -O1 options, then it works fine and produce correct results. However, if I try to compile it with -O2 or -O3 options, then it produce another (incorrect) results.

Further, if you uncomment the line with "std::cout", then it'll produce correct results even with -O2 or -O3 options. Can it be the bug in loops optimization?

Thank you.

Sergey
0 Kudos
1 Solution
JenniferJ
Moderator
537 Views
Sorry to get to you so late.

Yes, it's a bug in the vectorizer. I'll file a bug report for it.

Adding the "cout ..." turns off the optimization. That's why it works. Or you can add "#pragma novector" in front of the loop and it should work around the problem as well.

Thanks,
Jennifer

View solution in original post

0 Kudos
8 Replies
jimdempseyatthecove
Honored Contributor III
537 Views

[cpp]>>a[i+1] -= a[i+1]*a;
[/cpp]

On last iteration of i, you are indexing outside the bounds of the array (that is one problem)

Jim
0 Kudos
sergey_82
Beginner
537 Views

[cpp]>>a[i+1] -= a[i+1]*a;
[/cpp]

On last iteration of i, you are indexing outside the bounds of the array (that is one problem)

Jim

Hello Jim,

Thank you for reply.

I don't think that I'm indexing outside the bounds of the array. The last iteration of i is when i = N-1. In that case, j=i+1 = N-1+1 = N. So j = N, the condition j -= a[i+1]*a" is inside of that loop, so it will not be executed.

Sergey
0 Kudos
JenniferJ
Moderator
538 Views
Sorry to get to you so late.

Yes, it's a bug in the vectorizer. I'll file a bug report for it.

Adding the "cout ..." turns off the optimization. That's why it works. Or you can add "#pragma novector" in front of the loop and it should work around the problem as well.

Thanks,
Jennifer
0 Kudos
jimdempseyatthecove
Honored Contributor III
537 Views


a is dimensioned (indexes (0:N-1, 0:N-1)

void LU()
{
for(int i=0; ifor(int j=i+1; ja /= a;
a[i+1] -= a[i+1]*a;
// std::cout << a[i+1];
}

On last iteration of i, the for j loop will write an entire row of a[row][col] beyond array bounds when executing

a[i+1] -= a[i+1]*a;


IOW you will be depositing into

a -= a*a[N-1];

Jim Dempsey

P.S. A close brace } seems to be missing from your posted code.

0 Kudos
JenniferJ
Moderator
537 Views
a is dimensioned (indexes (0:N-1, 0:N-1)
On last iteration of i, the for j loop will write an entire row of a[row][col] beyond array bounds when executing

a[i+1] -= a[i+1]*a;

IOW you will be depositing into

a -= a*a[N-1];


If expand the last two iterations of "i", they're like following.

i = N-1
j = i+1 = N-1 +1 = N --- for (... j < N... ): condition false, so "N-1" is not the last iteration for inner loop

i = N-2
j = i+1 = N-2 +1 = N-1 --- for (... j < N ...): condition true, this is the last iteration. But it writes to a[N-1][N-1].
a[i+1] = a[N-1][N-1]

Right?

Jennifer

0 Kudos
jimdempseyatthecove
Honored Contributor III
537 Views

Your right (not enough coffee this morning).

Jim
0 Kudos
jimdempseyatthecove
Honored Contributor III
537 Views

Sergey,

First verify that the input data is consistent (table of srand's). Try

#pragma intel optimization_level 1
LUTest()
{
srand(25);
for(int i=0; i for(int j=0; j a = rand() % N;
}
// returns to option switch optimization here

Jim Dempsey
0 Kudos
sergey_82
Beginner
537 Views
Dear Jennifer and Jim,

Many thanks for your help.

I think my question is answered.

All the best,

Sergey


0 Kudos
Reply