为什么循环变量必须在并行 for 中进行签名?
我刚刚通过在线教程和资源学习 OpenMP。我想使用并行 for 循环对矩阵求平方(将其与其自身相乘)。在 IBM 编译器文档,我发现“迭代变量必须是有符号整数”的要求。 GCC 的实现也是如此吗? OpenMP 标准中有规定吗?如果是这样,这个要求有理由吗?
(这并不重要,因为预期的尺寸远小于 INT_MAX
,但它确实花费了我一些转换。)
I'm just learning OpenMP from online tutorials and resources. I want to square a matrix (multiply it with itself) using a parallel for
loop. In IBM compiler documentation, I found the requirement that "the iteration variable must be a signed
integer." Is this also true in the GCC implementation? Is it specified in the OpenMP standard? If so, is there a reason for this requirement?
(It doesn't matter much as the expected dimensions are far smaller than INT_MAX
, but it does cost me some casts.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引用 为什么不允许使用未签名的 OpenMP 索引变量?:
简而言之,它是标准的一部分,下一版本将允许无符号整数。
Quoting from Why aren't unsigned OpenMP index variables allowed? :
In short, it's part of the standard and the next version will allow unsigned integers.
这背后可能有一个原因。 同一篇文章说
b, ub, incr
是循环不变的有符号整数表达式,exit_cond
的形式为:iv <= ub< /code> 或
iv < ub
或iv >= ub
或iv >= ub
(其中iv
是您询问的迭代变量),因为
exit_cond
条件涉及比较,并且比较是针对有符号的ub< /code> 变量循环变量
iv
必须进行签名以避免有符号/无符号比较可能出现的问题。Here's a possible reason behind that. The same article says that
b, ub, incr
are loop invariant signed integer expressions andexit_cond
takes form:iv <= ub
oriv < ub
oriv >= ub
oriv > ub
(whereiv
is the iteration variable you ask about)since the
exit_cond
condition involves a comparison and the comparison is done against a signedub
variable the loop variableiv
has to be signed to avoid possible problems with signed/unsigned comparison.根据 OpenMP 3.0 规范: http://www.openmp.org/mp-documents/spec30 .pdf,变量可以是有符号或无符号整数类型,请参见2.5.1 循环构造。问题是给定的 OpenMP 实现是否符合这个最新规范。
According to OpenMP 3.0 specification: http://www.openmp.org/mp-documents/spec30.pdf, for variable may be of a signed or unsigned integer type, see 2.5.1 Loop Construct. The question is whether given OpenMP implementation matches this latest specification.
回答您关于
gcc
的第一个问题。不,看起来gcc
很容易接受unsigned
或size_t
循环变量,至少像我的(64位ubuntu上的gcc v 4.4)一样不要抱怨,并做正确的事。
To answer your first question about
gcc
. No, it seems thatgcc
easily acceptsunsigned
orsize_t
loop variables in something likeat least mine (gcc v 4.4 on a 64bit ubuntu) doesn't complain and does the right thing.