Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Declarative, Imperative, and Task-based Parallelism in .NET

Declarative, Imperative, and Task-based Parallelism in .NET

Daniel Moth has released four videos on Parallel Extensions for .NET. These cover the new declarative, imperative, and task-based parallelism APIs for the .NET framework.

The first video is a brief overview of the architecture and touches on some of the examples. While these examples are in VB and C#, they can be used in any .NET language.

In the second video, PLINQ is introduced. PLINQ is a declarative model for performing data parallel queries against XML and in-memory objects. Requiring trivial code changes to use, PLINQ is by far the easiest way to leverage multiple cores in .NET.

Just as LINQ is constructed as a set of extension methods on IEnumerable, PLINQ is a set of extension methods on IParallelEnumerable. These extensions mirror all of the methods in LINQ in name as function signature. To convert an IEnumerable to an IParallelEnumerable, simply call IEnumerable.AsParallel.

If needed, control over the parallelism is also done declaratively. Options such as preserving the original ordering and the number of threads used may be passed into the AsParallel method.

The type of parallelism used is also influenced by how the query is processed. If you call ToList on the query, all threads will be allocated to processing the query. Only once the entire job is complete will the main thread resume. This is known as "Stop and Go" processing.

If, on the other hand, you do a for-each loop against the query a completely different model is used. In this model, called Pipelining, the main thread is used to loop through the results as they are produced by the other threads. This model gives you access to the intermediate results, which is great when you want to flow data to a socket or file system. But if you are completely CPU bound, this may be slower than stop and go.

The final method is inverted enumeration. A new method called ForAll allows each thread to call a delegate directly against each item. Since you do not have to wait for a list to be generated (stop and go) or allocate a thread to processing the loop (pipelining), this can be the most efficient method.

The third video is on imperative parallelism. Imperative parallelism is based on parallel versions of Do, For, and For-Each. These all accept a delegate, usually in the form of an anonymous method. Since VB does not support multiline anonymous methods, C# has a definite advantage here.

On the surface this feels a lot like the OpenMP library used by Fortran and C programmers on supercomputers. However, it uses closures and thread local storage to distinguish between shared and isolated variables.

The fourth video is on the core of Parallel Extensions, the Task Parallel Library. The Task Parallel Library, or TPL, is essentially an advanced version of the .NET thread pool. Like a thread pool, the TPL can perform load balancing across threads and processors. Unlike the thread pool, the TPL has a rich API that supports many of the concepts exposed by the Thread class.

The TPL also has a new set of features not seen in either threads or thread pools. For example, Tasks have a safe Cancel method. Unlike Thread.Abort, this will not simply kill the job passed into the Task. If the job has not been assigned to a thread, it will simply never start. Otherwise, the job needs to periodically check the Task.IsCancelled property.

Cancelation can also flow to child tasks. Currently this does not happen by default, but the team is interested in what the community thinks the default should be.

Rate this Article