Programming4us
         
 
 
Programming

Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - Variations

12/2/2010 11:28:54 AM
Dynamic task parallelism has several variations.

1. Parallel While-Not-Empty

The examples shown so far in this chapter use techniques that are the parallel analogs of sequential depth-first traversal. There are also parallel algorithms for other types of traversals. These techniques rely on concurrent collections to keep track of the remaining work to be done. Here’s an example.

public static void ParallelWhileNotEmpty<T>(
IEnumerable<T> initialValues,
Action<T, Action<T>> body)
{
var from = new ConcurrentQueue<T>(initialValues);
while (!from.IsEmpty)
{
var to = new ConcurrentQueue<T>();
Action<T> addMethod = to.Enqueue;
Parallel.ForEach(from, v => body(v, addMethod));
from = to;
}
}

This method shows how you can use Parallel.ForEach to process an initial collection of values. While processing the values, additional values to process may be discovered. The additional values are placed in the to queue. After the first batch of values processes, the method starts processing the additional values, which may again result in more values to process. This process repeats until no additional values can be produced.

A method that walks a binary tree can use the ParallelWhileNot Empty method.

static void ParallelWalk4<T>(Tree<T> tree, Action<T> action)
{
if (tree == null) return;
ParallelWhileNotEmpty(new[] { tree }, (item, adder) =>
{
if (item.Left != null) adder(item.Left);
if (item.Right != null) adder(item.Right);
action(item.Data);
});
}

An example of a problem that would be an appropriate use of the ParallelWhileNotEmpty method is a website link checking tool. The walk task loads the initial page and searches it for links. Each link is checked and removed from the list, and additional links to unchecked pages from the same site are added to the list. Eventually, there are no more unchecked links and the application stops.

2. Task Chaining with Parent/Child Tasks

The TPL includes a task creation option named AttachedToParent. This option is used most frequently in code that uses the dynamic task parallelism pattern. The purpose of the AttachedToParent option is to link a subtask to the task that created it. In this case, the subtask is known as a child task and the task that created the child task is known as a parent task.

You use the AttachedToParent option in two situations. The first is when you want to link the status of a parent task to the status of its child tasks. The second is when you want to use the Microsoft® Visual Studio® development system debugger to see the parent/ child relationships. The order of execution is not changed by using the AttachedToParent option.

The following code shows an example.

static void ParallelWalk2<T>(Tree<T> tree, Action<T> action)
{
if (tree == null) return;
var t1 = Task.Factory.StartNew(
() => action(tree.Data),
TaskCreationOptions.AttachedToParent);
var t2 = Task.Factory.StartNew(
() => ParallelWalk2(tree.Left, action),
TaskCreationOptions.AttachedToParent);
var t3 = Task.Factory.StartNew(
() => ParallelWalk2(tree.Right, action),
TaskCreationOptions.AttachedToParent);
Task.WaitAll(t1, t2, t3);
}

The AttachedToParent option affects the behavior of the parent task. If a parent task with at least one running child task finishes running for any reason, its Status property becomes WaitingForChildrenToComplete. Only when all of its attached children are no longer running will the parent task’s Status property transition to one of the three final states. Figure 1 illustrates this.

Figure 1. Life cycle of a task that has attached children


Exceptions from attached children are observed in the parent task.

To access the parent/child view in the Visual Studio debugger, right-click the row headings in the Parallel Tasks window, and then click Parent Child View.

Other -----------------
- Keyword Research Tools (part 7) - comScore Marketer
- Keyword Research Tools (part 6)
- Keyword Research Tools (part 5)
- Keyword Research Tools (part 4)
- Keyword Research Tools (part 3)
- Keyword Research Tools (part 2)
- Keyword Research Tools (part 1) - Keyword Research Data from the Engines
- The Art of SEO : Traditional Approaches: Domain Expertise, Site Content Analysis
- The Art of SEO : The Theory Behind Keyword Research
- jQuery 1.3 : Headline rotator (part 7)
- jQuery 1.3 : Headline rotator (part 6)
- jQuery 1.3 : Headline rotator (part 5) - Pause on hover
- jQuery 1.3 : Headline rotator (part 4) - The headline rotate function
- jQuery 1.3 : Headline rotator (part 3) - Setting up the rotator
- jQuery 1.3 : Headline rotator (part 2) - Retrieving the feed
- jQuery 1.3 : Headline rotator (part1) - Setting up the page
- Benchmarking Current Rankingstages of SEO
- First Stages of SEO : Benchmarking Current Rankings
- First Stages of SEO : Benchmarking Current Indexing Status
- First Stages of SEO : Assessing Historical Progress
 
 
Top 10
- Windows Phone 7 : Changing Zune Sync Settings
- Software Testing with Visual Studio Team System 2008 : Unit testing web services & Code coverage unit test
- Microsoft Exchange Server 2003: Configuring Recipient Objects (part 2) - Managing Mailboxes
- Exchange Server 2010 Mailbox Services Configuration (part 1)
- Windows Phone 7 : Pinning a Favorite Place to Start
- Overview of Internet Explorer 8 (part 3) - Using New Security and Safety Features of IE8 & Working with SmartScreen Filters
- Microsoft Dynamics AX 2009 : Working with Forms - Creating Dialogs
- SQL Server 2008 Analysis Services : Understanding the SSAS Environment Wizards (part 2)
- SQL Server 2008 : Indexing for Performance - Putting It All Together (part 3) - Covering Your Queries
- SharePoint 2010 : Specify the Order of the Columns in a View
Most view
- Exchange Server 2007: Monitor Your Exchange Environment (part 4) - Microsoft Operations Manager (MOM 2005)
- SQL Server 2008 : Profiler Usage Scenarios (part 2)
- Windows Phone 7 : Browsing the Web - Browsing Basics
- Windows 7 : Encrypting a Disk with BitLocker (part 2) - Enabling BitLocker on a System Without a TPM
- Windows Phone 7 : Adding GPS Info to Pictures
- Exchange Server 2010 : Troubleshooting Methodology
- Client Access Server Architecture in Exchange 2010 (part 1) - Client Access Server Architecture
- SharePoint 2010, Putability, and Findability
- Windows 7 : Understanding and Resolving Installation Failures (part 1) - Verifying Software Installation Requirements
- Windows Server 2008 : Create and Apply Group Policies