【问题标题】:How to use vectorise option in gcc如何在 gcc 中使用矢量化选项
【发布时间】:2016-02-24 16:19:12
【问题描述】:

Linux 3.13.0-68-generic x86_64 Ubuntu 14.04.3 gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

我有一个简单的程序,我想实际使用矢量化。

#include <stdio.h>
#include <sys/time.h>
struct timeval stop, start;

int main() {
    int i;

    int x[8192], y[8192];
    int a = 1, b = 2, c = 3;

    gettimeofday(&start, NULL);

    for (i = 0; i < 8192; i++) {
            y[i] = a * x[i] * x[i] + b * x[i] + c;
    }

    gettimeofday(&stop, NULL);

    printf("%d us\n", stop.tv_usec - start.tv_usec);

      for (i = 0; i < 8192; i++) {
        printf("%d\r", y[i]);
      }
    return 0;
}

如果我使用

-O0时间是30 us

-O1时间是3 us

-O2时间是1 or 0 us

但是如果我使用

-finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-loop-distribute-patterns -ftree-slp-vectorize -fvect-cost-model -ftree-partial-pre -fipa-cp-clone -ftree-vectorize

这些都是我能找到的矢量化选项,但时间消耗与-O0相同

我应该使用什么正确的选项?

【问题讨论】:

  • -O3 启用矢量化和其他几个选项。您的一堆其他选项可能不会产生预期的效果,因为您没有指定与它们组合的优化级别。
  • 另外,您的程序包含未定义的行为,编译器可能根本不执行任何优化。如果我是你,我会尝试设计一个更好的测试用例。
  • 请注意,您的程序根本不使用循环的结果,因此它是一个无用的基准测试,因为编译器只是一起消除了循环,并且只执行了 2 个 gettimeofday 调用和一个 printf。您可以使用gcc -S 生成汇编代码并查看发生了什么,以获取各种优化级别/标志。
  • 不要只发布链接。网站规则要求将您的问题in中的所有相关部分作为文本(不是图片)发布。
  • @Olaf 我修改了它。

标签: c gcc optimization vector vectorization


【解决方案1】:

gcc -O2 -ftree-vectorize -ftree-vectorizer-verbose=1 main.c

这是输出:

main.c: In function ‘main’:
main.c:19:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__suseconds_t’ [-Wformat=]
   printf("%d us\n", stop.tv_usec - start.tv_usec);
   ^

Analyzing loop at main.c:21

Analyzing loop at main.c:13


Vectorizing loop at main.c:13

main.c:13: note: LOOP VECTORIZED.
main.c:5: note: vectorized 1 loops in function.

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 2011-12-08
    • 2017-05-15
    • 2017-02-18
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多