Tech Brief

Parallel Extensions

New .NET libraries support data and task parallelism.

In recent years, hardware manufacturers have been unable to translate Moore's Law into the approximately 50 percent increase per year in clock speed that we've become accustomed to seeing, and industry expectations are extremely low that we'll see such gains again in the foreseeable future. Instead, hardware manufacturers are beginning to increase the number of computational cores in both client and server machines at rates keeping with Moore's Law. Today, it's rare in the United States to find a desktop computer on sale with fewer than two cores, with quad-core being commonplace. And within a few years, industry experts expect to see eight-core and 16-core boxes as the norm.

Unfortunately, most software today is written in a largely serial manner, resulting in only single-core utilization, meaning that increases in the number of cores will have little positive impact on the performance of that software. To address this, we as an industry need to start parallelizing the software we write, such that workloads can be spread across available computing resources in order to take full advantage of a machine's power. Today, writing such code is quite challenging; it's a goal for Parallel Extensions to .NET Framework to make it significantly easier.

PLINQ and TPL
Parallel Extensions is a new set of libraries to be included in .NET Framework 4.0 and Visual Studio 2010 (a community technology preview of Parallel Extensions is available for download here). Through several encompassed components, these libraries enable both data parallelism (where the same operation is executed against multiple, discrete data elements) and task parallelism (where multiple, potentially divergent and unrelated operations are performed in parallel).

The first component is Parallel Language Integrated Query, also known as Parallel LINQ, or PLINQ for short. LINQ, which was introduced with .NET Framework 3.5, enables a declarative model for specifying operations to be performed on data, such as projections, filters, reductions and sorts, all of which are surfaced through a set of "Standard Query Operators." These operators are available through extension methods as well as through query comprehensions in the C# and Visual Basic (VB) languages. PLINQ extends LINQ by substituting parallelized implementations of these operators, enabling LINQ queries to be parallelized with minimal effort from a developer-in many cases, all it takes is adding ".AsParallel()" into the query.

Another piece of Parallel Extensions is the Task Parallel Library (TPL). TPL provides parallelized looping constructs, including a Parallel.For and a Parallel.ForEach, meant to be used in place of "for" and "for each" language constructs in languages like C# and VB. It also provides Parallel.Invoke, which can be used to efficiently parallelize blocks of statements. PLINQ and these Parallel constructs in TPL sit on top of a lower-level API set that forms the bulk of TPL and that enables efficient, fine-grained task parallelism. This set is formed around a Task class that represents an asynchronous operation that can be manipulated with a variety of operations, including being waited on, canceled and continued from. TPL also includes support for tasks that return results, for running tasks on isolated schedulers and much more.

Parallel Extensions is rounded out by a core set of coordination and synchronization types that are important to writing efficient, multithreaded applications (in fact, many of these types are used within the implementation of both PLINQ and TPL). This includes a multitude of scalable, thread-safe collections, such as a dictionary that enables multiple concurrent readers and writers. It includes low-level types for doing efficient spin waiting, on top of which a SpinLock has been built and provided. It includes types for doing thread-safe lazy initialization. And it includes synchronization primitives, such as barriers and countdown events for coordinating concurrent operations.

FedEx QuickShip works within the Microsoft Outlook interface.
[click image for larger view]
PLINQ parallelizing an example query: (from x in D.AsParallel() where p(x) select x*x*x).Sum();

The CCR
Some developers may be familiar with Microsoft's Concurrency & Coordination Runtime (CCR), available with Microsoft's CCR and DSS Toolkit 2008. Parallel Extensions and the CCR provide distinct programming models targeting different needs, and in this manner are complementary technologies. TPL is well-suited for synchronous parallelism and common patterns such as parallel loops, enabling the efficient parallelization of long-running, synchronous computations. Applications that use LINQ to query in-memory data can use PLINQ to parallelize those queries and get substantial performance improvements on multi-core hardware with minimal code changes. The CCR provides primitives for loosely coupled applications based on in-process message passing between in-memory queues; it's well-suited to orchestrate concurrent work streams within asynchronous I/O-driven apps.

Microsoft is supporting parallelism for more than just .NET. The next version of Visual C++ sports significant new functionality for developers writing parallel applications in native code, and Visual Studio 2010 will include enhanced tooling support to improve developers' abilities to debug and profile concurrent code.

About the Author

Stephen Toub is a senior program manager lead on the Parallel Computing Platform team at Microsoft.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube