JuliaParallel / Dagger.jl

A framework for out-of-core and parallel execution
Other
638 stars 67 forks source link

datadeps: Add at-stencil helper #564

Open jpsamaroo opened 2 months ago

jpsamaroo commented 2 months ago

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: