c++visual-studiovectorizationauto-vectorization

How to enable or use auto vectorization along with auto parallelization in Microsoft Visual Studio 2017 or 2019?


I am trying to test out auto-vectorization on the for loop below. I am unable to get the auto vectorization to work. The code is shown below.

float dotproduct(float a[], float b[], int size) {
    int x = 0.0;
    for (int i = 0; i < size; i++) {
        x = x + a[i] * b[i];
    }
    return x;
}

int main() {
    const int N = 8;

    float a[N] = {2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
    float b[N] = {9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0};
    
    dotproduct(a, b, N);
    cout << dotproduct(a, b, N);

    return 0;
}

I added the line /Qvec-report:2 /Qpar-report:2 to the additional options section in my project property page but I did not get a report stating whether vectorization failed or not.

Qvec and Qpar report added to the project property page

Enabled parallel code generation


Solution

  • There is no vectorization report because you compiled in Debug mode (/Od), in which case autovectorization is not applied because optimizations are disabled. So, switch to a Release build.

    /O1 and /OS would cause a report with failure reason 1404.

    /Ox and /O2 will attempt to autovectorize, but with /fp:precise (which you have set now) this loop with fail to vectorize due to reason 1105. Specifying /fp:fast would make it work.