Community
cancel
Showing results for 
Search instead for 
Did you mean: 
sergey_82
Beginner
61 Views

Bug in loop optimization?

Jump to solution
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
61 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

8 Replies
jimdempseyatthecove
Black Belt
61 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
sergey_82
Beginner
61 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
JenniferJ
Moderator
62 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

jimdempseyatthecove
Black Belt
61 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.

JenniferJ
Moderator
61 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

jimdempseyatthecove
Black Belt
61 Views

Your right (not enough coffee this morning).

Jim
jimdempseyatthecove
Black Belt
61 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
sergey_82
Beginner
61 Views
Dear Jennifer and Jim,

Many thanks for your help.

I think my question is answered.

All the best,

Sergey


Reply