Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel Matrix Multiplication

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] ])
like image 823
keep_learning Avatar asked Dec 12 '11 19:12

keep_learning


People also ask

How can matrix multiplication be done with parallel approach of Cannon's algorithm?

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).

Can a 3x2 and 2x3 matrix be multiplied?

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.

Can a 2x3 and 2x2 matrix be multiplied?

For example, the 2 × 2 and 2 × 3 matrices of multiplication are possible and the resultant matrix is a 2 × 3 matrix.

Can a 2x3 and 2x1 matrix be multiplied?

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.


1 Answers

Did you try very large (at least 1000x1000) matrices? It is possible that the computation is too short to paralellize.

like image 141
nponeccop Avatar answered Nov 15 '22 10:11

nponeccop