Programación con OpenMP* Intel Software College. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or.

Slides:



Advertisements
Presentaciones similares
Cable & Wireless Panamá. Entrar a Inicio, Programas y buscar Windows Live.
Advertisements

Documentos de Construcción y Detalles
Unifying Concepts and Processes in Science. Explanatory Framework Across Science Disciplines Science is a way of knowing, a process--- it is a systematic.
Tener que – to have to ___ Use conjugations of tener for the person you are talking about Add que Add infinitive verb (not conjugated) TengoTenemos TienesTenéis.
Metodología de programación paralela
Programación Multi-core: Conceptos básicos
Computadores de alta velocidad (la lista top500).
E-science grid facility for Europe and Latin America E2GRIS1 Jaime Parada, Edgar Perdomo – UCV Itacuruça (Brazil), 2-15 November 2008 CATIVIC.
Introducción al OpenMP
Como Configurar el Internet
PLEASE READ (hidden slide) This template uses Microsofts corporate font, Segoe Segoe is not a standard font included with Windows, so if you have not.
Los programas de Intel Educación son patrocinados por la Fundación Intel y la Corporación Intel. Derechos de autor © 2007, Corporación Intel. Todos los.
Quality Management (J07) Overview Argentina
111 © 2002, Cisco Systems, Inc. All rights reserved. Presentation_ID.
Me llamo __________ Clase 9IM La fecha es el 24 de octubre del 2012
You will now learn how to use the subjunctive with verbs and expressions of will and influence. Copyright © 2008 Vista Higher Learning. All rights reserved.
You already know that the verbs ir and ser are irregular in the preterite. You will now learn other verbs whose preterite forms are also irregular. Copyright.
In both English and Spanish, the verb to be (ser) and numbers are used to tell time. Copyright © 2008 Vista Higher Learning. All rights reserved.
¿Quién es este hombre muy importante? ¿Por qué cada niño en cada escuela de Cuba le conoce?
Arquitectura de Computadores I
ARQUITECTURA DE COMPUTADORES - PIPELINING
You will now learn how to form and use the past subjunctive (el pretérito imperfecto de subjuntivo), also called the imperfect subjunctive. Like the present.
Both Spanish and English use the present progressive, which consists of the present tense of the verb to be and the present participle (the -ing form in.
Conjunctions are words or phrases that connect other words and clauses in sentences. Certain conjunctions commonly introduce adverbial clauses, which describe.
Copyright © 2008 Vista Higher Learning. All rights reserved In the previous lesson, you learned how to use the subjunctive with expressions of will.
Remember present tense –AR verbs… Copyright © 2008 Vista Higher Learning. All rights reserved
The past perfect subjunctive (el pluscuamperfecto de subjuntivo), also called the pluperfect subjunctive, is formed with the past subjunctive of haber.
10 Diversiones Culturales In this chapter you will learn to: discuss movies, museums, and theater discuss cultural events relate more past actions or events.
Programando con Hilos POSIX* Intel Software College.
Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered.
Programando con Hilos de Windows* Intel Software College.
Programando con OpenMP*
Corrigiendo Errores de Paralelización con Intel® Thread Checker para Hilos Explícitos Intel Software College.
Una breve Introducción al proyecto Yussef Farrán Leiva
ALC 11 lunes el 26 de septiembre. Bienvenida lunes el 26 de septiembre Front row face the back row. Back row will read the words on the screen in the.
Copyright © 2008 Vista Higher Learning. All rights reserved Just as the subjunctive is required with expressions of emotion, influence, and will,
Corrigiendo Errores en la Paralelización con Intel® Parallel Inspector.
Estructura de la empresa
Libro p Vocabulario y gramática en contexto.
Español II Srta. Forgue El 11 de abril de La clase de hoy El 11 de abril Ahora mismo: Mirar el tutorial de 7.1 Repaso: Corregir INTÉNTALO en la.
Srta. Forgue El 29 de enero de 2011
Srta. Forgue El 14 de febrero
SRTA. FORGUE EL 14 DE ENERO DE 2011 La clase de español II.
Srta. Forgue El 8 de febrero de 2011 Español II. Ahora mismo ¿Cómo fue el fin de semana? Habla por 5 minutos sobre lo que hiciste.
Srta. Forgue El 6 de mayo de 2011
Antonio Gámir TSP – Windows Client Microsoft Ibérica.
Su Negocio Conectado. VisibilidadVisibilidad ColaboraciónColaboración PlanificaciónPlanificación EjecuciónEjecución Build Connections.
para desarrolladores Minimizar el cambio Concentrarse en estabilidad, confiabilidad y rendimiento. Ayudar a mejorar la productividad Reducir la curva.
11 Servidores basados en Arquitectura Intel. 2 * Other names and brands may be claimed as the property of others. Copyright © 2008, Intel Corporation.
SOA conference. Qué es WCF Fundamentos de la Arquitectura WCF Adaptadores BizTalk WCF Novedades en BizTalk Demo.
Green IT con Microsoft Visio. Nuestra empresa Green IT con Microsoft Visio.
Señora Campuzano. Complete both blue and white cards Publisher: PRENTICE HALL Dictation teacher: Ms. Campuzano MUST WRITE: 1.A GARDIAN’S NAME 2.ADDRESS.
1 DEFINITION OF A CIRCLE and example CIRCLES PROBLEM 1a PROBLEM 2a Standard 4, 9, 17 PROBLEM 1b PROBLEM 2b PROBLEM 3 END SHOW PRESENTATION CREATED BY SIMON.
 1. Why should a person learn Spanish? Give at least 3 reasons in your explanation.  2. What Spanish experiences have you had? (None is not an option.
LOGO MSc. Pastor Enrique Vargas E.. LOGO
10/ How to set up the PSNC educational program Cómo establecer el programa educacional de EPNC By Por Dave Batty PSNC #10.
iLogic - Automatización Inteligente para Diseño Mecánico e Industrial
INTEL CONFIDENTIAL Metodología de programación paralela Intel Software College.
Programación en los procecesadores Intel® Multi Core.
INTEL CONFIDENTIAL Paralelizando para mejorar el rendimiento con Intel® Threading Building Blocks Sesión:
Algo mas de OPENMP y todo sobre PBS!. Algunas rutinas que devuelven variables de entorno  void omp_set_num_threads(int num_threads); // numero de threads.
Ejemplos openMP.
Programación de Memoria Compartida
Programación de Memoria Compartida
Programación con OpenMP* Intel Software College. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or.
OpenMP C and C++ Application Program Interface. Version October 1998.
Programming with POSIX* Threads Intel Software College.
Conceptos básicos de procesamiento en procecesadores Intel® Multi Core.
Derechos de autor © 2008, Corporación Intel. Todos los derechos reservados. Intel e Intel  Educación son marcas comerciales o marcas registradas de la.
Programación con OpenMP* Dr. Marco Antonio Ramos Corchado Fuente Intel.
Transcripción de la presentación:

Programación con OpenMP* Intel Software College

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 2 Programming with OpenMP* Objetivos Al término de este módulo el estudiante será capaz de Crear hilos utilizando pragmas OpenMP Usar pragmas de sincronización OpenMP para coordinar la ejecución de los hilos y acceso a memoria

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 3 Programming with OpenMP* Agenda ¿Qué es OpenMP? Regiones Paralelas Bloques de construcción para trabajo en paralelo Alcance de los datos para proteger datos Sincronización Explícita Cláusulas de Planificación Otros bloques de construcción y cláusulas útiles

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 4 Programming with OpenMP* ¿Qué es OpenMP*? Directivas del compilador para programación multihilos Es fácil crear hilos en Fortran y C/C++ Soporta el modelo de paralelismo de datos Paralelismo incremental Combina código serial y paralelo en un solo código fuente

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 5 Programming with OpenMP* ¿Qué es OpenMP*? omp_set_lock(lck) #pragma omp parallel for private(A, B) #pragma omp critical C$OMP parallel do shared(a, b, c) C$OMP PARALLEL REDUCTION (+: A, B) call OMP_INIT_LOCK (ilok) call omp_test_lock(jlok) setenv OMP_SCHEDULE dynamic CALL OMP_SET_NUM_THREADS(10) C$OMP DO lastprivate(XX) C$OMP ORDERED C$OMP SINGLE PRIVATE(X) C$OMP SECTIONS C$OMP MASTER C$OMP ATOMIC C$OMP FLUSH C$OMP PARALLEL DO ORDERED PRIVATE (A, B, C) C$OMP THREADPRIVATE(/ABC/) C$OMP PARALLEL COPYIN(/blk/) Nthrds = OMP_GET_NUM_PROCS() !$OMP BARRIER La especificación actual es OpenMP Páginas (C/C++ y Fortran)

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 6 Programming with OpenMP* Arquitectura OpenMP* Modelo fork-join Bloques de construcción para trabajo en paralelo Bloques de construcción para el ambiente de datos Bloques de construcción para sincronización API (Application Program Interface) extensiva para afinar el control

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 7 Programming with OpenMP* Modelo de programación Paralelismo fork-join: El hilo maestro se divide en un equipo de hilos como sea necesario El Paralelismo se añade incrementalmente: el programa secuencial se convierte en un programa paralelo Regiones paralelas Hilo maestro

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 8 Programming with OpenMP* Sintaxis del Pragma OpenMP* La mayoría de los bloques de construcción en OpenMP* son directivas de compilación o pragmas. En C y C++, los pragmas toman la siguiente forma: #pragma omp construct [clause [clause]…]

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 9 Programming with OpenMP* Regiones Paralelas Define una región paralela sobre un bloque de código estructurado Los hilos se crean comoparallel Los hilos se bloquean al final de la región Los datos se comparten entre hilos al menos que se especifique otra cosa #pragma omp parallel Hilo C/C++ : #pragma omp parallel { bloque }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 10 Programming with OpenMP* ¿Cuántos hilos? Establecer una variable de ambiente para el número de hilos set OMP_NUM_THREADS=4 No hay un default estándar en esta variable En muchos sistemas: # de hilos = # de procesadores Los compiladores de Intel ® usan este default

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 11 Programming with OpenMP* Actividad 1: Hello Worlds Modificar el código serial de Hello, Worlds para ejecutarse paralelamente usando OpenMP*

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 12 Programming with OpenMP* Bloques de construcción de trabajo en paralelo Divide las iteraciones del ciclo en hilos Debe estar en la región paralela Debe preceder el ciclo #pragma omp parallel #pragma omp for for (i=0; i<N; i++){ Do_Work(i); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 13 Programming with OpenMP* Bloques de construcción de trabajo en paralelo Los hilos se asignan a un conjunto de iteraciones independientes Los hilos deben de esperar al final del bloque de construcción de trabajo en paralelo #pragma omp parallel #pragma omp for Barrera implícita i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 i = 11 #pragma omp parallel #pragma omp for for(i = 0; i < 12; i++) c[i] = a[i] + b[i]

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 14 Programming with OpenMP* Combinando pragmas Ambos segmentos de código son equivalentes #pragma omp parallel { #pragma omp for for (i=0; i< MAX; i++) { res[i] = huge(); } #pragma omp parallel for for (i=0; i< MAX; i++) { res[i] = huge(); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 15 Programming with OpenMP* Ambiente de datos OpenMP usa un modelo de programación de memoria compartida La mayoría de las variables por default son compartidas. Las variables globales son compartidas entre hilo

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 16 Programming with OpenMP* Ambiente de datos Pero, no todo es compartido... Las variables en el stack en funciones llamadas de regiones paralelas son PRIVADAS Las variables automáticas dentro de un bloque son PRIVADAS Las variables de índices en ciclos son privadas (salvo excepciones) C/C+: La primera variable índice en el ciclo en ciclos anidados después de un #pragma omp for

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 17 Programming with OpenMP* Atributos del alcance de datos El estatus por default puede modificarse default (shared | none) Clausulas del atributo de alcance shared(varname,…) private(varname,…)

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 18 Programming with OpenMP* La cláusula Private Reproduce la variable por cada hilo Las variables no son inicializadas; en C++ el objeto es construido por default Cualquier valor externo a la región paralela es indefinido void* work(float* c, int N) { float x, y; int i; #pragma omp parallel for private(x,y) for(i=0; i<N; i++) { x = a[i]; y = b[i]; c[i] = x + y; }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 19 Programming with OpenMP* Ejemplo: producto punto float dot_prod(float* a, float* b, int N) { float sum = 0.0; #pragma omp parallel for shared(sum) for(int i=0; i<N; i++) { sum += a[i] * b[i]; } return sum; } ¿Qué es incorrecto?

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 20 Programming with OpenMP* Proteger datos compartidos Debe proteger el acceso a los datos compartidos que son modificables float dot_prod(float* a, float* b, int N) { float sum = 0.0; #pragma omp parallel for shared(sum) for(int i=0; i<N; i++) { #pragma omp critical sum += a[i] * b[i]; } return sum; }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 21 Programming with OpenMP* #pragma omp critical [(lock_name)] Define una región crítica en un bloque estructurado OpenMP* Bloques de construcción para regiones críticas float R1, R2; #pragma omp parallel { float A, B; #pragma omp for for(int i=0; i<niters; i++){ B = big_job(i); #pragma omp critical consum (B, &R1); A = bigger_job(i); #pragma omp critical consum (A, &R2); } } Los hilos esperan su turno –en un momento, solo uno llama consum() protegiendo R1 y R2 de de condiciones de concurso. Nombrar las regiones críticas es opcional, pero puede mejorar el rendimiento. (R1_lock) (R2_lock)

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 22 Programming with OpenMP* OpenMP* Cláusula de reducción reduction (op : list) Las variables en list deben ser compartidas dentro de la región paralela Adentro de parallel o el bloque de construcción de trabajo en paralelo: Se crea una copia PRIVADA de cada variable de la lista y se inicializa de acuerdo al op Estas copias son actualizadas localmente por los hilos Al final del bloque de construcción, las copias locales se combinan de acuerdo al op a un solo valor y se almacena en la variable COMPARTIDA original

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 23 Programming with OpenMP* Ejemplo de reducción Una copia local de sum para cada hilo Todas las copias locales de sum se suman y se almacenan en una variable global #pragma omp parallel for reduction(+:sum) for(i=0; i<N; i++) { sum += a[i] * b[i]; }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 24 Programming with OpenMP* Un rango de operadores asociativos y conmutativos pueden usarse con la reducción Los valores iniciales son aquellos que tienen sentido C/C++ Operaciones de reducción OperadorValor Inicial +0 *1 -0 ^0 OperadorValor Inicial &~0 |0 &&1 ||0

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 25 Programming with OpenMP* Ejemplo de integración numérica (1+x 2 ) f(x) = 4.0 (1+x 2 ) dx = 0 1 X static long num_steps=100000; double step, pi; void main() { int i; double x, sum = 0.0; step = 1.0/(double) num_steps; for (i=0; i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0 + x*x); } pi = step * sum; printf(Pi = %f\n,pi); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 26 Programming with OpenMP* Actividad 2 - Calculando Pi Paraleliza el código de integración numérica usando OpenMP ¿Qué variables se pueden compartir? ¿Qué variables deben ser privadas? ¿Qué variables deberían considerarse para reducción? static long num_steps=100000; double step, pi; void main() { int i; double x, sum = 0.0; step = 1.0/(double) num_steps; for (i=0; i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0 + x*x); } pi = step * sum; printf(Pi = %f\n,pi);}

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 27 Programming with OpenMP* Asignando Iteraciones La cláusula schedule afecta en como las iteraciones del ciclo se mapean a los hilos schedule(static [,chunk]) Bloques de iteraciones de tamaño chunk a los hilos Distribución Round Robin schedule(dynamic[,chunk]) Los hilos toman un fragmento (chunk) de iteraciones Cuando terminan las iteraciones, el hilo solicita el siguiente fragmento

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 28 Programming with OpenMP* Asignando Iteraciones schedule(guided[,chunk]) Planificación dinámica comenzando desde el bloque más grande El tamaño de los bloques se compacta; pero nunca más pequeño que chunk

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 29 Programming with OpenMP* Cláusula ScheduleCuando utilizar STATIC Predecible y trabajo similar por iteración DYNAMIC Impredecible, trabajo altamente variable por iteración GUIDED Caso especial de dinámico para reducir la sobrecarga de planificación Qué planificación utilizar

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 30 Programming with OpenMP* Ejemplo de la cláusula Schedule #pragma omp parallel for schedule (static, 8) for( int i = start; i <= end; i += 2 ) { if ( TestForPrime(i) ) gPrimesFound++; } Las iteraciones se dividen en pedazos de 8 Si start = 3, el primer pedazo es i ={3,5,7,9,11,13,15,17}

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 31 Programming with OpenMP* Secciones paralelas Secciones independientes de código se pueden ejecutar concurrentemente SerialParalela #pragma omp parallel sections { #pragma omp section phase1(); #pragma omp section phase2(); #pragma omp section phase3(); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 32 Programming with OpenMP* Denota un bloque de código que será ejecutado por un solo hilo El hilo seleccionado es dependiente de la implementación Barrera implícita al final Bloque de construcción Single #pragma omp parallel { DoManyThings(); #pragma omp single { ExchangeBoundaries(); } // threads wait here for single DoManyMoreThings(); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 33 Programming with OpenMP* Denota bloques de código que serán ejecutados solo por el hilo maestro No hay barrera implícita al final Bloque de construcción Master #pragma omp parallel { DoManyThings(); #pragma omp master { // if not master skip to next stmt ExchangeBoundaries(); } DoManyMoreThings(); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 34 Programming with OpenMP* Barreras implícitas Varios bloques de construcción de OpenMP* tienen barreras implícitas parallel for single Barreras innecesarias deterioran el rendimiento Esperar hilos implica que no se trabaja! Suprime barreras implícitas cuando sea seguro con la cláusula nowait.

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 35 Programming with OpenMP* Cláusula Nowait Cuando los hilos esperarían entren cómputos independientes #pragma single nowait { [...] } #pragma omp for nowait for(...) {...}; #pragma omp for schedule(dynamic,1) nowait for(int i=0; i<n; i++) a[i] = bigFunc1(i); #pragma omp for schedule(dynamic,1) for(int j=0; j<m; j++) b[j] = bigFunc2(j);

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 36 Programming with OpenMP* Barreras Sincronización explícita de barreras Cada hilo espera hasta que todos lleguen #pragma omp parallel shared (A, B, C) { DoSomeWork(A,B); printf(Processed A into B\n); #pragma omp barrier DoSomeWork(B,C); printf(Processed B into C\n); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 37 Programming with OpenMP* Operaciones Atómicas Caso especial de una sección crítica Aplica solo para la actualización de una posición de memoria #pragma omp parallel for shared(x, y, index, n) for (i = 0; i < n; i++) { #pragma omp atomic x[index[i]] += work1(i); y[i] += work2(i); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 38 Programming with OpenMP* API de OpenMP* Obtener el número de hilo dentro de un equipo int omp_get_thread_num(void); Obtener el número de hilos en un equipo int omp_get_num_threads(void); Usualmente no se requiere para códigos de OpenMP Tiene usos específicos (debugging) Hay que incluir archivo de cabecera #include

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 39 Programming with OpenMP* Programación con OpenMP ¿Qué se cubrió? OpenMP* es: Una aproximación simple a la programación paralela para computadoras con memoria compartida Exploramos OpenMP para saber como: Hacer regiones de código en paralelo ( omp parallel ) Dividir el trabajo ( omp for ) Categorizar variables ( omp private ….) Sincronización ( omp critical …)

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 40 Programming with OpenMP*

Conceptos avanzados

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 42 Programming with OpenMP* Mas sobre OpenMP* Bloques de construcción para el ambiente de datos FIRSTPRIVATE LASTPRIVATE THREADPRIVATE

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 43 Programming with OpenMP* Variables inicializadas de una variable compartida Los objetos de C++ se construyen a partir de una copia Cláusula Firstprivate incr=0; #pragma omp parallel for firstprivate(incr) for (I=0;I<=MAX;I++) { if ((I%2)==0) incr++; A(I)=incr; }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 44 Programming with OpenMP* Cláusula Lastprivate Las variables actualizan la variable compartida usando el valor de la última iteración Los objetos de C++ se actualizan por asignación void sq2(int n, double *lastterm) { double x; int i; #pragma omp parallel #pragma omp for lastprivate(x) for (i = 0; i < n; i++){ x = a[i]*a[i] + b[i]*b[i]; b[i] = sqrt(x); } lastterm = x; }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 45 Programming with OpenMP* Preserva el alcance global en el almacenamiento por hilo Usa copia para inicializar a partir del hilo maestro Cláusula Threadprivate struct Astruct A; #pragma omp threadprivate(A) … #pragma omp parallel copyin(A) do_something_to(&A); … #pragma omp parallel do_something_else_to(&A); Las copias privadas de A persisten entre regiones

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 46 Programming with OpenMP* Problemas de rendimiento Los hilos ociosos no hacen trabajo útil Divide el trabajo entre hilos lo más equitativamente posible Los hilos deben terminar trabajos paralelos al mismo tiempo La sincronización puede ser necesaria Minimiza el tiempo de espera de recursos protegidos

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 47 Programming with OpenMP* Cargas de trabajo no balanceadas Cargas de trabajo desigual produce hilos ociosos y desperdicio de tiempo. time Ocupado Ocioso #pragma omp parallel { #pragma omp for for( ; ; ){ } tiempo

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 48 Programming with OpenMP* #pragma omp parallel { #pragma omp critical {... }... } Sincronización Tiempo perdido por locks tiempo Ocupado Ocioso En SC

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 49 Programming with OpenMP* Afinando el rendimiento Los profilers usan muestreo para proveer datos sobre el rendimiento. Los profilers tradicionales están limitados para usarse con códigos de OpenMP*: Miden tiempo del CPU, no tiempo real No reportan contención de objetos de sincronización No pueden reportar carga de trabajo desbalanceada Muchos de ellos no tienen todo el soporte de OpenMP Los programadores necesitan profilers específicamente diseñadas para OpenMP.

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 50 Programming with OpenMP* Planificación estática: Hacerlo por uno mismo Debe conocerse: Número de hilos (Nthrds) Cada identificador ID de cada hilo (id) Calcular iteraciones (start y end): #pragma omp parallel { int i, istart, iend; istart = id * N / Nthrds; iend = (id+1) * N / Nthrds; for(i=istart;i<iend;i++){ c[i] = a[i] + b[i];} }