-1

I am having a code which is using Eigen::vectors, I want to confirm that Eigen has optimized this code for SSE or not.

I am using Visual Studio 2012 Express, in which i can set the command line option "/Qvec-report:2" which gives the optimization details of C++ code. Is there any option in visual studio or Eigen which can tell me that code has been optimized or not?

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 by visual studio due to code 1304: 
    Because here comes the operations at level of Eigen, I want to 
    know here whether Eigen has optimized this operation or not? */
    for(int i=0;i<100;i++)
    {
        eV[i] = eV1[i] - eV2[i];
    }
    return 0;
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • There is not much to SSE-optimize for `Vector3d` (it could only be split into one packet and one scalar operation, at the cost of an unaligned load/store). – chtz Oct 17 '16 at 16:37
  • Yes @chtz , It is not optimizing vector3d but if i use vector4d it optimizes this code. As i am seeing through the assembly as specified below by PeterCordes. – Shubham Saini Oct 18 '16 at 09:45

1 Answers1

2

Look at the asm output.

If you see SUBPD (packed double) inside the inner loop, it vectorized. If you only see SUBSD (scalar double) and no SUBPD anywhere, it didn't.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I think i can look into asm output in debug mode by creating a breakpoint in the code. I don't know a way to look into ASM while i have compiled my build in release mode. AFAIK in visual studio, optimization takes place in release build only. Please correct me if i am wrong – Shubham Saini Oct 18 '16 at 05:59
  • @ShubhamSaini: You can use the debugger on an optimized executable, or more simply you can just disassemble the binary and look for that function. I only ever used VS for one project several years ago, so IDK *how* you do these things most easily on Windows, but it's not hard with the right tools (e.g. `objdump -Mintel -drwC my.exe | less`). You're correct that looking at a debug build would be useless. – Peter Cordes Oct 18 '16 at 07:13
  • Thanks for your suggestion. It's good for very small programs but it's not very much useful if your project is too big i.e. it's containing more than 20 classes and files – Shubham Saini Oct 18 '16 at 08:39
  • @ShubhamSaini: I have no problem finding the Class::Method I'm looking for in `objdump -drwC` output, when searching by name. `-C` demangles names back into `` from `<_ZN2AI11KeepStationER4ShipR7CommandRKS0_>`. Obviously you have to build with debug symbols and optimizations both enabled. – Peter Cordes Oct 18 '16 at 08:49
  • @ShubhamSaini: If you don't use Link-Time Optimization, you can disassemble a single `.obj` file. Or you can just tell the compiler to show you the optimized asm instead of disassembling a binary. http://stackoverflow.com/questions/1020498/how-to-view-the-assembly-behind-the-code-using-visual-c – Peter Cordes Oct 18 '16 at 08:52
  • Thanks @PeterCordes, Now i am able to see at the ASM by enabling the compiler options through visual studio. Which is creating a separate ASM file for each cpp file. – Shubham Saini Oct 18 '16 at 09:49