Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Esempi uso Task.Parallel.For - MultiplyMatrices.vb

MultiplyMatrices.vb

Caricato da: Ultimo
Scarica il programma completo

  1. ' How to: Write a Simple Parallel.For Loop
  2. Imports System.Threading.Tasks
  3. Imports System.Threading.Tasks.Parallel
  4.  
  5. Module MultiplyMatrices
  6.  
  7. #Region "Sequential_Loop"
  8.  
  9.     Private Sub MultiplyMatricesSequential(ByVal matA As Double(,), ByVal matB As Double(,), ByRef resultS As Double(,))
  10.         Dim matACols As Integer = matA.GetLength(1)
  11.         Dim matBCols As Integer = matB.GetLength(1)
  12.         Dim matARows As Integer = matA.GetLength(0)
  13.         Dim temp As Double = 0
  14.  
  15.         For i As Integer = 0 To matARows - 1
  16.             For j As Integer = 0 To matBCols - 1
  17.                 temp = 0
  18.                 For k As Integer = 0 To matACols - 1
  19.                     temp += matA(i, k) * matB(k, j) / 3
  20.                 Next
  21.                 resultS(i, j) += temp
  22.             Next
  23.         Next
  24.  
  25.     End Sub
  26.  
  27. #End Region
  28.  
  29. #Region "Parallel_Loop"
  30.  
  31.     Private Function MultiplyMatricesParallel(ByVal matA As Double(,), ByVal matB As Double(,), ByVal resultP As Double(,))
  32.         Dim matACols As Integer = matA.GetLength(1)
  33.         Dim matBCols As Integer = matB.GetLength(1)
  34.         Dim matARows As Integer = matA.GetLength(0)
  35.  
  36.  
  37.         ' A basic matrix multiplication.
  38.         ' Parallelize the outer loop to partition the source array by rows.
  39.         Threading.Tasks.Parallel.For(0, matARows, Sub(i)
  40.                                                       Dim temp As Double = 0
  41.                                                       For j As Integer = 0 To matBCols - 1
  42.                                                           ' Use a temporary to improve parallel performance.
  43.                                                           temp = 0
  44.                                                           For k As Integer = 0 To matACols - 1
  45.                                                               temp += matA(i, k) * matB(k, j) / 3
  46.                                                           Next
  47.                                                           resultP(i, j) += temp
  48.                                                       Next
  49.                                                   End Sub)
  50.         Return resultP
  51.     End Function
  52.  
  53. #End Region
  54.  
  55.  
  56. #Region "Main"
  57.  
  58.     Sub Main(ByVal args As String())
  59. Inizia:
  60.         ' Set up matrices. Use small values to better view
  61.         ' result matrix. Increase the counts to see greater
  62.         ' speedup in the parallel loop vs. the sequential loop.
  63.         Dim colCount As Integer = 500
  64.         Dim rowCount As Integer = 1500
  65.         Dim colCount2 As Integer = 700
  66.         Dim m1 As Double(,) = InitializeMatrix(rowCount, colCount)
  67.         Dim m2 As Double(,) = InitializeMatrix(colCount, colCount2)
  68.         Dim result_S As Double(,) = New Double(rowCount - 1, colCount2 - 1) {}
  69.         Dim result_P As Double(,) = New Double(rowCount - 1, colCount2 - 1) {}
  70.  
  71.         ' First do the sequential version.
  72.         Console.WriteLine("-S")
  73.         Console.WriteLine("Executing sequential loop...")
  74.         Dim stopwatch As New Stopwatch()
  75.         stopwatch.Start()
  76.  
  77.         MultiplyMatricesSequential(m1, m2, result_S)
  78.         stopwatch.[Stop]()
  79.         Console.WriteLine("Sequential loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds)
  80.  
  81.         ' For the skeptics.
  82.         OfferToPrint(rowCount, colCount2, result_S)
  83.  
  84.         ' Reset timer and results matrix.
  85.         stopwatch.Reset()
  86.         result_S = Nothing
  87.  
  88.         ' Do the parallel loop.
  89.         Console.WriteLine("-P")
  90.         Console.WriteLine("Executing parallel loop...")
  91.         stopwatch.Start()
  92.  
  93.         result_P = MultiplyMatricesParallel(m1, m2, result_P)
  94.         stopwatch.[Stop]()
  95.  
  96.         Console.WriteLine("Parallel loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds)
  97.         OfferToPrint(rowCount, colCount2, result_P)
  98.  
  99.         Console.WriteLine("Vuoi riprovare? y/n")
  100.         Dim c As Char = Console.ReadKey().KeyChar
  101.         If c = "y"c OrElse c = "Y"c Then
  102.             Console.WriteLine()
  103.             GoTo Inizia
  104.         End If
  105.         ' Keep the console window open in debug mode.
  106.         Console.WriteLine("Press any key to exit.")
  107.         Console.ReadKey()
  108.     End Sub
  109. #End Region
  110.  
  111. #Region "Helper_Methods"
  112.  
  113.     Function InitializeMatrix(ByVal rows As Integer, ByVal cols As Integer) As Double(,)
  114.         Dim matrix As Double(,) = New Double(rows - 1, cols - 1) {}
  115.  
  116.         Dim r As New Random()
  117.         For i As Integer = 0 To rows - 1
  118.             For j As Integer = 0 To cols - 1
  119.                 matrix(i, j) = r.[Next](100000)
  120.             Next
  121.         Next
  122.         Return matrix
  123.     End Function
  124.  
  125.     Sub OfferToPrint(ByVal rowCount As Integer, ByVal colCount As Integer, ByRef matrix As Double(,))
  126.         Console.WriteLine("Computation complete. Print results? y/n")
  127.         Console.WriteLine()
  128.         Dim c As Char = Console.ReadKey().KeyChar
  129.         If c = "y"c OrElse c = "Y"c Then
  130.             'Console.WindowWidth = 168
  131.             Console.WriteLine()
  132.             For x As Integer = 0 To rowCount - 1
  133.                 Console.WriteLine("ROW {0}: ", x)
  134.                 For y As Integer = 0 To colCount - 1
  135.                     Console.Write("{0:#.##} ", matrix(x, y))
  136.                 Next
  137.                 Console.WriteLine(" Continue ? y/n")
  138.                 c = Console.ReadKey().KeyChar
  139.                 If c = "n"c OrElse c = "N"c Then
  140.                     Exit For
  141.                 End If
  142.             Next
  143.         End If
  144.     End Sub
  145.  
  146. #End Region
  147. End Module