4. GESTIÓN INFRAESTRUCTURA Y CONSECUENCIAS DE LAS FALLAS EN LAS
4.2. LA GESTIÓN DE LA INFRAESTRUCTURA EN LAS REDES DE AGUA POTABLE
The current approaches to structured parallelism do not provide a general framework for parallel programming that combines the ability to reason about the correctness of program transformations, together with the abil- ity to reason about the run-time performance of parallel programs. The few approaches that do so are either not general, i.e. they are targeted at a specific kind of problems or architectures, or not flexible, i.e. they can- not be easily extended with new parallel structures, or alternative parallel implementations for the supported parallel structures.
The Structured Arrows (StA) developed in this thesis is a novel skeletal programming framework aimed at being extensible with new parallel struc- tures that have associated cost models. The structures and cost models are exposed as type abstractions, which enables a program to be implemented
once, and then parallelised by adding the associated type annotations. The
StAframework combines two formalisms: a queue-based operational seman- tics, and hylomorphisms. A parallel structure specified in the queue-based model must be proven equivalent to the corresponding hylomorphism. How- ever, when this is done, a simple type annotation can be used to replace a program structure to use the newly defined structure. The queue-based model is simple and predictable. The next chapter introduces the core part of the StA framework: the type-and-effect system and the notion of struc- tured arrows.
Chapter 3
Structured Arrows: A Type
System for Parallelism
This chapter contains a formal description of the main part of this thesis: the novel skeletal programming type-based framework, Structured Arrows (StA). StA aims to provide a general framework for parallel programming that allows simultaneously reasoning about:
1. code rewritings that introduce/change parallelism to a program; and
2. run-time performance of alternative parallelisations of a program.
This chapter presents the core language, a point-free, purely functional language with hylomorphisms; and its typing system, which is the one that guides the introduction of parallelism to sequential functions. A pro- totype implementation of this framework, and extensions, is available in
https://bitbucket.org/david_castro/skel.
3.1
Overview
The main idea behind this framework is to separate the specification of the functionality of a program from the possible parallelisations. At the value level, the languageHyloofstructured expressionsis used to specify the functionality of programs. There is no parallelism at this level. At the type- level, a type-and-effect system is provided that annotates function types,
arrows, with arbitrary program structures. During type-checking, function definitions are annotated with the underlying structure of the program, and checked against the structure annotations. This is illustrated by Figure 3.1 on page 71. At the value level, structured expressions in Hylo describe the functionality of a program as a composition of hylomorphisms. At the type level, the type A7−→σ B represents the type of a function from A toB (A→
B), that can be parallelised according to the structure σ. This structure
σ is an abstraction that captures the composition of hylomorphisms and algorithm skeletons that can be used to parallelise e. Structures σ do not need to be fully specified, and can contain holes that can be automatically instantiated by minimising the cost of the resulting expression.
To illustrate this, consider the functionimage merge below:
imgMerge:List(Img×Img)→ListImg imgMerge = map (merge ◦ mark)
This function takes a list of pairs of images as input, and returns a list of images that result from merging the pairs of input images. The images are merged in two stages. First, a function mark marks the pixels from both images that need to be merged, according to some dynamic condition on the images. Then, the function merge computes the resulting image from the marked pixels. The body of imgMergeis a structured expression in the language Hylo. The type annotation is a regular function type List(Img× Img) → ListImg.
In the StA framework, parallelising this function can be done by pro- viding a suitable type annotation. StA introduces a new form ofstructure- annotated arrows, or structured arrows. A structured arrow is a function type A 7−→σ B, that is annotated with a target parallel structure σ. For ex- ample, a programmer might specify thatimgMergecan be parallelised using a parallel pipeline, by instantiating σ = _ k _. This structure uses k to represent parallel pipelines, and underscore to represent holes.
imgMerge:List(Img×Img)7−−→_k_ ListImg
A code-generation stage would need to instantiate these holes with sub- expressions from the body of imgMerge, for example:
3.1. OVERVIEW 71
e
σ
Γ
`
e
:
A
7−→
σB
e
∼=
p
p
Figure 3.1: Structured Arrows: well-typed structured expressions (e) against a target (parallel) structure (σ) can be rewritten turned into functionally equivalent parallel programs (p) according to the specified structure.
The type-checking algorithm determines whether it is possible to instanti- ate the structure with sub-expressions from imgMerge, and then the code- generation selects an arbitrary instantiation from the set of possibilities.
In the StAframework, the structures σ can be parameterised with run- time information that can be used to statically predict the speedups of alternative parallelisations. The type-checking algorithm can usecost mod- els to predict statically how to instantiate any holes so that the cost of the overall structure is minimised. In the StA framework, cost models can be interpreted as functions from structures to run-time predictions.
cost: Σ→Integer
Given a suitable cost model, the type of imgMerge would change to:
imgMerge:List(Img×Img)7−−−−−−−−−→mincost(_k_) ListImg
The novel contributions of this chapter are:
• A denotational semantics for common algorithmic skeletons in terms of hylomorphisms (Section 3.2).
• Structure-annotated Arrows (StA). A novel type and effect system that annotates function types of a point-free programming language with the underlying program structure. This structure can be used to reason simultaneously about equivalent, alternative implementations and their cost on different architectures (Section 3.3).
• A reforestation procedure for deciding semantic equivalences between alternative parallel programs. This decision procedure is based on reintroducing intermediate data structures, “reforestation”, rather than the more common approach of eliminating them for efficiency reasons, “deforestation”. This enables the type system to introduce parallelism in a semi-automated and sound way (Section 3.4)