Developer Guide and Reference

Contents

User-Mandated or SIMD Vectorization

User-mandated or SIMD vectorization supplements automatic vectorization
just like OpenMP* parallelization
supplements automatic parallelization. The following figure illustrates this relationship. User-mandated vectorization is implemented as a single-instruction-multiple-data (SIMD) feature and is referred to as SIMD vectorization.
The SIMD vectorization feature is available 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
macOS*
), or
[Q]x
.
The following figure illustrates how SIMD vectorization is positioned among various approaches that you can take to generate vector code that exploits vector hardware capabilities. The programs written with SIMD vectorization are very similar to those written using auto-vectorization hints. You can use SIMD vectorization to minimize the amount of code changes that you may have to go through in order to obtain vectorized code.
SIMD vectorization uses the
#pragma omp simd
pragma
to effect loop vectorization.
You must add this
pragma
to a loop and recompile to vectorize the loop using the option
-qopenmp-simd
(Linux
and
macOS*
) or
Qopenmp-simd
(Windows*).
Consider an example in C++ where the function
add_floats()
uses too many unknown pointers for the compiler’s automatic runtime independence check optimization to kick in. You can give a data dependence assertion using the auto-vectorization hint via
#pragma ivdep
and let the compiler decide whether the auto-vectorization optimization should be applied to the loop. Or you can now enforce vectorization of this loop by using
#pragma omp simd
.
Example: without #pragma omp simd
[D:/simd] cat example1.c void add_floats(float *a, float *b, float *c, float *d, float *e, int n) {  int i;  for (i=0; i<n; i++){   a[i] = a[i] + b[i] + c[i] + d[i] + e[i];  } }
[D:/simd] icl -nologo -c -Qopt-report2 -Qopt-report-file=stderr -Qopt-report-phase=vec -Qopenmp-simd example1.c example1.c Begin optimization report for: add_floats(float *, float *, float *, float *, float *, int) Report from: Vector optimizations [vec] LOOP BEGIN at C:\Users\test\run\example1.c(3,2) remark #15344: loop was not vectorized: vector dependence prevents vectorization. First dependence is shown below. Use level 5 report for details remark #15346: vector dependence: assumed FLOW dependence between a[i] (4:3) and b[i] (4:3) LOOP END LOOP BEGIN at C:\Users\test\run\example1.c(3,2) <Remainder> LOOP END ===========================================================================
Example: with #pragma omp simd
[D:/simd] cat example1.c void add_floats(float *a, float *b, float *c, float *d, float *e, int n) { int i;
#pragma omp simd
for (i=0; i<n; i++){ a[i] = a[i] + b[i] + c[i] + d[i] + e[i]; } }
[D:/simd] icl -nologo -c -Qopt-report2 -Qopt-report-file=stderr -Qopt-report-phase=vec -Qopenmp-simd example1.c example1.c Begin optimization report for: add_floats(float *, float *, float *, float *, float *, int) Report from: Vector optimizations [vec] LOOP BEGIN at C:\iUsers\test\run\example1.c(4,2) <Peeled loop for vectorization> LOOP END LOOP BEGIN at C:\iUsers\test\run\example1.c(4,2) remark #15301: OpenMP SIMD LOOP WAS VECTORIZED LOOP END LOOP BEGIN at C:\iUsers\test\run\example1.c(4,2) <Alternate Alignment Vectorized Loop> LOOP END LOOP BEGIN at C:\iUsers\test\run\example1.c(4,2) <Remainder loop for vectorization> remark #15301: REMAINDER LOOP WAS VECTORIZED LOOP END LOOP BEGIN at C:\iUsers\test\run\example1.c(4,2) <Remainder loop for vectorization> LOOP END ===========================================================================
The one big difference between using
#pragma omp simd
and auto-vectorization hints is that with
#pragma omp simd
, the compiler generates a warning when it is unable to vectorize the loop. With auto-vectorization hints, actual vectorization is still under the discretion of the compiler, even when you use the
#pragma vector always
hint.
#pragma omp simd
has optional clauses to guide the compiler on how vectorization must proceed. Use these clauses appropriately so that the compiler obtains enough information to generate correct vector code. For more information on the clauses, see the
#pragma omp simd
description.

Additional Semantics

Note the following points when using the
omp simd
pragma
.
  • A variable may belong to zero or one of the following: private, linear, or reduction.
  • Within the vector loop, an expression is evaluated as a vector value if it is private, linear, reduction, or it has a sub-expression that is evaluated to a vector value. Otherwise, it is evaluated as a scalar value (that is, broadcast the same value to all iterations). Scalar value does not necessarily mean loop invariant, although that is the most frequently seen usage pattern of scalar value.
  • A vector value may not be assigned to a scalar L-value. It is an error.
  • A scalar L-value may not be assigned under a vector condition. It is an error.
  • The
    switch
    statement is not supported.
You may find it difficult to describe vector semantics using the SIMD pragma for some auto-vectorizable loops. One example is
MIN
/
MAX
reduction in C since the language does not have
MIN
/
MAX
operators.

Using
vector
Declaration

Consider the following C++ example code with a loop containing the math function,
sinf()
.
All code examples in this section are applicable for C/C++ on Windows* only.
Example: Loop with math function is auto-vectorized
[D:/simd] cat example2.c void vsin(float *restrict a, float *restrict b, int n) { int i; for (i=0; i<n; i++) { a[i] = sinf(b[i]); } }
[D:/simd] icl -nologo -c -Qrestrict -Qopt-report2 -Qopt-report-file=stderr -Qopt-report-phase=vec example2.c example2.c Begin optimization report for: vsin(float *restrict, float *restrict, int) Report from: Vector optimizations [vec] LOOP BEGIN at C:\Users\test\run\example2.c(3,1) <Peeled loop for vectorization> LOOP END LOOP BEGIN at C:\Users\test\run\example2.c(3,1) remark #15300: LOOP WAS VECTORIZED LOOP END LOOP BEGIN at C:\Users\test\run\example2.c(3,1) <Alternate Alignment Vectorized Loop> LOOP END LOOP BEGIN at C:\Users\test\run\example2.c(3,1) <Remainder loop for vectorization> LOOP END ===========================================================================
When you compile the above code, the loop with
sinf()
function is auto-vectorized using the appropriate Short Vector Math Library (SVML) library function provided by the Intel® C++ Compiler. The auto-vectorizer identifies the entry points, matches up the scalar math library function to the SVML function and invokes it.
However, within this loop if you have a call to your function,
foo()
, that has the same prototype as
sinf()
, the auto-vectorizer fails to vectorize the loop because it does not know what
foo()
does unless it is inlined to this call site.
Example: Loop with user-defined function is NOT auto-vectorized
[D:/simd] cat example2.c float foo(float); void vfoo(float *restrict a, float *restrict b, int n){ int i; for (i=0; i<n; i++){ a[i] = foo(b[i]); } }
[D:/simd] icl -nologo -c -Qrestrict -Qopt-report2 -Qopt-report-file=stderr -Qopt-report-phase=vec example2.c example2.c Begin optimization report for: vsin(float *restrict, float *restrict, int) Report from: Vector optimizations [vec] Non-optimizable loops: LOOP BEGIN at C:\Users\test\run\example2.c(3,1) remark #15543: loop was not vectorized: loop with function call not considered an optimization candidate. LOOP END
In such cases, you can use the
__declspec(vector)
(Windows*) or
__attribute__((vector))
(Linux) declaration to vectorize the loop. All you need to do is