简体   繁体   English

如何自动矢量化基于范围的循环?

[英]How to auto-vectorize range-based for loops?

A similar question was posted on SO for g++ that was rather vague, so I thought I'd post a specific example for VC++12 / VS2013 to which we can hopefully get an answer. 类似的问题发布在SO上,因为g ++相当模糊,所以我想我会发布一个VC ++ 12 / VS2013的具体示例,我们希望能够得到答案。

cross-link: g++ , range based for and vectorization 交叉链接: g ++,基于范围和矢量化

MSDN gives the following as an example of a loop that can be vectorized: MSDN给出以下作为可以向量化的循环的示例:

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

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

Here is my version of a range-based analogue to the above, a c-style monstrosity, and a similar loop using std::for_each . 这是我对上面的基于范围的模拟的版本,c风格的怪物,以及使用std::for_each的类似循环。 I compiled with the /Qvec-report:2 flag and added the compiler messages as comments: 我用/ 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;
}

Only the c-style loop gets vectorized. 只有c风格的循环才能被矢量化。 Reason 1304 is as follows as per the MSDN docs : 根据MSDN文档,原因1304如下:

1304: Loop includes assignments that are of different sizes. 1304:循环包括具有不同大小的分配。

It gives the following as an example of code that would trigger a 1304 message: 它给出了以下作为触发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;
    }
}

I'm no expert but I can't see the relationship. 我不是专家,但我看不出这种关系。 Is this just buggy reporting? 这只是错误的报道吗? I've noticed that none of my range-based loops are getting vectorized in my actual program. 我注意到我的基于范围的循环都没有在我的实际程序中进行矢量化。 What gives? 是什么赋予了?

(In case this is buggy behavior I'm running VS2013 Professional Version 12.0.21005.1 REL) (如果这是有缺陷的行为我正在运行VS2013专业版12.0.21005.1 REL)

EDIT: Bug report posted: 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

Posted bug report here: 在这里发布错误报告:

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

Response: 响应:

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. 我们将解决这个问题,并在将来的编译器版本中为其他C ++语言和库特性启用自动矢量化。

The emission of reason code 1304 (on x64) and reason code 1301 (on x86) are artifacts of compiler internals. 原因代码1304(在x64上)和原因代码1301(在x86上)的发射是编译器内部的伪像。 The details of that, for this particular code, is not important. 对于这个特定代码,细节并不重要。

Thanks for the report! 谢谢你的报道! I am closing this MSConnect item. 我正在关闭此MSConnect项目。 Feel free to respond if you need anything else. 如果您还有其他需要,请随时回复。

Eric Brumer Microsoft Visual C++ Team Eric Brumer Microsoft Visual C ++团队

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM