I wrote a simple parallel matrix multiplication using par and pseq.
After running this program, none of the sparks converted (SPARKS: 20 (0 converted, 0 pruned)).
I would like to hear your comment about improving this program.
Also about approaches for learning parallel programming in 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] ])
Did you try very large (at least 1000×1000) matrices? It is possible that the computation is too short to paralellize.