Multi core programming 1

844 views
742 views

Published on

TPL, Parallel Extensions

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
844
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Multi core programming 1

  1. 1. MultiCore Programming 1Presented by:Robin Aggarwal
  2. 2. Agenda• Parallel Extensions• TPL• Task• Parallel.For & Parallel.ForEach• Supporting Demos• DosDonts
  3. 3. .NETFramework Parallel LINQ Task Parallel Library4.0.NETFramework LINQ Entity Framework3.5.NET Windows Windows WindowsFramework Windows Presentation Communication Workflow Card Space3.0 Foundation Foundation Foundation WinForms ASP.NET ADO.NET.NETFramework Base Class Library2.0 Common Language Runtime .Net Framework Stack
  4. 4. Task ParallelLibrary(TPL) Structured Data Parallelism Features Parallel PLINQ Class Lazy Task Concurrent Synchronization Initialization Parallelism Collections Primitives Classes CLR Thread Pool ThreadsArchitecture for Parallel Programming in .net framework 4.0
  5. 5. Parallel Extensions• To the core libraries of Microsoft .Net Framework, set of APIs added collectively known Parallel Extensions.• Makes it easy for developers to write programs for multicore processors by taking off the burden of dealing explicitly with complexities of threads and locks.• Task Parallelism - Tasks• Data Parallelism - Parallel.For(), Parallel.Foreach()
  6. 6. Tasks• Task is unit of Async operation.• Benefits – Scalable & efficient use of system resources. – More programmatic control is possible with “Tasks” than with a thread or work item. Eg. TPL1• Features- Task waiting, Task cancellation, Task continutions, Exception Handling etc.• Task.wait()• task.ContinueWith(() => { Console.WriteLine("Computation completed"); });
  7. 7. • var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; Task task1 = Task.Factory.StartNew( () => { ... }, token); tokenSource.Cancel();• Task<T> types to represent asynchronously computed values. Eg. TaskResult
  8. 8. Donts• AVOID creating threads directly, except if you need direct control over the lifetime of the thread.• AVOID accessing loop iteration variables from the task body. More often than not, this will not do what youd expect. Eg. LoopVariables• AVOID waiting on tasks while holding a lock. Waiting on a task while holding a lock can lead to a deadlock if the task itself attempts to take the same lock.
  9. 9. DOs• DO use tasks instead of ThreadPool work items.• DO take advantage of Task capabilities instead of implementing similar functionality yourself.• CONSIDER wrapping asynchronous method calls with tasks. An asynchronous method call can be converted to a task by using the Task.Factory.FromAsync method• DO use a parallel loop instead of constructing many tasks in a loop. A parallel loop over N elements is typically cheaper than starting N independent tasks.
  10. 10. Parallel.For and Parallel.ForEach• The parallel looping constructs Parallel.For and Parallel.ForEach are conceptually similar to for and foreach loops, except that they use multiple threads to execute different iterations of the loop body.
  11. 11. DOs• DO use parallel loops Parallel.For and Parallel.ForEach to speed up operations where an expensive, independent operation needs to be performed for each input in a sequence.• DO make sure that the loop body delegate is thread-safe, since it will be called from multiple threads concurrently.• DO verify that the loop body delegate does not make assumptions about the order in which loop iterations will execute. For example, there is no guarantee that a thread will process its partition of input elements in the order in which they appear in the input, even though in the current version it will.• CONSIDER increasing the work done by each iteration in a parallel loop if it is very low. The body of a parallel loop is a delegate, and invoking it incurs some overhead. If the work done by the loop body is very small, the delegate invocation overhead may dominate the running time.
  12. 12. • Parallel.For(0, arr.Length, i =>• { arr[i] = (int)Math.Sqrt(arr[i]);• });• Parallel.ForEach( Partitioner.Create(0, arr.Length, 1024), range => { for (int i = range.Item1; i < range.Item2; i++) { arr[i] = (int)Math.Sqrt(arr[i]); } });
  13. 13. PLINQ• Parallel LINQ• PLINQ executes LINQ queries, but distributes the evaluation of the user delegates over multiple threads.• DO use PLINQ to express computations with an expensive operation applied over a sequence.• BE AWARE that by default, PLINQ does not preserve ordering of elements in a query.
  14. 14. • var q = Enumerable.Range(0, 100) .AsParallel() .Select(x => -x);foreach(var x in q) Console.WriteLine(x);BE AWARE that by default, PLINQ uses static partitioningon arrays and other collections that implement the IList<>interface. That means that the array will be statically splitinto as many partitions as there are cores on themachine. However, if the work distribution varies in thearray, static partitioning may lead to load imbalance.
  15. 15. Correctness• Thread Safety- DO make sure that if an object is accessed from multiple threads, all methods called on the object are thread-safe, or otherwise correctly protected by locks.
  16. 16. Concurrent Collections• ConcurrentQueue• ConcurrentStack• ConcurrentDictionary• Blocking Collection
  17. 17. Coordination Primitives• Lazy• ManualResetEventSlim• SemaphoreSlim

×