Search code examples
openmpoffloading

Reason to use declare target pragma in OpenMP


I wonder what is the reason to use the declare target directive. I can simply use target {, data} map (to/from/tofrom ...) in order to specify which variables should be used by the device. As for the functions, is that compulsory for a function called from a target region to be declared as target? Suppose, I have the following code:

int data[N];

#pragma omp target
{
    #pragma omp parallel for
    for (int i=0; i<N; i++)
        data[i] = my_function(i);
}

Is it required to surround my_function() declaration/definition with declare target?


Solution

  • In you example the data[N] array will be mapped to the device at the beginning of every target region, and unmapped at the end. In programs with multiple target regions it may be useful to map data[N] only once at startup using declare target directive.

    As for the functions, the OpenMP 4.0 specification is quite unclear about this. It says only:

    The declare target directive specifies that variables, functions (C, C++ and Fortran), and subroutines (Fortran) are mapped to a device.

    So, it doesn't clearly prohibit calls to non-target functions from target regions and other target functions.

    But I personally think that my_function must be declared as target. Otherwise why this pragma (for the functions) was introduced at all?