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] ])
This is done by shifting all submatrices A(i , j) to the left (with wraparound) by i steps and all submatrices B(i , j) up (with wraparound) by j steps. Perform local block multiplication. Each block of A moves one step left and each block of B moves one step up (again with wraparound).
Matrix Multiplication (3 x 2) and (2 x 3)Multiplication of 3x2 and 2x3 matrices is possible and the result matrix is a 3x3 matrix.
For example, the 2 × 2 and 2 × 3 matrices of multiplication are possible and the resultant matrix is a 2 × 3 matrix.
Multiplication of 3x2 and 2x1 matrices is possible and the result matrix is a 3x1 matrix. This calculator can instantly multiply two matrices and show a step-by-step solution.
Did you try very large (at least 1000x1000) matrices? It is possible that the computation is too short to paralellize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With