Home > ASP.NET, Parallel Programming > .Net 4.0 Parallelism #1: What is it all about?

.Net 4.0 Parallelism #1: What is it all about?

CPU manufacturers are moving away from increasing clock speed to putting more cores on a cpu, we now need to look at new development methods which makes full use of these multi core processors to improve performance gains, rather than hoping for a faster core to improve application performance.

With Microsoft .NET 4.0 (or 3.5 with the reactive extensions), Parallel Programming is now possible!

The Task Parallel Library and Parallel LINQ aims to introduce developers to parallel programming to make use of these new cpu technologies. Previously it was possible by using threading, but this is unwieldy and hard to use, not to mention the lousy performance if you were to program it incorrectly.

So what exactly does parallel programming mean?

Lets take a classic example of a simple foreach loop

foreach(var data in allProducts) { doSomething(data);}

In pseudocode, it simple means, do something to all products.

So now, imagine you have a single Quad Core server, and imagine you have a total of 10 products. This is what’s going to happen.

CPU #1 doSomething(1), doSomething(2), doSomething(3)….doSomething(10)
CPU #2 [no work]
CPU #3 [no work]
CPU #4 [no work]

What a waste of CPU resources! You are using just a single core and the rest of the cores are technically not helping you with the task processing at all. Imagine the improvements in performance you will get if you are somehow able to use the rest of the cores easily.

With the new parallels library, you are able to do exactly that. by making a simple change in code, you are able to utilize all the cores

CPU #1 doSomething(1), doSomething(2),
CPU #2 manage threads, doSomething(3), doSomething(10)
CPU #3 doSomething(4), doSomething(5)
CPU #4 doSomething(6), doSomething(8), doSomething(9)

so what exactly is the change in code you say, just add a parallel in front!

Parallel.ForEach(var data in allProducts) { doSomething(data);}

For more information, you can look at the PFX Team blog located at http://blogs.msdn.com/pfxteam/

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: