(We don't have time to study parallel algorithms at length, this is ment to make you aware of a new type of thinking about solving problems).
a0, a1, a2, ...., aN-1 |
|
PartialSum[0] = a[0];
for (i = 1; i < N; i++)
PartialSum[i] = PartialSum[i-1] + a[i];
|
The run time complexity is N
|
The goal is to compute ALL the partial sums:
(I used array notation a[i] in the figures for ai)
What happens is the following:
|
Resulting state:
|
What happens is the following:
|
Resulting state:
|
What happens is the following:
|
Resulting state:
OK, how can we formulate this as an compuetr algorithm ?
// k = loop index
// k runs: 1, 2, 4, 8, .... etc !!!
for (k = 1; k < N; k = 2*k)
{
// Check if I am a processor that needs to send data
if ( i + k < N )
send a[i] to processor (i+k);
// Check if I am a processor that needs to receive data
if ( i >= k )
{
recevive message x;
a[i] = a[i] + x;
}
}
|