Software Archive
Read-only legacy content
17061 Discussions

Simple Array Notation failing at run-time

Jorge_Martinis
Beginner
568 Views
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!
0 Kudos
7 Replies
Barry_T_Intel
Employee
568 Views
Which compiler version and OS are you using?

- Barry
0 Kudos
Jorge_Martinis
Beginner
568 Views
Intel Composer XE 12.1 Update 6 in Windows 7.
0 Kudos
SergeyKostrov
Valued Contributor II
568 Views
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:

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.
0 Kudos
SergeyKostrov
Valued Contributor II
568 Views
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

0 Kudos
Jorge_Martinis
Beginner
568 Views
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!
0 Kudos
SergeyKostrov
Valued Contributor II
568 Views
...
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!
0 Kudos
Barry_T_Intel
Employee
568 Views

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

0 Kudos
Reply