Exercise 1: Learning the Basics of the PPL

Figure 1

In this exercise you will be working with an application that fills in the values of an array and then calls different methods to carry out operations on the elements of the array. Each array element will be doubled and then will be printed to the screen.

You will learn how to parallelize serial “foreach” and “for” statements.

Part 1: Using the Parallel Patterns Library

  1. Open the Exercise-1 solution file found at the following folder:

    C:\Server 2008 R2 Labs\Working with the CRT\Exercise-1\

  2. From the Solution Explorer, double click the Exercise-1.cpp file under the Sources folder.

    Note:
    Take a minute or two to examine the code. The program calls various functions to fill an array with values and then duplicates all the values in the array. The values of the arrays are printed on screen using a for_each statement.

  3. From the Debug menu, select Start without Debugging, you will see the output of the program (click Yes if prompted to build the project):

    Figure 2

  4. Press any key to close the program’s window and return to Microsoft Visual Studio 2010.
  5. In order to use the PPL, include the following header file:

    C++

    #include <ppl.h>

  6. In order to include the namespace of the Concurrency namespace, type the following line below the header includes:

    C++

    using namespace Concurrency;

  7. Let’s parallelize a couple of loops using the PPL. In main, find the for_each loop:

    Figure 3

  8. Change the for_each statement to a parallel_for_each by replacing the text:

    Figure 4

    Note:
    By specifying the parallel_for_each, the loop will now execute in parallel. You do not need to worry on how to scale this up as the Concurrency Runtime scheduler will take care of these details for you.

    How do you think the values of the array will now be printed on screen using a parallel_for_each? Will anything change?

  9. From the Debug menu, select Start without Debugging, you will see the output of the program (click Yes if prompted to build the project):

    Figure 5

    Note:
    As you may have guessed it (and if your machine has more than one core/processor), because parallel algorithms such as parallel_for_each act concurrently, the parallel version of this example might produce different output than the serial version.

  10. Press any key to close the program’s window and return to Microsoft Visual Studio 2010.
  11. Let’s parallelize one of the for-loops in the program. Look for the function called DoubleArrayValues (around line 29).
  12. Comment out the following line of code:

    C++

    for (int i = 0; i < 10; i++) {

  13. Below the commented line, write down the following:

    C++

    parallel_for(0,10,1,[&theList](int i) {
    Note:
    The first parameter corresponds to the starting value of the loop (0), the second one corresponds to the end value of the loop (10), and the third value corresponds to the step value of the loop (1).

    The fourth parameter, [&theList](int i) is the beginning of a lambda expression. In C++0x, "lambda expressions" implicitly define and construct unnamed function objects, which then behave like handwritten function objects. In this case we are passing the local value theList by reference to the function that takes in an integer value.

    For more information on C++0x lambdas, please visit:

    https://bit.ly/lambda

  14. Since now what used to be the body of our loop is going to behave as the function parameter, we need to close the parenthesis right after the closing bracket of the old sequential for loop:

    Figure 6

  15. Feel free to run the program again, you should get a similar output.

    Note:
    Even though that loop is running in parallel, the program executes too fast to actually notice any performance improvements.

  16. Try and parallelize the for-loop found in the function GetTenNumbers(around line 12).
  17. Once you have parallelized the for-loop, from the Debug menu, select Start without Debugging(click Yes if prompted to build the project):.
Note:
Did you get the output you were expecting? What do you think is causing the problem?