Specifying Code Sections to Run on the Host and Target

In this step, you will look the code section defined to run on both the host and target.

Find the following code:

  #pragma offload target(mic) mandatory \
                             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 stores those numbers into an array. At the start of the code block is the offloadpragma. The compiler interprets this pragma to compile the following code block to run on both the host and target.

The mandatory clause reinforces the implied default and specifies the required execution on the target. Execution on the host is not allowed. If the target is not available, the program fails with an error message.

While the instruction sets are similar, the host and targets do not share the same system memory. Therefore, the variables used by the code block must exist in both the host and targets. To ensure that the variable exist in both the host and target, the offload pragma uses parameter specifiers to define the variables to copy between the host and targets.

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

Next: Running Guided Auto-parallelization


Submit feedback on this help topic