我使用par
和pseq
编写了一个简单的矩阵乘法并行程序。
运行该程序后,没有任何火花被转换(SPARKS: 20(0转换,0修剪))。
我希望听到您对改进此程序的建议。
还有关于在Haskell中学习并行编程的方法。
import Data.List
import Control.Parallel
parHelp :: ( Num a ) => [ a ] -> [ a ] -> a
parHelp [] [] = 0
parHelp ( x : xs ) ( y : ys ) = ret where
ret = par a ( pseq b ( a + b ) ) where
a = x * y
b = parHelp xs ys
helpMult :: ( Num a ) => [ a ] -> [ [ a ] ] -> [ a ]
helpMult _ [] = []
helpMult x ( y : ys ) = ret where
ret = par a ( pseq b ( a : b ) ) where
a = sum . zipWith ( *) x $ y
b = helpMult x ys
mult :: ( Num a ) => [ [ a ] ] -> [ [ a ] ] -> [ [ a ] ]
mult [] _ = []
mult ( x : xs ) ys = ret where
ret = par a ( pseq b ( a : b ) ) where
a = helpMult x ys
b = mult xs ys
main = print $ mult [[1 .. 4 ] , [ 1 .. 4 ] , [ 1 .. 4 ] , [ 1 .. 4] ] ( transpose [[1 .. 4 ] , [ 1 .. 4 ] , [ 1 .. 4 ] , [ 1 .. 4] ])