Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная параллельные вычисления.docx
Скачиваний:
6
Добавлен:
27.09.2019
Размер:
937.57 Кб
Скачать
      1. Figure 4

      1. Output from parallel for…each implementation

      1. Note: The Parallel Extensions Library also provides a useful Invoke() method, that allows parallel execution of anonymous methods or lambda expressions. To help illustrate how to use the Invoke method you will examine a common tree-walking algorithm and then see how it can be parallelized to reduce the total time needed to walk the entire tree.

      2. In this example you will walk an employee hierarchy and call the GetPayrollDeduction() method for each employee you encounter.

    1. Replace the current method calls from Main(), with a call to Ex1Task1_WalkTree() method. This code instantiate the employee hierarchy and call the tree walker method.

      1. C#

      1. static void Main(string[] args)

      2. {

      3. ...

      4. // Methods to call

      5. Ex1Task1_WalkTree();

      6. ...

      7. }

      1. Visual Basic

      1. Sub Main(ByVal args() As String)

      2. ...

      3. ' Methods to call

      4. Ex1Task1_WalkTree()

      5. ...

      6. End Sub

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex1 Ex1Task1_WalkTree CSharp)

      1. C#

      1. private static void Ex1Task1_WalkTree()

      2. {

      3. EmployeeHierarchy employeeHierarchy = new EmployeeHierarchy();

      4. WalkTree(employeeHierarchy);

      5. }

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex1 Ex1Task1_WalkTree VB)

      1. Visual Basic

      1. Private Sub Ex1Task1_WalkTree()

      2. Dim employeeHierarchy As New EmployeeHierarchy()

      3. WalkTree(employeeHierarchy)

      4. End Sub

    1. Add the following method to Program.cs (C#) or Module1.vb (Visual Basic):

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex1 WalkTree CSharp)

      1. C#

      1. private static void WalkTree(Tree<Employee> node)

      2. {

      3. if (node == null)

      4. return;

      5. if (node.Data != null)

      6. {

      7. Employee emp = node.Data;

      8. Console.WriteLine("Starting process for employee id {0}",

      9. emp.EmployeeID);

      10. decimal span = PayrollServices.GetPayrollDeduction(emp);

      11. Console.WriteLine("Completed process for employee id {0}",

      12. emp.EmployeeID);

      13. Console.WriteLine();

      14. }

      15. WalkTree(node.Left);

      16. WalkTree(node.Right);

      17. }

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex1 WalkTree VB)

      1. Visual Basic

      1. Private Sub WalkTree(ByVal node As Tree(Of Employee))

      2. If node Is Nothing Then

      3. Return

      4. End If

      5. If node.Data IsNot Nothing Then

      6. Dim emp As Employee = node.Data

      7. Console.WriteLine("Starting process for employee id {0}", emp.EmployeeID)

      8. Dim span As Decimal = PayrollServices.GetPayrollDeduction(emp)

      9. Console.WriteLine("Completed process for employee id {0}", emp.EmployeeID)

      10. Console.WriteLine()

      11. End If

      12. WalkTree(node.Left)

      13. WalkTree(node.Right)

      14. End Sub

    1. Build and run the application.

    2. You should observe the employees are processed in order of their IDs. Also note the total amount of time required to walk the tree (the exact time required will vary):

      1. Figure 5

      1. Output from a non-parallel tree walker

      1. Note: The tree has been structured so that the data will be written out in ID order when the tree is walked using the non-parallel algorithm provided above.

    1. To walk the tree in a parallel manner remove the two calls to WalkTree() at the end of the WalkTree() method and replace them with a call to the Invoke() Method of the static Parallel class:

      1. C#