VB.NET Parallel.Forをためす
Parallel.Invokeから引き続き試した機能
これも、なんらかの並列機能(Threadなど)が使えれば、Javaでもなんでも実装できるんだけど
面白いのはインタフェースw
Parallel.For([初期値], [最大値], [Delegate(or lambda式)])
これも、自動的にCPUのcore数にパラ度を最適化してくれる
うーん、『』いいぞww
demoコード
http://msdn.microsoft.com/ja-jp/library/dd460713.aspx
' How to: Write a Simple Parallel.For Loop Imports System.Threading.Tasks Module MultiplyMatrices #Region "Sequential_Loop" Sub MultiplyMatricesSequential(ByVal matA As Double(,), ByVal matB As Double(,), ByVal result As Double(,)) Dim matACols As Integer = matA.GetLength(1) Dim matBCols As Integer = matB.GetLength(1) Dim matARows As Integer = matA.GetLength(0) For i As Integer = 0 To matARows - 1 For j As Integer = 0 To matBCols - 1 For k As Integer = 0 To matACols - 1 result(i, j) += matA(i, k) * matB(k, j) Next Next Next End Sub #End Region #Region "Parallel_Loop" Private Sub MultiplyMatricesParallel(ByVal matA As Double(,), ByVal matB As Double(,), ByVal result As Double(,)) Dim matACols As Integer = matA.GetLength(1) Dim matBCols As Integer = matB.GetLength(1) Dim matARows As Integer = matA.GetLength(0) ' A basic matrix multiplication. ' Parallelize the outer loop to partition the source array by rows. Parallel.For(0, matARows, Sub(i) For j As Integer = 0 To matBCols - 1 ' Use a temporary to improve parallel performance. Dim temp As Double = 0 For k As Integer = 0 To matACols - 1 temp += matA(i, k) * matB(k, j) Next result(i, j) += temp Next End Sub) End Sub #End Region #Region "Main" Sub Main(ByVal args As String()) ' Set up matrices. Use small values to better view ' result matrix. Increase the counts to see greater ' speedup in the parallel loop vs. the sequential loop. Dim colCount As Integer = 180 Dim rowCount As Integer = 2000 Dim colCount2 As Integer = 270 Dim m1 As Double(,) = InitializeMatrix(rowCount, colCount) Dim m2 As Double(,) = InitializeMatrix(colCount, colCount2) Dim result As Double(,) = New Double(rowCount - 1, colCount2 - 1) {} ' First do the sequential version. Console.WriteLine("Executing sequential loop...") Dim stopwatch As New Stopwatch() stopwatch.Start() MultiplyMatricesSequential(m1, m2, result) stopwatch.[Stop]() Console.WriteLine("Sequential loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds) ' For the skeptics. OfferToPrint(rowCount, colCount2, result) ' Reset timer and results matrix. stopwatch.Reset() result = New Double(rowCount - 1, colCount2 - 1) {} ' Do the parallel loop. Console.WriteLine("Executing parallel loop...") stopwatch.Start() MultiplyMatricesParallel(m1, m2, result) stopwatch.[Stop]() Console.WriteLine("Parallel loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds) OfferToPrint(rowCount, colCount2, result) ' Keep the console window open in debug mode. Console.WriteLine("Press any key to exit.") Console.ReadKey() End Sub #End Region #Region "Helper_Methods" Function InitializeMatrix(ByVal rows As Integer, ByVal cols As Integer) As Double(,) Dim matrix As Double(,) = New Double(rows - 1, cols - 1) {} Dim r As New Random() For i As Integer = 0 To rows - 1 For j As Integer = 0 To cols - 1 matrix(i, j) = r.[Next](100) Next Next Return matrix End Function Sub OfferToPrint(ByVal rowCount As Integer, ByVal colCount As Integer, ByVal matrix As Double(,)) Console.WriteLine("Computation complete. Print results? y/n") Dim c As Char = Console.ReadKey().KeyChar If c = "y"c OrElse c = "Y"c Then 'Console.WindowWidth = 168 Console.WriteLine() For x As Integer = 0 To rowCount - 1 Console.WriteLine("ROW {0}: ", x) For y As Integer = 0 To colCount - 1 Console.Write("{0:#.##} ", matrix(x, y)) Next Console.WriteLine() Next End If End Sub #End Region End Module
結果
Executing sequential loop... Sequential loop time in milliseconds: 2644 Computation complete. Print results? y/n nExecuting parallel loop... Parallel loop time in milliseconds: 482 Computation complete. Print results? y/n nPress any key to exit.
職場の4coreで試した結果っス
なぜか、4coreで5倍以上の処理能力!
コレ、モノによっちゃあ簡単に性能向上できる処理もあるんじゃないだろうか?(バッチとかで)
Work! Enjoy it!