Channels ▼

Parallel Pattern 2: Speculative Selection

Structured Patterns: An Overview

Parallel Pattern 1: Superscalar Sequences and Task Graphs

Parallel Pattern 2: Selection

Parallel Pattern 3: Map

Parallel Pattern 4: Gather

Parallel Pattern 5: Stencil

Parallel Pattern 6: Partition

Parallel Pattern 7: Reduce

Parallel Pattern 8: Scan

Parallel Pattern 9: Pack

The Selection pattern, which chooses the results from one of two alternative tasks based on a condition, can be used for speculative parallel execution, where both alternative tasks (as well as the condition) are executed in parallel while the condition is being evaluated. This pattern is most useful for complex conditions that take time to evaluate. It's also an example of a parallel pattern that may lead to more work overall than serial execution.

Once the condition is evaluated one of the two alternative tasks needs to be cancelled. This means stopping its consumption of resources as soon as possible. It also means ignoring its output. Being able to stop a task before completion is very useful in this context, and can lead to significant savings in execution time.In particular, with cancellation if an infinite number of processors are available execution only takes as long as the "right" path would have taken. In practice, computers don't have an infinite number of processors, but speculation can still be used if there are processors that would otherwise be idle.

Depending on the programming model, "ignoring the output" can also be challenging. Ideally the tasks are pure functions and so ignoring the return values of the functions representing the tasks is all that is needed. If tasks are allowed to have side effects, these effects need to be rolled back, which can be difficult or impossible to implement. Therefore, in order to expose opportunities for speculative parallelism, pure functions need to be used, or more generally, permanent modifications to global state need to be deferred until the condition has been evaluated.

Speculative execution is used at a very low level in some processor architectures. For example, the destination instruction for a branch might be fetched before the branch condition is evaluated. There is a possibility that some work might be wasted. In fact, the last pattern we discussed, superscalar sequence, is also frequently used in processor designs for out-of-order execution of independent instructions. Many of the other patterns we will present, such as map, are also common in low-level processor design. We are interested in applying patterns to larger tasks than single instructions, but one of the characteristics of a pattern is that it recurs at different scales.

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.