Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
26 Views

using icpc, what is the best way to optimize the following code with AVX option?

I have a for loop to identify min and max of a float array. Here is my implementation:

for (int i = 0; i < counter; i++) {

     imin = intervals < imin ? intervals : imin;

     imax = intervals > imax ? intervals : imax;

   }

What is the best way to optimize the code here? I am using -xAVX option and I am running the program in single thread mode. How does this compare to using std::minmax_element? 

Should I write some manual AVX code?

Thanks,

0 Kudos
4 Replies
Highlighted
Black Belt
26 Views

This should be fine.  std::max() and std::min() should work the same (with icpc, but not g++).  They should generate vmaxps|vminps instructions, which are the best you could do with intrinsics.

min_element|max_element are syntactically right for the case where you actually want a pointer to the position of the element, but will not vectorize.  For whatever reason, icc can do better when those STL operators are avoided.

Current versions of icc support omp simd reduction(max: ) but that is not needed for your example (unless your goal is to throw a visible warning at compile time if it is not optimized).  With icc, also (but not other compilers), the #pragma simd reduction may be used to over-ride conservative options such as -fp-model precise which would turn off simd optimization in the absence of the pragma, or to invoke pragma directed vectorization by compiling with -no-vec so that only loops with pragma omp simd are vectorized.

I would suggest unroll by 2.  icc default should be OK.

0 Kudos
Highlighted
Beginner
26 Views

Thanks,

A follow-up question: should I use 

   imin = intervals < imin ? intervals : imin;

   imax = intervals > imax ? intervals : imax;

or if else if statement?

     if(intervals < imin )
       imin = intervals;
     else if(intervals > imax )
       imax = intervals;

It seems the if else statement can cut the comparison in half as if the first comparison satisfies, the second is not necessary.

But I generated assembly code and I don't see obvious difference. Which one should I use?

Thanks

0 Kudos
Highlighted
Black Belt
26 Views

For simple cases such as this, the gain from vectorization outweighs the necessity of evaluating all the branches speculatively.

As you hope to have maxps and minps instructions generated, there aren't any code branches on which the other operation could be skipped.  In non-vector code with branches, the case where the first comparison results in a value update and the possibility of skipping the second normally is so infrequent that there's no need to optimize it.

0 Kudos
Highlighted
Employee
26 Views

Agree with Tim. Also, with regard to which one to use (conditional or if-else) I guess it's just a case of readability and the IL generated should still be similar with no significant performance difference either way and it's a choice on which is more readable for you.

_Kittur

0 Kudos