Parallel Programming in .Net 4

Please note: If you are not aware of Parallel Programming, this article is for you else read it for your revision.

Every developer had come across performances issues and lots of brainstorming goes on when customer is not happy with the app performance.

Traditionally, we Improve application performance by – Remove scope of bad-coding, increase application productivity, writing efficient code, improve and optimize SELECT queries, and what not we do to increase and improve application performances, but we tend to forgot that hardware has certain limitation too. Like a single core (processor aka CPU) will execute single thread a time … logical enough. Right? Then comes Asynchronous process into picture, but again with its limitation – defining locks and releasing those, is again a tedious task, and if any is missed, your application will tend to crash. Oh! Did I pointed you can have Async process in ASP.Net to scale your applications and improve performances to certain limit. But this is not parallel programming, its multi-threaded programming.

Parallel vs. Concurrent

Concurrent is accessing the same resources or sharing resources for different problems. old skool example – 2 users accessing same table and even more worse accessing same row. thus increase chance of dead locks.

Parallel is dividing a certain task into a set of related tasks to be executed concurrently. latest example, update tweets/post on twitter.com/facebook using its API to your followers/friends! (Social Media everyone loves it and share latest updates!)

Context of Parallel programming:

Multi-core machines and think of utilizing their power.

Partioning, dividing single piece of work into multiple pieces to execute concurrently.

Staic partitioning, dynamic partitioning, thread pooling.

Model of .Net Parallel Programming:

Task Parallel Library – Task based programming consists of Task Class, Parallel Class, Parallel LIN!(PLINQ).

Architecture:

Example:

Serial Task:

for (int i, i <= 100, i++)

{

//Post Wall on Facebook

}

Asynchronous Task:

thread A = addressOf(SendPosts);

thread B = addressOf(SendPosts);

A.execute(); B.execute();

void SendPosts()

{

for (int i, i <= 100, i++)

{

lock (object);

PostIt(“happy friendship day!”);

unlock(object);

}

}

void PostIt (Account facebook)

{

//Post Wall on Facebook

}

Parallel Task:

System.Threading.Tasks.Parallel.For(0, 10, i =>

{

//Post Wall on Facebook

}

);

By now, you know how to swim in ‘llel programing, its time to deep dive into its ocean. Make sure you are ready with the following kits:

Get’ started – http://msdn.microsoft.com/en-us/concurrency/ee847320.aspx

‘llel programming in .Net 4 – http://msdn.microsoft.com/en-us/library/dd460693.aspx

Blog ‘llel programming – http://blogs.msdn.com/b/pfxteam/

Thread programming in ASP.Net – http://msdn.microsoft.com/en-us/magazine/cc163463.aspx

‘llel patterns – http://parallelpatterns.codeplex.com/

.Net 4.5 is coming with lot of improvements as hardware world is changing with multi-core architecture and soon you will not find the machines with single core and 2 core will soon out-dated.

Try it on vLabs from Microsoft – https://cmg.vlabcenter.com/default.aspx?moduleid=cc0bc14f-b222-4c3b-b29b-7f729df2af08

It did work for me:

Advertisements

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: