Skip to content

datadeps: Add at-stencil helper #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2025
Merged

datadeps: Add at-stencil helper #564

merged 6 commits into from
Jul 3, 2025

Conversation

jpsamaroo
Copy link
Member

@jpsamaroo jpsamaroo commented Aug 9, 2024

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:

  • Add support for neighborhoods greater than 1
  • Fix neighborhood distances which reach the boundary Future work
  • Add support for non-wrapping behavior (pad zero/custom element) (consider using Wrap() or Pad(0) syntax)
  • Remove requirement on idx=tiles syntax - just infer from write var of each expression
  • Test GPU support
  • Automatically double-buffer read+written var Future work
  • Add Game of Life and convolution examples
  • Add intro docs
  • Add tests

@jpsamaroo jpsamaroo marked this pull request as ready for review June 24, 2025 22:11
@jpsamaroo jpsamaroo force-pushed the jps/stencils branch 2 times, most recently from 21c2bca to ffa566b Compare July 1, 2025 18:24
@jpsamaroo jpsamaroo force-pushed the jps/stencils branch 2 times, most recently from 4b9fcda to ea01b52 Compare July 2, 2025 18:01
@jpsamaroo jpsamaroo merged commit 1ee73a0 into master Jul 3, 2025
12 of 18 checks passed
@jpsamaroo jpsamaroo deleted the jps/stencils branch July 3, 2025 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant