Adds an @stencil macro (and an @neighbors helper) for specifying stencil-like operations (such as convolutions) over one or more DArrays, which operate in parallel via Datadeps. For example, Conway's Game of Life can be specified as:
# Allocate game tiles and double-buffer
tiles = zeros(Blocks(N, N), Bool, N*nt, N*nt)
outputs = zeros(Blocks(N, N), Bool, N*nt, N*nt)
# Create a glider
tiles[13, 14] = 1
tiles[14, 14] = 1
tiles[15, 14] = 1
tiles[15, 15] = 1
tiles[14, 16] = 1
import Dagger: @stencil, Wrap
# Run Game of Life for one iteration
Dagger.spawn_datadeps() do
@stencil begin
outputs[idx] = begin
nhood = @neighbors(tiles[idx], 1, Wrap())
neighs = sum(nhood) - tiles[idx]
if tiles[idx] && neighs < 2
0
elseif tiles[idx] && neighs > 3
0
elseif !tiles[idx] && neighs == 3
1
else
tiles[idx]
end
end
tiles[idx] = outputs[idx]
end
end
Todo:
[x] Add support for neighborhoods greater than 1
[ ] Fix neighborhood distances which reach the boundary
[x] Add support for non-wrapping behavior (pad zero/custom element) (consider using Wrap() or Pad(0) syntax)
[x] Remove requirement on idx=tiles syntax - just infer from write var of each expression
[ ] Add and validate GPU support
[ ] Consider renaming? This could become more general than stencils I like the current name
Adds an
@stencil
macro (and an@neighbors
helper) for specifying stencil-like operations (such as convolutions) over one or moreDArray
s, which operate in parallel via Datadeps. For example, Conway's Game of Life can be specified as:Todo:
Wrap()
orPad(0)
syntax)idx=tiles
syntax - just infer from write var of each expressionConsider renaming? This could become more general than stencilsI like the current name