ENTERPRISE

Parallel Programming with Microsoft .Net : Futures - Variations

2/19/2011 5:14:45 PM
So far, you’ve seen some of the most common ways to use futures and continuation tasks to create tasks. This section describes some other ways to use them.

1. Canceling Futures and Continuation Tasks

There are several ways to cancel futures and continuation tasks. You can handle cancellation entirely from within the task, as the Adatum Dashboard does, or you can pass cancellation tokens when the tasks are created.

The Adatum Dashboard application supports cancellation from the user interface. It does this by calling the Cancel method of the CancellationTokenSource class. This sets the IsCancellation Requested property of the cancellation token to true.

The application checks for this condition at various checkpoints. If a cancellation has been requested, the operation is canceled.

2. Continue When “At Least One” Antecedent Completes

It’s possible to invoke a continuation task when the first of multiple antecedents completes. To do this, use the Task.Factory object’s ContinueWhenAny method. The ContinueWhenAny method is useful when the result of any of the tasks will do. For example, you may have an application where each task queries a Web service that gives the local weather. The application returns the first answer it receives to the user.

3. Using .Net Asynchronous Calls with Futures

Tasks are similar in some ways to asynchronous methods that use the .NET Asynchronous Programming Model (APM) pattern and the IAsyncResult interface. In fact, tasks in .NET Framework 4 are IAsync Result objects. They implement this interface. This allows you to use the Task class when you implement the APM pattern.

You can convert a pair of begin/end methods that use IAsync Result into a task. To do this, use the Task.Factory object’s From Async method.

In general, tasks can be easier to use than other implementations of IAsyncResult because futures rethrow exceptions when the result is requested.


Note:

Tasks in .NET implement the IAsyncResult interface.


4. Removing Bottlenecks

The idea of a critical path is familiar from project management. A “path” is any sequence of tasks from the beginning of the work to the end result. A task graph may contain more than one path.  You can see that there are three paths, beginning with “Load NYSE,” “Load Nasdaq,” and “Load Fed Historical Data” tasks. Each path ends with the “Compare” task.

The duration of a path is the sum of the execution time for each task in the path. The critical path is the path with the longest duration. The amount of time needed to calculate the end result depends only on the critical path. As long as there are enough resources (that is, available cores), the noncritical paths don’t affect the overall execution time.

If you want to make your task graph run faster, you need to find a way to reduce the duration of the critical path. To do this, you can organize the work more efficiently. You can break down the slowest tasks into additional tasks, which can then execute in parallel. You can also modify a particularly time-consuming task so that it executes in parallel internally using any of the patterns that are described in this book.

The Adatum Dashboard example doesn’t offer much opportunity for breaking down the slowest tasks into additional tasks that execute in parallel. This is because the paths are linear. However, you can use the Parallel Loops and Parallel Aggregation patterns to exploit more of the potential parallelism within each of the Analyze tasks  if they take the most time. The task graph remains unchanged, but the tasks within it are now also parallelized.

5. Modifying the Graph At Run Time

The code in the financial program’s analysis engine creates a static task graph. In other words, the graph of task dependencies is reflected directly in the code. By reading the implementation of the analysis engine, you can determine that there are a fixed number of tasks with a fixed set of dependencies among them.

The extension of the analysis tasks in the UI layer is an example of dynamic task creation. The UI augments the graph of tasks by adding continuation tasks programmatically, outside of the context where these tasks were originally created.

Dynamically created tasks are also a way to structure algorithms used for sorting, searching, and graph traversal.

Other  
  •  Parallel Programming with Microsoft .Net : Futures - Example: The Adatum Financial Dashboard
  •  Parallel Programming with Microsoft .Net : Futures - The Basics
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Outlook Express
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Understanding Non-Windows–Based Mail Client Options
  •  Deploying the Client for Microsoft Exchange Server 2010 : Deploying with Microsoft System Center Configuration Manager 2007
  •  Deploying the Client for Microsoft Exchange Server 2010 : Pushing Outlook Client Software with Group Policies
  •  Deploying the Client for Microsoft Exchange Server 2010 : Installing the Outlook Client for Exchange Server
  •  Deploying the Client for Microsoft Exchange Server 2010 : Preparing the Deployment
  •  Parallel Programming with Microsoft .Net : Parallel Aggregation - Design Notes
  •  Parallel Programming with Microsoft .Net : Parallel Aggregation - Variations
  •  Leveraging and Optimizing Search in SharePoint 2010 : Uninstalling FAST Search Server 2010 for SharePoint
  •  Leveraging and Optimizing Search in SharePoint 2010 : Customizing the FAST Search User Interface
  •  Deploying the Client for Microsoft Exchange Server 2010 : Planning Considerations and Best Practices
  •  Deploying the Client for Microsoft Exchange Server 2010 : Understanding Deployment Options
  •  Deploying the Client for Microsoft Exchange Server 2010 : Outlook 2007 Auto Account Setup
  •  Leveraging and Optimizing Search in SharePoint 2010 : Deploying FAST Search Service Applications
  •  Leveraging and Optimizing Search in SharePoint 2010 : Customizing the Search User Interface
  •  Leveraging and Optimizing Search in SharePoint 2010 : Keywords and Best Bets
  •  Leveraging and Optimizing Search in SharePoint 2010 : Federating Search
  •  Leveraging and Optimizing Search in SharePoint 2010 : Search Scopes
  •  
    Most View
    Ableton Live 9 Suite - Music Production Software
    Windows Server 2008 : Creating Batch Files (part 1) - Using Notepad, Giving Feedback with echo, Using Parameters
    Windows Server 2008 R2 networking : Overview of Windows Server 2008 R2 Networking
    Away With The Cloud? (Part 1)
    Samsung Galaxy S4 Android Smartphone - Is It The Greatest Phone Ever? (Part 1)
    Olympus M.Zuiko Digital ED 9-18mm f/4-5.6 Lens
    Testing New Technologies – March 2013 (Part 1)
    Window 8: Why You Should Upgrade
    Smartphones and Accessories - January 2013 (Part 1)
    Learn How To… Stick Notes On Your Desktop
    Top 10
    ASUS Tytan – Tytanfall (Part 2)
    ASUS Tytan – Tytanfall (Part 1)
    Corsair Obsidian Series 350D Case
    Corsair Vengeance K70 Mechanical Gaming Keyboard
    CPU Buyer’s Guide - Hunting For Brains (Part 2)
    CPU Buyer’s Guide - Hunting For Brains (Part 1) : AMD A10-6800K, AMD FX-8350, AMD’s new FX-9590
    Crucial Ballistix Sport 16GB Kit - High-Performance Memory
    Epson WorkForce M1 O5 Printer - No Color Here
    EVGA Z77 Stinger - A New Mini-ITX Motherboard
    Gigabyte GeForce GTX Titan Graphics Card