2015-11-05 52 views
1

我想在repa-io使用writeImageToBMP將陣列寫入BMP文件之前將加速陣列轉換爲repa陣列。忽略加速中存在這樣一個函數的事實,我只是用它作爲問題的一個例子。採取:將加速度的A陣列表示轉換爲repa的U陣列表示

{-# 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 

這些軟件包加速,加速,io,repa和repa-io reuired編譯此代碼。

這並不編譯,因爲在repA的-IO writeImageToBmp類型是:

writeImageToBMP 
    :: FilePath 
    -> Repa.Array Repa.U Repa.DIM2 (Word8, Word8, Word8) 
    -> IO() 

U表示repA的清單陣列。該GHC錯誤是:

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是:

Repa.Array A.A Repa.DIM2 (Word8,Word8,Word8) 

這就是爲什麼GHC抱怨它作爲一個參數writeImageToBMP

問題:如何轉換的加速數組表示A到repA的清單數組表示U,這樣我就可以調用它writeImageToBMP

+0

與將任何Repa數組轉換爲unboxed Repa數組的方式相同:['computeUnboxed {S​​/P}'](https://hackage.haskell.org/package/repa-3.2.3.3/docs/Data-陣列Repa.html#五:computeUnboxedP)。 – user2407038

+1

@ user2407038 - 我能夠使'copyP'和'copyS'工作,但沒有任何計算方法,因爲缺少'Load'約束。 – ErikR

回答

2

注意,功能計算computePcopyScopyPData.Array.Repa有這樣的簽名:

...constraints... => Array r1 sh e -> m (Array r2 sh e) 

,因此它們的功能,讓你可以不同之間的轉換修理表示(即轉換r1 - > r2)。

考慮到這一點我得到這個代碼類型檢查:

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這裏。