Tutorial: #pragma simd

Previous: SIMD-Enabled Functions Up: Overview Next: Terms

SIMD (Single Instruction, Multiple Data) vectorization uses the #pragma simd pragma to enforce loop vectorization.

Consider an example in C++ where the function add_floats() uses too many unknown pointers, preventing automatic vectorization. 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 simd.

void add_floats(float *a, float *b, float *c, float *d, float *e, int n)
{
    int i;
#pragma simd
    for (i=0; i<n; i++)
    {
        a[i] = a[i] + b[i] + c[i] + d[i] + e[i];
    }
}

The one big difference between using the SIMD pragma and auto-vectorization hints is that with the SIMD pragma, 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.

The SIMD pragma 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 simd description in the Intel Cilk Plus documentation.

Previous: SIMD-Enabled Functions Up: Overview Next: Terms