Shapes

tmcdonell edited this page Jul 21, 2012 · 5 revisions

Operations in Accelerate take the form of collective operations over arrays of the type Array sh e. The Array type has two type parameters; sh is the shape of the array, the number of dimensions; and e to represent the element type of the array, such as Int or Float. Before we can get started manipulating arrays, we need to grasp Accelerate's notion of array shape. Much like the Repa library, Accelerate arrays are parameterised via a type which determines the dimension of the array and the type of the index.

Shape types, and multi-dimensional array indices, are built somewhat like lists (technically, a heterogenous snoc list):

data Z = Z
data tail :. head = tail :. head

Here, the constructor Z corresponds to a shape with zero dimension (or a scalar, with one element) and is used to mark the end of the list. The (:.) constructor adds additional dimensions to the shape. For example:

Z :. Int

is the shape type of a one-dimensional array (a vector) indexed by an Int, while

Z :. Int :. Int

is the shape of a two-dimensional array (a matrix) indexed by an Int in each dimension. In fact, Int is the only index type allowed at this time. Note that (:.) associates to the left, so Z :. Int :. Int is equivalent to (Z :. Int) :. Int, hence the heat / tail naming in the definition.

This style is used to construct both the type and value of a shape. For example, to define the shape of a vector of ten elements:

sh :: Z :. Int
sh =  Z :. 10

There are also some handy type synonyms available:

type DIM0 = Z
type DIM1 = DIM0 :. Int
type DIM2 = DIM1 :. Int
type DIM3 = DIM2 :. Int
  -- and so on...

type Array DIM0 e = Scalar e
type Array DIM1 e = Vector e

In the next section, we will further see how to use shapes when we discuss operations for inputting array data, before moving onto the Accelerate DSL itself.