为什么循环变量必须在并行 for 中进行签名?

发布于 2024-09-28 14:59:31 字数 360 浏览 9 评论 0原文

我刚刚通过在线教程和资源学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪淡了忧伤 2024-10-05 14:59:31

引用 为什么不允许使用未签名的 OpenMP 索引变量?

根据OpenMP 2.0 C/C++
API
规范 (pdf),部分
2.4.1,这是for循环的限制之一。没有给出理由
为了它,但我怀疑这只是为了
简化代码的假设
编译器必须做出,因为
有特殊的代码来确保
范围不超过最大值
类型的值。

OpenMP 3.0 显然允许
无符号类型也有,但我没见过
它还在行动。

简而言之,它是标准的一部分,下一版本将允许无符号整数。

Quoting from Why aren't unsigned OpenMP index variables allowed? :

According to the OpenMP 2.0 C/C++
API
specification (pdf), section
2.4.1, that's one of the restrictions of the for loop. No reason is given
for it, but I suspect it's just to
simplify the assumptions that the code
and compiler have to make, since
there's special code to ensure that
the range doesn't overflow the maximum
value of the type.

OpenMP 3.0 apparently allows for
unsigned types too, but I haven't seen
it in action yet.

In short, it's part of the standard and the next version will allow unsigned integers.

冷心人i 2024-10-05 14:59:31

这背后可能有一个原因。 同一篇文章

  • b, ub, incr 是循环不变的有符号整数表达式,
  • exit_cond 的形式为:iv <= ub< /code> 或 iv < ubiv >= ubiv >= 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 and
  • exit_cond takes form: iv <= ub or iv < ub or iv >= ub or iv > ub (where iv is the iteration variable you ask about)

since the exit_cond condition involves a comparison and the comparison is done against a signed ub variable the loop variable iv has to be signed to avoid possible problems with signed/unsigned comparison.

心凉 2024-10-05 14:59:31

根据 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.

只想待在家 2024-10-05 14:59:31

回答您关于gcc的第一个问题。不,看起来gcc很容易接受unsignedsize_t循环变量,

#pragma omp parallel for
for (size_t i = 0; i < N; ++i) {
  /* do it */
}

至少像我的(64位ubuntu上的gcc v 4.4)一样不要抱怨,并做正确的事。

To answer your first question about gcc. No, it seems that gcc easily accepts unsigned or size_t loop variables in something like

#pragma omp parallel for
for (size_t i = 0; i < N; ++i) {
  /* do it */
}

at least mine (gcc v 4.4 on a 64bit ubuntu) doesn't complain and does the right thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文