User:Aishwinth/sandbox

Loop-level parallelism is a form of parallelism in software programming that is concerned with extracting parallel tasks from loops. The opportunity for loop-level parallelism often arises in computing programs where data is stored in random access data structures. Where a sequential program will iterate over the data structure and operate on indices one at a time, a program exploiting loop-level parallelism will use multiple threads or processes which operate on some or all of the indices at the same time. Such parallelism provides a speedup to overall execution time of the program, typically in line with Amdahl's law.

Dependencies in code
Transforming a sequential program into a parallel version requires that dependencies within the code are preserved. There are several types of dependencies that can be found:

Anti-Dependence and Output Dependence can be dealt with by giving each process or thread its own copy of variables (known as privatization). However, true dependence must be preserved, requiring process synchronization.

Example of True Dependence
, meaning that S2 has a true dependence on S3 because S2 writes to the variable, which S3 reads from.

Example of Anti-dependence
, meaning that S2 has an anti-dependence on S3 because S2 reads from the variable  before S3 writes to it.

Example of Output-dependence
, meaning that S2 has an output dependence on S3 because both write to the variable.

Example of Input-dependence
, meaning that S2 has an input dependence on S3 because S2 and S3 both read from variable.

Loop-carried vs loop-independent dependence
Loops can have two types of dependence:


 * Loop-carried dependence
 * Loop-independent dependence

In loop-independent dependence, loops have inter-iteration dependence but do not have dependence between iterations. Each iteration may be treated as a block and performed in parallel without other synchronization efforts.

In the following example code used for swapping the values of two array of length n, there is a loop-independent dependence of. In loop-carried dependence, statements in an iteration of a loop depend on statements in another iteration of the loop. Loop-Carried Dependence uses a modified version of the dependence notation seen earlier. The following denotes a statement carries a true dependence to a later iteration, meaning that one iteration of the loop writes to a location read by a subsequent iteration of the loop.

Example of loop-carried dependence where.

Loop carried dependence graph
A Loop-carried dependence graph graphically shows the loop-carried dependencies between iterations. Each iteration is listed as a node on the graph, and directed edges show the true, anti, and output dependencies between each iteration.

Types of Loop-level parallelism
Loops are one of the most common sources of parallelism found within code, and can be parallelized in a variety of ways, including:


 * DOALL Parallelism
 * DOACROSS Parallelism
 * DOPIPE Parallelism
 * DISTRIBUTED Loop
 * HELIX

Each implementation varies slightly in how threads work together to manage data. In addition, parallel tasks must somehow be mapped to a processor. These tasks can either be allocated statically or dynamically. Research has shown that load-balancing can be better achieved through some dynamic allocation algorithms than when done dynamically.

DOALL parallelism
DOALL parallelism exists when statements within a loop can be executed independently (situations where there is no loop-carried dependence). For example, the following code does not read from the array, and does not update the arrays. No iterations have a dependence on any other iteration.

Let's say the time of one execution of S1 be $$Ts1$$ then the execution time for sequential form of above code is $$n*Ts1$$, Now because DOALL Parallelism exists when all iterations are independent, speedup may be achieved by executing all iterations in parallel which gives us an execution time of $$Ts1$$(the time taken for one iteration in sequential execution).

The following example, using a simplified pseudocode, shows how a loop might be parallelized to execute each iteration independently.

DOACROSS parallelism
DOACROSS Parallelism exists where iterations of a loop are parallelized by extracting calculations that can be performed independently and running them simultaneously.

Synchronization exists to enforce loop-carried dependence.

Consider the following, synchronous loop with dependence.

Each loop iteration performs two actions


 * Calculate
 * Assign the value to

Calculating the value, and then performing the assignment can be decomposed into two lines:

The first line,, has no loop-carried dependence. The loop can then be parallelized by computing the temp value in parallel, and then synchronizing the assignment to.

DOPIPE parallelism
DOPIPE Parallelism implements pipelined parallelism for loop-carried dependence where a loop iteration is distributed over multiple, synchronized loops. The goal of DOPIPE is to act like an assembly line, where one stage is started as soon as there is sufficient data available for it from the previous stage.

Consider the following, synchronous code with dependence.

S1 must be executed sequentially, but S2 has no loop-carried dependence. S2 could executed in parallel using DOALL Parallelism after performing all calculations needed by S1 in series. However, the speedup is limited if this is done. A better approach is to parallelize such that the S2 corresponding to each S1 executes when said S1 is finished.

Implementing pipelined parallelism results in the following set of loops, where the second loop may execute for an index as soon as the first loop has finished its corresponding index.

DISTRIBUTED loop
When a loop has a loop-carried dependence, another way to parallelize it is to distribute the loop into several different loops. Statements that are not dependent with each other are separated so that these distributed loops can be executed in parallel. For example, consider the following code. The loop has a loop carried dependence   but S2 and S1 do not have a loop-carried dependence so we can rewrite the code as follows. Note that now loop1 and loop2 can be executed in parallel. Instead of single instruction being performed in parallel on different data as in data level parallelism, here different loops perform different tasks on different data. We call this type of parallelism either function or task parallelism.