Specifying the Code Section to Run on the Coprocessor

In this step, you will look at a section of the source code that is defined to run on both the CPU and the coprocessor.

Find the following code:

  #pragma offload target(mic : target_id) \
                             in(all_Vals : length(MAXSZ)) \
                             inout(numEs) out(E_vals : length(MAXSZ/2))
  for (k=0; k < MAXSZ; k++) {
      if ( all_Vals[k] % 2 == 0 ) {
         E_vals[numEs] = all_Vals[k];
         numEs++;
      }
  }

This code block finds the first ten even numbers and then puts those numbers into an array. At the start of the code block is the offload directive. The compiler interprets this directive to compile the following code block to run on both the CPU and coprocessor.

While the instruction sets for the host CPU and coprocessors are similar, they do not share the same system memory. This means that the variables used by the code block must exist in both the host CPU and coprocessor. To ensure that the variable exist in both the host CPU and coprocessor, the directives use parameter specifiers to define the variables to copy between the host CPU and coprocessor.

The directive also has the target parameter to direct the code to a specific coprocessor in a system with multiple coprocessors.

Although the compiler by default compiles the source code into an application that runs on both the host CPU and coprocessor, you can also compile the same source code into an application that runs on just the CPU. In the next step, you will compile the source code into an application that runs only on the CPU.

Previous: Introduction to Guided Auto-parallelization Next: Running Guided Auto-parallelization


Submit feedback on this help topic
Click here for more Getting Started Tutorials