CPU Session 2 Praktikum Parallele Rechnerarchtitekturen

Praktikum Parallele Rechnerarchitekturen / Johannes Hofmann

October 22, 2015

1

Overview • Introduction to OpenMP • How to Pin Threads • Exercise 2

Praktikum Parallele Rechnerarchitekturen / Johannes Hofmann

October 22, 2015

2

Multicore Parallelism

Praktikum Parallele Rechnerarchitekturen / Johannes Hofmann

Memory

QPI

QPI

MC

PCIx

PCIx

QPI QPI

shared LLC

PCIx

PCIx

PCIx

PCIx

PCIx

shared LLC

PCIx

PCIx

PCIx

MC

Memory

Design of luna • Two-Socket Ivy Bridge-EP System (2x Xeon E5-2650 v2) • Two NUMA Domains Package 0 Package 1 • Ten Cores per Socket 0 5 0 5 1 6 1 6 2 7 2 7 • Nominal CPU 3 8 3 8 Frequency: 2.6 GHz 4 9 4 9 • Turbo up to 3.4 GHz • 2-SMT • Advanced Vector Extensions (AVX) • DDR3-1866 Memory

October 22, 2015

3

Exploiting Multicore Parallelism • OpenMP basiert auf fork-join Programmiermodel o o o o

4

Programme starten mit nur einem Thread Zusätzliche Threads (thread team) werden für parallele Regionen geforkt Implizite Barriere am Ende einer parallelen Region Join nach paralleler Region

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP Ziele von OpenMP • Sehr einfache Handhabung • Sequentielle äquivalenz o Paralleles Programm produziert die selben Ergebnisse wie sequentielle Variante

• Inkrementelle Parallelisierung o Ein bereits vorhandenes Programm soll sich leicht Schritt-für-Schritt parallelisieren lassen

5

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP Beispiel: Hello Word!

Parallele Variante

Sequentielle Variante

$ cat hello_omp.c #include #include $ cat hello.c int main(int argc, char **argv) { #include #pragma omp parallel { int main(int argc, char **argv) { printf(“Hello World!\n”); printf(“Hello World!\n”); } } } $ icc -o hello hello.c $ icc –openmp -o hello_omp \ $ ./hello helloomp.c Hello World! $ ./hello_omp $ Hello World! Hello World! Praktikum Parallele ... 6

Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP • Zahl der Threads wird von Laufzeitumgebung festgesetzt • Kann vom Programmierer beeinflusst werden o Umgebungsvariable OMP_NUM_THREADS $ OMP_NUM_THREADS=3 ./hello_omp Hello World Hello World Hello World o In Compiler-Direktive im C Code #pragma omp parallel num_threads(3)

7

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP – Threadzahl zur Laufzeit #include #include int main(int argc, char **argv) { #pragma omp parallel num_threads(4) { int my_num, total_threads; my_num = omp_get_thread_num(); total_threads = omp_get_num_threads(); printf(“Hello World from thread %d of %d!\n, my_num, total_threads); } } Hello Hello Hello Hello

8

World World World World

from from from from

thread thread thread thread

2 0 3 1

of of of of

4! 4! 4! 4!

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP – Private vs. Shared Variables #include #include int main(int argc, char **argv) { int a; /* shared variable */ #pragma omp parallel { int b; /* private variable */ a = omp_get_thread_num(); // Probably not what you want b = omp_get_thread_num(); // Okay } }

• Race Condition/Wettlaufsituation • Declare shared variables as private using: #pragma omp parallel private(var1, var2, …) 9

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP – Worksharing Constructs In OpenMP gibt es mehrere Pragmas, die dem Programmierer die Aufgabe der Arbeitsverteilung und das Thread-Management erleichtern, z.B. • Parallel for Construct • Section Constructs • Single Constructs

10

Praktikum Parallele Rechnerarchitekturen October/ Johannes 22, 2015

OpenMP – For Construct void add(double *A, double *B, double *C, int N) { #pragma omp parallel { #pragma omp for for (int i=0; i