Loop unrolling, or can also be called loop unwinding, is the most fundamental technique in the parallel programming world, and it is also powerful. The idea is to break down a loop into a sequence of identical operations, and attach the data index pointer accordingly.
On a serial processor, loop unrolling can fasten the execution because it reduces the loop control instructions, such as pointer arithmetic and branching instructions. In most cases, this type of software pipelining can also induce the advantage of pipeline-enabled processors, which is to hide the latency of data read-write.
1 2 3
Loop unrolling appliance:
1 2 3 4 5 6
On a device where more than one processor can execute the same instruction (i.e. parallel processors), each iteration in a loop is given to a single processor, and the next iteration is then enqueued.
The most important thing to note when dividing the workload to each thread, is the most infamous data dependency. If you are not conscientious enough when resolving this problem, it may lead to a wrong execution result, or slow execution speed. In this article, iteration in the sample snippet does not have any data dependency to the previous execution, so I will keep this topic for later.
1 2 3 4
1 2 3 4 5
1 2 3 4 5