Auto-vectorization in visual studio 2012 on vector

2019-09-04 04:43发布

问题:

I have std::vector of Eigen::vector3d types and when i am compiling this code using Microsoft Visual Studio 2012 having the /Qvec-report:2 flag on for reporting vectorization details. It's showing Loop not vectorized due to reason 1304 (Loop contains assignments that are of different types) as specified on the msdn page -https://msdn.microsoft.com/en-us/library/jj658585.aspx

My code is as below:

#include <iostream>
#include <vector>
#include <time.h>
#include<Eigen/StdVector>
int main(char *argv[], int argc)
{
    int tempSize=100;
/** I am aligning these vectors as specfied on http://eigen.tuxfamily.org/dox/group__TopicStlContainers.html */
    std::vector<Eigen::Vector3d,Eigen::aligned_allocator<Eigen::Vector3d>> eiVec(tempSize);
    std::vector<Eigen::Vector3d,Eigen::aligned_allocator<Eigen::Vector3d>> eiVec1(tempSize);
    std::vector<Eigen::Vector3d,Eigen::aligned_allocator<Eigen::Vector3d>> eiVec2(tempSize);

    for(int i=0;i<100;i++)
    {
        eiVec1[i] = Eigen::Vector3d::Zero();
        eiVec2[i] = Eigen::Vector3d::Zero();
    }

    Eigen::Vector3d *eV = &eiVec.front();
    const Eigen::Vector3d *eV1 = &eiVec1.front();
    const Eigen::Vector3d *eV2 = &eiVec2.front();

/** Below loop is not vectorized due to code 1304  */
    for(int i=0;i<100;i++)
    {
        eV[i] = eV1[i] - eV2[i];
    }
    return 0;
}

So i am not able to understand that how to tell compiler, this is the fixed size data and fixed size assignment will happen here.

回答1:

From the Eigen documentation

In Eigen, arithmetic operators such as operator+ don't perform any computation by themselves, they just return an "expression object" describing the computation to be performed. The actual computation happens later, when the whole expression is evaluated, typically in operator=

So eV1[i] - eV2[i] is not returning an Eigen::Vector3d - so reason 1304 applies. You might be able to cast it - but I wouldn't bet on it.



回答2:

Consider changeing eiVec1 and eiVec12 to a matrix<double, 3, 100> and doing the subtraction as eiVec = eiVec1 - eiVec2