告诉GCC专门展开循环 [英] Tell gcc to specifically unroll a loop
问题描述
我怎么能告诉GCC解开一个特定的循环?
我已经使用CUDA SDK,其中循环可以使用手动解开的#pragma解开
。是否有GCC类似的功能?我GOOGLE了一下,但没有找到任何东西。
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优化(串......)
,因为看到在<一个href=\"http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas\">GCC文档。请注意,编译使得优化全球的余下的功能。如果你使用的#pragma push_options
和pop_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
andpop_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功能属性文档的关于这个问题的更详细的论文。举个例子:
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有多好是展开反反复循环(我这样做是为了让降价打我的code不错)。这些例子应编译正常,虽然。
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屋!