gpt4 book ai didi

c++ - 如何自动矢量化基于范围的 for 循环?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:39 24 4
gpt4 key购买 nike

在 SO for g++ 上发布了一个类似的问题,这个问题相当模糊,所以我想我应该发布一个针对 VC++12/VS2013 的具体示例,希望我们能得到答案。

cross-link: g++ , range based for and vectorization

MSDN 给出了以下作为可以向量化的循环的示例:

for (int i=0; i<1000; ++i)
{
A[i] = A[i] + 1;
}

( http://msdn.microsoft.com/en-us/library/vstudio/jj658585.aspx )

这是我的基于范围的类似上述内容的版本,一个 c 风格的怪物,以及一个使用 std::for_each 的类似循环。我使用 /Qvec-report:2 标志进行编译,并将编译器消息添加为注释:

#include <vector>
#include <algorithm>

int main()
{
std::vector<int> vec(1000, 1);

// simple range-based for loop
{
for (int& elem : vec)
{
elem = elem + 1;
}
} // info C5002 : loop not vectorized due to reason '1304'

// c-style iteration
{
int * begin = vec.data();
int * end = begin + vec.size();

for (int* it = begin; it != end; ++it)
{
*it = *it + 1;
}
} // info C5001: loop vectorized

// for_each iteration
{
std::for_each(vec.begin(), vec.end(), [](int& elem)
{
elem = elem + 1;
});
} // (no compiler message provided)

return 0;
}

只有 c 风格的循环被向量化。原因 1304 根据 the MSDN docs 如下所示:

1304: Loop includes assignments that are of different sizes.

它给出了以下作为触发 1304 消息的代码示例:

void code_1304(int *A, short *B)
{
// Code 1304 is emitted when the compiler detects
// different sized statements in the loop body.
// In this case, there is an 32-bit statement and a
// 16-bit statement.

// In cases like this consider splitting the loop into loops to
// maximize vector register utilization.

for (int i=0; i<1000; ++i)
{
A[i] = A[i] + 1;
B[i] = B[i] + 1;
}
}

我不是专家,但我看不出其中的关系。这只是错误报告吗?我注意到我的基于范围的循环在我的实际程序中都没有被矢量化。给了什么?

(如果这是错误行为,我正在运行 VS2013 专业版 12.0.21005.1 REL)

编辑:已发布错误报告:https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized

最佳答案

在此处发布错误报告:

https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized

响应:

Hi, thanks for the report.

Vectorizing range-based-for-loop-y code is something we are actively making better. We'll address vectorizing this, plus enabling auto-vectorization for other C++ language & library features in future releases of the compiler.

The emission of reason code 1304 (on x64) and reason code 1301 (on x86) are artifacts of compiler internals. The details of that, for this particular code, is not important.

Thanks for the report! I am closing this MSConnect item. Feel free to respond if you need anything else.

Eric Brumer Microsoft Visual C++ Team

关于c++ - 如何自动矢量化基于范围的 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19801691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com