Channels ▼
RSS

Design

Visual Studio 2010 Brings Parallelism Mainstream


Parallel LINQ

Parallel LINQ (PLINQ) enables developers to take the declarative data query language built into the .NET platform and scale it up to run on multiple cores when used with in-memory data. For example, consider the following simple LINQ query:


var sequentialQuery = from b in babies
      where b.Name.Equals("Steve",
      StringComparison.InvariantCultureIgnoreCase) &&
      b.State == "WA" &&
      b.Year >= 1960 && b.Year <= 2010
      orderby b.Year
      select b;

The above query searches an IEnumerable data set called babies for all records where the Name field is "Steve," the State is "WA," and the Year is between 1960 and 2010. This data is then sorted by Year and returned into sequentialQuery. When I run this code, the LINQ runtime decomposes my query and runs the components of the query sequentially, fully utilizing a single core on my machine until the query computation is complete. With a small modification to my code, however, PLINQ give me much faster time to insight by decomposing my LINQ query into tasks that will run on all available cores:


var sequentialQuery = from b in babies.AsParallel()
      where b.Name.Equals("Steve",
      StringComparison.InvariantCultureIgnoreCase) &&
      b.State == "WA" &&
      b.Year >= 1960 && b.Year <= 2010
      orderby b.Year
      select b;

Thanks to the magic of extension methods, PLINQ provides the full scope of LINQ operators, which means that you should be able to leverage PLINQ with virtually all of your existing LINQ queries. PLINQ also works over any IEnumerable data, so you can also use PLINQ to manage many kinds of in-memory data.

One of the most difficult aspects of parallel programming is managing shared state. To guard against race conditions and other errors, developers protect shared state code with locks to prevent unwanted simultaneous access to shared resources. However, this practice also introduces additional complexity, which invariably leads to bugs. What's more, locks also tend to reduce scalability since some threads or tasks will invariably spend part of their lives waiting at those locks doing no useful work.

Asynchronous agents follow an actor-message pattern, whereby agents operate asynchronously from one another and share no state, communicating only via messages sent between agents. Conceptually, think of an asynchronous agent as a course-grained entity intended to serve a relatively long-lived role in an application. Agents can be connected in a message passing network, where messages propagate from source to target using cooperative send and receive functions. Key Asynchronous Agents concepts are described in the Table 2.

Table 2: Asynchronous Agents

Concurrency Visualizer

New to the Visual Studio 2010 profiler is the ability to visualize the performance of a multithreaded application. The Concurrency Visualizer includes three views intended to help developers understand the performance characteristics of their application and how to improve performance. These views show time on the x-axis with different data on the y-axis, and remained synchronized with one another as developers zoom, scroll, and pan their way through a visual representation of their application's execution.

The CPU Utilization view displays core count on the y-axis, graphing utilization of cores over time. This information gives developers insight into how many cores are being used as the application is running, which can provide clues for further investigation. For example, if a portion of the application that is meant to be parallelized is only using one core, the developer knows this is an area where more investigation is required to understand what might be standing in the way of scalability.

The Threads view displays threads within swim lanes on the y-axis, with different activities for each thread being shown in a different color over time. For example, over time, a thread's swim lane may change from green to red to green to purple, which would indicate a thread's transitions over time from running to blocked to running to performing disk I/O. Call stack and hot links back to source are also available for a given thread segment, which ties the visual representation back to actual behavior in source. Figure 4 shows the Threads view of the Concurrency Visualizer.

Figure 4: Threads view

The Cores view displays cores within swim lanes on the y-axis, with different threads running on each core being painted in a different color. This view lets developers see when a thread might be frequently jumping between cores, which is often a problem for performance due to the cache thrashing it can cause.

Conclusion

No longer the domain of a select few highly specialized developers, parallel computing has officially broken into the mainstream. As software developers, we must react to the changing nature of hardware by parallelizing the performance-intensive portions of our software. Visual Studio 2010 makes this easier than ever, with a host of new features in the programming models, runtimes, and tooling. For the first time, parallel development is as easy as multi-core hardware is common.

Of course, while parallel computing is a big focus for Visual Studio 2010 and the .NET Framework 4, it's not the only focus. The team has worked hard to make sure that Visual Studio 2010 and .NET 4 have something for every developer. Indeed, the guiding principle behind these two releases is straighforward: We wanted to simplify the entire development cycle, while at the same time making it easier for developers to unleash their creativity, and build quality applications.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video