parallel extensions in .net 4.0

25
Parallel Extensions in .Net 4.0 Dmytro Maleev for Lviv .Net User Group

Upload: dima-maleev

Post on 10-May-2015

2.299 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Parallel extensions in .Net 4.0

Parallel Extensions in .Net 4.0

Dmytro Maleev for Lviv .Net User Group

Page 2: Parallel extensions in .Net 4.0

Agenda

Куда катится мир? Hello, Parallel Extensions! Parallel vs Multithreading Parallel Loops Tasks PLINQ CDS & Thread Enchantments References Q&A

Page 3: Parallel extensions in .Net 4.0

Куда катится мир

1998 Intel 80486

80Mhz1 Core

2000Celeron 400Mhz

1 Core

2003Athlon XP 1.6+GHz

1 Core

2007Core 2 Duo 2.13 GHz

2 Core

2010Intel i5 3.3GHz

2 Core 4 Threads

?

Forget about CPU frequency increase!

There are a lot of issues with this, which will be

fixed in future!

Page 4: Parallel extensions in .Net 4.0

Куда катится мир

For now…ENLARGE YOUR…

CPU CORE COUNT!PENPEN

Page 5: Parallel extensions in .Net 4.0

Hello, Parallel Extensions!

Parallel Extensions, previously known as the Parallel Framework Extensions or PFX, is a managed concurrency library being developed by a collaboration between Microsoft Research and the CLR team at Microsoft. It is composed of two parts: Parallel LINQ (PLINQ) andTask Parallel Library (TPL).It also consists of a set of coordination data structures (CDS) – sets of data structures used to synchronize and co-ordinate the execution of concurrent tasks.The library was released as a CTP on November 29, 2007 and refreshed again in December 2007 and June 2008. Microsoft has announced that the Parallel Extensions to .NET will release as part of the .NET 4.0 Framework release.

©Wikipedia

Page 6: Parallel extensions in .Net 4.0

Hello, Parallel Extensions!

Visual Studio Debugging and profiling support

Task Scheduler

PLINQ

Task Parallel Library

CDS

Parallel Extensions

Page 7: Parallel extensions in .Net 4.0

Parallel vs Multithreading

Multithreaded!=parallelization

“ If on a single core machine you are using threads and it makes perfect sense for your scenario, then you are not "doing parallelism", you are just doing multithreading”

”On a single core you can use threads and you can have concurrency, but to achieve

parallelism on a multi-core box you have to identify in your code the exploitable

concurrency: the portions of your code that can truly run at the same time.”

Daniel Moth

Page 8: Parallel extensions in .Net 4.0

Parallel vs Multithreading

Page 9: Parallel extensions in .Net 4.0

Dark Side of Paprallelization and Multithreading

1. Race conditionshttp://en.wikipedia.org/wiki/Race_condition

2. Deadlockshttp://en.wikipedia.org/wiki/Deadlockhttp://en.wikipedia.org/wiki/Dining_philosophers_problem

3. Thread starvation4. Difficult to code and debug5. Environmental

Page 10: Parallel extensions in .Net 4.0

Народная мудрость!

Page 11: Parallel extensions in .Net 4.0

Parallel Loops

Parallel.For() Parallel.For(0, 100, i => { Console.WriteLine("This is I-I-I-I-I-I: {0}", i); });

Parallel.ForEach()Parallel.ForEach(ThisIsCollection, collectionItem =>

{collectionItem.Hello();

});

Parallel Options Parallel.Invoke()

Page 12: Parallel extensions in .Net 4.0

Parallel Loops. Use Force wisely!

Parallelization Can Hurt Performance

http://msdn.microsoft.com/en-us/library/dd560853(VS.100).aspxhttp://en.wikipedia.org/wiki/Context_switch

Page 13: Parallel extensions in .Net 4.0

DEMO

AntiSocial Robots

Page 14: Parallel extensions in .Net 4.0

Task - Task Scheduler

Task is a new class that represents the work you want completed. There are methods to create, schedule, and synchronize tasks in your application.

Tasks are controlled by task scheduler. Tasks scheduler works with thread pool.

Page 15: Parallel extensions in .Net 4.0

Task In Deep

Tasks can: Task can be created Task can wait! Task can simply Wait(), WaitAll() or

WaitAny(). Task knows when it is completed ( IsCompleted

property) Task can ContinueWith() Task can return value Task has Options and Status

Page 16: Parallel extensions in .Net 4.0

Demo

Strassen algorithm

Page 17: Parallel extensions in .Net 4.0

PLINQ

PLINQ is just parallelized version of LINQ

Not parallelized: LINQ-to-SQL & LINQ-to-Entity.

LINQ:var query = from s in someCollectionlet result = CoolService.CallService(s)select result;

PLINQ:var query = from s in someCollection.AsParallel()let result = CoolService.CallService(s)select result;

Page 18: Parallel extensions in .Net 4.0

PLINQ. How it works?

Page 19: Parallel extensions in .Net 4.0

PLINQ. Overview

Ordering Results AsOrdered()

ForAll Operator() AsSequential() WithMergeOptions Parallel Performance Analyzer. Just

for rich

Page 20: Parallel extensions in .Net 4.0

DEMO

Baby Names

Page 21: Parallel extensions in .Net 4.0

CDS & Thread Enchantments

Thread Enchantments: Thread.Yield() Monitor.Enter()

Concurrent Collections ConcurrentStack (LIFO) ConcurrentQueue (FIFO) ConcurrentDictionary ConcurrentBag BlockingCollection

Page 22: Parallel extensions in .Net 4.0

Synchronization Primitives

Barrier“Let’s meet near monument and then go

to have a beer” Cancellation Tokens CountDownEvent ManualResetEventSlim and

SemaphoreSlim SpinLock ThreadLocal<T>

Page 23: Parallel extensions in .Net 4.0

References

Parallel Programming with .NEThttp://blogs.msdn.com/b/pfxteam/

Wikihttp://

en.wikipedia.org/wiki/Parallel_Extensions Introducing .NET 4.0

http://www.amazon.com/Introducing-NET-4-0-Visual-Experts/dp/143022455X

Lviv .Net User Grouphttp://dotnetug-lviv.blogspot.com/

Page 24: Parallel extensions in .Net 4.0

Q&A

?

Page 25: Parallel extensions in .Net 4.0

If you still have a questions

Mail me:[email protected]

Skype me:hmmidma

Twitter:dimko1