mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-25 14:28:05 +02:00
MPI
This commit is contained in:
parent
c2db4122e7
commit
deea4832fe
7 changed files with 104 additions and 5 deletions
|
@ -15,7 +15,7 @@ gestartet.
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\section{Funktionen}
|
\section{Funktionen}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-size.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-size.c}\xindex{MPI\_Comm\_size}
|
||||||
Liefert die Größe des angegebenen Kommunikators; dh. die Anzahl der Prozesse in der Gruppe.
|
Liefert die Größe des angegebenen Kommunikators; dh. die Anzahl der Prozesse in der Gruppe.
|
||||||
|
|
||||||
\textbf{Parameter}
|
\textbf{Parameter}
|
||||||
|
@ -27,7 +27,7 @@ Liefert die Größe des angegebenen Kommunikators; dh. die Anzahl der Prozesse i
|
||||||
\textbf{Beispiel}
|
\textbf{Beispiel}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-size-example.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-size-example.c}
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\rule{\textwidth}{0.4pt}
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Comm\_rank}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-rank.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-rank.c}
|
||||||
Bestimmt den Rang des rufenden Prozesses innerhalb des Kommunikators.
|
Bestimmt den Rang des rufenden Prozesses innerhalb des Kommunikators.
|
||||||
|
|
||||||
|
@ -42,7 +42,44 @@ Der Rang wird von MPI zum Identifizieren eines Prozesses verwendet. Die Rangnumm
|
||||||
\textbf{Beispiel}
|
\textbf{Beispiel}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-rank-example.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/comm-rank-example.c}
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\rule{\textwidth}{0.4pt}
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Send}
|
||||||
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-send.c}
|
||||||
|
Senden einer Nachricht an einen anderen Prozeß innerhalb eines Kommunikators. (Standard-Send)
|
||||||
|
|
||||||
|
\textbf{Parameter}
|
||||||
|
\begin{itemize}
|
||||||
|
\item \textbf{buf}: Anfangsadresse des Sendepuffers
|
||||||
|
\item \textbf{count}: Anzahl der Elemente des Sendepuffers (nichtnegativ)
|
||||||
|
\item \textbf{datatype}: Typ der Elemente des Sendepuffers (handle)
|
||||||
|
\item \textbf{dest}: Rang des Empfängerprozesses in comm (integer)
|
||||||
|
\item \textbf{tag}: message tag zur Unterscheidung verschiedener Nachrichten;
|
||||||
|
Ein Kommunikationsvorgang wird durch ein Tripel (Sender, Empfänger, tag) eindeutig beschrieben.
|
||||||
|
\item \textbf{comm}: Kommunikator (handle)
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\textbf{Beispiel}
|
||||||
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-send-example.c}
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Recv}
|
||||||
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-receive.c}
|
||||||
|
Empfangen einer Nachricht (blockierend)
|
||||||
|
|
||||||
|
\textbf{Parameter}
|
||||||
|
\begin{itemize}
|
||||||
|
\item \textbf{buf}: Anfangsadresse des Empfangspuffers
|
||||||
|
\item \textbf{status}: Status, welcher source und tag angibt (\texttt{MPI\_Status})
|
||||||
|
\item \textbf{count}: Anzahl der Elemente im Empfangspuffer (nichtnegativ)
|
||||||
|
\item \textbf{datatype}: Typ der zu empfangenden Elemente (handle)
|
||||||
|
\item \textbf{source}: Rang des Senderprozesses in comm oder \texttt{MPI\_ANY\_SOURCE}
|
||||||
|
\item \textbf{tag}: message tag zur Unterscheidung verschiedener Nachrichten
|
||||||
|
Ein Kommunikationsvorgang wird durch ein Tripel (Sender, Empfänger, tag) eindeutig beschrieben. Um Nachrichten mit beliebigen tags zu empfangen, benutzt man die Konstante \texttt{MPI\_ANY\_TAG}.
|
||||||
|
\item \textbf{comm}: Kommunikator (handle)
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\textbf{Beispiel}
|
||||||
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-receive-example.c}
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Reduce}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-reduce.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-reduce.c}
|
||||||
Führt eine globale Operation \textbf{op} aus; der Prozeß \enquote{root} erhält das Resultat.
|
Führt eine globale Operation \textbf{op} aus; der Prozeß \enquote{root} erhält das Resultat.
|
||||||
|
|
||||||
|
@ -56,7 +93,7 @@ Führt eine globale Operation \textbf{op} aus; der Prozeß \enquote{root} erhäl
|
||||||
\item \textbf{comm}: Kommunikator (handle)
|
\item \textbf{comm}: Kommunikator (handle)
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\rule{\textwidth}{0.4pt}
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Bcast}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-bcast.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-bcast.c}
|
||||||
Sendet eine Nachricht vom Prozess \texttt{root} an alle anderen Prozesse des
|
Sendet eine Nachricht vom Prozess \texttt{root} an alle anderen Prozesse des
|
||||||
angegebenen Kommunikators.
|
angegebenen Kommunikators.
|
||||||
|
@ -70,7 +107,7 @@ angegebenen Kommunikators.
|
||||||
\item \textbf{comm}: Kommunikator (handle)
|
\item \textbf{comm}: Kommunikator (handle)
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\rule{\textwidth}{0.4pt}
|
\rule{\textwidth}{0.4pt}\xindex{MPI\_Scatter}
|
||||||
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-scatter.c}
|
\inputminted[numbersep=5pt, tabsize=4]{c}{scripts/mpi/mpi-scatter.c}
|
||||||
Verteilt Daten vom Prozess \texttt{root} unter alle anderen Prozesse in der Gruppe, so daß, soweit möglich, alle Prozesse gleich große Anteile erhalten.
|
Verteilt Daten vom Prozess \texttt{root} unter alle anderen Prozesse in der Gruppe, so daß, soweit möglich, alle Prozesse gleich große Anteile erhalten.
|
||||||
|
|
||||||
|
|
Binary file not shown.
20
documents/Programmierparadigmen/scripts/mpi/for-example.c
Normal file
20
documents/Programmierparadigmen/scripts/mpi/for-example.c
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <mpi.h>
|
||||||
|
int main (int argc, char** args) {
|
||||||
|
int size, i;
|
||||||
|
int myrank;
|
||||||
|
MPI_Init(&argc, &args);
|
||||||
|
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
||||||
|
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
|
||||||
|
|
||||||
|
for (i=0; i<size; i++) {
|
||||||
|
MPI_Barrier(MPI_COMM_WORLD);
|
||||||
|
if (i == myrank) {
|
||||||
|
printf("Hello World, I have rank %d out of %d.\n",
|
||||||
|
myrank, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MPI_Finalize();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include "mpi.h"
|
||||||
|
|
||||||
|
int msglen, again=1;
|
||||||
|
void *buf;
|
||||||
|
MPI_Datatype datatype
|
||||||
|
MPI_Comm comm;
|
||||||
|
MPI_Status status;
|
||||||
|
|
||||||
|
...
|
||||||
|
while (again) {
|
||||||
|
MPI_Probe(ROOT, MPI_ANY_TAG, comm, &status);
|
||||||
|
MPI_Get_count(&status, datatype, &msglen);
|
||||||
|
buf=malloc(msglen*sizeof(int));
|
||||||
|
MPI_Recv(buf, msglen, datatype, status.MPI_SOURCE,
|
||||||
|
status.MPI_TAG, comm, &status);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
...
|
|
@ -0,0 +1,3 @@
|
||||||
|
int MPI_Recv(void *buf, int count,
|
||||||
|
MPI_Datatype datatype, int source, int tag,
|
||||||
|
MPI_Comm comm, MPI_Status *status)
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include "mpi.h"
|
||||||
|
...
|
||||||
|
int signal, i, numprogs, me;
|
||||||
|
MPI_Status stat;
|
||||||
|
MPI_Comm_rank(MPI_COMM_WORLD, &me);
|
||||||
|
MPI_Comm_size(MPI_COMM_WORLD,
|
||||||
|
&numprocs);
|
||||||
|
if (me==ROOT) {
|
||||||
|
...
|
||||||
|
for (i=1; i<numprocs; i++) {
|
||||||
|
MPI_Send(&signal, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
|
||||||
|
}
|
||||||
|
...
|
||||||
|
else {
|
||||||
|
MPI_Recv(&sig, 1, MPI_INT, ROOT, MPI_ANY_TAG,
|
||||||
|
MPI_COMM_WORLD, &stat);
|
||||||
|
...
|
||||||
|
}
|
3
documents/Programmierparadigmen/scripts/mpi/mpi-send.c
Normal file
3
documents/Programmierparadigmen/scripts/mpi/mpi-send.c
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
int MPI_Send(void *buf, int count,
|
||||||
|
MPI_Datatype datatype, int dest,
|
||||||
|
int tag, MPI_Comm comm)
|
Loading…
Add table
Add a link
Reference in a new issue