weird optimization
-
This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way)
int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; }
But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused:volatile bool duplicate = false;
orfor( volatile int j = 0; j < i; j++ )
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
I don't see a srand() to seed rand(). I think the default is srand( 1 ) if none is given but that might not be the case in your implementation.
-
This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way)
int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; }
But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused:volatile bool duplicate = false;
orfor( volatile int j = 0; j < i; j++ )
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
Congratulations, you've found an optimization bug. Please report this to your compiler vendor. I'm guessing it's erroneously reusing a register, or not saving a value back to a memory location, or something like that.
volatile
requires the compiler to go back to the memory location every time (because it could have changed in a different code path) rather than use a previous value stored in a register.Stability. What an interesting concept. -- Chris Maunder
-
Along with what compiler was used. -- modified at 16:30 Wednesday 8th August, 2007 I've been unable to reproduce it with Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
Posted code was just part of a method, and originally it's not usin built-in random generator. I have made test project, to see what's wrong.
int _tmain(int argc, _TCHAR* argv[])
{
srand( (unsigned int) time(NULL) );int size = 10; int\* old = new int\[ size \]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand() % size; for( int j = 0; j < i; j++ ) { duplicate = old\[ j \] == index; if( duplicate ) break; } } while( duplicate ); old\[ i \] = index; } for( int i = 0; i < size; i++ ) cout << old\[ i \] << "\\n"; for( int i = 0; i < size; i++ ) { for( int j = i + 1; j < size; j++ ) { if( old\[ i \] == old\[ j \] ) cout << "old\[" << i << "\]==old\[" << j << "\]==" << old\[ i \] << "\\n"; } } delete old; return 0;
}
Ouput: 8 3 7 6 0 9 8 3 1 4 old[0]==old[6]==8 old[1]==old[7]==3 Compiler used is MS VC++ 8.0. Building project as Debug produces no duplicates, but as Release produces more the one duplicate.
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
Congratulations, you've found an optimization bug. Please report this to your compiler vendor. I'm guessing it's erroneously reusing a register, or not saving a value back to a memory location, or something like that.
volatile
requires the compiler to go back to the memory location every time (because it could have changed in a different code path) rather than use a previous value stored in a register.Stability. What an interesting concept. -- Chris Maunder
And what if I don't? :D By the way, I'm I the only one who think this message board (last two threads) are messed up?
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
Posted code was just part of a method, and originally it's not usin built-in random generator. I have made test project, to see what's wrong.
int _tmain(int argc, _TCHAR* argv[])
{
srand( (unsigned int) time(NULL) );int size = 10; int\* old = new int\[ size \]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand() % size; for( int j = 0; j < i; j++ ) { duplicate = old\[ j \] == index; if( duplicate ) break; } } while( duplicate ); old\[ i \] = index; } for( int i = 0; i < size; i++ ) cout << old\[ i \] << "\\n"; for( int i = 0; i < size; i++ ) { for( int j = i + 1; j < size; j++ ) { if( old\[ i \] == old\[ j \] ) cout << "old\[" << i << "\]==old\[" << j << "\]==" << old\[ i \] << "\\n"; } } delete old; return 0;
}
Ouput: 8 3 7 6 0 9 8 3 1 4 old[0]==old[6]==8 old[1]==old[7]==3 Compiler used is MS VC++ 8.0. Building project as Debug produces no duplicates, but as Release produces more the one duplicate.
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
Mladen Jankovic wrote:
Compiler used is MS VC++ 8.0
Oh, well, there's your problem. :-D Could you try my version and let me know whether or not it works?
-
Mladen Jankovic wrote:
Compiler used is MS VC++ 8.0
Oh, well, there's your problem. :-D Could you try my version and let me know whether or not it works?
PIEBALDconsult wrote:
Could you try my version and let me know whether or not it works?
Yes it works.
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
And what if I don't? :D By the way, I'm I the only one who think this message board (last two threads) are messed up?
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
Just out of curiosity, which compiler are you using?
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
Just out of curiosity, which compiler are you using?
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighistVC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
VC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
That's freaking scary!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
That's freaking scary!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighistBug or what? ;)
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way)
int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; }
But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused:volatile bool duplicate = false;
orfor( volatile int j = 0; j < i; j++ )
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
Your code snippet seems a bit inefficient. Try something like:
__inline void Swap( int &a, int &b )
{
int t = b;
b = a;
a = t;
}void main( void )
{
int array[10] = {0};
int n = 10;for (int i = 0; i < n; i++) array\[i\] = i; for (i = 0; i < n; i++) { int j = rand() % n; Swap(array\[j\], array\[i\]); }
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way)
int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; }
But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused:volatile bool duplicate = false;
orfor( volatile int j = 0; j < i; j++ )
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
-
VC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)
Well done, good catch! "Codegen problem with nested do-while / for loops (/O2)" I just gave my vote of Very-Important. Note-to-self: try to avoid flying with Microsoft powered airplanes.