I would like to know how to vectorize code in C ++? because the material I found on the internet is a bit fake about it.
I understand as a vectorization the use not only of vectors, but of doing in a single step a whole sequence of steps, that is, doing in one step d= (c+e)/2;
instead of repeating those steps for each position of the array d[i][j] = (c[i][j]+e[i][j])/2;
For example how to vectorize 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 vectorization flag to see how many loops are being vectored with the help of -O2 -ftree-vectorize -fopt-info-vec-optimized
it responds to me "loop vectorized" ie only one loop was vectorized and if I use -all instead of -optimized
it returns me that many parts of the program have not been vectorized.