我希望将acceleration数组转换为repa数组,然后再使用repa-io中的Prefix
将数组写入BMP文件。忽略在加速中存在这样一个功能的事实,我只是用它作为问题的一个例子。取:
writeImageToBMP
需要加速,加速,修复和修复软件包以编译此代码。
这不会编译,因为repa-io中{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.Array.Accelerate.Interpreter as A (run)
import Data.Array.Accelerate
import qualified Data.Array.Accelerate as A
import qualified Data.Array.Accelerate.IO as A
import qualified Data.Array.Repa as Repa
import qualified Data.Array.Repa.IO.BMP as Repa
import GHC.Word
main :: IO ()
main = do
let accelArr = A.fromList
(Z :. (2::Int) :. (1::Int))
([(1,2,3),(4,5,6)] :: [(Word8,Word8,Word8)])
computation :: A.Acc (A.Array A.DIM2 (Word8,Word8,Word8))
computation = A.map
(\triple ->
let (r',g',b') = unlift triple :: (Exp Word8,Exp Word8,Exp Word8)
in lift (constant 0,g',b')) (use accelArr)
repaArr :: Repa.Array A.A Repa.DIM2 (Word8,Word8,Word8)
repaArr = A.toRepa (A.run computation)
Repa.writeImageToBMP "out_repa.bmp" repaArr
的类型是:
writeImageToBmp
其中writeImageToBMP
:: FilePath
-> Repa.Array Repa.U Repa.DIM2 (Word8, Word8, Word8)
-> IO ()
代表一个清单数组。 GHC错误是:
U
Couldn't match type ‘A.A’ with ‘Repa.U’
Expected type: Repa.Array Repa.U Repa.DIM2 (Word8, Word8, Word8)
Actual type: Repa.Array A.A Repa.DIM2 (Word8, Word8, Word8)
In the second argument of ‘Repa.writeImageToBMP’, namely ‘repaArr’
In a stmt of a 'do' block:
Repa.writeImageToBMP "out_repa.bmp" repaArr
的类型是:
repaArr
这就是为什么GHC抱怨它是Repa.Array A.A Repa.DIM2 (Word8,Word8,Word8)
的论据。
问题:如何将加速数组表示writeImageToBMP
转换为修复清单数组表示A
,以便我可以在其上调用U
?
答案 0 :(得分:2)
请注意Data.Array.Repa中的 computeS , computeP , copyS 和 copyP 功能具有此功能签名:
...constraints... => Array r1 sh e -> m (Array r2 sh e)
因此它们是允许您在不同的修复表示之间进行转换的函数(即转换r1 - > r2)。
考虑到这一点,我得到了这个代码来键入check:
main :: IO ()
main = do
let accelArr = A.fromList
(Z :. (2::Int) :. (1::Int))
([(1,2,3),(4,5,6)] :: [(Word8,Word8,Word8)])
computation :: A.Acc (A.Array A.DIM2 (Word8,Word8,Word8))
computation = A.map
(\triple ->
let (r',g',b') = unlift triple :: (Exp Word8,Exp Word8,Exp Word8)
in lift (constant 0,g',b')) (use accelArr)
repaArr :: Repa.Array A.A Repa.DIM2 (Word8,Word8,Word8)
repaArr = A.toRepa (A.run computation)
zzz <- Repa.copyP repaArr
Repa.writeImageToBMP "out_repa.bmp" zzz
您也可以在此处使用Repa.copyS
。