Welcome to SpellCoder Sign in | Join | Help

PLINQ is coming up soon (PFX)

I've been hearing about PLINQ (Parallel Linq) since the first days of announcing LINQ, the idea of making use of the new functional style programming provided in DotNet 3.5 in order to give better performance on Multi Core machines, the idea sounds cool since first day, and it now comes true in a new name Parallel FX or PFX

The programming model provided is quite simple and utilizes the same LINQ model, the new assembly is called System.Concurrency.dll which is the library that contains the new interface called IParallelEnumerable<T>, also it adds an extension methods for all collections and arrays that implement old IEnumerable, the extension method is called AsParrallel<T> which converts any collection to a Parallel enabled collection of type IParallelEnumerable<T>

Take a look of the following code

IEnumerable<int> data = new int[] {1, 2, 3, 4, 5, 6}; // or whatever complex data collection
var q = data.Where(x => x > 4).OrderBy(x=>x).Select(x => x);
foreach (var i in q) ....

This code is what you already write in C# 3.0, now if you want to add PLINQ support all what you have to do is adding the AsParallel function call before using any query operation so the previous code will look like this

IEnumerable<int> data = new int[] {1, 2, 3, 4, 5, 6}; // or whatever complex data collection
var q = data.AsParallel().Where(x => x > 4).OrderBy(x=>x).Select(x => x);
foreach (var i in q) ....

or you can write it in the LINQ query style

IEnumerable<int> data = new int[] {1, 2, 3, 4, 5, 6}; // or whatever complex data collection
var q = from i in data.AsParallel()
        where i > 4
        orderby i
        select i;
foreach(var i in q) .... 

Once you've added the AsParallel function call, PLINQ will be ready to execute transparently all the OrderBy, Where, Select, GroupBy ... etc on all the available processors.

You don't need to explicitly create threads, locks and manage concurrent execution (Unless you are making something big).

This doesn't mean that you can make use of the PLINQ power on anything other than Queries, the ParallelEnumerable class also adds some extra extension methods like the ForAll method

The ForAll method is useful if you are applying some kind of operation on all the members of a certain collection, so the ForAll function will do this operation in parallel for all the members of the collection

IEnumerable<int> data = new int[] {1, 2, 3, 4, 5, 6}; // or whatever complex data collection
data.ForAll(i=>Console.WriteLine(i));

The previous code sample will print out all the members of the array, if you imagined calling more complex function that does some heavy work on each array member, the ForAll will give you extra power to do the job faster by making use of the parallel data processing techniques.

This is not all the new stuff introduced in the System.Concurrency library, the new Parallel class is also a nice addition, it provides some extra general purpose parallel execution, so it is not related to LINQ, the most important part is the Parallel.For function, which as you expected from the name executes a parallel loop.

Check the following code

void ParMatrixMult(int size, double[,] m1, double[,] m2, double[,] result)
{
  Parallel.For( 0, size, delegate(int i) {
    for (int j = 0; j < size; j++) {
      result[i, j] = 0;
      for (int k = 0; k < size; k++) {
        result[i, j] += m1[i, k] * m2[k, j];
      }
    }
  });
}

This is an example I got from here, it illustrates a Matrix Multiplication using Parallel.For, as you can see the Parallel.For method accepts the start index and the length, then a delegate to execute.

There is also Parallel.Aggregate function which can be used to aggregate a certain data item over a parallel loop safely.

This all what I could write in one post, however the System.Concurrency contains more cool API's.

Here comes the resources for further readings.

  1. Optimize Managed Code for Multi-Core Machines
  2. Running Queries on Multi-Core processors
  3. Channel9 Video Programming in the Age of Concurrency (Andres Hejlsberg and Joe Duffy)

kick it on DotNetKicks.com
Published Sunday, October 14, 2007 1:18 AM by Mohammed Hossam
Filed Under: , ,

Comments

# re: PLINQ is coming up soon (PFX)

Sunday, October 14, 2007 8:05 AM by Adel
Cool, here is another resource.. this is a podcast Scott Hanselman recorded couple of days ago with one of the devs on the concurrency team
http://www.hanselminutes.com/default.aspx?showID=102

# re: PLINQ is coming up soon (PFX)

Wednesday, October 17, 2007 6:02 AM by Amir Magdy
very nice

BUT i think this should have been wraped in th normal where it should be doing that by default, mosh kedah walla eh ?

why would u make something that u know wouldn't perform its best

# re: PLINQ is coming up soon (PFX)

Wednesday, October 17, 2007 9:10 AM by Mohammed Hossam
Because not all operations can be done in parallel, Some algorithms are impossible to be done in parallel.

# re: PLINQ is coming up soon (PFX)

Wednesday, October 17, 2007 11:23 AM by Mohammad Tayseer
@Amir: it's also because parallelizing compilers are hard to write.

# re: PLINQ is coming up soon (PFX)

Monday, January 14, 2008 5:07 AM by Korayem
Basha, i am just starting my linq path. Man, enta ananny gedan. You've been living in this heaven that long we ma2olteleesh!!

Enta la tatakhayal ana mabsoot ad eh...then we were discussing the possibilities of multithreading in case linq to xml crawls down according to the complexity and size. A friend of mine found pLinq. I did a google search and found my beloved friend's post dating back to October 2007.

.NET 3.5 IS THE BEST, PERIOD.

# Parallel FX – nova generacija konkurentnog programiranja

Sunday, May 04, 2008 8:18 AM by Blog o C++ i C#
Uvod Sa .Net Frameworkom danas možemo razvijati aplikacije koje, bez dodatnog korištenja APIa niskog

# Parallel FX II DIO - PLINQ

Sunday, May 04, 2008 11:41 AM by Blog o C++ i C#
PLINQ – Parallel LINQ U prethodnom dijelu upoznali smo se sa TPL – bibliotekom za paralelno programiranje

# PLINQ (PFX) o LINQ en paralelo. &laquo; THE .NET WAY

Thursday, April 16, 2009 5:36 AM by PLINQ (PFX) o LINQ en paralelo. « THE .NET WAY

# Parallel FX &#8211; nova generacija konkurentnog programiranja &laquo; Bahrudin Hrnjica Web Page

# Parallel FX II DIO &#8211; PLINQ &laquo; Bahrudin Hrnjica Web Page

Wednesday, July 15, 2009 9:11 AM by Parallel FX II DIO – PLINQ « Bahrudin Hrnjica Web Page

# ???????????? ???????????????????? ???????????? &raquo; ?????????? ???????? &raquo; ???????????? ???????????????? &laquo;Power teister

Anonymous comments are disabled