[Shootout-list] Matrix multiplication woes

Einar Karttunen ekarttun@cs.helsinki.fi
Fri, 22 Oct 2004 10:44:24 +0300


This is a MIME-formatted message.  If you see this text it means that your
E-mail software does not support MIME-formatted messages.

--=_courier-19855-1098431069-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hello

The current matrix multiplication seems to fall for the common
"do something N times and take the result of last one" problem.
Fixing it in some way could be nice. Attached is a faster ghc
solution to it trying still to keep evaluating all the extra 
phases. Using the intermediate results for something could make 
things better...

- Einar Karttunen

--=_courier-19855-1098431069-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="parr.hs"

-- $Id: matrix.ghc.html,v 1.7 2004/10/07 07:18:29 bfulgham Exp $
-- http://shootout.alioth.debian.org/
-- by Einar Karttunen 22.10.2004, compile with -fparr

import System(getArgs)
import GHC.PArr

size = 30 :: Int

idx x y = (y * size) + x

idxs = [: (x,y) | x <- [: 0..(size-1) :], y <- [: 0..(size-1) :] :]

mult m1 m2 = mapP rr idxs
    where rr (i,j) = foldl (\s k -> s + (m1 !: (idx i k) * m2 !: (idx k j))) 0 [0..(size-1)]

newA = [: 1 .. (size*size) :]
m1 = newA
m2 = newA

main = do [n] <- getArgs
	  let mm = mult (foldl (\m1 i -> mult m1 m2 `seq` m1) m1 [2..(read n)]) m2
	  putStrLn $ unwords $ map (\(x,y) -> show $ mm !: (idx x y)) [(0,0), (2,3), (3,2), (4,4)]

--=_courier-19855-1098431069-0001-2--