How to vector code in C++?

Asked

Viewed 395 times

4

Would you like to know how to vector code in C++ ? because the material I found on the internet is a bit excasso.

I understand as vectorization the use, not only of vectors, but of doing in a single step a whole sequence of steps, that is, to do at once d= (c+e)/2; instead of repeating these steps for each position of the matrix d[i][j] = (c[i][j]+e[i][j])/2;

For example how to vector the following program ?

#include <iostream>

using namespace std;
int main(){

    int d[4][4],c[4][4],e[4][4];

    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            c[i][j] =i+j;
            e[i][j] = 4*i;
        }
    }
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            d[i][j] = (c[i][j]+e[i][j])/2;
            if(d[i][j]<3){
                d[i][j]=3;
            } 
        }  
    }
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
           cout << d[i][j] << " ";
        }
        cout << endl;
    }


    return 0;
}

When I use the vector flag to see how many loops are being vectored with the help of -O2 -ftree-vectorize -fopt-info-vec-optimized it answers me " loop vetorized " ie only a loop was vectorized and if I use a -all instead of the -optimized it returns to me that many parts of the program have not been vectored.

  • What do you call vectorizing code? This concept of vectorization for me only applies to analytical geometry and related areas

  • 2

    @Jeffersonquesado Vectorization is to use SIMD instructions to perform parallel operations. For example, when adding two vectors, normally vc sums each component of the vector separately. With SIMD you can sum all components simultaneously https://en.wikipedia.org/wiki/Automatic_vectorization

  • I don’t understand your question. Look at coliru Has 2 vector loops

  • @Amadeus the question is primarily aimed at how to vector loops that have conditional structures. I don’t know which version of g++ you are using but here really only vector 1 loop gives you initialization, not being vectorized what has the conditional if and what system call to print the values.

1 answer

2


The problem is that the parole if contained within the second loop does not allow it to be optimized by the compiler:

for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
        d[i][j] = (c[i][j]+e[i][j])/2;
        if(d[i][j]<3){
            d[i][j]=3;
        }
    }
}

One solution to this problem is the conditional substitution if on parole ternário, for example:

for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
        d[i][j] = (c[i][j]+e[i][j])/2;
        d[i][j] = ( d[i][j] < 3 ) ? 3 : d[i][j];
    }
}

Compilation Test GCC:

$ g++ -v -O2 -ftree-vectorize -fopt-info-vec-optimized vect.cpp -o vect

Exit:

[...]

Analyzing loop at vect.cpp:21

Analyzing loop at vect.cpp:14

vect.cpp:14: note: vect_recog_divmod_pattern: detected: 
vect.cpp:14: note: pattern recognized: patt_3 = patt_4 >> 1;

Analyzing loop at vect.cpp:15

vect.cpp:15: note: vect_recog_divmod_pattern: detected: 
vect.cpp:15: note: pattern recognized: patt_77 = patt_1 >> 1;


Vectorizing loop at vect.cpp:15

vect.cpp:15: note: LOOP VECTORIZED.
Analyzing loop at vect.cpp:8

Analyzing loop at vect.cpp:9


Vectorizing loop at vect.cpp:9

vect.cpp:9: note: LOOP VECTORIZED.
vect.cpp:4: note: vectorized 2 loops in function.

[...]

References:

https://locklessinc.com/articles/vectorize/

https://gcc.gnu.org/projects/tree-ssa/vectorization.html

EDIT:

The answer applies only to the version 4.8 of GCC.

The version 7.0, already able to vector loops without the need to replace the conditional ones if by ternary operators through the optimization option -fsplit-loops.

Reference: https://clearlinux.org/blogs/gcc-7-importance-cutting-edge-compiler

  • It does not answer the original question. If you already had 2 vector loops, nothing has changed with your proposal Original in coliru

  • @Amadeus: The question mentions that only one loop has been optimized. My answer explains the reason and presents a solution for the optimization of the two loops. Note well.

  • @Amadeus: His example in coliru uses the GCC 7.2.0, and of course, it already 'knows' to vector loops with conditional without using ternary operators. In older versions, such as 4.8, this does not seem to happen.

  • @Perfect lacobus, that’s right. Now vector the conditional loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.