Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli

Werbung
Datenparallele Algorithmische
Skelette in Muesli
Dominik Pfeiffer
Münster, 31. Mai 2007
Seminar: Parallele und Verteilte Programmierung
Gliederung
1.
2.
3.
4.
5.
2
Motivation
Grundlegende Begriffe
Die Skelettbibliothek Muesli
Beispielprogramm
Zusammenfassung und Bewertung
Motivation
Probleme in der parallelen Programmierung:
Synchronisation von Prozessen
Deadlocks
Starvation
Mögliche Lösung des Problems:
Von der Parallelität der Ausführung abstrahieren
Auf einer höheren Ebene Funktionen zur parallelen Verarbeitung
von Aufgaben und Daten bereitstellen
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
3
Gliederung
1. Motivation
2. Grundlegende Begriffe
1. Funktionen höherer Ordnung
2. Partielle Applikationen
3. Algorithmische Skelette
3. Die Skelettbibliothek Muesli
4. Beispielprogramm
5. Zusammenfassung und Bewertung
4
Funktionen höherer Ordnung
Funktionen mit Funktionen als Parameter oder
Rückgabewerte
Gleichstellung von Funktionen und Werten
Beispiel:
map (funktion, [liste])
map wendet eine Funktion sukzessiv auf jedes Element
einer Liste an
Realisierung bspw. in C++ durch
Funktionszeiger
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
5
Partielle Applikationen (1/2)
Anwendung von weniger Argumenten auf eine Funktion
als erwartet werden
Umwandeln dieser Funktion in neue Funktion mit nur noch den
fehlenden Argumenten
Anwendung beliebiger Werte auf restliche Argumente führt zum
selben Ergebnis wie Auflösen der Ursprungsfunktion
Lässt sich beliebig frei und tief verschachteln
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
6
Partielle Applikationen (2/2)
Realisierung durch die Hilfsfunktion curry()
Beispiel:
Ausgangsfunktion:
map (funktion, [liste])
Hilfsfunktion:
mult(x, y)
Anwendung von curry() auf mult(x,y) mit dem Parameter 2
liefert mult2(y)
Partielle Applikation:
map(curry(mult)(2), [liste])
Ergebnis:
Alle Elemente der Liste werden mit 2 multipliziert
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
7
Algorithmische Skelette (1/2)
Stellen ein Gerüst für bestimmte Operationen und
Kommunikationsmuster bereit
Verbergen die parallele Ausführung
Vermeiden unnötige Spezialisierung
Können in der sequentiellen Programmierung genutzt werden
Kümmern sich um Low-Level-Probleme
Deadlocks
Starvation
Synchronisation
Implementierung unter Verwendung von:
Polymorphismus
Funktionen höherer Ordnung
Partielle Applikationen
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
8
Algorithmische Skelette (2/2)
Taskparallele Skelette
Teilen Probleme in sequentiell und parallel abzuarbeitende
Aufgaben
Datenparallele Skelette
Führen Operationen auf verteilten Datenstrukturen aus
Laufen intern parallel ab
Rechenskelette
Bearbeiten die Inhalte der verteilten Datenstruktur
Kommunikationsskelette
Versenden Daten
Synchronisieren Prozesse
Ändern die Verteilung der Partitionen auf einzelne Prozesse
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
9
Gliederung
1. Motivation
2. Grundlegende Begriffe
3. Die Skelettbibliothek Muesli
1.
2.
3.
4.
5.
Übersicht
Verteilte Datenstrukturen
Map
ZipWith
Fold
4. Beispielprogramm
5. Zusammenfassung und Bewertung
10
Übersicht
Muesli ist eine Skelettbibliothek
Algorithmische Skelette
Verteilte Datenstrukturen
Keine eigene Programmiersprache
Implementierung
C++
Interprozesskommunikation mit MPI
Funktionen höherer Ordnung
Partielle Applikationen
Polymorphismus (Templates)
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
11
Verteilte Datenstrukturen (1/2)
Verteilte Datenstrukturen Array und Matrix
Aufteilung in Partitionen
Verteilung der Partitionen auf mehrere Prozesse
Konstruktoren
Größe der Matrix
Größe der Partition
Initialwert oder –funktion
Hilfsfunktionen
get
set
show
…
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
12
Verteilte Datenstrukturen (2/2)
Kommunikation ist nicht erforderlich
Alle Prozesse führen den gleichen Code aus
Partition wird anhand der ProzessID bestimmt
Alle Prozesse haben die gleiche Datenbasis
 x11
x
 21
 x31

 x41
x12
x22
x32
x42
x13
x23
x33
x43
x14 
x24 
x34 

x44 
 x11
x
 21
x12 
x22 
Prozess 1
 x13
x
 23
x14 
x24 
Prozess 2
 x31
x
 41
x32 
x42 
Prozess 3
 x33
x
 43
x34 
x44 
Prozess 4
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
13
Map (1/2)
Rechenskelett
Wendet eine Funktion sukzessiv auf jedes Element
einer Datenstruktur an
Die Anwendung kann parallel von jedem Prozess auf
seiner Partition erfolgen
Kommunikation ist nicht erforderlich
Skelettdefinition in Muesli:
DistributedMatrix<R> map (R (*f)(E))
Varianten
DistributedMatrix<R> mapIndex(R (*f)(int,int,E))
void mapInPlace(E (*f)(E))
void mapIndexInPlace(E (*f)(int,int,E))
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
14
Map (2/2)
Beispiel
Multiplikation aller Elemente der Matrix mit 2
 1
 5

 9

 13
2
6
10
14
3
7
11
15
4
8
12
16






 2
 10

 18

 26
4
12
20
28
6
14
22
30
8 
16 
24 

32 
2
6 
P1
 2
 10

4 
12 
3
7

4
8 
P2
 6
 14

8 
16 
 9 10 
13 14 


P3
 18
 26

20 
28 
11 12 
15 16 


P4
 22
 30

24 
32 
1
5

Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
15
ZipWith (1/2)
Rechenskelett
Kombiniert zwei gleiche Datenstrukturen
Gleiche Größe der Matrix
Gleiche Partitionierung
Elementweise Kombination
Kommunikation ist nicht erforderlich
Skelettdefinition in Muesli:
DistributedMatrix<R> zipWith (
const DistributedMatrix<E2>& b, R (*f)(E,E2))
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
16
ZipWith (2/2)
Beispiel: Kombination durch elementweise Multiplikation
1
5

9

13
&
1
1

1

1
Prozess 1 
5
2
6 
1
& 
1
2
2 
=
1
5

3
7

4
8 
3
3

4
4 
=
 9 16 
 21 32 


9
10 
1
2
2 
=
 9 20 
13 28 


11 12 
3
4
4 
=
 33 48 
 45 64 


2
6
10
14
3
4
7
8 
11 12 

15 16 
1
Prozess 2
&
Prozess 3 
 & 1
13 14 

Prozess 4 15 16  &  3

 
2
2
2
2
4
4 
4

4
3
3
3
3
=
4
1
 5 12

 9 20

13 28
9 16 
21 32 
33 48 

45 64 
4
12 
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
17
Fold (1/3)
Rechenskelett
Vereint alle Elemente einer Datenstruktur
Nacheinander Vereinigung jedes Elements mit dem bisherigen
Zwischenergebnis
Die Art der Vereinigung wird durch Parameter bestimmt:
Assoziative Funktion f
f erwartet 2 Parameter vom Typ der Elemente der Datenstruktur
Ergebnis liegt danach bei jedem Prozess vor
Ergebnis = f(Elem1, f(Elem2, … f(Elemn-1, Elemn)…))
Kommunikation erforderlich
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
18
Fold (2/3)
Beispiel
Schritt 1: Vereinigung der Elemente jeder lokalen Partition
Schritt 2: Austausch der Zwischenergebnisse zwischen den Prozessen
(1 + (2 + (5 + 6)))
= 14
 9 10 
13 14 


Se
nd
/R
e
3
7

Send/
Receive
ce
i ve
n
Se
P3
(9 + (10 + (13 + 14)))
= 46
Send/
Receive
d/ R
4
8 
e
ec
P2
(3 + (4 + (7 + 8)))
= 22
i ve
11 12 
15 16 


Send/Receive
2
6 
Send/Receive
1
5

P1
P4
(11 + (12 + (15 + 16)))
= 54
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
19
Fold (3/3)
Beispiel
Schritt 3: Vereinigung der Zwischenergebnisse durch jeden Prozess
P1
(14 + (22 + (46 + 54))) = 136
P2
(14 + (22 + (46 + 54))) = 136
P3
(14 + (22 + (46 + 54))) = 136
P4
(14 + (22 + (46 + 54))) = 136
Skelettdefinition in Muesli:
E fold (E (*f)(E,E))
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
20
Gliederung
1.
2.
3.
4.
Motivation
Grundlegende Begriffe
Die Skelettbibliothek Muesli
Beispielprogramm
1. Hintergrund
2. Programmablauf
3. Implementierung
5. Zusammenfassung und Bewertung
21
Hintergrund
Beispielprogramm unter Verwendung der hier
vorgestellten Skelette
Anwendungskontext:
Verschmelzen von zwei Übergangsmatrizen aus der
Wahrscheinlichkeitstheorie
Praktische Anwendung in der Physik bei der Beschreibung
physikalischer Zustandsveränderungen
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
22
Programmablauf
1. Erstellen zweier 4x4-Übergangsmatrizen
Matrix 1: Die ersten beiden Zeilen mit 0,5 und der Rest
mit 0 belegt
Matrix 2: Einheitsmatrix
2. Kombinieren der Matrizen
3. Berechnung der Summe aller Werte der
Ergebnismatrix
Die Werte müssen in der Summe 4 ergeben
Stellt ansatzweise die Korrektheit der Vereinigung sicher
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
23
Implementierung (1/5)
Initialisieren der Skelettbibliothek
InitSkeletons(argc, argv);
Initialisieren der ersten Übergangsmatrix
DistributedMatrix<double> m1(4,4,1,4);
m1.mapIndexInPlace(&firsttwo);
m1=
Hilfsfunktion firsttwo
double firsttwo(const int a, const int b, const double d) {
return (a == 0 || a == 1) ? 0.5 : 0;
}
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
24
Implementierung (2/5)
Initialisieren der zweiten Übergangsmatrix
DistributedMatrix<double> m2(4,4,&identmx,1,4);
m2=
Hilfsfunktion identmx
double identmx(const int a, const int b) {
return (a == b) ? 1 : 0;
}
Kombinieren der Matrizen
m1.zipWithInPlace(m2,&combine);
m1=
Hilfsfunktion combine
double combine(const double a, const double b) {
return ((a + b) / 2);
}
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
25
Implementierung (3/5)
Berechnung der Summe aller
Werte der Ergebnismatrix
m1=
double sum = m1.fold(&add);
Hilfsfunktion add
double add(const double a, const double b) {
return a + b;
}
Skelettbibliothek beenden
TerminateSkeletons();
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
26
Implementierung (4/5)
Vollständiger Quellcode (Teil 1)
#include "Skeleton.h"
double firsttwo(const int a, const int b, const double d) {
return (a == 0 || a == 1) ? 0.5 : 0;}
double identmx(const int a, const int b) {
return (a == b) ? 1 : 0;}
double combine(const double a, const double b) {
return ((a + b) / 2);}
double add(const double a, const double b) {
return a + b;}
(…)
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
27
Implementierung (5/5)
Vollständiger Quellcode (Teil 2)
int main(int argc, char **argv) {
InitSkeletons(argc, argv);
// Initialisieren der Skelette
DistributedMatrix<double> M1(4, 4, 1, 4);
// Initialisieren der Matrizen
M1.mapIndexInPlace(&firsttwo);
DistributedMatrix<double> M2(4, 4, &identmx, 1, 4);
M1.zipWithInPlace(M2, &combine);
// Kombinieren der Matrizen
double sum = M1.fold(&add);
// Summe der Werte bilden
TerminateSkeletons();
// Skelettbibliothek beenden
}
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
28
Gliederung
1.
2.
3.
4.
5.
29
Motivation
Grundlegende Begriffe
Die Skelettbibliothek Muesli
Beispielprogramm
Zusammenfassung und Bewertung
Bewertung (Positiv)
Abstraktion von Low-Level-Programmierung
Deadlocks, Starvation, Synchronisation
Jedes Skelett als Funktion höherer Ordnung und als
partielle Applikation verfügbar
Einheitliche Namensgebung bei Endungen
-Index
-InPlace
Programmierer kann weiterhin sequentiell arbeiten
Nur noch Bereitstellen einer adäquaten
problemspezifischen Funktion durch den Entwickler
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
30
Bewertung (Negativ)
Anzahl beteiligter Prozesse muss Potenz zur Basis 2
sein
Keine freie Partitionierung
Größe der Partition muss Teiler der Größe der Datenstruktur sein
Anzahl der Partitionen muss gleich Anzahl der Prozesse sein
Alle Partitionen müssen gleich groß sein
Keine automatische Partitionierung
Mögliche Schwierigkeiten beim Kompilieren
GNU Compiler G++ 3.3.3
MPI 1.2.x
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
31
Zusammenfassung
Algorithmische Skelette
Gerüst für die parallele Verarbeitung
Hohes Abstraktionsniveau
Effizient implementiert
Polymorphismus, Funktionen höherer Ordnung und partielle Applikationen
Nutzung wie bei sequentieller Programmierung möglich
Muesli
Keine eigene Programmiersprache, sondern Bibliothek
Verteilte Datenstrukturen Array und Matrix
Konstruktoren und Hilfsfunktionen
Rechen- und Kommunikationsskelette
Bereitstellen einer adäquaten problemspezifischen Funktion
durch den Entwickler
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
32
Vielen Dank
für eure
Aufmerksamkeit!
Seminar: Parallele und Verteilte Programmierung
PermutePartition
Kommunikationsskelett
Ändert die Verteilung der Partitionen auf die Prozesse
Erwartet bijektive Funktionen als Parameter
permutePartition
 a1
a
 3
d1

d3
34
a2
a4
d2
d4
b1
b3
c1
c3
b2 
b4 
c2 

c4 
 a1
a
 3
a2 
a 4 
 b1
b
 3
b2 
b4 
 c1
c
 3
c2 
c 4 
 d1
d
 3
d2 
d 4 
P1
P2
P3
P4
 b1
b
 3
b2 
b4 
 a1
a
 3
a2 
a 4 
 d1
d
 3
d2 
d 4 
 c1
c
 3
c2 
c 4 
b1
b
 3
c1

c3
b2 a1 a2 
b4 a3 a4 
c2 d1 d 2 

c4 d3 d 4 
Global vs. Lokal
Wahl zwischen der Betrachtungsweise des Problems
Global
Index der verteilten Datenstruktur
Geeignet für die Lösung eines Problems im Gesamtzusammenhang
Lokal
Index der lokalen Partition
Geeignet für Optimierungen
lokal
1
2
1
2
1
2
 x11
x
 21
 x31

 x41
x12
x22
x32
x42
1
2
1
2
x13
x23
x33
x43
x14  1
x24  2
x34  3

x44  4
3
4
Motivation – Grundlegende Begriffe – Die Skelettbibliothek Muesli – Beispielprogramm – Zusammenfassung
35
global
Herunterladen