Questions tagged [loop-unrolling]

Loop unrolling is a loop optimization strategy.

164 questions
108
votes
9 answers

When, if ever, is loop unrolling still useful?

I've been trying to optimize some extremely performance-critical code (a quick sort algorithm that's being called millions and millions of times inside a monte carlo simulation) by loop unrolling. Here's the inner loop I'm trying to speed up: //…
45
votes
1 answer

What does #pragma unroll do exactly? Does it affect the number of threads?

I'm new to CUDA, and I can't understand loop unrolling. I've written a piece of code to understand the technique __global__ void kernel(float *b, int size) { int tid = blockDim.x * blockIdx.x + threadIdx.x; #pragma unroll for(int…
Magzhan Abdibayev
  • 657
  • 1
  • 7
  • 12
25
votes
2 answers

std::array with aggregate initialization on g++ generates huge code

On g++ 4.9.2 and 5.3.1, this code takes several seconds to compile and produces a 52,776 byte executable: #include #include int main() { constexpr std::size_t size = 4096; struct S { float f; S() :…
isanae
  • 3,253
  • 1
  • 22
  • 47
21
votes
3 answers

How can I stop Clang from overexpanding nested loops via templates?

Consider this code: #include typedef long xint; template struct foz { template static void foo(xint t) { for (int j=0; j<10; ++j) { foo (t+j); } } template<> static void…
19
votes
5 answers

Self-unrolling macro loop in C/C++

I am currently working on a project, where every cycle counts. While profiling my application I discovered that the overhead of some inner loop is quite high, because they consist of just a few machine instruction. Additionally the number of…
Karsten
  • 1,814
  • 2
  • 17
  • 32
15
votes
2 answers

How to vectorize my loop with g++?

The introductory links I found while searching: 6.59.14 Loop-Specific Pragmas 2.100 Pragma Loop_Optimize How to give hint to gcc about loop count Tell gcc to specifically unroll a loop How to Force Vectorization in C++ As you can see most of them…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
15
votes
8 answers

Alternative to if, else if

I have a lot of if, else if statements and I know there has to be a better way to do this but even after searching stackoverflow I'm unsure of how to do so in my particular case. I am parsing text files (bills) and assigning the name of the service…
Milne
  • 850
  • 1
  • 11
  • 28
13
votes
4 answers

How can GCC unroll a loop if its number of iterations is unknown at compile time?

I was reading the optimization options for GCC when I found the option -funroll-all-loops. Its description reads: Unroll all loops, even if their number of iterations is uncertain when the loop is entered. This usually makes programs run more…
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
13
votes
4 answers

Can modern compilers unroll `for` loops expressed using begin and end iterators

Consider the following code vector v; // fill v const vector::iterator end =v.end(); for(vector::iterator i = v.bgin(); i != end; ++i) { // do stuff } Are compilers like g++, clang++, icc able to unroll loops like…
san
  • 4,144
  • 6
  • 32
  • 50
10
votes
2 answers

Java JIT loop unrolling policy?

What is the loop unrolling policy for JIT? Or if there is no simple answer to that, then is there some way i can check where/when loop unrolling is being performed in a loop? GNode child = null; for(int i=0;i<8;i++){ child =…
R.K
  • 153
  • 1
  • 7
10
votes
5 answers

Allowing struct field to overflow to the next field

Consider the following simple example: struct __attribute__ ((__packed__)) { int code[1]; int place_holder[100]; } s; void test(int n) { int i; for (i = 0; i < n; i++) { s.code[i] = 1; } } The for-loop is writing to the field code, which…
Amir Gonnen
  • 3,525
  • 4
  • 32
  • 61
10
votes
1 answer

Loop unrolling behaviour in GCC

This question is in part a follow up question to GCC 5.1 Loop unrolling. According to the GCC documentation, and as stated in my answer to the above question, flags such as -funroll-loops turn on "complete loop peeling (i.e. complete removal of…
Pyves
  • 6,333
  • 7
  • 41
  • 59
9
votes
4 answers

How do optimizing compilers decide when and how much to unroll a loop?

When a compiler performs a loop-unroll optimization, how does it determined by which factor to unroll the loop or whether to unroll the whole loop? Since this is a space-performance trade-off, on average how effictive is this optimization technique…
Mike G
  • 4,829
  • 11
  • 47
  • 76
9
votes
2 answers

How to ask GCC to completely unroll this loop (i.e., peel this loop)?

Is there a way to instruct GCC (I'm using 4.8.4) to unroll the while loop in the bottom function completely, i.e., peel this loop? The number of iterations of the loop is known at compilation time: 58. Let me first explain what I have tried. By…
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
9
votes
3 answers

Why does loop unrolling have no effect for a large dataset?

I wanted to benchmark the difference in execution speed between an unrolled loop and a for loop applied on a triangle object. The entire example is available here. Here is the complete code: #include #include #include…
tmaric
  • 5,347
  • 4
  • 42
  • 75
1
2 3
10 11