Declare Target
Up until now, we have assumed that all our functions and subroutines are in the same file as our structured blocks. In real-world applications, functions can be scattered across multiple source files. In OpenMP, functions and subroutines defined in the same compile unit as a target region are automatically compiled for both the host and device. However, when a function is defined with a different compile unit (e.g., a separate source file), the compiler does not recognize that it needs to compile a device version of the function. The declare target directive instructs the compiler to generate both host and device versions of the specified function to make them available in the target region. This directive also works with global variables in C/C++ and module variables in Fortran, making them available on the device for the duration of the program.
Alternative Syntax Forms
There are several ways to write the declare target directive. In C/C++, you can wrap declarations between a begin declare target and end declare target pair, list specific functions and variables directly, or use the to clause. In Fortran, only the latter two are supported.
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)