27
OpenMP: Vectorization and #pragma omp simd Markus H¨ ohnerbach 1 / 26

OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

OpenMP: Vectorization and #pragma omp simd

Markus Hohnerbach

1 / 26

Page 2: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Where does it come from?

ci = ai + bi ∀i

b1 b2 b3 b4 b5 b6 b7 b8

a1 a2 a3 a4 a5 a6 a7 a8

c1 c2 c3 c4 c5 c6 c7 c8

+

=

2 / 26

Page 3: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

3 / 26

Page 4: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Why would I care?

4 / 26

Page 5: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Why would I care?

4 / 26

Page 6: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Everywhere...

x86SSE 128 bitAVX(2) 256 bitAVX-512 (IMCI) 512 bit

ARM NEON 128 bit

POWERAltiVec/VMX/VSX 128 bitQPX 256 bit

SPARCHPC-ACE 128 bitHPC-ACE2 256 bit

5 / 26

Page 7: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Vectorization on Intel CPUs

[v]mova[p/s]s reg1, reg2/mem

reg: xmm0-xmm15 (128bit)

ymm0-ymm15 (256bit)

zmm0-zmm15 (512bit)

vaddps: Vectorized

vaddss: Scalar

How to see assembly: add -S to command line.

6 / 26

Page 8: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Vectorization: Indication

Profiling! Worth it on the hot path!

I Increases available memory bandwidth to cache

I Increases throughput of compute operations

I More power efficient

I Reduce frontend pressure (fewer instructions to decode)

Keep in mind Ahmdahl’s law!

7 / 26

Page 9: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Cool! How can I use that?

I Libraries(MKL, OpenBLAS, BLIS, fftw, numpy, OpenCV)

I Hoping for a good compiler: Autovectorization

I Assisting compiler through annotations: OpenMP SIMDpragma

I Writing intrinsics/assembly code (not covered here)

8 / 26

Page 10: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Autovectorization

I The compiler needs to prove that the optimization is legal

I And the compiler needs to prove that the optimization isbeneficial (under almost all circumstances)

I What could possibly go wrong?

I Conditionals (different vector lanes executing different code)

I Inner loops (might have different trip counts)

I Function calls (the functions might not be vectorized)

I Cross-iteration dependencies

I OpenMP addresses the last two points in particular

9 / 26

Page 11: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

The OpenMP simd pragma

I Unifies the enforcement of vectorization for for loop

I Introduced in OpenMP 4.0

I Explicit vectorization of for loops

I Same restrictions as omp for, and then some

I Executions in chunks of simdlength, concurrently executed

I Only directive allowed inside: omp ordered simd (OpenMP4.5)

I Can be combined with omp for

I No exceptions

10 / 26

Page 12: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Clauses

I safelen(len): Maximum number of iterations per chunk

I simdlen(len): Recommended number of iterations perchunk

I linear(stride: var, ...): with respect to iterationvariable

I aligned(alignment: var): alignment of variable

I private, lastprivate, reduction, collapse: As withomp for

11 / 26

Page 13: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Issues with your code

I Aliasing

I Alignment

I Floating point issues

I Correctness

I Function calls

I Ordering

12 / 26

Page 14: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Aliasing

float * a = ...;

float * b = ...;

float s;

...

for (int i = 0; i < N; i++) {

a[i] += s * b[i];

}

Compiler does not know that a and b do not overlap.Has to be conservative.

13 / 26

Page 15: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Aliasing

float * a = ...;

float * b = ...;

float aa[N];

memcpy(aa, a, N * sizeof(float));

float bb[N];

memcpy(bb, b, N * sizeof(float));

float s;

...

for (int i = 0; i < N; i++) {

aa[i] += s * bb[i];

}

memcpy(a, aa, N * sizeof(float));

14 / 26

Page 16: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Aliasing

float * __restrict__ a = ...;

float * __restrict__ b = ...;

float s;

...

for (int i = 0; i < N; i++) {

a[i] += s * b[i];

}

15 / 26

Page 17: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Aliasing

float * a = ...;

float * b = ...;

float s;

...

#pragma omp simd

for (int i = 0; i < N; i++) {

a[i] += s * b[i];

}

16 / 26

Page 18: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Alignment

I Loading a chunk of data is cheaper if the address is aligned.

I Allows for faster hardware instructions to load a vector.

I Avoid cache line splits.

I Ex: Recent Intel CPUs have 64 byte cache lines, and 32 bytevectors, best alignment is 32 bytes.

Cache lines A,B,...:

AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIII

#### <- Want to load this data? Unaligned.

#### <- Want to load this data? Aligned.

17 / 26

Page 19: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Alignment

float * a;

posix_memalign(&a, ...);

float * b;

posix_memalign(&b, ...);

float s;

...

__assume_aligned(a, 32); // Intel

__assume_aligned(b, 32); // Intel

#pragma omp simd

for (int i = 0; i < N; i++) {

a[i] += s * b[i];

}

18 / 26

Page 20: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Alignment

float * a;

posix_memalign(&a, ...);

float * b;

posix_memalign(&b, ...);

float s;

...

#pragma omp simd aligned(a, b: 32)

for (int i = 0; i < N; i++) {

a[i] += s * b[i];

}

19 / 26

Page 21: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Floating Point Models

for (int i = 0; i < n; i++) {

sum += a[i];

}

-ffast-math /fp:fast -fp-model fast=2

#pragma omp simd reduction(+: sum)

for (int i = 0; i < n; i++) {

sum += a[i];

}

https://msdn.microsoft.com/en-us/library/aa289157.aspx

20 / 26

Page 22: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Correctness

for (int i = 0; i < N; i++) {

int j = d[i];

a[j] += s * b[i];

}

Vectorization is only legal is the elements in d are distinct.This case occurs in applications!

#pragma omp simd

for (int i = 0; i < N; i++) {

int j = d[i];

a[j] += s * b[i];

}

21 / 26

Page 23: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Functions

// This won’t vectorize unless foo inlined.

foo(float a, float * b, float c);

float s;

#pragma omp simd

for (int i = 0; i < N; i++) {

int j = d[i];

a[j] += foo(s, &b[i], a[j]);

}

22 / 26

Page 24: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

The OpenMP declare simd directive

I asks compiler to generate veectorized version of a function

I allows vectorization of loops with function calls

I notinbranch, inbranch: Generate masking code, non-maskingcode

I everything from the simd pragma + uniform

I uniform: does not change

I linear: increases with index

23 / 26

Page 25: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Functions

#pragma omp declare simd uniform(a) linear(1: b)

foo(float a, float * b, float c);

float s;

#pragma omp simd

for (int i = 0; i < N; i++) {

int j = d[i];

a[j] += foo(s, &b[i], a[j]);

}

24 / 26

Page 26: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Masks

#pragma omp simd

for (int i = 0; i < n; i++) {

if (a[i] < 1.0) continue;

// ..

int j = d[i];

a[j] = ...;

}

25 / 26

Page 27: OpenMP: Vectorization and #pragma omp simdhpac.rwth-aachen.de/teaching/pp-16/material/08.OpenMP-4.pdf · Vectorization: Indication Pro ling! Worth it on the hot path! I Increases

Ordering

#pragma omp simd

for (int i = 0; i < n; i++) {

if (a[i] < 1.0) continue;

// ..

int j = d[i];

#pragma omp ordered simd

a[j] = ...;

}

If d is not containing distinct elements.

26 / 26