Structured Patterns: An Overview
Parallel Pattern 1: Superscalar Sequences and Task Graphs
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.


