CompilerAutovectorizationGuide
CompilerAutovectorizationGuide
CompilerAutovectorizationGuide
Vectorization with
Intel® C++ Compilers
Contents
1. Introduction .............................................................................................................................................. 4
5.3 Prefer Structure of Arrays (SoA) over Array of Structures (AoS) .......................... 15
7. Let the compiler help you to help the compiler to vectorize ............................................. 25
2
7.2 User-mandated Vectorization (pragma simd) ............................................................... 27
8. Conclusion............................................................................................................................................... 35
9. Appendix.................................................................................................................................................. 35
3
1. Introduction
In this context, it is the unrolling of a loop combined with the generation of packed SIMD
instructions by the compiler. Because the packed instructions operate on more than one
data element at a time, the loop can execute more efficiently. It is sometimes referred to
as auto-vectorization, to emphasize that the compiler identifies and optimizes suitable
loops on its own, without requiring any special action by the programmer.
If you are familiar with this concept, you may choose to skip the additional technical
background below and move on to the next section, “When does the compiler try to
vectorize?”
Today’s CPUs are highly parallel processors with different levels of parallelism. We find
parallelism everywhere from the parallel execution units in a CPU core, up to the SIMD
(Single Instruction, Multiple Data) instruction set and the parallel execution of multiple
threads. The use of the Intel SSE instruction set, which is an extension to the x86
architecture, is called vectorization. In Computer science 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.
4
Intel originally added eight new 128-bit registers known as XMM0 through XMM7. For
the 64-bit extensions additional eight registers XMM8-XMM15 were added.
Programmers can exploit vectorization to speedup certain parts of their code. Writing
vectorized code can take additional time but is mostly worth the effort, because the
performance increase may be substantial. One major research topic in computer science
is the search for methods of automatic vectorization: seeking methods that would allow
a compiler to convert scalar algorithms into vectorized algorithms without human
assistance. The Intel® Compiler can automatically generate Intel SSE instructions.
This Guide will focus on using the Intel® Compiler to automatically generate SIMD code,
a feature which will be referred as auto-vectorization henceforth. We also give some
guidelines that will help the compiler to auto-vectorize. However, in some cases, certain
keywords or directives have to be applied in the code for auto-vectorization to occur.
So where does the vectorization speedup come from? Let’s look at a 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 (e.g. because we used /Od, /O1 or /Qvec-), the compiler
will do something like this:
5
So we have a lot of unused space in our SIMD registers which could hold three additional
integers. If vectorization is enabled, the compiler may use the additional registers to
perform 4 additions in a single instruction:
6
The source line number (92 in the above example) refers to the beginning of the loop.
From the 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”.
NoVectMult
VectMult
./NoVectMult
./VectMult
Compare the timing of the two runs. You may see that the vectorized version ran faster.
3. Straight-line code Because SIMD instructions perform the same operation on data
elements from multiple iterations of the original loop, it is not possible for different
iterations to have different control flow, i.e., they must not branch. Thus switch
statements are not allowed. However, “if” statements are allowed if they can be
implemented as masked assignments, which is usually the case. The calculation is
performed for all data elements, but the result is stored only for those elements for
which the mask evaluates to true. Thus, the following example may be vectorized:
#include <math.h>
void quad(int length, float *a, float *b,
float *c, float *restrict x1, float *restrict x2)
{
for (int i=0; i<length; i++) {
float s = b[i]*b[i] - 4*a[i]*c[i];
if ( s >= 0 ) {
s = sqrt(s) ;
x2[i] = (-b[i]+s)/(2.*a[i]);
x1[i] = (-b[i]-s)/(2.*a[i]);
}
else {
x2[i] = 0.;
x1[i] = 0.;
}
}
}
> icc -c -restrict -vec-report2 quad.cpp
quad5.cpp(5) (col. 3): remark: LOOP WAS VECTORIZED.
8
4. The innermost loop of a nest The only exception is if an original outer loop is
transformed into an inner loop as a result of some other prior optimization phase,
such as unrolling, loop collapsing or interchange.
5. No function calls The two major exceptions are for intrinsic math functions and
for functions that may be inlined. Even a print statement is sufficient to render a
loop unvectorizable. The vectorization report message is typically “nonstandard
loop is not a vectorization candidate”.
Intrinsic math functions such as sin(), log(), fmax(), etc., are allowed, since the compiler
runtime library contains vectorized versions of these functions. Table 1 lists these
functions. Most exist in both float and double versions.
Thus the loop in the following example may be vectorized, since sqrtf() is vectorizable
and func() gets inlined. Inlining is enabled at default optimization for functions in the
same source file. (An inlining report may be obtained by setting /Qopt-report-phase
ipo_inl (-opt-report-phase ipo_inl). )
float trap_int
(float y, float x0, float xn, int nx, float xp, float yp) {
float x, h, sumx;
int i;
h = (xn-x0) / nx;
sumx = 0.5*( func(x0,y,xp,yp) + func(xn,y,xp,yp) );
9
for (i=1;i<nx;i++) {
x = x0 + i*h;
sumx = sumx + func(x,y,xp,yp);
}
sumx = sumx * h;
return sumx;
}
> icc -c -vec-report2 trap_integ.c
trap_int.c(16) (col. 3): remark: LOOP WAS VECTORIZED.
4. 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.
Four consecutive ints or floats, or two consecutive doubles, may be loaded directly from
memory in a single SSE instruction. But if the four ints are not adjacent, they must be
loaded separately using multiple instructions, which is considerably less efficient. The
most common examples of non-contiguous memory access are loops with non-unit
stride or with indirect addressing, as in the examples below. The compiler rarely
vectorizes such loops, unless the amount of computational work is large compared to
the overhead from non-contiguous memory access.
10
4.2 Data Dependencies
Vectorization entails changes in the order of operations within a loop, since each SIMD
instruction operates on several data elements at once. Vectorization is only possible if
this change of order does not change the results of the calculation.
The simplest case is when data elements that are written (stored to) do not appear
in any other iteration of the individual loop. In this case, all the iterations of the
original loop are independent of each other, and can be executed in any order,
without changing the result. The loop may be safely executed using any parallel
method, including vectorization. All the examples considered so far fall into this
category.
A[0]=0;
The above loop cannot be vectorized safely because if the first two iterations are
executed simultaneously by a SIMD instruction, the value of A[1] may be used by
the second iteration before it has been calculated by the first iteration which could
lead to incorrect results.
This is not safe for general parallel execution, since the iteration with the write
may execute before the iteration with the read. However, for vectorization, no
iteration with a higher value of j can complete before an iteration with a lower
value of j, and so vectorization is safe (i.e., gives the same result as non-
vectorized code) in this case. The following example, however, may not be safe,
since vectorization might cause some elements of A to be overwritten by the first
SIMD instruction before being used for the second SIMD instruction.
11
A[j-1]=A[j]+1;
B[j]=A[j]*2;
However, there is a very important exception that apparently contains all of the above
types of dependency:
sum=0;
Although sum is both read and written in every iteration, the compiler recognizes such
reduction idioms, and is able to vectorize them safely. The loop in example 1 was
another example of a reduction, with a loop-invariant array element in place of a scalar.
These types of dependencies between loop iterations are sometimes known as loop-
carried dependencies.
The above examples are of proven dependencies. However, the compiler cannot safely
vectorize a loop if there is even a potential dependency. In the following example,
the compiler must ask itself whether, for some iteration i, c[i] might refer to the same
memory location as a[i] or b[i] for a different iteration. (Such memory locations are
sometimes said to be “aliased”). For example, if a[i] pointed to the same memory
location as c[i-1], there would be a read-after-write dependency as in the earlier
example. If the compiler cannot exclude this possibility, it will not vectorize the loop (at
least, not without help, as discussed under “Helping the compiler to vectorize” under
#pragma ivdep and the restrict keyword).
12
5. Guidelines for writing vectorizable code
Prefer countable single entry and single exit “for” loops. Avoid complex loop
termination conditions – the loop lower bound and upper bound must be invariant
within the loop. It’s OK for it to be a function of the outer loop indices, for the
innermost loop in a nest of loops.
Write straight line code (avoid branches such as switch, goto or return statements,
most function calls, or “if” constructs that can’t be treated as masked assignments).
Prefer array notation to the use of pointers. C programs in particular impose very
few restrictions on the use of pointers; aliased pointers may lead to unexpected
dependencies. Without help, the compiler often cannot tell whether it is safe to
vectorize code containing pointers.
Use the loop index directly in array subscripts where possible, instead of
incrementing a separate counter for use as an array address.
Choose carefully a suitable data layout. Most multimedia extension instruction sets are
rather sensitive to alignment. The data movement instructions of SSE/SSEx, for
example, operate much more efficiently on data that is aligned at a 16-byte boundary in
memory. Therefore, the success of a vectorizing compiler also depends on its ability to
select an appropriate data layout that, in combination with code restructuring (like loop
peeling), will result in aligned memory accesses throughout the program. If your
compilation targets the Intel® AVX instruction set, you should try to align data on a 32-
byte boundary. This may result in improved performance.
struct MyData{
short Data1;
short Data2;
short Data3;};
If the type short is stored in two bytes of memory then each member of the data
structure depicted above would be aligned to a boundary of 2 bytes. Data1 would be at
offset 0, Data2 at offset 2 and Data3 at offset 4. The size of this structure then would be
6 bytes. The type of each member of the structure usually has a required alignment,
meaning that it will, unless otherwise requested by the programmer, be aligned on a
pre-determined boundary. In cases where the compiler has taken sub-optimal
alignment decisions, however, the programmer can use the directive
declspec(align(base,offset)), where 0 <= offset < base and base is a power of two,
to allocate a data structure at offset from a certain base
. . .
a[i+1] = b[i] * 3;
If the first element of both arrays is aligned at a 16-byte boundary, then either an
unaligned load of elements from “ b” or an unaligned store of elements into “a”, has to
be used after vectorization. (Note that in this case, peeling off an iteration would not
help.) However, the programmer can enforce the alignment shown below, which will
result in two aligned access patterns after vectorization (assuming an 8-byte size for
doubles):
/* or simply "align(16)" */
14
If pointer variables are used, the compiler is usually not able to determine the alignment
of access patterns at compile time. Consider the following simple fill function:
int i;
x[i] = 1;
Without more information, the compiler cannot make any assumption on the alignment
of the memory region accessed by this loop. At this point, the compiler may decide to
vectorize this loop using unaligned data movement instructions or, as done by the Intel
C/C++ compiler, generate the run-time alignment optimization shown here:
peel = x & 0x0f;
if (peel != 0) {
peel = 16 - peel;
x[i] = 1;
/* aligned access */
x[i] = 1;
Point Structure :
R G B
Structure of Arrays: R G B R G B R G B
R R R G G G B B B
With the AoS arrangement, a loop that visits all components of an RGB point before
moving to the next point exhibits a good locality of reference because all elements in
fetched cache lines are utilized. The disadvantage of the AoS arrangement is that each
individual memory reference in such a loop exhibits a non-unit stride, which, in general,
adversely affects vector performance. Furthermore, a loop that visits only one
component of all points exhibits less satisfactory locality of reference because many of
the elements in the fetched cache lines remain unused. In contrast, with the SoA
arrangement the unit-stride memory references are more amenable to effective
vectorization and still exhibit good locality of reference within each of the three data
streams. Consequently, an application that uses the SoA arrangement may ultimately
outperform an application based on the AoS arrangement when compiled with a
vectorizing compiler, even if this performance difference is not directly apparent during
the early implementation phase.
16
• Make sure that inner loop indices correspond to the outermost (last) array
index in your data (row-major order).
For instance when dealing with 3 dimensional coordinates, use 3 separate arrays for
each component (SOA), instead of using one array of 3-component structures (AOS). To
avoid dependencies between loops that will eventually prevent vectorization, it is
therefore recommended to rather use 3 separate arrays for each component (SOA),
instead of one array of 3-component structures (AOS), when processing 3 dimensional
coordinates. In AOS, each iteration produces one result by computing XYZ_, but can at
best use only 75% of the SSE unit, because the fourth component is not used.
Sometimes, it may use only one component (25%). In SOA, each iteration produces 4
results by computing XXXX, YYYY and ZZZZ and using 100% of the SSE unit. A drawback
for SOA is that your code will likely be 3 times as long. On the other hand, the compiler
might not be able to vectorize AOS code at all.
If your original data layout is in AOS format, you may even want to consider a
conversion to SOA on the fly, before the critical loop. If it gets vectorized, it may be
worth the effort!
AoS Example:
AoSvsSoA.cpp
17
SoA Example:
AoSvsSoA.cpp
Avoid mixing vectorizable data types in the same loop (except for integer arithmetic
on array subscripts). Vectorization of type conversions may be either unsupported
or inefficient. The latest compilers, including the version 12 of the Intel® C++
Compilers for Windows* and Linux*, have greatly improved and can auto-vectorize
mixed type loops. But avoid if possible. The example below illustrates the mixing of
data types, which may prevent auto-vectorization.
18
void mixed(float *restrict a, double *restrict b, float *c)
Avoid operations not supported in SIMD hardware. Arithmetic with (80 bit) long
doubles on Linux, and the remainder operator “%” are examples of operations not
supported in SIMD hardware.
Use all the instruction sets available for your processor. Use the appropriate
command line option for your processor type, or select the appropriate IDE option
under “Project / Properties / C/C++ / Code Generation / Intel Processor-Specific
Optimization” (/QxSSE4.1, /QxSSE4.2, etc., on Windows*) and (-xSSE4.1, -xSSE4.2,
etc, on Linux* or Mac OS* X), if your application will run only on Intel processors, or
“Project / Properties / C/C++ / Code Generation / Enable Enhanced Instruction Set”
(/arch:SSE2, /arch:SSE3 on Windows) and (-msse2, -msse3 on Linux or Mac OS X), if
your application may run on compatible, non-Intel processors.
If your application will run only on the processor type on which it was built, you
may simply choose the command line option /QxHost (Windows) or –xhost (Linux
or Mac OS X), or in the IDE, select “Project / Properties / C/C++ / Code Generation /
Intel Processor-Specific Optimization / The processor performing the compilation
(/QxHost)”. This option is available for both Intel® and non-Intel microprocessors
but it may perform more optimizations for Intel microprocessors than it performs
for non-Intel microprocessors.
For more details about the available processor-specific options, see the article at
http://software.intel.com/en-us/articles/performance-tools-for-software-
developers-intel-compiler-options-for-sse-generation-and-processor-specific-
optimizations/
19
Vectorizing compilers usually have some built-in efficiency heuristics to decide
whether vectorization is likely to improve performance. The Intel® C/C++ Compiler
will disable vectorization of loops with many unaligned or non-unit stride data
access patterns. However, if experimentation reveals that vectorization will still
improve performance, the programmer can override this behaviour with a
“#pragma vector always” hint before the loop, which asks the compiler to vectorize
any loop regardless of the outcome of the efficiency analysis (provided, of course,
that vectorization is safe).
6. Vectorization Reports
The Intel® Compiler provides a vectorization report option that provides two important
kinds of information. First, the vectorization report will inform you which loops in your
code are being vectorized. The end result of a vectorized loop is an instruction stream
for that loop that contains packed SIMD instructions. Secondly and at least as important,
is report information about why the compiler did NOT vectorize a loop. This
information assists a programmer by highlighting the barriers that the compiler finds to
vectorization. With the Intel® compiler, one must enable the vectorization reporting
mechanism, since it is not enabled by default. In the Microsoft* Visual Studio* IDE
(Integrated Development Environment), this is done by selecting from the Project menu
“Properties \ C/C++ \ Diagnostics \ Optimization Diagnostic Phase” as “The High
Performance Optimizer phase, /Qopt-report-phase:hpo” and “Optimization Diagnostic
Level” as “medium”. This corresponds to n=2 below.
“Not Inner Loop”: Only the inner loop of a loop nest may be vectorized.
20
“Existence of vector dependence”: The compiler did not vectorize the loop because
of a proven or potential dependence. If you are sure that any potential dependencies are
not in fact realized, you may invite the compiler to ignore them with #pragma ivdep.
“Condition may protect exception”: When the compiler tries to vectorize a loop
containing an IF statement, it typically evaluates the RHS expressions for all values of
the loop index, but only makes the final assignment in those cases where the conditional
evaluates to TRUE. In some cases, the compiler may not vectorize out of concern that
the condition may be protecting against accessing an illegal memory address.
An IVDEP pragma may be used to reassure the compiler that the conditional is not
protecting against a memory exception in such cases.
“data type unsupported on given target architecture”: For example, this message
might occur when compiling a loop containing complex arithmetic for a target processor
that supported only the SSE2 instruction set. SSE3 instructions are needed for effective
vectorization of arithmetic involving complex data types.
“Subscript too complex”: An array subscript may be too complicated for the compiler
to decipher the memory access pattern. Try to write subscripts as an explicit function of
the main loop counter.
“Unsupported Loop Structure”, “Top test could not be found”: Loops that don’t
fulfill the requirements of countability, single entry and exit, etc., may generate these
messages.
“Operator unsuited for vectorization”: Certain operators, such as the “%” (modulus)
operator, can’t be vectorized.
6.2.1 Pragmas
Please see the compiler user and reference guide for more information, but here are a
few of the key ones.
21
#pragma ivdep may be used to tell the compiler that it may safely ignore any
potential data dependencies. (The compiler will not ignore proven
dependencies). Use of this pragma when there are in fact dependencies may
lead to incorrect results.
There are cases, where the compiler can’t tell by a static analysis that it is safe to
vectorize, but you as a developer would know. Consider the following loop:
cp_a[i] = cp_b[i];
/* vector loop */
else
/* serial loop */
cp_a[i] = cp_b[i];
#pragma loop count (n) may be used to advise the compiler of the typical trip
count of the loop. This may help the compiler to decide whether vectorization is
worthwhile, or whether or not it should generate alternative code paths for the loop.
#pragma vector always asks the compiler to vectorize the loop if it is safe to do
so, whether or not the compiler thinks that will improve performance.
#pragma vector align asserts that data within the following loop is aligned (to a
16 byte boundary, for SSE instruction sets).
#pragma vector nontemporal gives a hint to the compiler that data will not be
reused, and therefore to use streaming stores that bypass cache.
The example under #pragma ivdep above can also be handled using the restrict
keyword.
The programmer may use the restrict keyword in the declarations of cp_a and cp_b, as
shown below, to inform the compiler that each pointer variable provides exclusive
access to a certain memory region. The restrict qualifier in the argument list will let the
compiler know that there are no other aliases to the memory to which the pointers
point. In other words, the pointer for which it is used provides the only means of
accessing the memory in question in the scope in which the pointers live. If the loop
vectorizes without using the restrict keyword, the compiler will be checking for aliasing
at runtime. The compiler will not do any runtime checks for aliasing if you use the
restrict keyword. Using such a language extension requires an extra compiler switch,
such as -Qrestrict for the Intel C/C++ compiler.
23
void copy(char * restrict cp_a, char * restrict cp_b, int n) {
Another example is the following loop which may also not get vectorized, because of a
potential aliasing problem between pointers a, b and c:
If the restrict keyword is added to the parameters, the compiler will trust you, that you
will not access the memory in question with any other pointer and vectorize the code
properly:
// let the compiler know, the pointers are safe with restrict
void add(float * restrict a, float * restrict b, float * restrict c) {
for (int i=0; i<SIZE; i++) {
c[i] += a[i] + b[i];
}
}
Note, however, that both the loop-specific #pragma ivdep hint, as well as the pointer
variable-specific restrict hint must be used with care, since incorrect usage may change
the semantics intended in the original program.
The down-side of using restrict is, that not all compilers support this keyword, so your
source code may lose portability. If you care about source code portability you may
want to consider using the compiler options /Qalias-args- (-fargument-noalias) or
/Qansi-alias (-ansi-alias) instead. The first option tells the compiler that function
arguments cannot alias each other in the entire file, but they can alias global storage.
The latter tells the compiler that the different types do not alias each other. This
method is also convenient in case the exclusive access property holds for pointer
variables that are used in a large portion of code with many loops, because it avoids the
need to annotate each of the vectorizable loops individually. However, compiler options
work globally, so you have to make sure it does not cause harm at other code fragments.
24
6.2.3 Switches
Interprocedural optimization may be enabled by /Qipo (-ipo) across source files.
This may give the compiler additional information about a loop, such as trip counts,
alignment or data dependencies. It may also allow inlining of function calls. In the
example under “Which loops can be vectorized”, “no function calls”, if the functions
func() and trap_int() appear in separate source files, the loop in trap_int() may still
be vectorized by compiling both with /Qipo (-ipo).
High level loop optimizations (HLO) may be enabled with /O3 (-O3). These
additional loop optimizations may make it easier for the compiler to vectorize the
transformed loops. This option is available for both Intel® and non-Intel
microprocessors but they may result in more optimizations for Intel
microprocessors than for non-Intel microprocessors.
The advice may include suggestions for source code medications, applying specific
pragmas, or add compiler options. In all cases, applying a particular advice requires the
user to verify that it is safe to apply that particular suggestion. For example, if the
25
advice is to apply a particular pragma, the user has to understand the semantics of the
pragma and carefully consider if it can be safely applied to the loop in question. If the
user applies the pragma without verifying its validity based on the application data-
access patterns, the compiler may generate incorrect code causing the application to
execute incorrectly.
The compiler does not produce any objects or executables when the “/Qguide” option is
specified. By default the compiler does not produce guidance on how to improve
optimizations for parallelization, vectorization, and data transformation. If a loop does
not vectorize, compile your code at the “/Qguide” option and follow the GAP
recommendations when applicable.
Example:
Scaler_dep.cpp:
procedure: test_scalar_dep
26
7.1.3 Making changes suggested by GAP and rebuilding to vectorize gap.cpp
Scaler_dep.cpp:
Pragma simd can assert or not assert an error if a #pragma simd annotated loop fails to
vectorize. By default “#pragma simd” is set to "noassert", and the compiler will issue a
warning if the loop fails to vectorize. To direct the compiler to assert an error when
the #pragma simd annotated loop fails to vectorize, add the "assert" clause to the
#pragma simd. If a #pragma simd annotated loop is not vectorized by the compiler, the
loop holds its serial semantics
The countable loop for the pragma simd must be an innermost loop and must conform
to the (C/C++) for-loop style usable for OpenMP* worksharing loop construct. See
http://www.openmp.org/mp-documents/spec30.pdf (Section 2.5.1). While the
example below will auto-vectorize without “#pragma simd”, it does not conform to the
above requirement for User-mandated vectorization:
27
icl /c simd2.cpp /Qvec-report2
simd2.cpp
#pragma simd
Note that using pragma simd to force vectorization of loops that are not vectorizable due
to data dependencies or other reasons could cause the compiler to generate incorrect
code. In such cases, using the simd clauses such as “reduction”, “private”, etc, might help
the compiler to generate correct vector code. Please refer to the Intel® C++ Compiler
12.0 User’s Guide for details on the syntax and usage of the simd clauses.
In the following example, the compiler reports that the loop cannot be vectorized due to
existence of vector dependence. The compiler will generate incorrect code if it is
instructed to force vectorization of the loop in this example:
simd3.cpp
simd3.cpp(15) (col. 3): remark: loop was not vectorized: existence
of vector dependence.
28
Forcing the loop to vectorize will result in incorrect code being
generated:
simd3.cpp
simd3.cpp(15) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
Adding the reduction clause hints the compiler to generate correct code as shown below:
simd3.cpp
simd3.cpp(15) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
Here an example where the loop does not auto-vectorize due to loop-carried
dependencies even with the use of “pragma ivdep” but can be vectorized with “pragma
29
simd” . There are no loop-carried dependencies within the vector lengths 4, 8, and 16 so
the “vectorlength(16)” clause is used to tell the compiler that vectorizing the loop with
vectorlength 4, 8, and 16 is legal but beyond that is not legal due to loop-carried
dependencies.
30
>icl /c /Qvec-report2 simd4.cpp /DSIMD
simd4.cpp
simd4.cpp(27) (col. 3): remark: LOOP WAS VECTORIZED.
simd4.cpp(35) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
simd4.cpp(35) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
simd4.cpp(42) (col. 7): remark: SIMD LOOP WAS VECTORIZED.
simd4.cpp(17) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
Here is another example where the compiler does not vectorize the loop because it does
not know whether the variable “t” is assigned in every iteration. With the programmer’s
use of the “private clause” to assert that every iteration of the loop has its own private
copy of “t”, the compiler can vectorize this loop correctly. Please note that the “private”
clause may introduce sequential inconsistency which is well known for vectorization
and parallelization. It is the user’s responsibility to maintain sequential consistency if
needed. For example, in the example below, the serial execution result of “t” may not be
the same as the SIMD execution. In the SIMD execution, the last value of “t” is from the
last iteration of the loop, while for the serial execution, the value of “t” may not come
from the last iteration depending on A[i] and B[i].
simd5.cpp
simd5.cpp(8) (col. 3): remark: loop was not vectorized: existence of
vector dependence.
31
>icl /c /Qvec-report2 /Qrestrict simd5.cpp /DPRIVATE
simd5.cpp
simd5.cpp(8) (col. 3): remark: SIMD LOOP WAS VECTORIZED.
32
Elemental Functions
When you declare a function as elemental the compiler generates a short vector form of
the function, which can perform your function’s operation on multiple arguments in a
single invocation. The compiler also generates a scalar implementation of the function,
and the program can invoke the function either on single arguments or array arguments,
at different places in the program. The vector form of the function can be invoked in
parallel contexts in the following ways:
2. From a for-loop with pragma simd. If the elemental function is called from a
loop with “pragma simd”, the compiler no longer does any performance
heuristics, and is guaranteed to call the vector version of the function. Note that
if the loop contains a call to the “printf” C library function, the compiler will issue
the following remark:
3. From a cilk_for
33
>icl /c /Qvec-report2 vectorFunc.cpp
vectorFunc.cpp
For more details on elemental functions and their limitations please see the Intel® C++
Compiler XE 12.0 User and Reference Guide, and the article titled “Elemental functions:
Writing data parallel code in C/C++ using Intel® Cilk Plus “
34
8. Conclusion
There are no hard and fast rules that guarantee auto-vectorization. However, applying
the recommendations described above increases the success rate. Our experience
shows that in cases where auto-vectorization is successful, the speedups are significant.
It is worthwhile to try the auto-vectorization feature in the Intel Compiler before writing
your own SIMD code because using the compiler is less time consuming than writing
your own SIMD code. Furthermore, using the Intel Compiler to auto-vectorize is more
future-proof. To update your optimization for new processors, you can recompile the
code at that time to target the new processors rather than having to re-write the SIMD
code. There will be cases when writing your own SIMD code is unavoidable, e.g. when
the compiler cannot auto-vectorize or when you can vectorize the code much better
than the compiler.
9. Appendix
Below are some performance and diagnostic switches available for use. Some of these
options like the “-no-vec”, are to be used for diagnostic purposes only.
These options are available for both Intel® and non-Intel microprocessors but they may
result in more optimizations for Intel microprocessors than for non-Intel
microprocessors.
35
Linux Flag Windows Flag Description
Table 2. Pragmas that can help the compiler to auto-vectorize the following loop.
36
Compiler Hint Description
__declspec(align(<int>,<int>))
(Windows*)
__attribute__(align(<int>,<int>))
(Linux*)
10. References
The Software Vectorization Handbook, Aart Bik, Intel Press, 2004. ISBN 0-9743649-2-4
http://software.intel.com/en-us/articles/requirements-for-vectorizable-loops/
37
http://software.intel.com/en-us/articles/how-to-check-auto-vectorization/
http://software.intel.com/en-us/articles/vectorization-writing-cc-code-in-vector-
format/
Image Processing Acceleration Techniques using Intel® Streaming SIMD Extensions and
Intel® Advanced Vector Extensions, Petter Larsson, Eric Palmer
http://software.intel.com/en-us/articles/image-processing-acceleration-techniques-
using-intel-streaming-simd-extensions-and-intel-advanced-vector-extensions/
http://software.intel.com/en-us/articles/image-processing-acceleration-techniques-
using-intel-streaming-simd-extensions-and-intel-advanced-vector-extensions/
Optimization Notice
Intel® compilers, associated libraries and associated development tools may include or
utilize options that optimize for instruction sets that are available in both Intel® and
non-Intel microprocessors (for example SIMD instruction sets), but do not optimize
equally for non-Intel microprocessors. In addition, certain compiler options for Intel
compilers, including some that are not specific to Intel micro-architecture, are
reserved for Intel microprocessors. For a detailed description of Intel compiler
options, including the instruction sets and specific microprocessors they implicate,
please refer to the “Intel® Compiler User and Reference Guides” under “Compiler
Options." Many library routines that are part of Intel® compiler products are more
highly optimized for Intel microprocessors than for other microprocessors. While the
compilers and libraries in Intel® compiler products offer optimizations for both Intel
and Intel-compatible microprocessors, depending on the options you select, your code
and other factors, you likely will get extra performance on Intel microprocessors.
Intel® compilers, associated libraries and associated development tools may or may
not optimize to the same degree for non-Intel microprocessors for optimizations that
are not unique to Intel microprocessors. These optimizations include Intel®
Streaming SIMD Extensions 2 (Intel® SSE2), Intel® Streaming SIMD Extensions 3
(Intel® SSE3), and Supplemental Streaming SIMD Extensions 3 (Intel® SSSE3)
instruction sets and other optimizations. Intel does not guarantee the availability,
functionality, or effectiveness of any optimization on microprocessors not
manufactured by Intel. Microprocessor-dependent optimizations in this product are
intended for use with Intel microprocessors.
While Intel believes our compilers and libraries are excellent choices to assist in
obtaining the best performance on Intel® and non-Intel microprocessors, Intel
recommends that you evaluate other compilers and libraries to determine which best
meet your requirements. We hope to win your business by striving to offer the best
performance of any compiler or library; please let us know if you find we do not.
38
*Other brands and names may be claimed as the property of others
Intel, the Intel logo and Itanium are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
and other countries.
Performance tests and ratings are measured using specific computer systems and/or components and reflect the approximate
performance of Intel products as measured by those tests. Any difference in system hardware or software design or
configuration may affect actual performance. Buyers should consult other sources of information to evaluate the performance
of systems or components they are considering purchasing. For more information on performance tests and on the performance
of Intel products, visit Intel http://www.intel.com/performance/resources/limits.htm
39