Creating stencils from arrays

23 views

Rob Stewart

Apr 26, 2014, 7:30:32 AM4/26/14
Hi,

It appears that the two ways for creating stencils is either to use
`stencil2` using quasi quotation, or `makeStencil` with a lambda
expression:

I'd like to create stencils at runtime from arrays, e.g. creating an array from:

AUnboxed ((Z :. 3) :. 3) (fromList [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0])

Is there a repa function for creating stencils from arrays?

fromArray :: Num a -> Array U DIM2 a -> Stencil sh a

Thanks,

--
Rob

Ben Lippmeier

Apr 29, 2014, 7:17:06 AM4/29/14
No. The existing compilation process requires all coefficients to be statically known. This is described in the paper "Efficient Parallel Stencil Computation in Haskell". It would be useful to support stencils with coefficients that are only available at runtime, but I haven't implemented it.

Ben.

Rob Stewart

Apr 29, 2014, 7:32:25 AM4/29/14
Hi Ben,

I had a stab at this, which is almost certainly wrong. Is it just
slightly wrong, or wrong because it is currently not possible?

--8<---------------cut here---------------start------------->8---
{-# LANGUAGE BangPatterns,LambdaCase #-}

module StencilGen where

import Data.Array.Repa hiding ((++),map)
import Data.Array.Repa.Stencil

mkStencil :: Array U DIM2 Int -> Stencil DIM2 Int
mkStencil !template = makeStencil (Z :. 3 :. 3) values
where
normalisePixel :: Int -> Maybe Int
normalisePixel i = if i == 0 then Nothing else Just i

values =
\case
Z :. -1 :. -1 -> normalisePixel \$ template ! (Z :. 0 :. 0)
Z :. -1 :. 0 -> normalisePixel \$ template ! (Z :. 0 :. 1)
Z :. -1 :. 1 -> normalisePixel \$ template ! (Z :. 0 :. 2)
Z :. 0 :. -1 -> normalisePixel \$ template ! (Z :. 1 :. 0)
Z :. 0 :. 0 -> normalisePixel \$ template ! (Z :. 1 :. 1)
Z :. 0 :. 1 -> normalisePixel \$ template ! (Z :. 1 :. 2)
Z :. 1 :. -1 -> normalisePixel \$ template ! (Z :. 2 :. 0)
Z :. 1 :. 0 -> normalisePixel \$ template ! (Z :. 2 :. 1)
Z :. 1 :. 1 -> normalisePixel \$ template ! (Z :. 2 :. 2)
_ -> Nothing
--8<---------------cut here---------------end--------------->8---

Thanks,

--
Rob