【问题标题】:g++ optimization breakes for loopg ++优化中断循环
【发布时间】:2021-02-04 01:57:53
【问题描述】:

所以,我这里有这个coden-p

for (int t = WILSON_TEMPORAL_START, t_i = 0; t <= WILSON_TEMPORAL_END; t++, t_i++)
{
    for (int r = WILSON_SPATIAL_START, r_i = 0; r <= WILSON_SPATIAL_END; r++, r_i++)
    {
        std::cout << r << " " << WILSON_SPATIAL_END << std::endl;
        wilson_loop[conf_id][t_i][r_i] = compute_wilson_loop(t, r, iu1, iu2);
    }
}

我在两个不同的优化版本-O1-O2 中运行我的g++ 编译器,但是终端输出不同。

-O1

2 12
3 12
4 12
5 12
6 12
7 12
8 12
9 12
10 12
11 12
12 12
2 12
3 12
4 12
...

-O2

2 12
3 12
4 12
5 12
6 12
7 12
8 12
9 12
10 12
11 12
12 12
13 12
14 12
15 12
...

如果我将内部循环更改为:

for (int t = WILSON_TEMPORAL_START, t_i = 0; t <= WILSON_TEMPORAL_END; t++, t_i++)
{
    for (int r = WILSON_SPATIAL_START, r_i = 0; r <= WILSON_SPATIAL_END; r++, r_i++)
    {
        std::cout << r << " " << WILSON_SPATIAL_END << std::endl;
        // wilson_loop[conf_id][t_i][r_i] =
        compute_wilson_loop(t, r, iu1, iu2);
    }
}

一些有用的定义是:

static double compute_wilson_loop(int time_extend, int space_extend, SUN_mat iu1[VOL][DIM], SUN_mat iu2[VOL][DIM])
void wilson::measure_wilson_loop(SUN_mat pu[VOL][DIM], double wilson_loop[CLEN][TLEN][SLEN], int conf_id) {
...
}

我知道,我可以在这里找到解决方法,但我真的很想了解为什么会发生这种情况。

【问题讨论】:

  • compute_wilson_loop 是如何声明的?什么是wilson_loop
  • 如果优化影响该循环的输出,我猜你有未定义的行为。您需要创建一个minimal reproducible example,以便我们可以看到所有涉及的变量的声明。 wilson_loop 数组和 conf_id 的值特别有趣。
  • 顺便说一句,spatial 不是这样拼写的。
  • 使用g++ -Wall -Wextra -g-O1-O2 编译您的代码。使用 GDBvalgrind 可能还有 Clang static analyser 和/或 address sanitizer
  • -fsanitize=undefined -fsanitize=address -fsanitize=leak -fstack-check -lasan -lubsan 也可能有用

标签: c++ for-loop optimization g++


【解决方案1】:

我刚刚解决了这个问题,我在定义预处理器常量时出错了。我仍然不确定,为什么它在使用不同的优化模式时会改变输出,但它现在可以工作了:)

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    相关资源
    最近更新 更多