Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Vectorizing array min/max search

gordan
Beginner
587 Views
Hi,

I've been playing with ICC for a few days and I've managed to get it to vectorize most of the loops in my code, but there's one I'm not having luck with. Here's the code:

for (x = 0; x < DataC; x++)
{
if (DataV[Max] < DataV) Max = x;
else if (DataV[Min] > DataV) Min = x;
}

Essentially, it searches an array of floats (DataV) for minimum and maximum values, and it saves the index of min and max.

ICC says:
remark: vector dependence: assumed FLOW dependence between Max line 231 and Max line 231.
remark: vector dependence: assumed FLOW dependence between Min line 232 and Min line 232.

Is there a better, vectorizable way to do this? (The data is random readings, and not ordered in any predictable way).

Thanks.

Gordon
0 Kudos
4 Replies
TimP
Honored Contributor III
587 Views
It's probably better to leave out the else, if necessary making separate loops for the min and max searches. The compiler team worked hard to make STL min_element() and max_element() vectorize, as well as code written in a C loop more similar to yours.

Here is the (somewhat ugly) C++ way:

Max = *max_element(&DataV[0], &DataV[DataC]+1) - &DataV[0];
likewise with *min_element()


0 Kudos
gordan
Beginner
587 Views
Thanks for the advice.

It turns out I don't strictly need the index and just the raw min/max values of my floats will do.

So I changed my code to this:

Max = *max_element(DataV, (DataV + DataC));
Min = *min_element(DataV, (DataV + DataC));


However, I still get:
remark: vector dependence: assumed FLOW dependence between __result line 237 and __result line 237.
remark: loop was not vectorized: existence of vector dependence.
remark: vector dependence: assumed FLOW dependence between __result line 238 and __result line 238.
remark: loop was not vectorized: existence of vector dependence.


Lines 237 and 238 are listed above.

Could this be related to the fact that DataC and DataV[] are object properties rather than local variables? I have had a problem before where a loop wouldn't vectorize when using private object properties, but vectorizes when I use static declared variables in the function instead. Could this be similar? Unfortunately, whereas I could move the data to the function in the other case, that isn't possible now because DataV[] is the main data payload of the object and most methods refer to it.


Any ideas?

Thanks
0 Kudos
TimP
Honored Contributor III
587 Views
You probably won't get the right result from min_element() this way. It takes a somewhat warped mind to accept STL as high level syntax.
If you aren't keeping the index, you can do it more efficiently than min_element():

for(x=0; x if(minval>DataV)
minval=DataV;

Versions of icc vary as to which options are required to vectorize this, and whether vectorization actually speeds it up, depending on your CPU model. Core CPUs can do this efficiently with or without vectorization, so non-vector code would be preferable on grounds of code size.
0 Kudos
gordan
Beginner
587 Views
Thanks, that works. :-)
0 Kudos
Reply