4

F# で 2 つの行列の要素乗算によって要素を並列化しようとしています。考えてもよくわかりません。タスクを作成しようとし続けていますが、コンパイルしたくありません。私の動作しない乱雑なコードは次のとおりです。

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]注意すべきことの 1 つは、行列内の要素にアクセスする方法が非効率的であるため、このコードのパフォーマンスが非常に悪いことです。2D 配列を使用することをお勧めします。

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

意味がありません。行列のペアの場違いな要素ごとの乗算は、単一のコアがマシンのメモリ帯域幅全体を喜んで最大化し、コアを追加してもパフォーマンスが向上しない時点でのコピーにすぎません。したがって、ほぼ確実に時間の無駄です。

于 2010-06-04T19:16:28.167 に答える