Why does the performance of my #pragma-unrolled loop degrade if the trip count is not constant?

半腔热情 提交于 2019-12-04 19:01:31

问题


I have following code using loop unrolling:

#pragma unroll
for (int i=0;i<n;i++)
{
    ....
}

here if n is a defined constant, everything works fine. However, if n is a variable, performance dramatically reduced. I noticed roughly 3 times the instructions are issued and executed. I guess I am looking for a way to do loop unrolling at run time, may be that's just not feasible.


回答1:


CUDA is a compiled language. Loop unrolling is a compiler optimization. Runtime loop unrolling would imply some sort of runtime interpreter or dynamic code generation. That clearly can't happen.

It would make sense that the unrolled case executes as many or more instructions than the naïve loop, because the compiler will replace the loop with repetitions of the loop contents. If the unrolled case executes less instructions, that would imply that the compiler is pre-calculating some or all of the loop contents and replacing code with a constant result.

It all depends on what is contained in the loop.



来源:https://stackoverflow.com/questions/5495634/why-does-the-performance-of-my-pragma-unrolled-loop-degrade-if-the-trip-count-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!