Trouble with matrix multiplication

61 views
Skip to first unread message

Matthew Daniels

unread,
Jul 19, 2012, 1:18:56 PM7/19/12
to accelerat...@googlegroups.com
Hi,

Another Haskell developer and I were trying to use Accelerate to implement simple matrix multiplication the other day and ran into what seemed to be an internal error. We posted it as an issue on github (https://github.com/AccelerateHS/accelerate/issues/63), but matrix multiplication seems simple enough that there must be another way. Is the error we ran into something that's really a bug, or are we using the Accelerate library improperly?

Thanks!

Trevor L. McDonell

unread,
Jul 20, 2012, 12:39:08 AM7/20/12
to accelerat...@googlegroups.com
Hi Matthew,

Sorry for the late reply, I got a bit swamped recently.

I think the problem is in line 4, in the definition of (*^). You use `the` inside an array computation to get the value out of a scalar array, but that implies nested data parallelism because the array depends on the index given it by generate! Accelerate does not handle nested parallelism at all (although I'm not sure why the "possibly nested" warning does not appear… hmm…)

If I have my maths right (always dubious), this might do what you require:

(*^) :: (Elt e, IsNum e) => Acc (Array DIM1 e) -> Acc (Array DIM2 e) -> Acc (Array DIM1 e)
(*^) vec mat =
  let (Z :. _ :. cols) = unlift (shape mat) :: Z :. Exp Int :. Exp Int
      vec'             = A.replicate (lift (Z :. All :. cols)) vec
  in
  fold1 (+) $ transpose $ A.zipWith (*) vec' mat

The trick is that operations like zipWith and fold1 in accelerate are all multi-dimensional. We can use replicate to duplicate out the elements of the vector, do the element-wise multiply on two matrices, and fold1 to reduce along the innermost dimension, which is why we need to transpose (alternatively, transpose the matrix and replicate in the opposite direction).

Currently the replication does actually duplicate the data, which might be difficult if your arrays are large, but I'm working on ways to avoid that.


Hope that helps!

Cheers,
-Trev


--
Sie haben diese Nachricht erhalten, weil Sie der Google Groups-Gruppe Accelerate beigetreten sind.
Wenn Sie aus dieser Gruppe austreten möchten, senden Sie eine E-Mail an accelerate-hask...@googlegroups.com.
Weitere Optionen: https://groups.google.com/groups/opt_out
 
 

Reply all
Reply to author
Forward
0 new messages