[OMP Target] Difference in mapping of global arrays (malloc vs static)

[OMP Target] Difference in mapping of global arrays (malloc vs static)

There are 2 ways to have a global array: Use a pointer and malloc, or just define it as an array:

#omp declare target
int gArray[10];
int* gVals /*somewhere later:*/ = malloc(10*sizeof(int));
#omp end declare target

I though, these where equivalent in handling but just discovered a huge difference: When I map them to a target, only the gVals will be actually mapped. If I want the values of gArray on my device I have to use "target update". Is this a bug in the current icc or is this covered in the spec? I could not find anything specific to this.

BTW: Using a local array (no declare target) works as intended.

Related to this: The icc makes me add the declare target directives to the global array although I feel that those are not required according to the spec. Consider this example:

#include <stdio.h>
#include <stdlib.h>

int gArray[2];

void testGArrayParam(int gArray[]){
  #pragma omp target
  {
    gArray[1]=3;
  }
}

int main (int argc, char *argv[]) {
  #pragma omp target map(gArray[0:2])
  {
    gArray[0]=2;
  }
  #pragma omp target data map(gArray[0:2])
  testGArrayParam(gArray);

  printf("gArrayParamBack(23): %d%d\n",gArray[0],gArray[1]); // Output is 00 with declare target added
  
  return EXIT_SUCCESS;
}

The spec states "A variable referenced in a target region but not the target construct that is not declared in the target region must appear in a declare
target directive."
This variable is referenced in the target region AND target construct so according to the above it does not need to have a declare target.
Furthermore there is this in the spec: "A variable referenced in a target construct that is not declared in the construct is implicitly treated as if it had appeared in a map clause with a map-type of tofrom", so even the map clauses in this case are redundant.

Could you please explain, why icc requires me to add the declare target, which breaks the map clauses and requires target updates instead?

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.