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

标签 c++ vectorization visual-studio-2013

在 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/

相关文章:

performance - 向量化三重循环 - MATLAB

algorithm - 向量化搜索包含给定子排列(带重复)的排列(带重复)

c++ - VS2013中在哪里定义宏

c++ - 仅需要 k 位时的快速求幂 - 续

c++ - 在创建 QApplication 之前使用 Qt 元类型系统是否可以?

c++ - 如何将字符串复制到新分配的内存中?

c++ - 协助调试 OpenGL glsl 着色器或使用它的代码

macros - 半自动函数向量化 (Julia)

Azure SDK for .NET (VS2013) 2.5 需要 Visual Studio 2013 更新 3 或更高版本

c++ - 我是否需要在我的 for 循环中使用小于 (<) 运算符来进行自动矢量化?