Tuesday, December 25, 2007

Parallel Extensions to .Net Framework - PLINQ

Microsoft released the CTP version of Parallel Extensions to the .Net Framework.
With the use of parallel extensions, we can write the program that using MULTI core (Dual,QUAD,etc) processors.

Usually we write the program for single core processor, so our hardware resources are not fully utilized. But with the use of Parallel Extensions, we can utilize the hardware resources effectively.

You can download the CTP version of Parallel Extension here

Parallel Extensions provide several new ways to express parallelism in your code:

Declarative data parallelism(PLINQ) - Parallel Language Integrated Query (or Parallel LINQ) is an implementation of LINQ-to-Objects that executes queries in parallel, scaling to utilize the available cores and processors of the machine. Because queries are declarative, you are able to express what you want to accomplish, rather than how you want to accomplish it.

Imperative data parallelism - Parallel Extensions also contains mechanisms to express common imperative data-oriented operations such as for and foreach loops, automatically dividing the work in the loop to run on parallel hardware.

Imperative task parallelism - Rather than using data to drive parallelism, Parallel Extensions enables you to express potential parallelism via expressions and statements that take the form of lightweight tasks. Parallel Extensions schedules these tasks to run on parallel hardware and provides capabilities to cancel and wait on tasks.


1. Create the new Project from VS 2008.
2. Reference the System.Threading.dll from the Parallel Extension installation path.
3. Create the static class like the following.

class ValidNumbers
//Sequential Method
public static int SequentialValidNumberCount(IEnumerable<string> list)
return list.Where(n => IsNumber(n)).Count();

//Parallel Method
public static int ParallelValidNumberCount(IEnumerable<string> list)
return list.AsParallel().Where(n => IsNumber(n)).Count();

//validate the string whether its number
private static bool IsNumber(string input)
return !(Regex.IsMatch(input, "[^0-9]"));

In the Static class, we have declared the two methods for calculating the number of integers in the array.

SequentialValidNumberCount - This is sequential way of running the method. It uses single core.

ParallelValidNumberCount - This is Parallel way of running the method. AsParallel() is the extension method. It uses all the core in the processor.

Calling code:

string[] arr = { "1", "a", "2" };
int count = 0;

//Sequetial way of accessing
count = ValidNumbers.SequentialValidNumberCount(arr);

//Parallel way of accessing
count = ValidNumbers.ParallelValidNumberCount(arr);

Parallel Method runs faster than sequential method because it uses all the core in the processor. You can see the difference between two in the Task Manager Performance monitor.


Bala said...

Thanks for your information.

I have some questions for you.

I use LINQ to retreive results from database. So I used code something like this.

var query= from rec in table_name
select rec.fieldname;

Parallel.ForEach(string)(query, a=>

it works fine.

But problem raises if i try to
retrieve two fields... like

var query= from rec in table_name
select rec.fieldname1,rec.fieldname2;

Parallel.ForEach(string)(query, a=>

Ayyanar said...

Did you get any error while retrieving 2 fields from database?