An overview of… Luis Guerrero Plain Concepts

41
Concurrent programming and MEF An overview of… Luis Guerrero Plain Concepts http://www.luisguerrero.net http://geeks.ms/blogs/luisguerrero/

Transcript of An overview of… Luis Guerrero Plain Concepts

Page 1: An overview of… Luis Guerrero Plain Concepts

Concurrent programming and MEF

An overview of…

Luis GuerreroPlain Conceptshttp://www.luisguerrero.nethttp://geeks.ms/blogs/luisguerrero/

Page 2: An overview of… Luis Guerrero Plain Concepts

Parallel Programing in the .NET Framework

Page 3: An overview of… Luis Guerrero Plain Concepts

Why?

Page 4: An overview of… Luis Guerrero Plain Concepts

Searching for Parallelism

Finding exploitable parallelism Organizing by Tasks Organizing by Data Organizing by Ordering

Page 5: An overview of… Luis Guerrero Plain Concepts

Organizing by Tasks

Linear or recursive? Task parallel Divide and conquer

Enough task? Too many – thrashing Too few – under utilization

Dependencies between tasks Scheduling work to tasks

{}

{}

{}

{}

Page 6: An overview of… Luis Guerrero Plain Concepts

Organizing by Tasks

Work per task? Small workloads Variable workloads

Dependencies between tasks? Removable Separable Read only or read/write

{}

{}

{}

{}

Page 7: An overview of… Luis Guerrero Plain Concepts

Organizing by Data

Linear or recursive? Geometric decomposition Recursive data

Data “chunk” size? Too big – under utilization Too small – trashing

Chunk layout? Cache and cache line size False cache sharing

{}

{}

{}

Page 8: An overview of… Luis Guerrero Plain Concepts

Patterns for Parallelism

Implementation Patterns Fork / Join Loop Parallel Divide and Conquer Producer / Consumer Pipeline Asynchronous Agents

Page 9: An overview of… Luis Guerrero Plain Concepts

What’s about this talk?

Task Parallel Library Parallel LINQ (PLINQ) Data Structures for Parallel

Programming Parallel Diagnostic Tools

Page 10: An overview of… Luis Guerrero Plain Concepts

Task Parallel Library - Overview

Data parallelism: The Parallel Class Parallel

For For<> Foreach Invoke

Page 11: An overview of… Luis Guerrero Plain Concepts

demoParallel class

Page 12: An overview of… Luis Guerrero Plain Concepts

Task – Overview

Task is the minimum unit of work in TPL

Can be scheduled by request 2 Scheduled inside .NET box, ThreadPool

y UI Can have a result Is observable Can be continue with another task Can be securely cancelled Someone can wait for a completion

Page 13: An overview of… Luis Guerrero Plain Concepts

demoCreating Tasks

Page 14: An overview of… Luis Guerrero Plain Concepts

demoTask continuation

Page 15: An overview of… Luis Guerrero Plain Concepts

demoExceptions and cancellation support

Page 16: An overview of… Luis Guerrero Plain Concepts

New System.Threading Primitives

A Barrier is a synchronization primitive that enforces the stopping of execution between a number of threads or processes at a given point and prevents further execution until all threads or processors have reached the given point.

A CountdownEvent is a synchronization primitive that enables ongoing tracking of a given workload in order to determine if processing of that workload is finished or not.

Page 17: An overview of… Luis Guerrero Plain Concepts

demoBarrier y CountdownEvent

Page 18: An overview of… Luis Guerrero Plain Concepts

demoPLINQ

Page 19: An overview of… Luis Guerrero Plain Concepts

Managed Extensibility Framework (MEF)

An introduction to….

Page 20: An overview of… Luis Guerrero Plain Concepts

Managed Extensibility Framework

The Managed Extensibility Framework (MEF) is a new library in .NET 4 for building applications that can be incrementally extended.

For customers For you and your team Always there, always ready

Page 21: An overview of… Luis Guerrero Plain Concepts

Managed Extensibility Framework?

The Managed Extensibility Framework (MEF) is a new library in the .NET Framework that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed

Page 22: An overview of… Luis Guerrero Plain Concepts

What is “Extensibility”?

In software engineering, extensibility is a systemic measure of the ability to extend a system and the level of effort required to implement the extension.

Page 23: An overview of… Luis Guerrero Plain Concepts

Open/Closed Principle

Software entities should be open for extension, but closed for modification.

Page 24: An overview of… Luis Guerrero Plain Concepts

Known Unknown

VS.

Page 25: An overview of… Luis Guerrero Plain Concepts

MEF Basics…

An Application is built of parts.

Page 26: An overview of… Luis Guerrero Plain Concepts

MEF Basics…

Export it.

Import it.

Compose it.

Page 27: An overview of… Luis Guerrero Plain Concepts

Export it.

Widget1

[Export(typeof(UserControl))]public class Widget1 : UserControl{

public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Export

UserControl

Page 28: An overview of… Luis Guerrero Plain Concepts

Import it.

Widget1

[Export(typeof(UserControl))]public class Widget1 : UserControl{

[Import]public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Import

String

Page 29: An overview of… Luis Guerrero Plain Concepts

Import it.

Widget1

[Export(typeof(UserControl))]public class Widget1 : UserControl{

[Import(“HelloMEF.Message”)]public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Import

“HelloMEF.Message”

Page 30: An overview of… Luis Guerrero Plain Concepts

Import it.

MainPage

[Export(typeof(UserControl))]public class MainPage: UserControl{

[ImportMany(typeof(UserControl))]public IEnumerable<UserControl> { get;set;}

}

ImportMany

UserControl

Page 31: An overview of… Luis Guerrero Plain Concepts

Compose it.PartIntializer: “Compose

yourself”

MainPage Compose

public MainPage() { InitializeComponent(); PartInitializer.SatisfyImports(this); }

Page 32: An overview of… Luis Guerrero Plain Concepts

Widget

Widget

Where does the widget

go?

Page 33: An overview of… Luis Guerrero Plain Concepts

Export it - Metadata

Widget1

[ExportMetadata(“Location”,Location.Top)][Export(typeof(UserControl))]public class Widget1 : UserControl{

public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Export

UserControl

Put me in the top

Page 34: An overview of… Luis Guerrero Plain Concepts

Import it - Metadata

MainPage

[Export(typeof(UserControl))]public class MainPage: UserControl{

[ImportMany(typeof(UserControl))]public IEnumerable<Lazy<UserControl, IWidgetMetadata> { get;set;}

}

ImportMany

UserControl

Page 35: An overview of… Luis Guerrero Plain Concepts

Export it - Metadata

Widget1

[ExportMetadata(“Location”,Location.Top)][Export(typeof(UserControl))]public class Widget1 : UserControl{

public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Export

UserControl

Put me in the top

Page 36: An overview of… Luis Guerrero Plain Concepts

Customize it – Custom exports

Widget1

[Widget(Location=Location.Top)]public class Widget1 : UserControl{

public string Message { get{return(string) Button.Content;}

set{Button.Content=value;} }}

Export

UserControl

Put me in the top

Page 37: An overview of… Luis Guerrero Plain Concepts

Container is the Match Maker

Container

Page 38: An overview of… Luis Guerrero Plain Concepts

Catalogs provide PartsContainer

Catalog

Page 39: An overview of… Luis Guerrero Plain Concepts

Catalogs provide PartsContainer

Catalog

TypeCatalog

AssemblyCatalog

DirectoryCatalog

AggregatingCatalog

Page 40: An overview of… Luis Guerrero Plain Concepts

Parts can be lazy…

Part B

Export<A>

[Import(typeof(ILogger))]public ILogger Export<ILogger> Logger { get; set; }

Page 41: An overview of… Luis Guerrero Plain Concepts

Lifetime

ContainerContainer

Part A Part B Part B Part B

[CompositionOptions(CreationPolicy=CreationPolicy.NonShared)][CompositionOptions(CreationPolicy=CreationPolicy.Shared)]

Shared Non-Shared

On Export

[Import(RequiredCreationPolicy=CreationPolicy.NonShared)][Import(RequiredCreationPolicy=CreationPolicy.Shared)]

On Import