« VS 2010 and Silverlight 4 Release | Main | Better surfing with Win7 »

.NET 4.0 Parallel Programming

Most of the machines today are multi core whereas the software programs that run on them are not designed to make use of multi core architecture and hence even though the processor is multi core, we don't see sizable improvements in application performance. The key to improve performance is to distribute work amongst multiple cores, writing programming logic to achieve this is complex. 

 

Programs written till date primarily targets single core CPUs and have no built in intelligence to benefit from multi core processors. At the same time, multi core CPUs is becoming the order of the day but until late .NET 3.5 framework languages have not found a way to exploit and benefit from the multi core CPU environments. Not only this, writing thread safe parallel programs using thread pool and delegates has been a challenging task for developer.

 

.NET 4.0 parallel API is a managed programming model for data parallelism, task parallelism, scheduling, and coordination on parallel hardware. It makes it easier for developers to write programs that take advantage of parallel hardware (multi core processors), without having to explicitly deal with the complexities of threads and locks.

 

With .NET 4.0 parallel APIs program logic can be written to execute in parallel by assigning it to different processor cores making the program execution time as a function of cores giving tremendous improvement in performance- throughput and response times as appropriate. With dual core CPUs we have seen it gives as much as twice the improvement in performance.

 

With parallel APIs, it is very easy to write new parallel programs and also to adapt existing C# program to parallel APIs. At a very broad level, it is as simple as "for loop" needs to be changed to "Parallel.For( )"

 

The parallel library contains sophisticated algorithms for dynamic work distribution and automatically adapts to the workload and particular machine. With parallel API the program will run on a multi core machine and allocate work to the cores depending on their availability, in absence of multi cores the same program can run on a single core machine without any changes.

 

The two breeds of logic which can be executed in parallel are application program logic and database logic.

 

To execute following sequential application program logic in parallel, one can use Task Parallel library (TPL). Parallel is static class in TPL.

 

for (int i = 0; i < 10000; i++)

{

  a[i] = a[i]*a[i];

}          

 

Parallel.For(0, 10000, delegate(int i)

{

  a[i] = a[i]*a[i];

};

 

For the above Parall.For loop, for a quad proc machine, it is highly likely that the each core will get approximately 2500 counts to process in parallel. The output from 4 cores can not necessarily come in the same order as allocated, hence while identifying the parallel logic, it is important that these cores are not interdependent or modify each other's data.

 

Parallelizing database logic can be done using PLINQ and is a query execution engine that accepts any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple processors or cores for execution when they are available.

 

With parallel framework although the threads and delegates have been abstracted in to a unit of work called Task. If needed, the developer can go and still use threads and delegates to work at lower level of granularity.

 

The parallel extension API was available as a separate downloadable in .NET 3.5 with very limited Visual Studio debugging support. With release of .NET 4.0 it will be part of .NET framework and not a separate download. Microsoft Visual Studio 2010 provides excellent debugging and profiling support for parallel programs. We strongly feel .NET 4.0 parallel APIs will become one of the key reasons for enterprises to adopt .NET 4.0 for planning their new applications or re-engineer/migrate the existing legacy applications.

 

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/apps/mt-tb.cgi/3128

Comments

Hi,

About the performance, You had mentioned that it gives twice the performance when you have dual core, but in reality it is not a linear scale, when it comes to parallelism....

Regards,
-Vinayak

As you have rightly said, the performance doesn't increase (infinitely) linearly with every increase in CPU. However it does (in the beginning) if the right parallelization logic is identified like the use case explained in the blog.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter