Simple Array Notation failing at run-time

Simple Array Notation failing at run-time

Imagen de Jorge Martinis

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!

publicaciones de 8 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.
Imagen de Barry Tannenbaum (Intel)

Which compiler version and OS are you using?

- Barry

Imagen de Jorge Martinis

Intel Composer XE 12.1 Update 6 in Windows 7.

Imagen de Sergey Kostrov

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.

Imagen de Sergey Kostrov

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[i] = ( tPx[i] < ( T )0 ) ? ( ( T )0 ) : ( tPx[i] );
}

...

Best regards,
Sergey

Imagen de Jorge Martinis

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!

Imagen de Sergey Kostrov

...
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!

Imagen de Barry Tannenbaum (Intel)

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

Inicie sesión para dejar un comentario.