#pragma omp declare target

Purpose

The omp declare target directive specifies that variables and functions are mapped to a device so that these variables and functions can be accessed or executed on the device.

Syntax

Syntax form 1
Read syntax diagramSkip visual syntax diagram
>>-#--pragma--omp declare target-------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-declaration-definition-seq----------------------------------><

Read syntax diagramSkip visual syntax diagram
>>-#--pragma--omp end declare target---------------------------><

Syntax form 2
Read syntax diagramSkip visual syntax diagram
>>-#--pragma--omp declare target--(--extended-list--)----------><

Syntax form 3
Read syntax diagramSkip visual syntax diagram
                                  .-+---+--.   
                                  | '-,-'  |   
                                  V        |   
>>-#--pragma--omp declare target----clause-+-------------------><

where clause is to(extended-list). If extended-list is present with no clause, the to clause is implied.

extended-list in Syntax form 2 and Syntax form 3 is a comma-separated list of one or more extended list items. An extended list item is a variable, array section, or function name.

Usage

Declarations for global variables and functions in an omp declare target directive create device versions of the variables and functions and allocate storage on the device environment. Device variables can be accessed from target regions either directly or through an omp declare target function. The to clause maps the items when the device is initialized.

Restrictions
  • The omp declare target directives must not be nested.
  • You cannot specify the same item more than once.
  • A variable type must be one of the allowed types in the map clause on target regions. For more information, see the OpenMP Application Program Interface Language Specification, which is available at http://www.openmp.org.
  • All declarations and definitions for a function must have an omp declare target directive.
  • C++ only begins You can specify overloaded functions or template functions only through an implicit extended list. The syntax of the omp declare target directive with an implicit extended list is Syntax form 1.C++ only ends
  • The omp declare target and omp threadprivate directives are mutually exclusive.

Example

The following example shows how to specify variables and functions in an omp declare target directive.
#include <stdio.h>
#pragma omp declare target
struct vector {  
  vector(int x, int y) : _x(x), _y(y) {}  
  int dot(vector o) { return _x * o._x + _y*o._y; }  
  int _x, _y;
} v1(1,1);
#pragma omp end declare target
int main() {
  vector v2(1,3);
  int res;
  #pragma omp target map(from:res)
  {  res = v1.dot(v2); }
  printf("(%d, %d) . (%d, %d) = %d\n", v1._x, v1._y, v2._x, v2._y, res);
}

The constructor function vector::vector(int,int), nonstatic member function vector::dot(vector), and static variable v1 are declared in the omp declare target directive. Thus, a device copy of each variable and function is created based on the host version. On entry to the target region, the variable v2 is implicitly mapped to and from the device, while the device copy of the variable v1 is accessed directly. The device version of the function vector::dot is invoked and the result is stored in the mapped variable res.



Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us