GCC自动矢量化对运行时没有影响,即使被称为"有利可图"

use*_*263 8 c gcc auto-vectorization

在过去的几天里,我一直在阅读gcc 4.7的自动向量化.我按照我在网上看到的一些例子,设置似乎是正确的.但是当我实际运行代码并在矢量化开启或关闭之间进行比较时,运行时没有明显的差异.

这是我一直在使用的代码:

#include <string.h>
#include <stdlib.h>
#include <emmintrin.h>
#include <stdio.h>
#include <math.h>

int main(int argc, char** argv) {

    long b = strtol(argv[2], NULL, 0); 
    unsigned long long int i;
    unsigned long long int n = (int)pow(2,29);                                                                                                                                                                                            
    float total = 0;

    float *__restrict__ x1; 
    float *__restrict__ y1; 

    posix_memalign((void *)&x1, 16, sizeof(float)*n);
    posix_memalign((void *)&y1, 16, sizeof(float)*n);


    float *__restrict__ x = __builtin_assume_aligned(x1,16);
    float *__restrict__ y = __builtin_assume_aligned(y1,16);

    for (i=0;i<n;i++) {
            x[i] = i;
            y[i] = i;
    }   

    for (i=0; i<n; i++) {
            y[i] += x[i];
    }   

    printf("y[%li]: \t\t\t\t%f\n",  b,y[b]);
    printf("correct answer: \t\t\t%f\n", (b)*2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

其中一些对我来说似乎是多余的,但是有必要让编译器了解发生了什么(特别是数据已经对齐的事实).从命令行读取的"b"变量就在那里,因为我对编译器完全优化掉循环感到偏执.

以下是启用vectorizeration时的编译器命令:

gcc47 -ftree-vectorizer-verbose=3 -msse2 -lm -O2 -finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload -fipa-cp-clone test.c -ftree-vectorize -o v
Run Code Online (Sandbox Code Playgroud)

基本上,这相当于只使用-O3.我把旗帜放在自己身上,这样我所需要做的就是删除"ftree-vectorize"并能够测试结果sans vectorization.

这是ftree-vectorize-verbose标志的输出,以显示代码实际上是矢量化的:

Analyzing loop at test.c:29

29: vect_model_load_cost: aligned.
29: vect_model_load_cost: inside_cost = 1, outside_cost = 0 .
29: vect_model_load_cost: aligned.
29: vect_model_load_cost: inside_cost = 1, outside_cost = 0 .
29: vect_model_simple_cost: inside_cost = 1, outside_cost = 0 .
29: vect_model_store_cost: aligned.
29: vect_model_store_cost: inside_cost = 1, outside_cost = 0 .
29: cost model: Adding cost of checks for loop versioning aliasing.

29: Cost model analysis: 
  Vector inside of loop cost: 4
  Vector outside of loop cost: 4
  Scalar iteration cost: 4
  Scalar outside cost: 1
  prologue iterations: 0
  epilogue iterations: 0
  Calculated minimum iters for profitability: 2

29:   Profitability threshold = 3


Vectorizing loop at test.c:29

29: Profitability threshold is 3 loop iterations.
29: created 1 versioning for alias checks.

29: LOOP VECTORIZED.
Analyzing loop at test.c:24

24: vect_model_induction_cost: inside_cost = 2, outside_cost = 2 .
24: vect_model_simple_cost: inside_cost = 2, outside_cost = 0 .
24: not vectorized: relevant stmt not supported: D.5806_18 = (float) D.5823_58;

test.c:7: note: vectorized 1 loops in function.
Run Code Online (Sandbox Code Playgroud)

请注意,矢量化在3次迭代后是有利可图的,并且我正在运行2 ^ 29~ = 500,000,000次迭代.所以我应该期待关闭矢量化的运行时差异很大,对吧?

好吧,这是代码的运行时(我连续运行了20次):

59.082s                                                                                                                                                                                                                                       
79.385s
57.557s
57.264s
53.588s
54.300s
53.645s
69.044s
57.238s
59.366s
56.314s
55.224s
57.308s
57.682s
56.083s
369.590s
59.963s
55.683s
54.979s
62.309s
Run Code Online (Sandbox Code Playgroud)

抛弃那些奇怪的~370s异常值,它的平均运行时间为58.7s,标准偏差为6.0s.

接下来,我将使用与之前相同的命令进行编译,但是没有-ftree-vectorize标志:

gcc47 -ftree-vectorizer-verbose=3 -msse2 -lm -O2 -finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload -fipa-cp-clone test.c -o nov
Run Code Online (Sandbox Code Playgroud)

再次连续运行程序20次会产生以下时间:

69.471s                                                                                                                                                                                                                                       
57.134s
56.240s
57.040s
55.787s
56.530s
60.010s
60.187s
324.227s
56.377s
55.337s
54.110s
56.164s
59.919s
493.468s
63.876s
57.389s
55.553s
54.908s
56.828s
Run Code Online (Sandbox Code Playgroud)

再次抛弃异常值,这给出了57.9s的平均运行时间,标准偏差为3.6s.

因此,这两个版本在统计上无法区分运行时.

谁能指出我做错了什么?编译器吐出的"盈利门槛"是不是意味着我的意思?我真的很感激人们可以给我的任何帮助,我一直试图在过去一周内弄清楚这一点.

编辑:

我实施了@nilspipenbrinck建议的更改,它似乎有效.我将矢量化循环卡在一个函数中,并将该函数称为多次.对于无矢量化,相对运行时间现在为24.0s(西格玛值<0.1s),而对于矢量化,相对运行时间为20.8s(西格玛值<0.2s),或速度提高13%.没有我希望的那么多,但至少现在我知道它的工作!感谢您花时间看我的问题并写下答案,我真的很感激.

Nil*_*nck 5

您不需要做太多的算术运算。因此,测试代码的运行时受内存限制。例如,您花费大部分时间通过在CPU和内存之间移动数据。

此外,您的n非常大,包含2 ^ 29个元素。因此,您不会以任何方式受益于第一级和第二级缓存。

如果要查看SSE的改进,请使用较小的n,以便仅触摸8或16 KB的数据。还要确保数据是“热”数据,例如,CPU最近已访问过该数据。这样,数据不必从主存储器中移出,而是从高速缓存中移出,这快了几个数量级。

作为替代,你还可以做很多更多的运算。当您利用CPU进行数学运算时,这将使内存预取系统有机会从后台的主内存中获取数据。

总结:如果算术速度快于系统可以移动内存的速度,您将看不到任何好处。内存访问时间将成为瓶颈,使用SSE指令集保存的几个周期将因内存访问时序的混乱而丢失。