ExSystolic.Grid (ex_systolic v0.2.0)

Copy Markdown View Source

A rectangular coordinate grid that provides neighbour lookups.

The grid is the spatial substrate on which processing elements are placed. It knows nothing about PEs or data -- it is pure geometry.

Coordinate system

  • {row, col} with row increasing southward and col increasing eastward (standard matrix indexing).
  • Row and column indices start at 0.

Why a separate module?

Separating geometry from computation keeps the Array module focused on PE orchestration and makes neighbour logic independently testable.

Summary

Functions

Returns all valid coordinates in the grid, row-major order.

Returns the neighbour to the east (same row, col + 1) or :none.

Checks whether a coordinate lies inside the grid.

Returns the neighbour to the north (row - 1, same col) or :none.

Creates a rectangular grid of the given dimensions.

Returns the neighbour to the south (row + 1, same col) or :none.

Returns the neighbour to the west (same row, col - 1) or :none.

Types

coord()

@type coord() :: {non_neg_integer(), non_neg_integer()}

t()

@type t() :: %ExSystolic.Grid{cols: pos_integer(), rows: pos_integer()}

Functions

coords(grid)

@spec coords(t()) :: [coord()]

Returns all valid coordinates in the grid, row-major order.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 2, cols: 2)
iex> ExSystolic.Grid.coords(grid)
[{0, 0}, {0, 1}, {1, 0}, {1, 1}]

east(grid, arg)

@spec east(t(), coord()) :: coord() | :none

Returns the neighbour to the east (same row, col + 1) or :none.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 3, cols: 3)
iex> ExSystolic.Grid.east(grid, {1, 1})
{1, 2}
iex> ExSystolic.Grid.east(grid, {1, 2})
:none

member?(grid, arg)

@spec member?(t(), coord()) :: boolean()

Checks whether a coordinate lies inside the grid.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 2, cols: 2)
iex> ExSystolic.Grid.member?(grid, {1, 1})
true
iex> ExSystolic.Grid.member?(grid, {2, 0})
false
iex> ExSystolic.Grid.member?(grid, {-1, 0})
false

north(grid, arg)

@spec north(t(), coord()) :: coord() | :none

Returns the neighbour to the north (row - 1, same col) or :none.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 3, cols: 3)
iex> ExSystolic.Grid.north(grid, {1, 1})
{0, 1}
iex> ExSystolic.Grid.north(grid, {0, 1})
:none

rect(opts)

@spec rect(keyword()) :: t()

Creates a rectangular grid of the given dimensions.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 3, cols: 4)
iex> grid.rows
3
iex> grid.cols
4

south(grid, arg)

@spec south(t(), coord()) :: coord() | :none

Returns the neighbour to the south (row + 1, same col) or :none.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 3, cols: 3)
iex> ExSystolic.Grid.south(grid, {1, 1})
{2, 1}
iex> ExSystolic.Grid.south(grid, {2, 1})
:none

west(grid, arg)

@spec west(t(), coord()) :: coord() | :none

Returns the neighbour to the west (same row, col - 1) or :none.

Examples

iex> grid = ExSystolic.Grid.rect(rows: 3, cols: 3)
iex> ExSystolic.Grid.west(grid, {1, 1})
{1, 0}
iex> ExSystolic.Grid.west(grid, {1, 0})
:none