c++ - Why is the loop not being vectorized? -
i know vectorization can take place if objects being accessed contiguous in memory. have created struct has pointer , create vector of struct , ensure pointers inside vector of objects pointing contiguous data blocks set them point elements in vector of double same size.
#include <iostream> #include <vector> struct vec { vec() {} double* a; }; int main(int argc, char* argv[]) { std::vector<double> vec_double(10000000, 1.0); std::vector<vec> vec_vec(10000000); (unsigned = 0; < 10000000; ++i) vec_vec[i].a = &(vec_double[i]); // why loop not vectorized (unsigned = 0; < 10000000; ++i) vec_double[i] += *(vec_vec[i].a); double sum = 0.0; (unsigned = 0; < 10000000; ++i) sum += vec_double[i]; std::cout << sum << std::endl; return 0; }
however, o3 optimization loop @ line number 16 not getting vectorized. can please explain why happening?
just guessing here, when looking @ specific loop, compiler not know vec_vec[i].a
points memory location next vec_vec[i+1].a
. therefore, cannot calculation without dereferencing each .a
member separately.
it know that, when looking @ loop above. if that, @ loop below, calculate final result , print it.
Comments
Post a Comment