Parallel extensions in .Net 4.0

  • 2,224 views
Uploaded on

 

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,224
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
67
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Parallel Extensions in .Net 4.0
    Dmytro Maleev for Lviv.Net User Group
  • 2. Agenda
    Куда катится мир?
    Hello, Parallel Extensions!
    Parallel vs Multithreading
    Parallel Loops
    Tasks
    PLINQ
    CDS & Thread Enchantments
    References
    Q&A
  • 3. Куда катится мир
    Forget about CPU frequency increase! There are a lot of issues with this, which will be fixed in future!
    1998
    Intel 80486
    80Mhz
    1 Core
    2007
    Core 2 Duo 2.13 GHz
    2 Core
    2000
    Celeron 400Mhz
    1 Core
    2010
    Intel i5 3.3GHz
    2 Core 4 Threads
    2003
    Athlon XP 1.6+GHz
    1 Core
    ?
  • 4. Куда катится мир
    For now…ENLARGE YOUR…
    CPU CORE COUNT!
    PEN
    PEN
  • 5. 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.Thelibrary was released as a CTP on November 29, 2007and 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
  • 6. Hello, Parallel Extensions!
    Visual Studio Debugging and profiling support
    Parallel Extensions
    PLINQ
    Task Parallel Library
    Task Scheduler
    CDS
  • 7. 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
  • 8. Parallel vs Multithreading
  • 9. Dark Side of Paprallelization and Multithreading
    Race conditions
    http://en.wikipedia.org/wiki/Race_condition
    2. Deadlocks
    http://en.wikipedia.org/wiki/Deadlock
    http://en.wikipedia.org/wiki/Dining_philosophers_problem
    Thread starvation
    Difficult to code and debug
    Environmental
  • 10. Народная мудрость!
  • 11. 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()
  • 12. Parallel Loops. Use Force wisely!
    Parallelization Can Hurt Performance
    http://msdn.microsoft.com/en-us/library/dd560853(VS.100).aspx
    http://en.wikipedia.org/wiki/Context_switch
  • 13. DEMO
    AntiSocialRobots
  • 14. 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.
  • 15. 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
  • 16. Demo
    Strassen algorithm
  • 17. PLINQ
    PLINQ is just parallelized version of LINQ
    Not parallelized: LINQ-to-SQL & LINQ-to-Entity.
    LINQ:
    var query = from s in someCollection
    let result = CoolService.CallService(s)
    select result;
    PLINQ:
    varquery = from s in someCollection.AsParallel()
    let result = CoolService.CallService(s)
    select result;
  • 18. PLINQ. How it works?
  • 19. PLINQ. Overview
    Ordering Results
    AsOrdered()
    ForAll Operator()
    AsSequential()
    WithMergeOptions
    Parallel Performance Analyzer. Just for rich
  • 20. DEMO
    Baby Names
  • 21. CDS & Thread Enchantments
    Thread Enchantments:
    Thread.Yield()
    Monitor.Enter()
    Concurrent Collections
    ConcurrentStack (LIFO)
    ConcurrentQueue (FIFO)
    ConcurrentDictionary
    ConcurrentBag
    BlockingCollection
  • 22. Synchronization Primitives
    Barrier
    “Let’s meet near monument and then go to have a beer”
    Cancellation Tokens
    CountDownEvent
    ManualResetEventSlim and SemaphoreSlim
    SpinLock
    ThreadLocal<T>
  • 23. References
    Parallel Programming with .NET
    http://blogs.msdn.com/b/pfxteam/
    Wiki
    http://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 Group
    http://dotnetug-lviv.blogspot.com/
  • 24. Q&A
    ?
  • 25. If you still have a questions
    Mail me:
    diwingless@gmail.com
    Skype me:
    hmmidma
    Twitter:
    dimko1