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
– Jefferson Quesado
@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
– user5299
I don’t understand your question. Look at coliru Has 2 vector loops
– user5299
@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.
– Beto