如何使用g ++对我的循环进行矢量化?

时间:2015-03-27 03:29:28

标签: c++ optimization g++ vectorization loop-unrolling

我在搜索时找到的介绍性链接:

  1. 6.59.14 Loop-Specific Pragmas
  2. 2.100 Pragma Loop_Optimize
  3. How to give hint to gcc about loop count
  4. Tell gcc to specifically unroll a loop
  5. How to Force Vectorization in C++
  6. 正如您所看到的,大多数都是针对C的,但我认为它们也适用于C ++。这是我的代码:

    template<typename T>
    //__attribute__((optimize("unroll-loops")))
    //__attribute__ ((pure))
    void foo(std::vector<T> &p1, size_t start,
                size_t end, const std::vector<T> &p2) {
      typename std::vector<T>::const_iterator it2 = p2.begin();
      //#pragma simd
      //#pragma omp parallel for
      //#pragma GCC ivdep Unroll Vector
      for (size_t i = start; i < end; ++i, ++it2) {
        p1[i] = p1[i] - *it2;
        p1[i] += 1;
      }
    }
    
    int main()
    {
        size_t n;
        double x,y;
        n = 12800000;
        vector<double> v,u;
        for(size_t i=0; i<n; ++i) {
            x = i;
            y = i - 1;
            v.push_back(x);
            u.push_back(y);
        }
        using namespace std::chrono;
    
        high_resolution_clock::time_point t1 = high_resolution_clock::now();
        foo(v,0,n,u);
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
    
        duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
    
        std::cout << "It took me " << time_span.count() << " seconds.";
        std::cout << std::endl;
        return 0;
    }
    

    我使用上面提到的提示,但我没有得到任何加速,因为示例输出显示(第一次运行已取消注释#pragma GCC ivdep Unroll Vector

    samaras@samaras-A15:~/Downloads$ g++ test.cpp -O3 -std=c++0x -funroll-loops -ftree-vectorize -o test
    samaras@samaras-A15:~/Downloads$ ./test
    It took me 0.026575 seconds.
    samaras@samaras-A15:~/Downloads$ g++ test.cpp -O3 -std=c++0x -o test
    samaras@samaras-A15:~/Downloads$ ./test
    It took me 0.0252697 seconds.
    

    有希望吗?或者优化标志O3只是诀窍?欢迎任何加速此代码的建议(foo函数)!

    我的g ++版本:

    samaras@samaras-A15:~/Downloads$ g++ --version
    g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
    

    请注意,循环体是随机的。我以其他形式重写它并不感兴趣。


    修改

    答案说没有其他事情可以做也是可以接受的!

2 个答案:

答案 0 :(得分:11)

O3标志自动打开-ftree-vectorize。 https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

  

-O3打开-O2指定的所有优化,并打开-finline-functions,-funswitch-loops,-fpredictive-commoning,-fgcse-after-reload,-ftree-loop-vectorize,-ftree- loop-distribute-patterns,-ftree-slp-vectorize,-fvect-cost-model,-ftree-partial-pre和-fipa-cp-clone选项

因此,在这两种情况下,编译器都在尝试进行循环向量化。

使用g ++ 4.8.2编译:

g++ test.cpp -O2 -std=c++0x -funroll-loops -ftree-vectorize -ftree-vectorizer-verbose=1 -o test

给出这个:

Analyzing loop at test.cpp:16                                                                                                                                                                                                                                               


Vectorizing loop at test.cpp:16                                                                                                                                                                                                                                             

test.cpp:16: note: create runtime check for data references *it2$_M_current_106 and *_39                                                                                                                                                                                    
test.cpp:16: note: created 1 versioning for alias checks.                                                                                                                                                                                                                   

test.cpp:16: note: LOOP VECTORIZED.                                                                                                                                                                                                                                         
Analyzing loop at test_old.cpp:29                                                                                                                                                                                                                                               

test.cpp:22: note: vectorized 1 loops in function.                                                                                                                                                                                                                          

test.cpp:18: note: Unroll loop 7 times                                                                                                                                                                                                                                      

test.cpp:16: note: Unroll loop 7 times                                                                                                                                                                                                                                      

test.cpp:28: note: Unroll loop 1 times  

在没有-ftree-vectorize标志的情况下进行编译:

g++ test.cpp -O2 -std=c++0x -funroll-loops -ftree-vectorizer-verbose=1 -o test

仅返回:

test_old.cpp:16: note: Unroll loop 7 times

test_old.cpp:28: note: Unroll loop 1 times

第16行是循环函数的开始,因此编译器肯定会对其进行向量化。检查汇编程序也确认了这一点。

我似乎在我正在使用的笔记本电脑上进行了一些积极的缓存,这使得很难准确地测量该功能运行的时间。

但是你可以尝试其他一些事情:

  • 使用__restrict__限定符告诉编译器阵列之间没有重叠。

  • 告诉编译器阵列与__builtin_assume_aligned对齐(不可移植)

这是我生成的代码(我删除了模板,因为您希望对不同的数据类型使用不同的对齐方式)

#include <iostream>
#include <chrono>
#include <vector>

void foo( double * __restrict__ p1,
          double * __restrict__ p2,
          size_t start,
          size_t end )
{
  double* pA1 = static_cast<double*>(__builtin_assume_aligned(p1, 16));
  double* pA2 = static_cast<double*>(__builtin_assume_aligned(p2, 16));

  for (size_t i = start; i < end; ++i)
  {
      pA1[i] = pA1[i] - pA2[i];
      pA1[i] += 1;
  }
}

int main()
{
    size_t n;
    double x, y;
    n = 12800000;
    std::vector<double> v,u;

    for(size_t i=0; i<n; ++i) {
        x = i;
        y = i - 1;
        v.push_back(x);
        u.push_back(y);
    }

    using namespace std::chrono;

    high_resolution_clock::time_point t1 = high_resolution_clock::now();
    foo(&v[0], &u[0], 0, n );
    high_resolution_clock::time_point t2 = high_resolution_clock::now();

    duration<double> time_span = duration_cast<duration<double>>(t2 - t1);

    std::cout << "It took me " << time_span.count() << " seconds.";
    std::cout << std::endl;

    return 0;
}

就像我说的那样,我无法获得一致的时间测量值,因此无法确认这是否会使您的性能提升(甚至可能会降低!)

答案 1 :(得分:1)

GCC对编译器进行了扩展,创建了将使用SIMD指令的新原语。有关详细信息,请查看here

大多数编译器都说它们会自动向量化操作,但这取决于编译器模式匹配,但正如你想象的那样,这可能非常受欢迎。