- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there!
I wonder how come the IC's vectorizer fails to vectorize the Array Notation in the following function:
-
template inline void EnforcePositive(std::vector& x)
{
size_t size = x.size();
T* px = &x.front();
px[0:size] = (px[0:size] < 0) ? 0 : px[0:size];
}
-
The function compiles and runs on debug. It also compiles on release under /O2 (though never being vectorized) and fails at run-time.
The fact that the IC cannot vectorize this AN is alright. However, I do not think it should fail at run-time.
Thanks!
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Which compiler version and OS are you using?
- Barry
- Barry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Intel Composer XE 12.1 Update 6 in Windows 7.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Jorge,
There is definetely something wrong. Here are a couple comments:
- I don't think that anoption '/O2' should really affect "stability" of the code. What I understood you wanted tooptimize for Maximum Speed, right?
- The name of your template-based function is 'EnforcePositive', but it assigns '0' toelements ifthey less than '0'. In general, Zero is Zero! It is not a positive or negative.I would change it to:
or
px[0:size] = (px[0:size] < 0) ?( abs( px[0:size] ) ): ( px[0:size] );
In that case, if an element is less than'0' than change it to positive;
- Then, anSTL vector is actually an array of numbers and in C/C++ arrays are always Zero-indexed. Your example looks for me a little bit strange:
...
px[0:size] = ...
^^^^^
...
It means, from an index 0 to index size. But, an assignment topx[size] element is anout-of-range assignment and it will generate an Access Violation Exception ( AVE )! Why it doesn't generate AVE in Debug configuration I don't know, but in Release configuration you have it.
Look, if you have anarray 'A'of 8 elements ( size = 8 )all valid index values are as follows:
0, 1, 2, 3, 4, 5 ,6, 7
andaccess to an element A[8] or A[size]will generate an Access Violation.
It looks like you could try to change to:
px[0:size-1] = (px[0:size-1] < 0) ?( -px[0:size-1] ) : ( px[0:size-1] );
Best regards,
Sergey
PS: Just for interest, Itried to compileyour example with MS Visual Studio 2005 PE and it fails on:
...
px[0:size] = (px[0:size] < 0) ?( -px[0:size] ) : ( px[0:size] );
...
with anError C2143: syntax error : missing ']' before ':'
Unfortunately, it doesn't understand px[0:size]declaration.
There is definetely something wrong. Here are a couple comments:
- I don't think that anoption '/O2' should really affect "stability" of the code. What I understood you wanted tooptimize for Maximum Speed, right?
- The name of your template-based function is 'EnforcePositive', but it assigns '0' toelements ifthey less than '0'. In general, Zero is Zero! It is not a positive or negative.I would change it to:
px[0:size] = (px[0:size] < 0) ?( -px[0:size] ) : ( px[0:size] );
or
px[0:size] = (px[0:size] < 0) ?( abs( px[0:size] ) ): ( px[0:size] );
In that case, if an element is less than'0' than change it to positive;
- Then, anSTL vector is actually an array of numbers and in C/C++ arrays are always Zero-indexed. Your example looks for me a little bit strange:
...
px[0:size] = ...
^^^^^
...
It means, from an index 0 to index size. But, an assignment topx[size] element is anout-of-range assignment and it will generate an Access Violation Exception ( AVE )! Why it doesn't generate AVE in Debug configuration I don't know, but in Release configuration you have it.
Look, if you have anarray 'A'of 8 elements ( size = 8 )all valid index values are as follows:
0, 1, 2, 3, 4, 5 ,6, 7
andaccess to an element A[8] or A[size]will generate an Access Violation.
It looks like you could try to change to:
px[0:size-1] = (px[0:size-1] < 0) ?( -px[0:size-1] ) : ( px[0:size-1] );
Best regards,
Sergey
PS: Just for interest, Itried to compileyour example with MS Visual Studio 2005 PE and it fails on:
...
px[0:size] = (px[0:size] < 0) ?( -px[0:size] ) : ( px[0:size] );
...
with anError C2143: syntax error : missing ']' before ':'
Unfortunately, it doesn't understand px[0:size]declaration.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Also, you could try a more generic form of your template-function:
...
...
template< typename T > inline void EnforcePositive( std::vector< T > &x )
{
size_t iSize = x.size();
T *tPx = &x.front();
for( size_t i = 0; i < iSize; i++ )
tPx = ( tPx < ( T )0 ) ? ( ( T )0 ) : ( tPx );
}
...
Best regards,
Sergey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sergey,
I appreciate your feedback.
It might be a good idea for you to check on the Extensions for Array Notations section at the Intel Cilk documentation. There you will find the section operator defined as follows: [ : : ]
The expression px[0:size] is correct. It fails on you because it requires the Intel Compiler. The /O2 option is necessary to trigger the vectorizer.
As for the name of the function, name it anyway you want.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
...
a good idea for you to check on the Extensions for Array Notations section at the Intel Cilk documentation
...
Thanks, Jorge! I expected to hear that this issome kind ofExtension!
a good idea for you to check on the Extensions for Array Notations section at the Intel Cilk documentation
...
Thanks, Jorge! I expected to hear that this issome kind ofExtension!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I've been unable to reproduce the problem with 12.1 Update 6 on Windows 7. Here's a zipfile for the project I created.
I built the project for both x86 and Intel64, debug and release, and no run failed. If you can modify the project and demonstrate the bug, I'll be happy to investigate further.
- Barry
![](/skins/images/7FDC486457F5FB4D87FED5D8C92AC987/responsive_peak/images/icon_anonymous_message.png)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page