Professional Documents
Culture Documents
Concurrencia
Roberto Costumero Moreno
Arquitecturas Multiprocesador
Curso 2011 / 2012
Ventajas
Menor tiempo de cmputo.
Mayor aprovechamiento de los recursos.
Permite en muchas tareas el acceso
simultneo de varios usuarios.
Desventajas
Mayor complejidad de los programas.
Condiciones de carrera, ejecuciones no
determinsticas...
Qu son la
concurrencia y el
paralelismo?
Concurrencia
Dos operaciones pueden estar realizndose
en el mismo tiempo o no. Depende del
paralelismo.
Paralelismo
Implica la ejecucin simultnea de dos o
Por qu es importante
la concurrencia?
Permite la simulacin de una ejecucin
paralela en un entorno monocore.
Por qu es importante
el paralelismo?
Creciente nmero de cores en los
ordenadores.
Ejemplos
Clculo secuencial, concurrente (1 CPU) y
paralelo (2 CPU) del nmero PI.
package main
PI secuencial
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)
}
package main
PI Concurrente
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)
}
package main
PI Paralelo
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)
}
Tiempos ejecucin
3.1416026534897203
Real
0m0.032s
User
0m0.027s
Sys
0m0.002s
Tiempos ejecucin
3.1416026534897203
Real
0m0.032s
0m0.689s
User
0m0.027s
0m0.333s
Sys
0m0.002s
0m0.347s
Tiempos ejecucin
3.1416026534897203
Real
0m0.032s
0m0.689s
0m1.497s
User
0m0.027s
0m0.333s
0m0.493s
Sys
0m0.002s
0m0.347s
0m1.210s
package main
Vector secuencial
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)
}
Vector Concurrente
import (
"fmt"
"container/vector"
"runtime"
)
Vector Paralelo
import (
"fmt"
"container/vector"
"runtime"
)
Tiempos ejecucin
887459712
Real
0m3.694s
User
0m3.031s
Sys
0m0.626s
Tiempos ejecucin
887459712
Real
0m3.694s
0m3.725s
User
0m3.031s
0m3.068s
Sys
0m0.626s
0m0.632s
Tiempos ejecucin
887459712
Real
0m3.694s
0m3.725s
0m3.179s
User
0m3.031s
0m3.068s
0m3.025s
Sys
0m0.626s
0m0.632s
0m0.638s
Bibliografa
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