Download - Concurrencia paralelismo
![Page 1: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/1.jpg)
Paralelismo vs. Concurrencia
Roberto Costumero Moreno
Arquitecturas MultiprocesadorCurso 2011 / 2012
![Page 2: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/2.jpg)
• Programas cada vez más complejos.
• Sistemas con múltiples CPUs multicore.
• Posibilidad de reducir el tiempo de ejecución de un programa.
Una visión general
![Page 3: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/3.jpg)
Ventajas
• Menor tiempo de cómputo.
• Mayor aprovechamiento de los recursos.
• Permite en muchas tareas el acceso simultáneo de varios usuarios.
• Favorece una mayor cohesión y un menor acoplamiento entre las tareas a realizar.
![Page 4: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/4.jpg)
Desventajas
• Mayor complejidad de los programas.
• Condiciones de carrera, ejecuciones no determinísticas...
• Mayor grado de especialización de los ingenieros.
• Código muy difícil de depurar.
![Page 5: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/5.jpg)
¿Qué son la concurrencia y el
paralelismo?
![Page 6: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/6.jpg)
• Concurrencia: Ejecución de programas de forma no determinística.
• Paralelismo: Explotación de la eficiencia de programas de forma determinística.
![Page 7: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/7.jpg)
• Dos operaciones pueden estar realizándose en el mismo tiempo o no. Depende del paralelismo.
• Es necesaria para implementar paralelismo.
• Provee sistemas de sincronización y comunicación para programas ejecutados en una o más CPU.
Concurrencia
![Page 8: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/8.jpg)
Paralelismo
• Implica la ejecución simultánea de dos o más operaciones en el mismo número de CPUs.
• Necesita de mecanismos de concurrencia para juntar las partes de los cálculos realizados.
![Page 9: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/9.jpg)
¿Por qué es importante la concurrencia?
• Permite la simulación de una ejecución paralela en un entorno monocore.
• Gestiona el acceso seguro a elementos compartidos y secciones críticas.
• Garantiza que el resultado de las operaciones es siempre el mismo, independientemente del nº de threads.
![Page 10: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/10.jpg)
¿Por qué es importante el paralelismo?
• Creciente número de cores en los ordenadores.
• Permite una gran disminución del tiempo de cómputo en programas de gran escala.
• Contribuye a un mayor aprovechamiento de los recursos.
![Page 11: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/11.jpg)
Ejemplos
• Cálculo secuencial, concurrente (1 CPU) y paralelo (2 CPU) del número PI.
• Cálculo de la suma de los elementos de un vector de forma secuencial, concurrente y paralela.
![Page 12: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/12.jpg)
PI secuencialpackage main
import ( "fmt" "math" "runtime")
func main() { runtime.GOMAXPROCS(1) fmt.Println(pi(100000))}
// pi launches n goroutines to compute an// approximation of pi.func pi(n int) float64 { f := 0.0 for k := 0; k <= n; k++ { f += term(float64(k)) } return f}
func term(k float64) float64 { return 4 * math.Pow(-1, k) / (2*k + 1)}
![Page 13: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/13.jpg)
PI Concurrentepackage main
import ( "fmt" "math" "runtime")
func main() { runtime.GOMAXPROCS(1) fmt.Println(pi(100000))}
// pi launches n goroutines to compute an// approximation of pi.func pi(n int) float64 { ch := make(chan float64) for k := 0; k <= n; k++ { go term(ch, float64(k)) } f := 0.0 for k := 0; k <= n; k++ { f += <-ch } return f}
func term(ch chan float64, k float64) { ch <- 4 * math.Pow(-1, k) / (2*k + 1)}
![Page 14: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/14.jpg)
PI Paralelopackage main
import ( "fmt" "math" "runtime")
func main() { runtime.GOMAXPROCS(2); fmt.Println(pi(100000))}
// pi launches n goroutines to compute an// approximation of pi.func pi(n int) float64 { ch := make(chan float64) for k := 0; k <= n; k++ { go term(ch, float64(k)) } f := 0.0 for k := 0; k <= n; k++ { f += <-ch } return f}
func term(ch chan float64, k float64) { ch <- 4 * math.Pow(-1, k) / (2*k + 1)}
![Page 15: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/15.jpg)
Tiempos ejecución
3.1416026534897203 Secuencial Concurrente Paralelo
Real 0m0.032s
User 0m0.027s
Sys 0m0.002s
![Page 16: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/16.jpg)
Tiempos ejecución
3.1416026534897203 Secuencial Concurrente Paralelo
Real 0m0.032s 0m0.689s
User 0m0.027s 0m0.333s
Sys 0m0.002s 0m0.347s
![Page 17: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/17.jpg)
Tiempos ejecución
3.1416026534897203 Secuencial Concurrente Paralelo
Real 0m0.032s 0m0.689s 0m1.497s
User 0m0.027s 0m0.333s 0m0.493s
Sys 0m0.002s 0m0.347s 0m1.210s
![Page 18: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/18.jpg)
Vector secuencialpackage main
import ( "fmt" "container/vector" "runtime")
func sum (v *vector.IntVector, n int) int { var total int = 0 for i := n; i < v.Len(); i += 4 { total += v.At(i) } return total}
func main () { runtime.GOMAXPROCS(1) var v *vector.IntVector = new (vector.IntVector) for i := 0; i < 100000000; i++ { v.Push(i) } var t int = 0 for i := 0; i < 4; i++ { t += sum (v, i) } fmt.Println(t)}
![Page 19: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/19.jpg)
Vector Concurrenteimport ( "fmt" "container/vector" "runtime")
func sum (v *vector.IntVector, n int, ch chan int) { var total int = 0 for i := n; i < v.Len(); i += 4 { total += v.At(i) } ch <- total}
func main () { runtime.GOMAXPROCS(1) var v *vector.IntVector = new (vector.IntVector) for i := 0; i < 100000000; i++ { v.Push(i) } var t int = 0 ch := make(chan int) for i := 0; i < 4; i++ { go sum (v, i, ch) } for i := 0; i < 4; i++ { t += <-ch } fmt.Println(t)}
![Page 20: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/20.jpg)
Vector Paraleloimport ( "fmt" "container/vector" "runtime")
func sum (v *vector.IntVector, n int, ch chan int) { var total int = 0 for i := n; i < v.Len(); i += 4 { total += v.At(i) } ch <- total}
func main () { runtime.GOMAXPROCS(2) var v *vector.IntVector = new (vector.IntVector) for i := 0; i < 100000000; i++ { v.Push(i) } var t int = 0 ch := make(chan int) for i := 0; i < 4; i++ { go sum (v, i, ch) } for i := 0; i < 4; i++ { t += <-ch } fmt.Println(t)}
![Page 21: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/21.jpg)
Tiempos ejecución
887459712 Secuencial Concurrente Paralelo
Real 0m3.694s
User 0m3.031s
Sys 0m0.626s
Tiempos de inserción de elementos: real 0m2.716s || user 0m1.977s || sys 0m0.668s
![Page 22: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/22.jpg)
Tiempos ejecución
887459712 Secuencial Concurrente Paralelo
Real 0m3.694s 0m3.725s
User 0m3.031s 0m3.068s
Sys 0m0.626s 0m0.632s
Tiempos de inserción de elementos: real 0m2.716s || user 0m1.977s || sys 0m0.668s
![Page 23: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/23.jpg)
Tiempos ejecución
887459712 Secuencial Concurrente Paralelo
Real 0m3.694s 0m3.725s 0m3.179s
User 0m3.031s 0m3.068s 0m3.025s
Sys 0m0.626s 0m0.632s 0m0.638s
Tiempos de inserción de elementos: real 0m2.716s || user 0m1.977s || sys 0m0.668s
![Page 24: Concurrencia paralelismo](https://reader034.vdocuments.pub/reader034/viewer/2022052123/545cff24b1af9f500a8b496c/html5/thumbnails/24.jpg)
• http://existentialtype.wordpress.com/2011/03/17/parallelism-is-not-concurrency/
• http://existentialtype.wordpress.com/2011/03/15/teaching-fp-to-freshmen/
• http://ghcmutterings.wordpress.com/2009/10/06/parallelism-concurrency/
• http://my.opera.com/Vorlath/blog/2009/10/08/parallel-vs-concurrent
• http://golang.org
Bibliografía