views:

228

answers:

3

I'm trying to parallelize the element by element multiplication of two matrices in F#. I can't quite figure it out thought. I keep trying to create tasks but it never wants to compile. My non-working messy code is the following:

let myBigElemMultiply (m:matrix) (n:matrix) = 
  let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) =
      for i in 0 .. destination.NumCols
          destination.[row, i] <- source1.[row,i] + source2.[row,i]
      destination
  let result = Matrix.zero(m.NumRows)
  let operations = [ for i in 0 .. m.NumRows -> AddTwoRows i result m n ]
  let parallelTasks = Async.Parallel operations
  Async.RunSynchronously parallelTasks
  result
+4  A: 

You have made several small mistakes, e.g., you haven't figured how to do matrix multiplication.

let myBigElemMultiply (m:matrix) (n:matrix) = 
  let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) =
      for col=0 to destination.NumCols-1 do
        let mutable sum = 0.0
        for k=0 to m.NumCols-1 do
          sum <- sum + source1.[row,k] * source2.[k,col]
        destination.[row,col] <- sum

  let result = Matrix.zero m.NumRows n.NumCols
  let operations = [ for i=0 to m.NumRows-1 do yield async { AddTwoRows i result m n} ]
  let parallelTasks = Async.Parallel operations
  Async.RunSynchronously parallelTasks |> ignore
  result

One thing to notice is that this code would perform very badly because m.[i,j] is an inefficient way to access elements in a matrix. You'd better use a 2D array:

let myBigElemMultiply2 (m:matrix) (n:matrix) = 
  let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) =
      let destination = destination.InternalDenseValues
      let source1 = source1.InternalDenseValues
      let source2 = source2.InternalDenseValues
      for col=0 to Array2D.length2 destination - 1 do
        let mutable sum = 0.0
        for k=0 to Array2D.length1 source2 - 1 do
          sum <- sum + source1.[row,k] * source2.[k,col]
        destination.[row,col] <- sum

  let result = Matrix.zero m.NumRows n.NumCols
  let operations = [ for i=0 to m.NumRows-1 do yield async { AddTwoRows i result m n} ]
  let parallelTasks = Async.Parallel operations
  Async.RunSynchronously parallelTasks |> ignore
  result

testing:

let r = new Random()
let A = Matrix.init 280 10340 (fun i j -> r.NextDouble() )
let B = A.Transpose

some timing:

> myBigElemMultiply A B;;
Real: 00:00:22.111, CPU: 00:00:41.777, GC gen0: 0, gen1: 0, gen2: 0
val it : unit = ()
> myBigElemMultiply2 A B;;
Real: 00:00:08.736, CPU: 00:00:15.303, GC gen0: 0, gen1: 0, gen2: 0
val it : unit = ()
> A*B;;
Real: 00:00:13.635, CPU: 00:00:13.166, GC gen0: 0, gen1: 0, gen2: 0
val it : unit = ()
> 

Check here by using ParallelFor, which should have a better performance than async.

Yin Zhu
FWIW, 2D arrays are also inefficient to access...
Jon Harrop
The OP did actually ask for element-wise multiplication, i.e. what the .* operator does and not matrix-matrix multiplication that I think you have described.
Jon Harrop
+2  A: 

Here's at least some code that compiles, perhaps this will get you headed in the right direction?

let myBigElemMultiply (m:matrix) (n:matrix) =  
    let AddTwoRows (row:int) (destination:matrix) (source1:matrix) (source2:matrix) = 
        async {    
            for i in 0 .. destination.NumCols do
                destination.[row, i] <- source1.[row,i] + source2.[row,i] 
        }
    let result = Matrix.zero m.NumRows m.NumCols 
    let operations = [ for i in 0 .. m.NumRows -> AddTwoRows i result m n ] 
    let parallelTasks = Async.Parallel operations 
    Async.RunSynchronously parallelTasks |> ignore
    result 
Brian
A: 

There's no point. Out-of-place element-wise multiplication of a pair of matrices is little more that copying at which point a single core will happily max out the entire memory bandwidth of your machine and adding more cores will not improve performance. So it is almost certainly a waste of time.

Jon Harrop