410 likes | 480 Views
An overview of…. Concurrent programming and MEF. Luis Guerrero Plain Concepts http://www.luisguerrero.net http://geeks.ms/blogs/luisguerrero/. Parallel Programing in the .NET Framework. Why?. Searching for Parallelism. Finding exploitable parallelism Organizing by Tasks
E N D
An overview of… Concurrent programming and MEF Luis Guerrero Plain Concepts http://www.luisguerrero.net http://geeks.ms/blogs/luisguerrero/
Searching for Parallelism • Finding exploitable parallelism • Organizing by Tasks • Organizing by Data • Organizing by Ordering
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
Organizing by Tasks • Work per task? • Small workloads • Variable workloads • Dependencies between tasks? • Removable • Separable • Read only or read/write
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
Patterns for Parallelism • Implementation Patterns • Fork / Join • Loop Parallel • Divide and Conquer • Producer / Consumer • Pipeline • Asynchronous Agents
What’s about this talk? • Task Parallel Library • Parallel LINQ (PLINQ) • Data Structures for Parallel Programming • Parallel Diagnostic Tools
Task Parallel Library - Overview • Data parallelism: The Parallel Class • Parallel • For • For<> • Foreach • Invoke
Parallel class demo
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
Creating Tasks demo
Task continuation demo
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.
PLINQ demo
An introduction to…. Managed Extensibility Framework (MEF)
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
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
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.
Open/Closed Principle Software entities should be open for extension, but closed for modification.
VS. Known Unknown
MEF Basics… • An Application is built of parts.
MEF Basics… • Export it. • Import it. • Compose it.
Export it. [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 UserControl Export
Import it. [Export(typeof(UserControl))] public class Widget1 : UserControl { [Import] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 String Import
Import it. [Export(typeof(UserControl))] public class Widget1 : UserControl { [Import(“HelloMEF.Message”)] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 “HelloMEF.Message” Import
Import it. [Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<UserControl> { get;set; } } Main Page UserControl ImportMany
Compose it. PartIntializer: “Compose yourself” public MainPage() { InitializeComponent(); PartInitializer.SatisfyImports(this); } MainPage Compose
Where does the widget go? Widget Widget
Export it - Metadata [ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
Import it - Metadata [Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<Lazy<UserControl, IWidgetMetadata> { get;set; } } Main Page UserControl ImportMany
Export it - Metadata [ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
Customize it – Custom exports [Widget(Location=Location.Top)] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
Container is the Match Maker Container
Catalogs provide Parts Container Catalog
Catalogs provide Parts TypeCatalog Container AssemblyCatalog DirectoryCatalog Catalog AggregatingCatalog
Parts can be lazy… [Import(typeof(ILogger))] public ILoggerExport<ILogger> Logger { get; set; } Part B Export <A>
Lifetime On Export [CompositionOptions(CreationPolicy=CreationPolicy.NonShared)] [CompositionOptions(CreationPolicy=CreationPolicy.Shared)] On Import [Import(RequiredCreationPolicy=CreationPolicy.NonShared)] [Import(RequiredCreationPolicy=CreationPolicy.Shared)] Shared Non-Shared Container Container Part A Part B Part B Part B