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

      1. Output from a non-parallelized LINQ query

    1. It is easy to parallelize this query by making use of the static ParallelEnumerable class’s version of the same LINQ methods. Additionally you’ll need to add an AsParallel() call to the query’s data source. Modify the Main() method just to call the PLINQAsParallel method.

      1. C#

      1. static void Main(string[] args)

      2. {

      3. ...

      4. // Methods to call

      5. Ex4Task1_PLINQAsParallel();

      6. ...

      7. }

      1. Visual Basic

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

      2. ...

      3. ' Methods to call

      4. Ex4Task1_PLINQAsParallel()

      5. ...

      6. End Sub

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

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task1_PLINQAsParallel CSharp)

      1. C#

      1. private static void Ex4Task1_PLINQAsParallel()

      2. {

      3. var q = ParallelEnumerable.Select(

      4. ParallelEnumerable.OrderBy(

      5. ParallelEnumerable.Where(employeeData.AsParallel(),

      6. x => x.EmployeeID % 2 == 0),

      7. x => x.EmployeeID),

      8. x => PayrollServices.GetEmployeeInfo(x))

      9. .ToList();

      10. foreach (var e in q)

      11. {

      12. Console.WriteLine(e);

      13. }

      14. }

      1. (Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task1_PLINQAsParallel VB)

      1. Visual Basic

      1. Private Sub Ex4Task1_PLINQAsParallel()

      2. Dim q = ParallelEnumerable.Select(

      3. ParallelEnumerable.OrderBy(

      4. ParallelEnumerable.Where(

      5. employeeData.AsParallel(),

      6. Function(x) x.EmployeeID Mod 2 = 0),

      7. Function(x) x.EmployeeID),

      8. Function(x) PayrollServices.GetEmployeeInfo(x)) _

      9. .ToList()

      10. For Each e In q

      11. Console.WriteLine(e)

      12. Next e

      13. End Sub

      1. Note: The calls to the Select(), OrderBy(), and Where() methods, which were previously being made on Enumerable, are now being made on ParallelEnumerable. Also notice that a call to AsParallel() has been added to the data source.

    1. Build and run the application.

    2. You should observe the LINQ query no longer performs the operations in a particular order. Also note that in this example the parallelized version completes in less time than the non-parallelized version (in this case, it completed in roughly half the time due to it being run on a dual-core machine; your results will vary based on the hardware you run this example on).