diff --git a/c++-ss22/vortrag.tex b/c++-ss22/vortrag.tex index da77ea2..805a5ff 100644 --- a/c++-ss22/vortrag.tex +++ b/c++-ss22/vortrag.tex @@ -6864,6 +6864,956 @@ gcc main.o -L/opt/hdf5/lib -lhdf5 -o main \end{frame} + + + + + +\section{Basics of OpenMP} + +\begin{frame}[fragile] +\frametitle{OpenMP} +\only<1>{ +Was ist OpenMP?\\ +{\Large \textbf{Open} Specifications for \textbf{M}ulti \textbf{P}rocessing} + +\begin{itemize} + \item Standard API für paralleles Programmieren + \item Von vielen Compilern unterstützt + \item Implementiert in C/C++ und Fortran + \item Nur für Systeme mit Shared Memory +\end{itemize} + +Was macht OpenMP nicht? +\begin{itemize} + \item Keine Laufzeitprüfung für Deadlocks + \item Keine Laufzeitprüfung für Datenabhängigkeit +\end{itemize} +} + +\only<2>{ +\begin{columns} + \column{0.65\textwidth} + OpenMP basiert auf dem \emph{fork/join} Modell \begin{itemize} + \item Ein OpenMP Programm startet als Single Thread (Master Thread) + \item Weitere Threads (team) werden in parallelen Regionen erzeugt + \item Am Ende einer parallelen Region werden alle Threads barrier synchronized + \item Der folgende Code wird dann wieder sequenziell ausgeführt + \end{itemize} + \column{0.35\textwidth} + \pgfimage[width=\textwidth]{omp1} +\end{columns} + +} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Hello World! - OpenMP} + + +Code +\begin{cpp}[fontsize=\footnotesize] +int main(void) +{ + #pragma omp parallel + printf("Hello World\n"); +} +\end{cpp} + +Compile +\begin{bash} +icc -openmp -o hello hello.c +gcc -fopenmp -o hello hello.c + +export OMP_NUM_THREADS=4 +./hello +Hello world +Hello world +Hello world +Hello world +\end{bash} + + + +\end{frame} + +\begin{frame} + \frametitle{Variablen} + \only<1>{ + Daten werden entweder gemeinsam oder privat gespeichert + \begin{itemize} + \item Der Kontext der Variable wird im Code definiert + \item Variablen sind per default shared + \item Das trifft nicht für Schleifenvariablen zu! + \end{itemize} + Weiterhin gibt es Reduktionsvariablen, die am Ende eines parallelen Blocks durch eine arithmetische Funktion reduziert werden. +} +\only<2>{ +\pgfimage[width=.8\textwidth]{omp3} +} + +\only<3>{ +\pgfimage[width=.8\textwidth]{omp4} + +} +\end{frame} +\begin{frame}[fragile] + \frametitle{Allgemeine pragmas} + +C-style pragma +\begin{cpp}[fontsize=\footnotesize] +#pragma omp directive [clause[[,] clause] ...] +structured block +\end{cpp} +\vspace*{-10mm} +Zur Laufzeit kann man auf OpenMP-Support prüfen +\begin{cpp}[fontsize=\footnotesize] +#ifdef _OPENMP +iam = omp_get_thread_num(); +#endif +\end{cpp} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Nützliche OpenMP Runtime Commands} + +Anzahl der Threads im Team setzen +\begin{cpp}[fontsize=\footnotesize] +void omp_set_num_threads(int num_threads); +int omp_get_num_threads(void); +\end{cpp} + +Maximale Zahl der Threads ausgeben +\begin{cpp}[fontsize=\footnotesize] +int omp_get_max_threads(void); +\end{cpp} + +Thread-ID ausgeben (von 0 bis $n-1$) +\begin{cpp}[fontsize=\footnotesize] +int omp_get_thread_num(void); +\end{cpp} + +Liefert die Zahl der Prozessoren, auf denen das Programm laufen kann +\begin{cpp}[fontsize=\footnotesize] +int omp_get_num_procs(void); +\end{cpp} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Nützliche OpenMP Runtime Commands} +ändert die Zahl der Threads im Team +\begin{cpp}[fontsize=\footnotesize] +int omp_in_parallel(void); +void omp_set_dynamic(int dynamics_threads); +\end{cpp} + +liefert die Zahl der dynamischen Threads +\begin{cpp}[fontsize=\footnotesize] +int omp_get_dynamic(void); +\end{cpp} +ändert die Tiefe der verschachtelten Schleifen +\begin{cpp}[fontsize=\footnotesize] +void omp_set_nested(int nested); +\end{cpp} + +gibt den Status der verschachtelten Schleife aus +\begin{cpp}[fontsize=\footnotesize] +int omp_get_nested(void); +\end{cpp} + + + +\end{frame} + +\begin{frame}[fragile] +\frametitle{Parallel regions: Syntax} + +Clauses +\begin{itemize} +\item \cppinline{private (list)} +\item \cppinline{shared (list)} +\item \cppinline{default (shared | none)} +\item \cppinline{firstprivate (list)} +\item \cppinline{reduction (operator: list)} +\item \cppinline{if (scalar-expression)} +\item \cppinline{copyin (list)} +\item \cppinline{num_threads (integer-expression)} +\item \cppinline{proc_bind (master | close | spread)} +\end{itemize} +\begin{cpp}[fontsize=\footnotesize] +#pragma omp parallel [clause[[,] clause] ...] +structured block +\end{cpp} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Ausführungsmodell} +\begin{itemize} + \item Am Beginn jeder parallelen Region wird ein Team gestartet + \item Thread 0 ist der Masterthread +\end{itemize} +\begin{columns} + \column{0.5\textwidth} + \begin{cpp}[fontsize=\footnotesize] + #pragma omp parallel + { + printf("Hello World!\n"); + } + \end{cpp} + \column{0.5\textwidth} + \pgfimage[width=\textwidth]{nomp_1} +\end{columns} +\end{frame} + + + +\begin{frame}[fragile] +\frametitle{Parallele Schleifen} + +\begin{itemize} + \item Haupteinsatzgebiet von OpenMP + \item Viele Programme können effizient durch Schleifenparallelisierung beschleunigt werden + \item Einfache Implementation: Pragma vor die Schleife setzen + \item Nicht alle Schleifen können parallelisiert werden + \item Die \cppinline{#pragma omp for}-Direktive ist notwendig +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Parallel loops} +Syntax: Clauses +\begin{itemize} + \item \cppinline{private (list)} + \item \cppinline{firstprivate (list)} + \item \cppinline{lastprivate (list)} + \item \cppinline{reduction (operator: list)} + \item \cppinline{schedule (kind [, chunksize])} + \item \cppinline{collapse (n)} + \item \cppinline{ordered} + \item \cppinline{nowait} +\end{itemize} + +\end{frame} + +\begin{frame} +\frametitle{Parallel loops} +Schleifen müssen in kanonischer Form sein +\begin{itemize} + \item die Zahl der Durchgänge muß vor der ersten Schleife bekannt sein + \item die Zählvariable darf nicht innerhalb der Schleife geändert werden + \item das Schleifenende muß vom Programm ausgelöst werden + \item kein vorheriger Abbruch durch \cppinline{break} + \item \cppinline{exit} ist in der Schleife erlaubt +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Parallele Schleife - Beispiel} +Zeilensumme +\begin{cpp}[fontsize=\footnotesize] +#pragma omp parallel for +for ( i = 0; i < n; i++ ) { + a[i][0] = 0; + for ( j = 0; j < m; j++ ) + a[i][0] = a[i][0] + a[i][j]; +} +\end{cpp} + +Smoothing +\begin{cpp}[fontsize=\footnotesize] +for ( i = 1; i < n; i++ ) { + #pragma omp parallel for + for ( j = 1; j < m - 1; j++ ) + a[i][j] = ( a[i-1][j-1] + a[i-1][j] + a[i-1][j+1] ) / 3.0; + } +\end{cpp} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Parallele Schleifen - Collapse} +\begin{itemize} + \item Perfekt geschachtelte Schleifen können zusammengefaltet werden + \item Alle Zählvariablen müssen unabhängig sein + \item Nur die innerste Schleife darf Code enthalten +\end{itemize} +\cppinline{#pragma omp parallel for collapse(n)}\\ +$n$ ist die Zahl der gefalteten Schleifen +\end{frame} + +\begin{frame}[fragile] +\frametitle{Parallele Schleifen - Collapse} +\begin{cpp}[fontsize=\footnotesize] +#pragma omp parallel for collapse(3) +for(int l=0; l<10; ++l) + for(int j=0; j<3; ++j) + for(int k=0; k<7; ++k){ + /* Only here is arbitrary code */ + foo[l][j][k] = 0; +} +\end{cpp} + +\end{frame} + + +\begin{frame}[fragile] +\frametitle{Geordnete Ausführung} +\cppinline{ordered}-Keyword +\begin{itemize} +\item Mit \cppinline{ordered} wird der folgende Block in der Originalreihenfolge ausgeführt +\item Z.B. für sequentielle Ausgabe in eine Datei +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Geordnete Ausführung} +\begin{cpp}[fontsize=\footnotesize] +/* ordered as clause */ +#pragma omp parallel for ordered +for ( i=0; i; +int main(int argc, char** argv) { + // Initialize the MPI environment + MPI_Init(NULL, NULL); + // Get the number of processes + int world_size; + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + // Get the rank of the process + int world_rank; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + // Get the name of the processor + char processor_name[MPI_MAX_PROCESSOR_NAME]; + int name_len; + MPI_Get_processor_name(processor_name, &name_len); + // Print off a hello world message + printf("Hello world from processor %s, rank %d" + " out of %d processors\n", + processor_name, world_rank, world_size); + // Finalize the MPI environment. + MPI_Finalize(); +} +\end{cpp} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{MPI - Basic commands} +\begin{cpp}[fontsize=\footnotesize] +MPI_Init(int* argc, char*** argv) +\end{cpp} + +Während \cppinline{MPI_Init} werden alle globalen und internen Variablen von MPI aufgebaut. Zum Beispiel wird ein Kommunikator um alle Prozesse gebildet, die gestartet wurden, und jedem Prozess werden eindeutige Ränge zugewiesen. Derzeit nimmt \cppinline{MPI_Init} zwei Argumente entgegen, die nicht notwendig sind, und die zusätzlichen Parameter werden einfach als zusätzlicher Platz belassen, für den Fall, dass zukünftige Implementierungen sie benötigen. +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - Basic commands} + +\begin{cpp}[fontsize=\footnotesize] +MPI_Get_processor_name(char* name, int* name_length) +\end{cpp} + +\cppinline{MPI_Get_processor_name} ermittelt den tatsächlichen Namen des Prozessors, auf dem der Prozess ausgeführt wird. + +The final call in this program is: +\begin{cpp}[fontsize=\footnotesize] +MPI_Finalize() +\end{cpp} + +\cppinline{MPI_Finalize} wird verwendet, um die MPI-Umgebung aufzuräumen. Danach können keine weiteren MPI-Aufrufe mehr gemacht werden. +\end{frame} + +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - Kommunicatoren} +MPI verwendet sog. Communicators. Zu Beginn wird \cppinline{MPI_COMM_WORLD} erstellt, der alle Prozesse enthält + +\begin{cpp}[fontsize=\footnotesize] +MPI_Comm_size(MPI_Comm communicator,int* size) +\end{cpp} +\cppinline{MPI_Comm_size} liefert die Größe des Kommunikators. Für \cppinline{MPI_COMM_WORLD} ist dies die Gesamtzahl der Prozesse + +\begin{cpp}[fontsize=\footnotesize] +MPI_Comm_rank(MPI_Comm communicator,int* rank) +\end{cpp} +\cppinline{MPI_Comm_rank} gibt den Rang eines Prozesses in einem Kommunikator zurück. Jedem Prozess innerhalb eines Kommunikators wird ein inkrementeller Rang zugewiesen, der bei Null beginnt. Die Ränge der Prozesse werden in erster Linie zur Identifizierung beim Senden und Empfangen von Nachrichten verwendet. + +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - Kommunikatoren} +Für die meisten Zwecke sind zwei Kommunikatoren relevant +\begin{itemize} + \item \cppinline{MPI_COMM_WORLD}: alle Prozesse, keine Topologie + \item \cppinline{MPI_CART_COMM}: ein Kommunikator mit kartesischer Topologie +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - CartCommunicators} +\begin{cpp}[fontsize=\footnotesize] +MPI_Dims_create( nnodes, ndims, dims ) +\end{cpp} +liefert einen Kommunikator mit passenden Prozessorzahlen zurück\\ +Input Parameter: +\begin{itemize} + \item \cppinline{nnodes} Anzahl der Prozesse (integer) + \item \cppinline{ndims} Anzahl der Dimension (integer) +\end{itemize} +Inout Parameter +\begin{itemize} + \item \cppinline{dims} Array der Größe \cppinline{ndims} mit der Prozessorzahl in jeder Dimensiondimension (array of integer)\\ Werden Werte $>0$ als Input Parameter übergeben, werde diese verwendet +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - CartCommunicators} +\begin{cpp}[fontsize=\footnotesize] +int MPI_Cart_create(MPI_Comm comm_old, int ndims, const int dims[], + const int periods[], int reorder, MPI_Comm *comm_cart) +\end{cpp} +erzeugt einen kartesischen Kommunikator\\ +Input Parameters +\begin{itemize} +\item \cppinline{comm_old} Eingangskommunikator (Handle, normalerweise \cppinline{MPI_COMM_WORLD}) +\item \cppinline{ndims} Anzahl der Dimensionen des kartesischen Gitters (Ganzzahl) +\item \cppinline{dims} ganzzahliges Array der Größe \cppinline{ndims} zur Angabe der Anzahl der Prozesse in jeder Dimension (die Ausgabe von \cppinline{MPI_Dims_create} kann und sollte verwendet werden) +\item \cppinline{periods} logisches Array der Größe \cppinline{ndims}, das angibt, ob das Gitter in jeder Dimension periodisch (true) oder nicht periodisch (false) ist +\item \cppinline{reorder} Rangfolge kann neu geordnet werden (true) oder nicht (false) (logisch) +\end{itemize} + +Output Parameters +\begin{itemize} +\item \cppinline{comm_cart} Kommunikator mit kartesischer Topologie (handle) +\end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI - CartCommunicators} +\begin{cpp}[fontsize=\footnotesize] +int MPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int coords[]) +\end{cpp} +Liefert die Position des Prozesses in der kartesischen Topologie +\begin{itemize} +\item \cppinline{comm} + kartesischer Kommunikator (handle) +\item \cppinline{rank} + Rang innerhalb des Kommunikators (integer, from \cppinline{MPI_Cart_Rank}) +\item \cppinline{maxdims} + Länge des Koordinatenvektors (integer) +\end{itemize} +Output Parameters +\begin{itemize} +\item \cppinline{coords} + Integerarray (der Größe \cppinline{ndims}) mit den kartesischen Koordinaten (integer) +\end{itemize} +\end{frame} + + + +\begin{frame}[fragile] + \frametitle{MPI - CartCommunicators} +\begin{cpp}[fontsize=\footnotesize] +int MPI_Cart_shift(MPI_Comm comm, int direction, int disp, int *rank_source, + int *rank_dest) +\end{cpp} +Gibt die verschobenen Quell- und Zielränge unter Angabe von Verschiebungsrichtung und -menge zurück +Synopsis\\ +Eingabe-Parameter +\begin{itemize} +\item \cppinline{comm} + Kommunikator mit kartesischer Struktur (Handle) +\item \cppinline{direction} + Koordinatendimension der Verschiebung (Ganzzahl) +\Eintrag \cppinline{disp} + Verschiebung ($> 0$: Verschiebung nach oben, $< 0$: Verschiebung nach unten) (Integer) +\end{itemize} +Ausgabeparameter +\begin{itemize} +\Einzelposten \cppinline{rank_source} + Rang des Quellprozesses (ganze Zahl) +\item \cppinline{rank_dest} + Rang des Zielprozesses (Integer) +\end{itemize} + + + +\end{frame} + + + + +\begin{frame}[fragile] + \frametitle{MPI - Send \& Receive} +\begin{cpp}[fontsize=\footnotesize] +MPI_Send(void* data, int count, MPI_Datatype datatype, + int destination, int tag, MPI_Comm communicator) +MPI_Recv(void* data, int count, MPI_Datatype datatype, int source, + int tag, MPI_Comm communicator, MPI_Status* status) +\end{cpp} +Prozesse können Daten an \cppinline{destination} mit einem bestimmten \cppinline{datatype} senden (vgl. nächste Folie)\\\ +Daten haben bestimmte Tags, um sie zu identifizieren.\\ +Der Empfänger muss die Daten mit diesem Tag aktiv erwarten\\ +Da der Rang des Prozesses vom Kommunikator abhängt, muss dieser \cppinline{communicator} explizit angegeben werden.\end{itemize} + +\end{frame} + + +\begin{frame}[fragile] + \frametitle{MPI - Send \& Receive} +Datatypes\\ +\begin{tabular}{ll} +\cppinline{MPI datatype} & C equivalent\\ +\cppinline{MPI_SHORT} & short int\\ +\cppinline{MPI_INT} & int\\ +\cppinline{MPI_LONG} & long int\\ +\cppinline{MPI_LONG_LONG} & long long int\\ +\cppinline{MPI_UNSIGNED_CHAR} & unsigned char\\ +\cppinline{MPI_UNSIGNED_SHORT} & unsigned short int\\ +\cppinline{MPI_UNSIGNED} & unsigned int\\ +\cppinline{MPI_UNSIGNED_LONG} & unsigned long int\\ +\cppinline{MPI_UNSIGNED_LONG_LONG} & unsigned long long int\\ +\cppinline{MPI_FLOAT} & float\\ +\cppinline{MPI_DOUBLE} & double\\ +\cppinline{MPI_LONG_DOUBLE} & long double\\ +\cppinline{MPI_BYTE} & char +\end{tabular} +\end{frame} + +\begin{frame}[fragile] + \frametitle{MPI Barriers} +\begin{cpp}[fontsize=\footnotesize] +int MPI_Barrier( MPI_Comm comm ) +\end{cpp} +Input Parameters +\begin{itemize} +\item \cppinline{comm} communicator (handle) +\end{itemize} +Blockiert den Aufrufer, bis alle Prozesse im Kommunikator ihn aufgerufen haben; d.h. der Aufruf kehrt bei jedem Prozess erst zurück, wenn alle Mitglieder des Kommunikators den Aufruf eingegeben haben. +\end{frame} + + +\begin{frame}[fragile] + \frametitle{MPI - Send \& Receive} +Minimal example +\begin{cpp}[fontsize=\footnotesize] +// Find out rank, size +int world_rank; +MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); +int world_size; +MPI_Comm_size(MPI_COMM_WORLD, &world_size); + +int number; +if (world_rank == 0) { + number = -1; + MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); +} else if (world_rank == 1) { + MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + printf("Process 1 received number %d from process 0\n", + number); +} +\end{cpp} +\end{frame} + + +\begin{frame}[fragile] + \frametitle{MPI - Reduction} +\begin{cpp}[fontsize=\footnotesize] +int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, + MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) +\end{cpp} +Reduziert die Werte für alle Prozesse auf einen einzigen Wert +\begin{columns} + \column{0.5\textwidth} +Eingabeparameter +\begin{itemize} +\item \cppinline{sendbuf} + Adresse des Sendepuffers (pointer) +\item \cppinline{count} + Anzahl der Elemente im Sendepuffer (Integer) +\item \cppinline{datatype} + Datentyp der Elemente des Sendepuffers (Handle) +\item \cppinline{op} + Operation reduzieren (Handle) +\item \cppinline{root} + Rang des Wurzelprozesses (Integer) +\item \cppinline{comm} + Kommunikator (Handle) +\end{itemize} +Ausgabeparameter +\begin{itemize} +\item \cppinline{recvbuf} + Adresse des Empfangspuffers (Auswahl, nur an der Wurzel signifikant) +\end{itemize} + +\column{0.5\textwidth} +\begin{tabular}{ll} +\cppinline{MPI_LAND} & logical \verb+AND+\\ +\cppinline{MPI_LOR} & logical \verb+OR+\\ +\cppinline{MPI_LXOR} & logical \verb+XOR+\\ +\cppinline{MPI_BAND} & binary \verb+AND+\\ +\cppinline{MPI_BOR} & binary \verb+OR+\\ +\cppinline{MPI_BXOR} & binary \verb+XOR+\\ +\cppinline{MPI_MIN} & minimum\\ +\cppinline{MPI_MAX} & maximum\\ +\cppinline{MPI_SUM} & sum\\ +\cppinline{MPI_PROD} & product\\ +\cppinline{MPI_MINLOC} & maximum and index \\ +\cppinline{MPI_MAXLOC} & minimum and index \\ +\end{tabular} + +\end{columns} +\end{frame} + + +\begin{frame}[fragile] +\frametitle{MPI Example: Burgers} +\begin{cpp}[fontsize=\footnotesize] +#include +#include +#include +#include +#include +#include +#define DIM 1 +//Define constants for directions +#define LEFT 0 +#define RIGHT 1 +#define B 2 +//Namespaces +using namespace blitz; +using namespace std; +//Some constants +const int N=1000; +const int tend=1000; +const int outputstep=50; +const double dx=1.; +const double dt=0.1; +\end{cpp} +\end{frame} + +\begin{frame}[fragile] +\frametitle{MPI Example: Burgers} +\begin{cpp}[fontsize=\footnotesize] +int main(int argc, char *argv[]) { + +blitz::Array un, unp; + +//Buffers for data transfer +double in[B], out[B]; + +int localsize, localindex, localmin, localmax; +//MPI variables +int size, rank, dims[DIM]; +int periods[DIM], coords[DIM]; +int neighbours[2]; +MPI_Comm cartcomm; +MPI_Status status; +\end{cpp} +\end{frame} + +\begin{frame}[fragile] +\frametitle{MPI Example: Burgers} +\begin{cpp}[fonzsize=\footnotesize] +//Initialize MPI +MPI_Init(&argc,&argv); +//Find out how many processes are available +MPI_Comm_size(MPI_COMM_WORLD,&size); +MPI_Comm_rank(MPI_COMM_WORLD,&rank); +cerr <<"CPU " <