parallel extensions a glimpse into the parallel universe by eric de carufel microsoft.net solution...
TRANSCRIPT
Parallel Extensions
A glimpse into the parallel universe
By Eric De CarufelMicrosoft .NET Solution Architect at [email protected]@decarufel.nethttp://blog.decarufel.net
Agenda
• Introduction• Overview• Library Core• TPL (Task Parallel Library)• Parallel Linq (PLINQ)• Parallel Data Structures• Questions
Introduction
• Why do we have to bother? Moore’s law is over, no more free lunch Multi cores systems will be more and more available
• Type of Parallelism Asynchronous operation (better user experience) Data parallelism Task parallelism
• Options Manual treading
Thread, ThreadPool, BackgroundWorkerThread Asynchronous calls Event driven
• Problems Resource sharing Locking Non-deterministic sequence of execution Hard to debug
Overview
Task Parallel Library (TPL)
• Lightweight task framework (Task) Create(Action<T>) factory method Wait, WaitAll, WaitAny to catch exception ContinueWith to chain Tasks together
• Lazy function call Future<T>
• Task scheduler and manager TaskManager
Parallel API
• Parallel Loops Parallel.For Parallel.ForEach
• Lazy Initialisation LazyInit<T>
• Locking SpinWait SpinLock
• CountdownEvent
Parallel API
• Standard for loop for (int i = 0; i < N; i++)
{ a[i] = Compute(i);}
• Parallel for loop Parallel.For(0, N, i =>
{ a[i] = Compute(i);});
Parallel Linq (PLINQ)
• Parallel Query AsParallel()
• Return to sequential execution AsSequential()
• Preserve order AsOrdered()
• Order doesn’t matter AsUnordered()
Parallel Linq (PLINQ)
• var query = from c in Customers
where c.Name = “Smith” select c;
• var query = from c in Customers.AsParallel()
where c.Name = “Smith” select c;
Parallel Data Structures
• IConcurrentCollection Add(T item) Remove(out T item)
• ConcurrentStack Push(T item) TryPop(out T item)
• ConcurrentQueue Enqueue(T item) TryDequeue(out T item)
• BlockingCollection Add(T item), Remove(out T item) TryAdd(T item), TryRemove(out T item)
CLR Thread Pool: Work-Stealing
Worker Thread 1
Worker Thread p
Program Thread
Program Thread
User Mode Scheduler For Tasks
GlobalQueue
LocalQueue
LocalQueue
Task 1Task 1Task 2Task 2
Task 3Task 3Task 5Task 5Task 4Task 4
Task 6Task 6
What’s next
• Visual Studio 2010• .NET Framework 4.0• New multi cores computer (4, 16, 32, 64, …)
• Think parallel!