harlan icon indicating copy to clipboard operation
harlan copied to clipboard

Stencil syntax for kernels

Open calvis opened this issue 13 years ago • 2 comments

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.

calvis avatar Apr 28 '12 17:04 calvis

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).

calvis avatar Apr 30 '12 18:04 calvis

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.

eholk avatar Mar 07 '13 22:03 eholk