Parallel Extension in .NET 4.0 (Part 3 – PLINQ)

I am very delighted as Visual Studio 2010 has finally released on last 12 April 2010. Now let me continue my Parallel Extension blogs series, but this time in release version of Visual Studio Smile.

Introduction

In the first post of this series, I illustrated the significance of parallel computing and impact of un-readiness parallel-enabled software. Furthermore, I talked about Task Parallel Library in the second post. In this third post, I’ll talk about PLINQ (parallel version of LINQ). I believe this would be another interesting topic.

To comprehend this post, I assume that the reader is familiar with LINQ (Language Integrated Query). If you are not familiar with LINQ, no worries. The following link will give you a quick start of understanding LINQ:

The Parallel LINQ

So, what is PLINQ? It’s just parallel implementation of LINQ. The idea of PLINQ is to enable developer to utilize the multi-core processor hardware effectively, yet retain the capability of LINQ. It is designed to be as usable as possible, thus developer will save the learning curve.

Now, let me give you some code example the comparison of LINQ and PLINQ.

LINQ

List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var query = from i in intList
            where i % 2 == 0
            select i;

PLINQ

List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var query = from i in intList.AsParallel()
            where i % 2 == 0
            select i;

Hm… That’s all. So simple? Yes it is. We could just simply put AsParallel() method on the list when we are performing LINQ.

Note that .AsParallel() method is actually an extension method to the collection that implement IEnumerable<T>.

Degree of Parallelism

By default, PLINQ uses all of the processors on the host computer up to a maximum of 64. You can instruct PLINQ to use no more than a specified number of processors by using the WithDegreeOfParallelism(Of TSource) method. This is useful when you want to make sure that other processes running on the computer receive a certain amount of CPU time.

List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var query = from i in intList.AsParallel().WithDegreeOfParallelism(2)
            where i % 2 == 0
            select i;

Reference

 

In the next post, I’ll talk about the Task Visualization support from IDE. See you!

Share this post: | | | |
Published Friday, April 16, 2010 9:44 AM by Wely

Comments

# Parallel Extension in .NET 4.0 (Part 4 – Debugging Support from IDE)

Monday, April 26, 2010 7:59 PM by Wely

What we have cover so far? So far in the Parallel Extension series, I’ve talked about: Introduction and

# Parallel Extension in .NET 4.0 (Part 5 – Visualization Support from IDE)

Sunday, May 02, 2010 7:39 PM by Wely Lau

What we have cover so far? So far in the Parallel Extension series, I’ve talked about: Introduction and

Powered by Community Server (Commercial Edition), by Telligent Systems