gpt4 book ai didi

c++ - 为什么向量化失败?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:16 24 4
gpt4 key购买 nike

我想使用

优化我的矢量化代码
-msse2 -ftree-vectorizer-verbose=2.

我有以下简单代码:

int main(){
int a[2048], b[2048], c[2048];
int i;

for (i=0; i<2048; i++){
b[i]=0;
c[i]=0;
}

for (i=0; i<2048; i++){
a[i] = b[i] + c[i];
}
return 0;
}

为什么我会收到便条

 test.cpp:10: note: not vectorized: not enough data-refs in basic block.

谢谢!

最佳答案

对于它的值(value),在添加 asm volatile("": "+m"(a), "+m"(b), "+m"(c)::"memory"); main 的末尾,我的 gcc 发出了这个:

400610:       48 81 ec 08 60 00 00    sub    $0x6008,%rsp
400617: ba 00 20 00 00 mov $0x2000,%edx
40061c: 31 f6 xor %esi,%esi
40061e: 48 8d bc 24 00 20 00 lea 0x2000(%rsp),%rdi
400625: 00
400626: e8 b5 ff ff ff callq 4005e0 <memset@plt>
40062b: ba 00 20 00 00 mov $0x2000,%edx
400630: 31 f6 xor %esi,%esi
400632: 48 8d bc 24 00 40 00 lea 0x4000(%rsp),%rdi
400639: 00
40063a: e8 a1 ff ff ff callq 4005e0 <memset@plt>
40063f: 31 c0 xor %eax,%eax
400641: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)
400648: c5 f9 6f 84 04 00 20 vmovdqa 0x2000(%rsp,%rax,1),%xmm0
40064f: 00 00
400651: c5 f9 fe 84 04 00 40 vpaddd 0x4000(%rsp,%rax,1),%xmm0,%xmm0
400658: 00 00
40065a: c5 f8 29 04 04 vmovaps %xmm0,(%rsp,%rax,1)
40065f: 48 83 c0 10 add $0x10,%rax
400663: 48 3d 00 20 00 00 cmp $0x2000,%rax
400669: 75 dd jne 400648 <main+0x38>

因此它认识到,第一个循环只是对几个数组执行 memset,第二个循环正在执行 vector 加法,并对其进行了适当的向量化。

我正在使用 gcc 版本 4.9.0 20140521(预发布)(GCC)

装有 gcc 4.7.2 版 (Debian 4.7.2-5) 的旧机器也对循环进行矢量化处理,但方式不同。您的 -ftree-vectorizer-verbose=2 设置使其发出以下输出:

Analyzing loop at foo155.cc:10


Vectorizing loop at foo155.cc:10

10: LOOP VECTORIZED.
foo155.cc:1: note: vectorized 1 loops in function.

您可能弄错了编译器标志(我使用 g++ -O3 -ftree-vectorize -ftree-vectorizer-verbose=2 -march=native foo155.cc -o foo155 来构建)或者有一个非常老的编译器。

关于c++ - 为什么向量化失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23937003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com