告诉 gcc 专门展开一个循环 [英] Tell gcc to specifically unroll a loop

查看:47
本文介绍了告诉 gcc 专门展开一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉 GCC 展开特定循环?我使用了 CUDA SDK,其中可以使用 #pragma unroll 手动展开循环.gcc 有类似的功能吗?我用谷歌搜索了一下,但没有找到任何东西.

How can I tell GCC to unroll a particular loop? I have used the CUDA SDK where loops can be unrolled manually using #pragma unroll. Is there a similar feature for gcc? I googled a bit but could not find anything.

推荐答案

GCC 为您提供了几种不同的处理方式:

GCC gives you a few different ways of handling this:

  • 使用#pragma 指令,例如#pragma GCC optimize ("string"...),如GCC 文档.请注意,编译指示对其余函数进行了全局优化.如果您巧妙地使用了 #pragma push_optionspop_options 宏,您可能可以仅围绕这样的一个函数来定义它:

  • Use #pragma directives, like #pragma GCC optimize ("string"...), as seen in the GCC docs. Note that the pragma makes the optimizations global for the remaining functions. If you used #pragma push_options and pop_options macros cleverly, you could probably define this around just one function like so:

#pragma GCC push_options
#pragma GCC optimize ("unroll-loops")

//add 5 to each element of the int array.
void add5(int a[20]) {
    int i = 19;
    for(; i > 0; i--) {
        a[i] += 5;
    }
}

#pragma GCC pop_options

  • 使用 GCC 的属性语法注释单个函数:检查 GCC函数属性 docs 有关该主题的更详细的论文.一个例子:

  • Annotate individual functions with GCC's attribute syntax: check the GCC function attribute docs for a more detailed dissertation on the subject. An example:

    //add 5 to each element of the int array.
    __attribute__((optimize("unroll-loops")))
    void add5(int a[20]) {
        int i = 19;
        for(; i > 0; i--) {
            a[i] += 5;
        }
    }
    

  • 注意:我不确定 GCC 在展开反向迭代循环方面有多好(我这样做是为了让 Markdown 能够很好地处理我的代码).不过,这些示例应该可以很好地编译.

    Note: I'm not sure how good GCC is at unrolling reverse-iterated loops (I did it to get Markdown to play nice with my code). The examples should compile fine, though.

    这篇关于告诉 gcc 专门展开一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆