Revision [6879]
This is an old revision of OpenMP made by ToBo on 2008-12-07 17:15:15.
OpenMP
1. Eigenschaften
● OpenMP ist nur für Parallelrechner mit gemeinsamem Speicher geeignet.
● Schnittstellen für Fortran und C/C++
● Industriestandard (V1.0 in 1998, V2.0 in 2002, V2.5 in 2005, V3.0 in 2008)
● Erhältlich auf vielen Plattformen (vergleichbar mit MPI)
● erlaubt eine schrittweise Parallelisierung
● kommerziell und als OpenSource (Sun Studio und GCC) verfügbar
● URL: http://www.openmp.org , Text des Standards auf der Webseite verfügbar.
http://de.wikipedia.org/wiki/OpenMP
2. Umsetzung im Code
#pragma omp <Direktive> <Klauseln>
{
<referenzierter Bereich, enthält C/C++ Anweisungen>
}
{
<referenzierter Bereich, enthält C/C++ Anweisungen>
}
3. Prinzip
- Aufteilung eines Bereichs auf verschiedene Threads
- Bearbeitung mit lokalen Variablen in verschiedenen Threads
4. Beispiel
#include <omp.h>
int main () {
int nthreads, tid;
omp_set_num_threads(3);
printf("maxthreads=%d\n", omp_get_max_threads());
/* Fork a team of threads giving them their
own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain and print thread id */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
/* All threads join master thread and terminate */
return 0;
}
int main () {
int nthreads, tid;
omp_set_num_threads(3);
printf("maxthreads=%d\n", omp_get_max_threads());
/* Fork a team of threads giving them their
own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain and print thread id */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
/* All threads join master thread and terminate */
return 0;
}
5. Implementierung
OpenMP anders als der MPI-Standard. Mit Compiler-Anweisungen (pragma) wird Parallelverarbeitung realisiert. Am Anfang leichter, aber nicht ganz Problemfrei.
Siehe auch Mpi - • • • •