SIGN UP MEMBER LOGIN:    
ARTICLE

Parallel Extension in .Net

Posted by Prasoon Articles | Learn .NET June 10, 2009
This tutorial gives the idea of Parallel Extension being built by Microsoft to many of its existing features.
Reader Level:


As a next step forward Microsoft is trying to introduce Parallel Extensions to various features of the .Net framework .

To try this, you need to first download Extension Libraries from following Location:-

www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3

I have started using this library in my applications and therefore will try to give some insight about how to use and what purpose it serves.

Intention behind Parallel For Loop :-

Parallel Extensions were designed to provide performance improves with multiple core or multiple processor machines. What it does is that various atomic operations like (For Loop) will be able to take advantage of multicore processors and there will not be any need of system programming to do that.

As of now processes can switch over from one core to another (taken care by Operating System) but what about threads within that process or atomic operations like this For Loop.

As an application  developer, I could do least about this without getting into System Programming.

To help us to deal with this situation, Microsoft has came up with these Parallel Extensions.

I will not go deep into how it actually functions but will concentrate on how as a developer I can utilize this library.

How I can use it in my Application:-

Conventionally we use a for loop as follows :-


int
  noOfHashtables =4;

Hashtable[] hashtable = new Hashtable[noOfHashtables];

for(int i=0;i< hashtable.Length; i++)

{

    hashtable[i] = new Hashtable();

}

Now with the help of Parallel Extension the code can be rewritten as follows :-

int noOfHashtables = 4;

Hashtable[] hashtable = new Hashtable[noOfHashtables];

Parallel.For(0, hashtable.Length, delegate(int i)

{

    hashtable[i] = new Hashtable();

});

Few things about above code :-

  1. Above code is only supported in .Net Framework 3.5 and above.
  2. You will need library reference of this new Parallel Extension DLL which is downloadable (address of which is given in starting of the tutorial).
  3. Parallel.For(0, hashtable.Length, delegate(int i) 

    In this part of the code 0 is the starting value of variable i, so you can replace that with any value that you wish to.

    To generalize the above code :-

    we can rewrite the syntax as follows:-

        Parallel.For(<your starting value >,<End criteria for loop>, delegate(int < your variable Name>)
                    {
                       // Your own code 
                    }); 
More on this in my next article.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor