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

      1. Output from parallelized LINQ query with extension methods

Task 3 – Using AsParallel() With Query Comprehension Syntax

    1. In this task you will use the Parallel Extensions library and the AsParallel() method to create parallelized LINQ queries using the query comprehension syntax.

    1. Replace the LINQ query in the Main() method with the following query comprehension syntax:

      1. C#

      1. static void Main(string[] args)

      2. {

      3. // Methods to call

      4. Ex4Task3_PLINQComprehensionSyntax();

      5. }

      1. Visual Basic

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

      2. ...

      3. ' Methods to call

      4. Ex4Task3_PLINQComprehensionSyntax()

      5. ...

      6. End Sub

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

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

      1. C#

      1. private static void Ex4Task3_PLINQComprehensionSyntax()

      2. {

      3. var q = from e in employeeData.AsParallel()

      4. where e.EmployeeID % 2 == 0

      5. orderby e.EmployeeID

      6. select PayrollServices.GetEmployeeInfo(e);

      7. foreach (var e in q)

      8. {

      9. Console.WriteLine(e);

      10. }

      11. }

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

      1. Visual Basic

      1. Private Sub Ex4Task3_PLINQComprehensionSyntax()

      2. Dim q = From e In employeeData.AsParallel()

      3. Where e.EmployeeID Mod 2 = 0

      4. Order By e.EmployeeID

      5. Select PayrollServices.GetEmployeeInfo(e)

      6. For Each e In q

      7. Console.WriteLine(e)

      8. Next e

      9. End Sub

    1. Build and run the application.

    2. You should observe that although the LINQ syntax was changed, the data was processed in the same parallel manner as it was with the ParallelEnumerable extension methods.

      1. Figure 17

      1. Output from parallelized LINQ query using the query comprehension syntax

Next Step:

    1. Summary

Summary

In this lab, you have worked with the Parallel Extensions library to understand its features to help you work with parallel tasks in a simple, controllable fashion. You’ve learned how to use Parallel Extensions classes like Parallel and Task to manage units of work. You’ve dealt with Parallel Extensions features like Wait(), WaitAll(), IsComplete(), and ContinueWith() to control the flow of your execution. You’ve also worked through examples of using PLINQ to deal with parallelizing queries.

This lab has given you a solid introduction to the Parallel Extensions library and its power and benefits. For more education we recommend you visit these locations:

  1. The Parallel Extensions blog on MSDN: http://blogs.msdn.com/pfxteam/

  2. The Parallel Computing Forms on MSDN: http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=551&SiteID=1

    1. The Parallel Computing Developer Center: http://msdn.microsoft.com/en-us/concurrency/default.aspx.