问题
我试图了解雷帕是如何工作的,我正在编写来自Repa实例包的“模糊”示例代码。代码使用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
使用它是可以的,但是我很想把它们保存在Repa中,所以我已经将代码更改为使用Repa,但是我的代码工作速度比原来的要慢2倍。
一些花哨的笔记,
我注意到,Repa作者使用硬编码的7×7矩阵来获得系数:http://hackage.haskell.org/package/repa-3.2.3.3/docs/src/Data-Array-Repa-Stencil-Dim2.html#forStencil2 (参见: template7x7)
问题
代码
原始模糊功能:
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
编译
发布于 2013-11-03 10:18:23
fixed-vector
s:
Dim2Stencil n3 n3 (VecList [VecList \ acc a -> return (acc + a),\ acc a -> (返回$ (acc + (2 * a),\ acc a ->返回(acc +a),VecList \ acc _ ->返回acc,\ acc _ ->返回acc,\ acc _ ->返回acc,VecList \ acc a ->返回(acc - a),\ acc a -> (返回$ (acc + (-2 * a) ),\ acc a ->返回(acc - a)]) (\ acc a减少->减少acc a)(返回0)https://stackoverflow.com/questions/19749343
复制相似问题