Simple algorithm examples
Algorithm design using PRAM:
- PRAM conflict variant must be determined
-
We assume there are always as many processors as convenient
Can be based on input size or fixed.
Basics
Initializing array
Sequential:
operations
Parallel:
If a processor is available for each
i
then
.
par (0 <= i < n) {
a[i] = i*i;
}
For loop
Sequential:
Total number of operations
The loop iterates times, each time executing instructions.
Parallel:
At most parallel steps.
Each instruction in loop is .
for (k = 1; k < n; k <<= 1) { // k = k*2
par (0 <= i <= n) a[i] = i/k; // k is a constant in this context
}
Finding max in array
Sequential: Performcomparisions (vs.) and find maximum.
Finding maximum in array - V1
par (0<=i<n) b[i]=true; // stays true if maxpar (0<=i<n, 0<=j<n)
if (a[i] < a[j]) b[i]=false; // check if smaller entry exists
par (0<=i<n) if (b[i]) x=a[i];
Complexity
for each parallel instruction - each with , and processors.
Therefore .
On a CRCW PRAM with numbers, the max can be found in time steps and operations in total.
This is a constant time algorithm with polynomial resources (operations, processors).
Finding maximum in array - V2
concurrent read, exclusive write
num = n;while (num > 1) {
half = ⌈num / 2⌉par (0 <= i < half) {
if (i + half < num) a[i] = max(a[i], a[i + half]); // boundary check
}num = half;
}
Complexity
The while loop performs iterations because in each iteration we half until
until
In the parallel part of the loop we perform operations at once with processors.
Therefore in total we have
Number of (sequential) operations
The while loop performs iterations, each with a parallel part.
In the parallel part of the loop we perform operations:
That means in total we do this:
On a CREW (but also EREW if modified) PRAM with numbers, the max can be found in time steps and operations.
Finding maximum in array - V3
This time we are wasting processors.
num = n;while (num > 1) {
half = ⌈num / 2⌉par (0 <= i < n) { // n instead of half
if (i + half < num) a[i] = max(a[i], a[i + half]); // boundary check
}num = half;
}
On a CREW PRAM with numbers, the max can be found in time steps, and operations in total using processors .
Finding maximum in array - V4
This time we are also using redundant processors for the parallel part.
num = n;while (num > 1) {
half = ⌈num / 2⌉par (0 <= i < num) {
for (j = i; j < i+(num/p); j++) {
if (j + half < num) a[i] = max(a[j], a[j + half]); //same as older version
}
}num = half;
}
On a CREW PRAM with numbers, the max can be found in time steps and operations in total.
Matrix-Matrix multiplication
Matrix-Matrix multiplication
Calculate with nested parallelism
Input:
matrix
matrix
Output:
matrix
par (0<=i<n) {
par (0<=j<m) {
C[i,j] = 0;
for (k=0; k<l; k++) {
C[i,j] += A[i,k]*B[k,j];
}
}
}
On a CREW PRAM, the multiplication can be done in time steps and operations in total, using processors.