4

我正在尝试并行化 F# 中两个矩阵的元素乘法。我想不通。我一直在尝试创建任务,但它从不想编译。我的非工作混乱代码如下:

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

3 回答 3

4

您犯了几个小错误,例如,您还没有弄清楚如何进行矩阵乘法。

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

需要注意的一件事是,这段代码的性能会很差,因为这m.[i,j]是一种访问矩阵中元素的低效方式。你最好使用二维数组:

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

测试:

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

一些时间:

> 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 = ()
> 

使用 ParallelFor检查这里,它应该比 async 有更好的性能。

于 2010-06-04T00:48:41.850 回答
2

这里至少有一些可以编译的代码,也许这会让你朝着正确的方向前进?

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 
于 2010-06-04T00:50:17.640 回答
1

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.

于 2010-06-04T19:16:28.167 回答