While attending Teched 2009 in South Africa, I attended a very good presentation by Bart de Smet on Parallelism. He discussed the history and the need for parallelism as well as what the future entails.It looks like the end goal is to create a framework that supports parallelism natively. This will unfortunately require the whole .Net CLR to be rewritten. In the mean time they have created a library of sorts that can be used with the latest version of the CLR - .Net parallel extensions. These will be included with .Net 4 and Visual Studio 2010 release. These extensions can also be downloaded and be used with the current .Net 3.5 runtime.They have provided 3 main parallel enhancements:- TPL (Task Parallel Library)
- System.Threading.Parallel
- PLINQ
All three of these extensions I find very fascinating. 1) Task Parallel Library (TPL)This, as I understand it, is the foundation of the parallel extensions for .Net. The Task Parallel Library (TPL) is designed to make it much easier to write managed code that can automatically use multiple processors.Using the library, you can conveniently express potential parallelism in existing sequential code, where the exposed parallel tasks will be run concurrently on all available processors. Usually this results in significant speedups.A simple example: For loopfor (int i = 0; i < 100; i++) {
a[i] = a[i]*a[i];
}
This code will be executed in sequence. There is no support currently for multiple CPU processing. The following snippet shows how easy it is to provide support for parallelism using the Parallel keyword:Parallel.For(0, 100, delegate(int i) {
a[i] = a[i]*a[i];
});
Only two changes were required. One, add the Parallel keyword to the for statement. Two, use an anonymous delegate to execute the code.A nice article on the TPL can be found at MSDN magazine2) System.Threading.ParallelThis extension allows you to use a .Net threadpool in a true parallel fashion. In our current framework we spawn multiple threads on a single CPU. When executing high volumes, check the system usage. For instance, if you have a dual core CPU, you will notice that CPU usage never peeks over 50%. With this new extension the threads will be load balanced over all available CPUs. You also have the option to specify which CPU to use. This enhancement is also quite simple to implement in code. All that is necessary (similar to TPL) is the addition of the .AsParallel() extension method.3) PLINQUsing PLINQ is almost exactly like using LINQ-to-Objects and LINQ-to-XML. You can use any of the operators available through C# 3.0 and Visual Basic® 9.0 query comprehension syntax or the System.Linq.Enumerable class, including OrderBy, Join, Select, Where, and so on. PLINQ supports all of the LINQ operators, not just those available in language comprehensions. And you can query any in-memory collection such as T[], List, or any other kind of IEnumerable in addition to XML documents loaded using the System.Xml.Linq APIs. If you already have a bunch of LINQ queries, PLINQ is equipped to run them.LINQ-to-SQL and LINQ-to-Entities queries will still be executed by the respective databases and query providers, so PLINQ does not offer a way to parallelize those queries. If you wish to process the results of those queries in memory, including joining the output of many heterogeneous queries, then PLINQ can be quite useful.Aside from writing LINQ queries the same way you're used to writing them, there are two extra steps needed to make use of PLINQ:- Reference the System.Concurrency.dll assembly during compilation.
- Wrap your data source in an IParallelEnumerable with a call to the System.Linq.ParallelEnumerable.AsParallel extension method.
Here is a simple example on PLINQ. You will notice that the only addition was the .AsParallel() extension method.var q = data.AsParallel().Where(x => p(x)).Orderby(x => k(x)).Select(x => f(x));
foreach (var e in q) a(e);
A nice article on this can be found at MSDN magazine.As you can see, there are some nice enhancements coming the .Net for parallelism support. We will keep an eye open for this in the near future.