oracle 作用域,7.1 使用作用域检查功能

7.1 使用作用域检查功能

要启用作用域检查,应使用 -xvpara 和 -xopenmp 选项在优化级别 -xO3 或更高级别上编译 OpenMP 程序。如果只使用 -xopenmp=noopt 编译该程序,作用域检查将不起作用。如果优化级别低于 -xO3,编译器将发出警告消息,且不执行任何作用域检查。

在作用域检查期间,编译器将检查所有 OpenMP 构造。如果确定有些变量的作用域时引发问题,编译器会发出警告消息,有时还会建议使用正确的数据共享属性子句。

例如:

示例 7-1 作用域检查% cat t.c

#include

#include

int main()

{

int g[100], b, i;

memset(g, 0, sizeof(int)*100);

#pragma omp parallel for shared(b)

for (i = 0; i < 100; i++)

{

b += g[i];

}

return 0;

}

%cc -xopenmp -xO3 -xvpara source1.c

"source1.c", line 10: Warning: inappropriate scoping

variable 'b' may be scoped inappropriately as 'shared'

. write at line 13 and write at line 13 may cause data race

"source1.c", line 10: Warning: inappropriate scoping

variable 'b' may be scoped inappropriately as 'shared'

. write at line 13 and read at line 13 may cause data race

如果优化级别低于 -xO3,编译器将不进行作用域检查:% cc -xopenmp=noopt -xvpara source1.c

"source1.c", line 10: Warning: Scope checking under vpara compiler

option is supported with optimization level -xO3 or higher.

Compile with a higher optimization level to enable this feature

更复杂的示例:

示例 7-2 source2% cat source2.c

#include

int main()

{

int g[100];

int r=0, a=1, b, i;

#pragma omp parallel for private(a) lastprivate(i) reduction(+:r)

for (i = 0; i < 100; i++)

{

g[i] = a;

b = b + g[i];

r = r * g[i];

}

a = b;

return 0;

}

% cc -xopenmp -xO3 -xvpara source2.c

"source2.c", line 8: Warning: inappropriate scoping

variable 'r' may be scoped inappropriately as 'reduction'

. reference at line 13 may not be a reduction of the specified type

"source2.c", line 8: Warning: inappropriate scoping

variable 'a' may be scoped inappropriately as 'private'

. read at line 11 may be undefined

. consider 'firstprivate'

"source2.c", line 8: Warning: inappropriate scoping

variable 'i' may be scoped inappropriately as 'lastprivate'

. value defined inside the parallel construct is not used outside

. consider 'private'

"source2.c", line 8: Warning: inappropriate scoping

variable 'b' may be scoped inappropriately as 'shared'

. write at line 12 and write at line 12 may cause data race

"source2.c", line 8: Warning: inappropriate scoping

variable 'b' may be scoped inappropriately as 'shared'

. write at line 12 and read at line 12 may cause data race

上述虚构示例显示了作用域检查可以检测到的一些典型作用域错误。

r 被指定为归约变量,其操作为 +,但实际上操作应为 *。

a 的作用域显式确定为 PRIVATE。由于 PRIVATE 变量没有初始值,因此第 11 行中对 a 的引用可能会显示一些乱码。编译器会指出此问题,并建议程序员考虑将 a 的作用域确定为 FIRSTPRIVATE。

变量 i 是循环索引变量。有些情况下,如果在循环后使用循环索引值,程序员可能希望将其指定为 LASTPRIVATE。但在以上示例中不是这样;在循环后根本没有引用 i。编译器会发出警告,并建议程序员将 i 的作用域确定为 PRIVATE。使用 PRIVATE 而不是 LASTPRIVATE 可以提高性能。

程序员不为变量 b 显式指定数据共享属性。根据 OpenMP 3.0 规范第 79 页的第 27-28 行,将 b 的作用域隐式确定为 SHARED。但是,将 b 的作用域确定为 SHARED 将导致数据争用。b 的正确数据共享属性应为 REDUCTION。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值