80 likes | 145 Views
`Parallel programming’ efficiently use all the cores and execute faster.Understanding Parallel Programming<br>
E N D
Introduction 1.Hardware industry rapidly shifting towards multiple-processor core technology. 2.Having multiple core in machine cannot expect sequential program to run faster in machine. 3.For that purpose `Parallel programming’ is required which can efficiently use all the cores and execute faster.
Feature Areas 1.Task Parallel Library(TPL) 2.Parallel LINQ
Task Parallel Library - The easiest way to use parallel library is to use the same in parallel loops. - If you have a loop where every iteration is doing something expensive and loop iterations are independent. - Then Launch different iterations in parallel. The below block shows the sequential - It can be paralleled by using TPL by following code.
Parallel LINQ - Parallel LINQ(PLINQ) is a query execution engine. - It accepts only LINQ-to-Objects or LINQ-to-XML query. - It automatically utilizes multiple processor or cores for execution when they are available. - With PLINQ you can transform your LINQ queries to parallel version by using the As Parallel extension method as shown below.
-Take ray-tracer algorithm sample to render image with both sequential and parallel programming.- Compare the execution time in both scenarios.- Img1 shows image rendering through sequential program and the bottom right of img1 shows the time taken to execute. Img1: Rendering Image through Sequential Program Img2: Rendering Image through Parallel Program
- It is clear from the above sample images that parallel execution is 1.5 seconds faster than the sequential rendering.- Lets see how the processor core has been consumed in both scenarios using performance monitor tool. In the above Parallel programming Graph we can see all the core of the processor are equally utilized. In the sequential programming graph we can observe only processor core one, highlighted with red mark, has been utilizing more and rest are least utilized.
Conclusion There are many ways to create a parallel executing program in C#. With multi-core processor there’s no excuse for creating a single threaded programs and penalize your users with unneeded delays.