Choose and Mark the Best Parallel Opportunities

Using the Survey tool data, choose places in your program to add parallelism and mark those places by inserting Intel Advisor annotations. To do this:

Display the Sources in the Survey Source Window

Double-click a line (or right-click a line and select View Source) in the Survey Report window for the hot function solve() (first Hot Loop) to display the Survey Source window:

Survey Source window

The recursive function call to setQueen() uses nearly all of this program's CPU time. You can see the CPU time for individual source lines using the Survey Source:

  • The Total Time column shows the measured time executing this statement or in functions invoked from this statement.

  • The Loop Time column shows the sum of the Total Time for all the code in this loop. It is displayed for one statement in the loop, such as the loop header.

Find Where to Add Intel Advisor Parallel Site and Task Annotations

You want to distribute frequently executed instructions to different tasks that can run at the same time. So rather than looking only at the function consuming all the time, you must also examine all the functions in the call tree from main() to the hot routine setQueen(). In this case, the main() function accepts command-line arguments, initializes an array, and calls the function solve(). The function solve() calls setQueen(), which calls itself recursively.

Either use the Survey Source window (double-click or right-click a line and select Edit Source) or the editor in the Solution Explorer to open the source file nqueens_serial.cpp. Get familiar with the code execution paths and data use. For example, you need to understand code paths to ensure that any annotations you add will be executed.

The nqueens_Advisor sample includes lines that begin with //ADVISOR COMMENT. In nqueens_serial.cpp, notice that:

  • The annotations for the parallel site include the loop in the solve() function (shown below). Also, the body of the loop containing the call to setQueen() is a task within that parallel site.

  • The #include statement references the Intel Advisor annotations definitions (header file). This is needed because annotations are present in this source file. With your own application modules that contain annotations, you need to insert this line:

    #include "advisor-annotate.h"

  • For your convenience, the annotations are commented out in this version of this project's source file. These annotations have been uncommented (are active) in the next project's source file (2_nqueens_annotated).

void solve() {

        //ADVISOR COMMENT: Remove the following declaration of the queens array and uncomment the declaration...
        //ADVISOR COMMENT: This privatizes the queens array to each task and eliminates the incidental sharing
        int * queens = new int[size]; //array representing queens placed on a chess board.  Index is row posi...
  for(int i=0; i<size; i++) {
      // try all positions in first row
      // create separate array for each recursion
      //int * queens = new int[size]; //array representing queens placed on a chess board. Index is row...
      //ADVISOR COMMENT: This is incidental sharing because all the tasks use the same copy of "queens"
      setQueen(queens, 0, i);

In your own program, choosing where to add task annotations may require some experimentation. If your parallel site has nested loops and the computation time used by the innermost loop is small, consider adding task annotations around the next outermost loop.

The following figure illustrates the original C/C++ nqueens_Advisor sample code to show the task (dark blue background) and its enclosing parallel site (orange background).

Adding Parallel Site and Task Annotations to Your Program

When adding annotations to your own program, remember to include the annotations definitions, such as advisor-annotate.h for C/C++ programs. For help completing this step, access the Advisor XE Workflow tab and click the button below 2. Annotate Sources to display instructions:

Advisor XE Workflow tab with expanded Step 2 instructions

At the bottom of the Survey Report or Survey Source windows, use the annotation assistant pane to copy the annotation example code that you can paste into your editor:

  • On the right side, select Iteration Loop, Single Task from the drop-down list.

  • View the displayed example annotation code.

  • Click the Copy to Clipboard button to copy the displayed text into the clipboard.

  • Paste the code snippet into an intermediate editing window or directly into your editor. To launch the Visual Studio code editor with that source file opened to the corresponding location, either double-click a source line in the Survey Source window or right click and choose Edit Source.

  • Use the code editor to change the placeholder site and task name parameters to meaningful ones. For example, change the site name from MySite1 to solve before you save the file.

For other task code structures, select the appropriate type from the list:

Intel Advisor annotation assistant drop-down list

For example, if loop(s) within the parallel site (proposed parallel code region) contain multiple tasks or a task does not include the entire loop body, select Loop, One or More Tasks. For multiple function calls that each might be tasks, select Function, One or More Tasks.

You can also copy language-specific build settings to paste into your application's build script by selecting Build Settings and clicking the Copy to Clipboard button.

Alternatively on Windows* OS systems, you can use a wizard to insert annotations into your sources:

  1. Within the Visual Studio code editor, select a code region.

  2. Right-click and select the Intel Advisor XE 2013 > Annotation Wizard from the pop-up context menu:

    After you select a code region, right-click to select the Annotation Wizard

  3. In the Choose the Annotation Type drop-down list, choose the type of annotations to be inserted. For example, if you just selected the code region for a parallel site, select Annotate Site only - select task annotations below.

  4. Click Next to specify the parameters of the opening annotation line. Site, task, and other annotations take name arguments. You should replace the placeholder name (such as MySite2) with a name that helps you quickly identify its source location. For example, type a name that describes the hot loop or function.

  5. The annotations appear in red color in the wizard. If the displayed annotations look correct, click Next to proceed. If needed, you can instead click Back.

  6. Click Finish to insert the annotations in the code editor.

  7. If you just inserted parallel site annotations, you can now select the code within the parallel site for a task, and choose one of the task annotation items from the Choose the Annotation Type drop-down list (repeat from step 4).

For more instructions on using the Annotation Wizard, view the wizard's dialog boxes or the relevant help topics.

Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.