8

問題

Repaがどのように機能するかを理解しようとしていますが、 Repa Examplesパッケージの「ぼかし」サンプル コードを使用していました。コードは次を使用しますstencil2 Quasi Quote

[stencil2|   2  4  5  4  2
             4  9 12  9  4
             5 12 15 12  5
             4  9 12  9  4
             2  4  5  4  2 |]

これは単なるTemplateHaskellスニペットで、関数を生成します:

makeStencil2 5 5 coeffs where
     {-# INLINE[~0] coeffs #-}
     coeffs = \ ix -> case ix of
                      Z :. -2 :. -2 -> Just 2
                      Z :. -2 :. -1 -> Just 4
                      Z :. -2 :. 0 -> Just 5
                      Z :. -2 :. 1 -> Just 4
                      Z :. -2 :. 2 -> Just 2
                      [...]
                      _ -> Nothing

TH を使用しても問題ありませんが、coefs を Repa Array に保持したいので、代わりに Repa Array を使用するようにコードを変更しましたが、私のコードは元のコードに比べて 2 倍遅く動作します。

いくつかの派手なメモ

Repa の作成者はハードコーディングされた 7 行 7 列の値の行列を使用して係数を取得していることに気付きました: http://hackage.haskell.org/package/repa-3.2.3.3/docs/src/Data-Array-Repa-Stencil- Dim2.html#forStencil2 (参照: template7x7)

質問

  1. なぜ元のように最適化されていないのか、どうすれば修正できるのかお聞きしたいです。イメージに対してステンシル (Repa 配列) の畳み込みを実行できるようにする「畳み込み」関数を作成したいと考えています。
  2. GHCにコードを最適化させるために、そのようなハードコードされた行列を本当に使わなければならないのでしょうか? そのような「ハック」を使わずに高速な Haskell コードを作成する方法は本当にありませんか?

コード

オリジナルぼかし機能:

blur    :: Monad m => Int -> Array U DIM2 Double -> m (Array U DIM2 Double)
blur !iterations arrInit
 = go iterations arrInit
 where  go !0 !arr = return arr
        go !n !arr  
         = do   arr'    <- computeP
                         $ A.smap (/ 159)
                         $ forStencil2 BoundClamp arr
                           [stencil2|   2  4  5  4  2
                                        4  9 12  9  4
                                        5 12 15 12  5
                                        4  9 12  9  4
                                        2  4  5  4  2 |]
                go (n-1) arr'

私のぼかし機能:

blur !iterations arrInit = go iterations arrInit
    where 
          stencilx7 = fromListUnboxed (Z :. 7 :. 7) 
                    [  0,  0,  0,  0,  0,  0, 0
                    ,  0,  2,  4,  5,  4,  2, 0
                    ,  0,  4,  9, 12,  9,  4, 0
                    ,  0,  5, 12, 15, 12,  5, 0
                    ,  0,  4,  9, 12,  9,  4, 0
                    ,  0,  2,  4,  5,  4,  2, 0
                    ,  0,  0,  0,  0,  0,  0, 0
                    ] :: Array U DIM2 Int
          magicf (Z :. x :. y) = Just $ fromIntegral $ unsafeIndex stencilx7 (Z:. (x+3) :. (y+3))
          go !0 !arr = return arr
          go !n !arr  
           = do   
                  arr'    <- computeP
                           $ A.smap (/ 159)
                           $ A.forStencil2 BoundClamp arr 
                            $ makeStencil2 5 5 magicf
                  go (n-1) arr'

コードの残りの部分:

{-# LANGUAGE PackageImports, BangPatterns, TemplateHaskell, QuasiQuotes #-}
{-# OPTIONS -Wall -fno-warn-missing-signatures -fno-warn-incomplete-patterns #-}

import Data.List
import Control.Monad
import System.Environment
import Data.Word
import Data.Array.Repa.IO.BMP
import Data.Array.Repa.IO.Timing
import Data.Array.Repa                          as A
import qualified Data.Array.Repa.Repr.Unboxed   as U
import Data.Array.Repa.Stencil                  as A
import Data.Array.Repa.Stencil.Dim2             as A
import Prelude                                  as P

main 
 = do   args    <- getArgs
        case args of
         [iterations, fileIn, fileOut]  -> run (read iterations) fileIn fileOut
         _                              -> usage

usage   = putStr $ unlines
        [ "repa-blur <iterations::Int> <fileIn.bmp> <fileOut.bmp>" ]


-- | Perform the blur.
run :: Int -> FilePath -> FilePath -> IO ()
run iterations fileIn fileOut
 = do   arrRGB  <- liftM (either (error . show) id) 
                $  readImageFromBMP fileIn

        arrRGB `deepSeqArray` return ()
        let (arrRed, arrGreen, arrBlue) = U.unzip3 arrRGB
        let comps                       = [arrRed, arrGreen, arrBlue]

        (comps', tElapsed)
         <- time $ P.mapM (process iterations) comps

        putStr $ prettyTime tElapsed

        let [arrRed', arrGreen', arrBlue'] = comps'
        writeImageToBMP fileOut
                (U.zip3 arrRed' arrGreen' arrBlue')


process :: Monad m => Int -> Array U DIM2 Word8 -> m (Array U DIM2 Word8)
process iterations 
        = promote >=> blur iterations >=> demote
{-# NOINLINE process #-}


promote :: Monad m => Array U DIM2 Word8 -> m (Array U DIM2 Double)
promote arr
 = computeP $ A.map ffs arr
 where  {-# INLINE ffs #-}
        ffs     :: Word8 -> Double
        ffs x   =  fromIntegral (fromIntegral x :: Int)
{-# NOINLINE promote #-}


demote  :: Monad m => Array U DIM2 Double -> m (Array U DIM2 Word8)
demote arr
 = computeP $ A.map ffs arr

 where  {-# INLINE ffs #-}
        ffs     :: Double -> Word8
        ffs x   =  fromIntegral (truncate x :: Int)

コンパイル:ghc -O2 -threaded -fllvm -fforce-recomp Main.hs -ddump-splices

4

1 に答える 1

1
  1. 配列から畳み込み係数を読み取ることは、理論的には、コンパイルされたコードで定数を直接はんだ付けするほど高速ではありません。後者のアプローチはマシンレベルで何もコストがかからないためです。

  2. いいえ、GHC は任意のサイズの静的ステンシルを煮詰めることができます。ラムダの s を使用した静的畳み込みの実装を参照してください。fixed-vector

    [dim2St| 1   2   1
             0   0   0
            -1  -2  -1 |]
    -->
    Dim2Stencil
     n3
     n3
     (VecList
        [VecList
           [\ acc a -> return (acc + a),
            \ acc a -> (return $ (acc + (2 * a))),
            \ acc a -> return (acc + a)],
         VecList
           [\ acc _ -> return acc,
            \ acc _ -> return acc,
            \ acc _ -> return acc],
         VecList
           [\ acc a -> return (acc - a),
            \ acc a -> (return $ (acc + (-2 * a))),
            \ acc a -> return (acc - a)]])
     (\ acc a reduce -> reduce acc a)
     (return 0)
    
于 2013-11-03T10:18:23.660 に答える