簡體   English   中英

如何自動矢量化基於范圍的循環?

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

類似的問題發布在SO上,因為g ++相當模糊,所以我想我會發布一個VC ++ 12 / VS2013的具體示例,我們希望能夠得到答案。

交叉鏈接: g ++,基於范圍和矢量化

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風格的循環才能被矢量化。 根據MSDN文檔,原因1304如下:

1304:循環包括具有不同大小的分配。

它給出了以下作為觸發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

響應:

嗨,謝謝你的報道。

矢量化基於范圍的循環代碼是我們積極做得更好的事情。 我們將解決這個問題,並在將來的編譯器版本中為其他C ++語言和庫特性啟用自動矢量化。

原因代碼1304(在x64上)和原因代碼1301(在x86上)的發射是編譯器內部的偽像。 對於這個特定代碼,細節並不重要。

謝謝你的報道! 我正在關閉此MSConnect項目。 如果您還有其他需要,請隨時回復。

Eric Brumer Microsoft Visual C ++團隊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM