【发布时间】: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编译您的代码。使用 GDB 和 valgrind 可能还有 Clang static analyser 和/或 address sanitizer -
-fsanitize=undefined -fsanitize=address -fsanitize=leak -fstack-check -lasan -lubsan也可能有用
标签: c++ for-loop optimization g++