简体   繁体   English

OMP目标中对全局数组(malloc与静态)的不同处理

[英]Different handling of global arrays (malloc vs static) in OMP target

There are 2 ways to have a global array: Use a pointer and malloc, or just define it as an array: 拥有全局数组的方法有2种:使用指针和malloc,或仅将其定义为数组:

#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. 我虽然在处理方面等效,但是却发现了巨大的区别:当我将它们映射到目标时,实际上只会映射gVal。 If I want the values of gArray on my device I have to use "target update". 如果要在设备上使用gArray的值,则必须使用“目标更新”。 Is this a bug in the current Intel compiler or is this covered in the spec? 这是当前Intel编译器中的错误,还是规范中涵盖了该错误? Could not find anything specific to this. 找不到与此相关的任何内容。

BTW: Using a local array (no declare target) works as intended. 顺便说一句:使用本地数组(不声明目标)可以按预期工作。

Test code: 测试代码:

for(i=0;i<ct;i++){
  gArray[i]=1;gVals[i]=1;
}

#pragma omp target map(gArray[0:2],gVals[0:2])
{
  printf("gArrayTarget(1111): %d%d%d%d\n",gArray[0],gArray[1],gVals[0],gVals[1]);
  gArray[1]=2;gVals[1]=2;
}
printf("gArrayHost(1212): %d%d%d%d\n",gArray[0],gArray[1],gVals[0],gVals[1]);

declare target puts the enclosed variables in the initial device context (2.9.4 -- declare target Directive): declare target将包含的变量放在初始设备上下文中(2.9.4- declare target指令):

If a list item is a variable then the original variable is mapped to a corresponding variable in the initial device data environment for all devices. 如果列表项是变量,则原始变量将在所有设备的初始设备数据环境中映射到相应的变量。 If the original variable is initialized, the corresponding variable in the device data environment is initialized with the same value. 如果原始变量已初始化,则设备数据环境中的相应变量将使用相同的值初始化。

Your gArray variable is not initialised, therefore the device copy is not initialised either. 您的gArray变量未初始化,因此设备副本也未初始化。 But it becomes part of the initial device data environment. 但这已成为初始设备数据环境的一部分。 The map clause then has no effect on gArray because (2.14.5 -- map Clause): 然后, map子句对gArray因为(2.14.5- map子句):

If a corresponding list item of the original list item is in the enclosing device data environment, the new device data environment uses the corresponding list item from the enclosing device data environment. 如果原始列表项的相应列表项位于封闭设备数据环境中,则新设备数据环境将使用封闭设备数据环境中的相应列表项。 No additional storage is allocated in the new device data environment and neither initialization nor assignment is performed, regardless of the map-type that is specified. 在新设备数据环境中,不会分配任何额外的存储,并且无论指定的映射类型如何,都不会执行初始化或分配。

The initial device data environment is enclosing in respect to the new data environment created by the target directive. 初始设备数据环境与target指令创建的新数据环境相对应。

As to gVals , the following apply (2.14.5 -- map Clause): 对于gVals ,适用以下规定(2.14.5- map子句):

If the type of the variable appearing in an array section is pointer, reference to array, or reference to pointer then the variable is implicitly treated as if it had appeared in a map clause with a map-type of alloc . 如果出现在数组部分中的变量的类型是指针,对数组的引用或对指针的引用,则该变量将被隐式处理,就好像它已出现在map子句中,且其map类型alloc The corresponding variable is assigned the address of the storage location of the corresponding array section in the new device data environment. 在新设备数据环境中,为相应的变量分配了相应数组部分的存储位置的地址。

Therefore it is not a bug in the Intel compiler but exactly the standard-described behaviour. 因此,这不是Intel编译器中的错误,而是标准描述的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM