Content deleted Content added
Citation bot (talk | contribs) m Alter: doi-broken-date. Add: url. | You can use this bot yourself. Report bugs here.| Activated by User:Headbomb | via #UCB_Headbomb |
→See also: add doacross parallelism |
||
(10 intermediate revisions by 6 users not shown) | |||
Line 8:
Consider the following code operating on a list <code>L</code> of length <code>n</code>.
{{sxhl|lang=c|1=
for (int i = 0; i < n;
S1:
}
}}
Each iteration of the loop takes the value from the current index of <code>L</code>, and increments it by 10. If statement <code>S1</code> takes <code>T</code> time to execute, then the loop takes time <code>n * T</code> to execute sequentially, ignoring time taken by loop constructs. Now, consider a system with <code>p</code> processors where <code>p > n</code>. If <code>n</code> threads run in parallel, the time to execute all <code>n</code> steps is reduced to <code>T</code>.
Line 18:
Less simple cases produce inconsistent, i.e. [[serializability|non-serializable]] outcomes. Consider the following loop operating on the same list <code>L</code>.
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: L[i] = L[i
}
}}
Each iteration sets the current index to be the value of the previous plus ten. When run sequentially, each iteration is guaranteed that the previous iteration will already have the correct value. With multiple threads, [[process scheduling]] and other considerations prevent the execution order from guaranteeing an iteration will execute only after its dependence is met. It very well may happen before, leading to unexpected results. Serializability can be restored by adding synchronization to preserve the dependence on previous iterations.
Line 28:
== Dependencies in code ==
There are several types of dependences that can be found within code.<ref name="Solihin">{{cite book|last1=Solihin|first1=Yan|title=Fundamentals of Parallel Architecture|date=2016|publisher=CRC Press|___location=Boca Raton, FL|isbn=978-1-4822-1118-4}}</ref><ref>{{cite
{| class="wikitable"
Line 56:
=== Example of true dependence ===
{{sxhl|lang=c|1=
S1: int a, b;
S2: a = 2;
S3: b = a + 40;
}}
<code>S2 ->T S3</code>, meaning that S2 has a true dependence on S3 because S2 writes to the variable <code>a</code>, which S3 reads from.
=== Example of anti-dependence ===
{{sxhl|lang=c|1=
S1: int a, b = 40;
S2: a = b - 38;
S3: b = -1;
}}
<code>S2 ->A S3</code>, meaning that S2 has an anti-dependence on S3 because S2 reads from the variable <code>b</code> before S3 writes to it.
=== Example of output-dependence ===
{{sxhl|lang=c|1=
S1: int a, b = 40;
S2: a = b - 38;
S3: a = 2;
}}
<code>S2 ->O S3</code>, meaning that S2 has an output dependence on S3 because both write to the variable <code>a</code>.
=== Example of input-dependence ===
{{sxhl|lang=c|1=
S1: int a, b, c = 2;
S2: a = c - 1;
S3: b = c + 1;
}}
<code>S2 ->I S3</code>, meaning that S2 has an input dependence on S3 because S2 and S3 both read from variable <code>c</code>.
Line 99:
In the following example code used for swapping the values of two array of length n, there is a loop-independent dependence of <code>S1 ->T S3</code>.
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: tmp = a[i];
S2: a[i] = b[i];
S3: b[i] = tmp;
}
}}
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.
Example of loop-carried dependence where <code>S1[i] ->T S1[i + 1]</code>, where <code>i</code> indicates the current iteration, and <code>i + 1</code> indicates the next iteration.
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: a[i] = a[i
}
}}
=== Loop carried dependence graph ===
Line 130:
* DOPIPE Parallelism
Each implementation varies slightly in how threads synchronize, if at all. In addition, parallel tasks must somehow be mapped to a process. 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 statically.<ref>{{cite journal|last1=Kavi|first1=Krishna|title=Parallelization of DOALL and DOACROSS Loops-a Survey|
The process of parallelizing a sequential program can be broken down into the following discrete steps.<ref name="Solihin" /> Each concrete loop-parallelization below implicitly performs them.
Line 154:
=== DISTRIBUTED loop ===
When a loop has a loop-carried dependence, one way to parallelize it is to distribute the loop into several different loops. Statements that are not dependent on each other are separated so that these distributed loops can be executed in parallel. For example, consider the following code.
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: a[i] = a[i
S2:
}
}}
The loop has a loop carried dependence <code>S1[i] ->T S1[i
{{sxhl|lang=c|1=
loop1: for (int i = 1; i < n;
S1: a[i] = a[i
}
loop2: for (int i = 1; i < n;
S2:
}
}}
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. Let's say the time of execution of S1 and S2 be <math>
</math> then the execution time for sequential form of above code is <math>n*(
=== DOALL parallelism ===
Line 176:
DOALL parallelism exists when statements within a loop can be executed independently (situations where there is no loop-carried dependence).<ref name="Solihin" /> For example, the following code does not read from the array <code>a</code>, and does not update the arrays <code>b, c</code>. No iterations have a dependence on any other iteration.
{{sxhl|lang=c|1=
for (int i = 0; i < n;
S1: a[i] = b[i] + c[i];
}
}}
Let's say the time of one execution of S1 be <math>
The following example, using a simplified pseudo code, shows how a loop might be parallelized to execute each iteration independently.
{{sxhl|lang=c|1=
begin_parallelism();
for (int i = 0; i < n;
S1: a[i] = b[i] + c[i];
end_parallelism();
}
block();
}}
=== DOACROSS parallelism ===
DOACROSS Parallelism exists where iterations of a loop are parallelized by extracting calculations that can be performed independently and running them simultaneously.<ref>{{citation|last1=Unnikrishnan|first1=Priya|title=Euro-Par 2012 Parallel Processing|volume=7484|pages=219–231|doi=10.1007/978-3-642-32820-6_23|series=Lecture Notes in Computer Science|year=2012|isbn=978-3-642-32819-0|chapter=A Practical Approach to DOACROSS Parallelization|
Synchronization exists to enforce loop-carried dependence.
Consider the following, synchronous loop with dependence <code>S1[i] ->T S1[i
{{sxhl|lang=c|1=
for (int i = 1; i < n;
a[i] = a[i
}
}}
Each loop iteration performs two actions
* Calculate <code>a[i
* Assign the value to <code>a[i]</code>
Calculating the value <code>a[i
{{sxhl|lang=c|1=
S1: int tmp = b[i] + 1;
S2: a[i] = a[i
}}
The first line, <code>int tmp = b[i] + 1;</code>, has no loop-carried dependence. The loop can then be parallelized by computing the temp value in parallel, and then synchronizing the assignment to <code>a[i]</code>.
{{sxhl|lang=c|1=
post(0);
for (int i = 1; i < n;
S1: int tmp = b[i] + 1;
wait(i
S2: a[i] = a[i
post(i);
}
}}
Let's say the time of execution of S1 and S2 be <math>T_{S_1}</math> and <math>T_{S_2}
</math> then the execution time for sequential form of above code is <math>n*( === DOPIPE parallelism ===
Line 238 ⟶ 239:
DOPIPE Parallelism implements pipelined parallelism for loop-carried dependence where a loop iteration is distributed over multiple, synchronized loops.<ref name="Solihin" /> 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.<ref>{{cite web|title=DoPipe: An Effective Approach to Parallelize Simulation|url=https://software.intel.com/sites/default/files/m/a/a/7/d/6/12758-MC_Forum_Zangbinyu_dopipe.pdf|website=Intel|accessdate=13 September 2016}}</ref>
Consider the following, synchronous code with dependence <code>S1[i] ->T S1[i
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: a[i] = a[i
S2:
}
}}
S1 must be executed sequentially, but S2 has no loop-carried dependence. S2 could be 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.
Line 251 ⟶ 252:
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.
{{sxhl|lang=c|1=
for (int i = 1; i < n;
S1: a[i] = a[i
post(i);
}
Line 259 ⟶ 260:
for (int i = 1; i < n; i++) {
wait(i);
S2:
}
}}
Let's say the time of execution of S1 and S2 be <math>
</math> then the execution time for sequential form of above code is <math>n*(
== See also ==
* [[Data parallelism]]
* [[DOACROSS parallelism]]
* [[Task parallelism]]
* Parallelism using different types of memory models like [[Shared memory|shared]] and [[Distributed memory|distributed]] and [[Message Passing Interface|Message Passing]]
|