Open MP
Open MP
Open MP
OpenMP
Introduccin Componentes
Bibliografa
Introduccin
Lenguajes C/C++ Fortran Plataformas UNIX WINDOWS NT
Historia
API nica FORTRAN, C/C++
Operadores Min y Max de Reduccin Extensiones del constructor atomic
1998
OpenMP C/C++1.0
2002
OpenMP C/C++ 2.0
2005
OpenMP 2.5
2008
OpenMP 3.0
2011
OpenMP 3.1
1997
1999
2000
Modelo de Programacin
Fork-Join Model
OpenMP
Introduccin Componentes
Bibliografa
Directivas de Compilacin
Sintaxis centinela nombre-directiva [clusula, ...] Ej:
FORTRAN
!$OMP PARALLEL DEFAULT(SHARED) PRIVATE(BETA,PI)
C/C++
Constructor Paralelo
#include <omp.h> #include <stdio.h> #include <stdlib.h> #define N 100 int main (int argc, char *argv[]) { int nthreads, tid, i; float a[N], b[N], c[N]; #pragma omp parallel shared(a,b,c,nthreads) private(i,tid) { tid = omp_get_thread_num(); if (tid == 0) { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); } printf("Thread %d starting...\n",tid); #pragma omp for for (i=0; i<N; i++) { c[i] = a[i] + b[i]; printf("Thread %d: c[%d]= %f\n",tid,i,c[i]); } } /* end of parallel section */ }
Clusulas
shared, private, firstprivate(var) reduction(op:var) default(shared/none) copyin(var) if (expresin) num_threads(expresin)
Paralelismo de Datos
Paralelismo Funcional
Serializacin
Directiva DO/For
#pragma omp parallel for private (j,X) for (i=0; i<N; i++) for (j=0; j<M; j++) { X = B[i][j] * B[i][j]; A[i][j] = A[i][j] + X; C[i][j] = X * 2 + 1; }
Clusulas
Directiva DO/For
#include <omp.h> #define CHUNKSIZE 100 #define N 1000 main () { int i, chunk; float a[N], b[N], c[N]; /* Some initializations */ for (i=0; i < N; i++) a[i] = b[i] = i * 1.0; chunk = CHUNKSIZE; #pragma omp parallel shared(a,b,c,chunk) private(i) { #pragma omp for schedule(dynamic,chunk) nowait for (i=0; i < N; i++) c[i] = a[i] + b[i]; } /* end of parallel section */ }
Directiva SECTIONS
#include <omp.h> #define N 1000
main ()
{ int i; float a[N], b[N], c[N], d[N]; /* Some initializations */ for (i=0; i < N; i++) { a[i] = i * 1.5; b[i] = i + 22.35; } #pragma omp parallel shared(a,b,c,d) private(i) { #pragma omp sections nowait {#pragma omp section for (i=0; i < N; i++) c[i] = a[i] + b[i]; #pragma omp section for (i=0; i < N; i++) d[i] = a[i] * b[i]; } /* end of sections */ } /* end of parallel section */ }
Directiva SINGLE
#pragma omp parallel { ... ; #pragma omp single inicializar(A); #pragma omp for for(i=0; i<N; i++) A[i] = A[i] * A[i] + 1; ... ; #pragma omp single copiar(B,A); }
Directiva WORKSHARE
Solo para Fortran
!$omp parallel !$omp workshare forall ( i = k+1:n, j = k+1:n ) a(i,j) = a(i,j) - a(i,k) * a(k,j) end forall !$omp end workshare !$omp end parallel
Asignaciones de array Asignaciones de escalares Instrucciones y contructoras FORALL Instrucciones y constructoras WHERE Constructoras Atomic Constructoras Critical Constructoras Parallel
Constructor de Tarea
#pragmaompparallel { #pragmaompsingle private(p) { p = listhead; while (p) { #pragmaomptaskprocess (p) p=next (p) ; } } }
Constructor de Sincronizacin
Directivas MASTER Seccin ejecutada solo por el Master-Thread CRITICAL Regin a ejecutar solo por un thread a la
vez
BARRIER Sincroniza todos los threads de un grupo TASKWAIT Especifica un wait para las tareas de los
hijos
ORDERED Especifica
ejecutar en serie
Resumen de Clusulas
OpenMP
Introduccin Componentes
Bibliografa
Rutinas de Bibliotecas
wtime = omp_get_wtime ( ) write ' Available processors: ', omp_get_num_procs ( ) write ' Available threads ', omp_get_max_threads ( ) write ' Threads in use ', omp_get_num_threads ( ) !$omp parallel private ( id ) id = omp_get_thread_num ( ) write ( *, * ) ' Hello from process ', id if ( id == 0 ) then write ' Threads in use ', omp_get_num_threads ( ) end if !$omp end parallel Resultado: wtime = omp_get_wtime ( ) - wtime Available processors: write ' Wtime = ', wtime 2 Available threads 2 Threads in use 1 Hello from process 0 Hello from process 1 Threads in use 2 Wtime = 0.732183E-
OpenMP
Introduccin Componentes
Bibliografa
Variables de Entorno
OMP_SCHEDULE OMP_NUM_THREADS OMP_DYNAMIC OMP_PROC_BIND OMP_NESTED OMP_STACKSIZE OMP_WAIT_POLICY OMP_MAX_ACTIVE_LEVELS OMP_THREAD_LIMIT
Bibliografa