User and Reference Guide for the Intel® C++ Compiler 14.0

Using Automatic Vectorization

The automatic vectorizer (also called the auto-vectorizer) is a component of the Intel® C++ Compiler that automatically uses SIMD instructions in the Intel® Streaming SIMD Extensions (Intel® SSE, Intel® SSE2, Intel® SSE3 and Intel® SSE4 Vectorizing Compiler and Media Accelerators), Intel® Supplemental Streaming SIMD Extensions (SSSE3) instruction sets, and Intel® Advanced Vector Extension (Intel® AVX) instruction set.

The vectorizer detects operations in the program that can be done in parallel, and then converts the sequential operations, like one SIMD instruction that processes 2, 4, 8 or up to 16 elements, to parallel, depending on the data type.

Vectorization is the process of converting an algorithm from a scalar implementation, which does an operation one pair of operands at a time, to a vector process where a single instruction can refer to a vector (series of adjacent values) is called vectorization. SIMD instructions operate on multiple data elements in one instruction and make use of the 128-bit SIMD floating-point registers.

Automatic vectorization occurs when the Intel® C++ Compiler generates packed SIMD instructions to unroll a loop. Because the packed instructions operate on more than one data element at a time, the loop executes more efficiently. This process is referred to as auto-vectorization only to emphasize that the compiler identifies and optimizes suitable loops on its own, without requiring any special action by you. However, it is useful to note that in some cases, certain keywords or directives may be applied in the code for auto-vectorization to occur.

Automatic vectorization is supported on IA-32 and Intel® 64 architectures.

Vectorization Speed-up

Where does the vectorization speedup come from? Consider the following sample code fragment, where a, b and c are integer arrays:

for (I=0;i<=MAX;i++)
   c[i]=a[i]+b[i];

If vectorization is not enabled (that is, you compile using O1 or [Q]vec- options), for each iteration, the compiler processes the code such that there is a lot of unused space in the SIMD registers, even though each of the registers could hold three additional integers. If vectorization is enabled (compiled using O2 or higher options), the compiler may use the additional registers to perform four additions in a single instruction. The compiler looks for vectorization opportunities whenever you compile at default optimization (O2) or higher.

Note

Using this option enables vectorization at default optimization levels for both Intel® microprocessors and non-Intel microprocessors. Vectorization may call library routines that can result in additional performance gain on Intel® microprocessors than on non-Intel microprocessors. The vectorization can also be affected by certain options, such as /arch (Windows*), -m (Linux* and OS X*), or [Q]x.

Tip

To allow comparisons between vectorized and not-vectorized code, disable vectorization using the /Qvec- (Windows*) or -no-vec (Linux* or OS X*) option; enable vectorization using the O2 option.

To get information on whether a loop was vectorized or not, enable generation of the vectorization report using the [Q]vec-report[:]1 or [Q]opt-report-phase hpo options. You will get a one line message for every loop that is vectorized, as follows:

> icl /Qvec-report1 Multiply.c 
Multiply.c(92): (col. 5) remark: LOOP WAS VECTORIZED.

The source line number (92 in the above example) refers to either the beginning or the end of the loop.

To get details about the type of loop transformations and optimizations that took place, use the [Q]opt-report-phase hpo option by itself or along with the [Q]opt-report option.

To get information on whether the loop was vectorized using the Visual Studio* IDE, select Project > Properties > C/C++ > Diagnostics > Optimization Diagnostic Level as minimum and Optimization Diagnostic Phase as The High Performance Optimizer phase, /Qopt-report-phase:hpo. To get a diagnostic message for every loop that was not vectorized, with a brief explanation of why the loop was not vectorized, choose /Qvec-report2 [-vec-report2] or medium.

How significant is the performance enhancement? To evaluate performance enhancement yourself, run vec_samples:

  1. Open an Intel® C++ Compiler command line window.
    • On Windows*: Go to Start > All Programs > Intel Parallel Studio > Intel C++ Compiler Professions > C++ build environment for applications running on …

    • On Linux* and OS X*: Source an environment script such as iccvars_intel64.sh in the compiler /bin/intel64 directory, or iccvars_ia32.sh in the /bin/ia32 directory, as appropriate.

  2. Navigate to the <install-dir>\Samples\<locale>\C++\ directory. Open the sample project vec_samples.zip. The small application multiplies a vector by a matrix using the following loop:
  3. for (j = 0;j < size2; j++) { b[i] += a[i][j] * x[j]; }
    Build and run the application, first without enabling auto-vectorization. Note the time taken for the application to run.
    // (Linux* and OS X*)
    icc -O2 -no-vec  Multiply.c -o NoVectMult 
    ./NoVectMult
    // (Windows*)
    icl /O2 /Qvec- Multiply.c /FeNoVectMult 
    NoVectMult
  4. Now build and run the application, this time enabling auto-vectorization. Note the time taken for the application to run.
    // (Linux* and OS X*)
    icc -O2 -vec-report1 Multiply.c -o VectMult 
    ./VectMult
    // (Windows*)
    icl /O2 /Qvec-report1 Multiply.c /FeVectMult 
    VectMult

When you compare the timing of the two runs, you may see that the vectorized version runs faster. The time for the non-vectorized version is only slightly faster than would be obtained by compiling with the O1 option.

Obstacles to Vectorization

The following do not always prevent vectorization, but frequently either prevent it or cause the compiler to decide that vectorization would not be worthwhile.

Helping the Intel® C++ Compiler to Vectorize

Sometimes the Intel® C++ Compiler has insufficient information to decide to vectorize a loop. There are several ways to provide additional information to the compiler:

See Also


Submit feedback on this help topic