Look at the source code of the single-threaded demo application. Start off by launching Visual Studio 2015 and then take your time as you step through the source code. Take note of the serialized structure of the application and see the functions that are being called. This will greatly help you in understanding the results the Intel Advisor shows you.
- [Narrator] Let's open the demo application that's included in the exercise files for this chapter. If you have multiple versions of Visual Studio installed, make sure you open it up with Visual Studio 2015. The reason for this is that the Intel Advisor is not yet integrated into Visual Studio 2017, since it just came out. Once the Visual Studio project is done loading, click on main.cpp. First we see the included header files for the other sorting algorithms. It's important to note that there are some variables defined in BubbleSort.h, which we will check out after main.cpp.
Below the included headers is a comment to include the task handler object. The task handler object is used to make this demo application multi-threaded. We use this later on. Below the task handler is the annotations definition file, which lets us mark regions of our code, where we think it can be executed on a separate thread. Annotations are macros in C++, and they do not change the computations of your program in any way. What this means is that later on, while we are doing our multi-threaded design, the single-threaded application will remain unchanged.
It will still build and run all test cases. After the headers and annotations definition file, we get to the main entry point of the demo application, where we can see this g_pUnsorted array being filled with random numbers. After this global array is filled with random integers, the sorting begins. We see function calls to BubbleSort, InsertionSort, SelectionSort, and other sorting functions. As it is right now, this application is single-threaded, and executes each of these sorting tasks one at a time, sequentially.
The first header file we are going to look at is BubbleSort.h. At the very top are the headers for printing to the command prompt. Below that, we define the size of the global array unsorted. The array size is set to 1,024 bytes, in order for the list to be big enough, so that sorting takes a while, and the Intel Advisor can obtain meaningful data. If the array size is too small, then the sorting is done too quickly, and Intel Advisor cannot gather any data. After the array size is defined, the global array that will hold the original list is declared.
This global array is used in all eight sorting algorithms, and could be a data-sharing problem. Next are the helper functions. The first helper function swaps the values of two integers. Then the BubbleSort helper function takes in a pointer to an integer array, and ascend sorts the values. The comments in the source code further describe each sorting algorithm. Below that is the BubbleSort task, which copies the values contained in the global array, unsorted. It performs the BubbleSort, and then prints the results.
The layout we see here is the same for the other sorting tasks. All the tasks will have helper functions, either a swap function or something called recursively, in order to perform the sorting algorithm. So as you can see in each header file, there is a helper function or... the sorting algorithm, followed by the task.
We eventually get to this task handler object, which is used in the optimized version. We will revisit this when we make optimization changes. But for now, let's move on to TreeSort. TreeSort is a binary search tree, which prints its contents in an in-order traversal, which means its items are printed in ascending order to the command prompt. The source code contains comments on what's going on in these sorting algorithms, but for more detailed description, check out these resources.
- Installing Intel Advisor
- Sorting source code
- Integrating Visual Studio and Intel Advisor
- Discovering parallel code
- Adding markers for parallelism
- Predicting parallel behavior
- Predicting data sharing problems
- Adding parallelism with a task handler
- Forecasting performance gains