Stencil syntax for kernels
It'd be nice to be able to specify stencils with special syntax in a kernel. For example, we could do something like this:
(kernel ([(stencil (a b c)
(d e f)
(g h i))
M])
(/ (+ a b c d e f g h i) 9))
This kernel would compute the average of a 3x3 window in a matrix and use each of these as a new value in a result matrix. Obviously we'd need to specify boundary conditions, and the stencil keyword might be too verbose, but this is the basic idea.
Is this someone we could express with a macro system for Harlan?
We could also do a bunch of stencil-specific optimizations really easily with this syntax.
Why not make this the syntax...
(stencil ([((a b c)
(d e f)
(g h i))
M])
(/ (+ a b c d e f g h i) 9))
... perform whatever optimizations and have it expand before/during/around make-kernel-dimensions-explict to ...
(kernel <type> (<2d>) (((e M)))
(let ((a (vector-ref (vector-ref M (- (get-global-id 0) 1)) (- (get-global-id 1) 1))
...)
...))
A few more ideas:
- How would the boundaries be defined?
- We could even calculate the boundaries separately, reduce the kernel dimensions by two in each dimension and run it without bounds-checking (possibly not in that order).
This would be easy enough to add as a macro now. We may not get all the optimizations, but we're trending more towards an "optimize from first principles" approach, which would hopefully benefit this case as well.