Está en la página 1de 28

Computacin paralela en plataforma

Windows

Luis Guerrero
lguerrero@plainconce
pts.com
http://geeks.ms/blogs/l
uisguerrero/

http://bit.ly/TPLCodeMotion

Agenda
El porqu de la computacin paralela.
Buscando paralelismo en nuestras
aplicaciones.
Plataforma de computacin paralela en Visual
Studio 2010 y Windows 8.
Soporte de depuradores y profilers.
Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Computacin paralela

POR QU?

Cada ao hay procesadores ms rpidos


con ms ncleos
Transitors
Clock Speed
(MHz)
Power (W)
Perf/Clock (ILP)

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Para cuantos cores se est


programando?

4000

4000

2007

2008

8000

8000

2009

2010

16000

16000

2011

2012

32000

32000

2013

2014

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Qu nivel de concurrencia necesita tu software


para explotar sus capacidades?

S
Socket

C/
S

Cores / Socket

T/
C

App
Concurrency

Hardware
Threads / Core

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Computacin paralela

BUSCANDO
PARALELISMO EN
NUESTRAS

Buscando paralelismo
Buscando paralelismo en nuestras
aplicaciones a travs de:
Organizacin por tareas
Organizacin por datos
Organizacin por orden

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Organizacin por Tareas


Linear o recursivo?

Tareas paralelas

Divide y vencers

{}

Lmite en las tareas?

Demasiadas? Sobrecarga del programador de tareas.

Pocas? Infrautilizacin.

Dependencias entre las tareas


Planificar trabajo como tareas.
Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

{}
{}
{}

Organizacin por datos


Linear o recursivo?
Descomposicin geomtrica
Datos recursivos

Tamao del bloque de datos?


Demasiado grande infrautilizacin
Demasiado pequeo Sobrecarga

Organizacin de los bloques


Tamao de la cach de datos
Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

{}
{}
{}

Patrones
Patrones de cdigo

Fork / Join
Loop Parallel
Divide y vencers
Productor / Consumidor
Pipeline
Asynchronous Agents
Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Pattern: Fork / Join


Partition

Execute

Merge

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Presentamos

TASK PARALLEL LIBRARY

Task
Es la nueva unidad mnima de ejecucin en
TPL, ya no se utiliza Thread.
Las tareas (Task) envuelven la ejecucin del
cdigo, para su ejecucin concurrente.

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Creacin de Task
Task task = new Task(() = > { });
Task< D ataItem > taskW ithResult = new
Task< D ataItem > (() = > { return new
D ataItem (); });
Esto slo define el objeto Task, no ejecuta el
cdigo.

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Creacin de Task II
Task.Factory.StartN ew (() = > { });
Task< int> .Factory.StartN ew (() = >
{ return 42; });
Este cdigo define y pone a ejecutar el objeto
Task.

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Trabajando con tareas

task.Start();
task.RunSynchronously();
task.Status = = TaskStatus.Running;
task.IsCom pleted;
task.IsFaulted;

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Enlazando tareas
Task task = new Task(() = > { });
task.ContinueW ith((previousTask) = > { });

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Soporte de cancelacin de tareas


La cancelacin ha sido uno de los problemas
tradicionales de la programacin concurrente.
No se puede interrumpir la ejecucin en
cualquier momento, puede ocasionar
bloqueos inesperados.
Se utiliza la clase CancellationToken.

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Uso de bloqueos
ReaderW riterLockSlim rw ls = new ReaderW riterLockSlim ();
rw ls.EnterReadLock();
try
{
// tu cdigo aqu
}
fi
n ally
{
rw ls.ExitReadLock();
}
Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

PLINQ

PARALLEL LINQ

PLINQ
var q = from p in people
.AsParallel()
var q = from p in people
where p.Name == queryInfo.Name &&
where p.Name == queryInfo.Name &&
p.State == queryInfo.State
p.State == queryInfo.State
&&
&&
p.Year >= yearStart &&
p.Year >= yearStart &&
p.Year <= yearEnd
p.Year <= yearEnd
orderby p.Year ascending
orderby p.Year ascending
select p;
select p;

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Ms opciones de PLINQ
list.AsParallel().W ithCancellation(new CancellationToken(false));
list.AsParallel().W ithD egreeO fParallelism (Environm ent.ProcessorCount);
list.AsParallel().W ithExecutionM ode(ParallelExecutionM ode.ForceParallelism );
list.AsParallel().W ithM ergeO ptions(ParallelM ergeO ptions.AutoBuff
ered);

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

Windows 8 Metro apps

ASYNC & AWAIT

Async & await


Async permite especificar que un mtodo es
de ejecucin asncrona. Un mtodo asncrono
siempre devolver un Task o Task<TResult>.
Await especifica que el flujo de ejecucin debe
esperar a ese mtodo asncrono.

Luis Guerrero
lguerrero@plainconcepts.com Plain Concepts

PROFILER &
DEBUGGER

Gracias

También podría gustarte