我使用par
和pseq
编写了一个简单的并行矩阵乘法。
运行此程序后,没有任何火花转换(火花: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] ])
答案 0 :(得分:3)
您是否尝试过非常大(至少1000 x 1000)的矩阵?计算可能太短而无法并行化。