Link
Parallel Algorithms
Evaluating and designing parallel folding and mapping algorithms with RecursiveTask and RecursiveAction.
Kevin Lin, with thanks to many others.
1

+
+
+
+
2
+
+
+
SumTask Call Tracing
Q
1
Q1: Trace the execution of SumTask if we call: left.fork(); right.compute(); left.join();








Q2: Trace the execution of SumTask if we call: left.fork(); left.join(); right.compute();

+
+
+
+
3
+
+
+
SumTask Call Tracing: left.fork(); right.compute(); left.join();
A
1

+
+
+
+
4
+
+
+
SumTask Call Tracing: left.fork(); left.join(); right.compute();
A
1

What’s missing from this parallel MaxTask class?
5
Q
class MaxTask {
    int lo; int hi; int[] a;
    MaxTask(int[] arr, int l, int h) { lo = l; hi = h; a = arr; }
    public Integer compute() {
        int mid = (hi + lo) / 2;
        MaxTask left  = new MaxTask(a, lo, mid);
        MaxTask right = new MaxTask(a, mid, hi);
        return Math.max(left.compute(), right.compute());
    }
}
Q1: What’s missing from this parallel MaxTask class?

What’s missing from this parallel MaxTask class?
6
A
class MaxTask {
    int lo; int hi; int[] a;
    MaxTask(int[] arr, int l, int h) { lo = l; hi = h; a = arr; }
    public Integer compute() {
        int mid = (hi + lo) / 2;
        MaxTask left  = new MaxTask(a, lo, mid);
        MaxTask right = new MaxTask(a, mid, hi);
        return Math.max(left.compute(), right.compute());
    }
}

RecursiveTask<T> vs. RecursiveAction
Sometimes, it’s not necessary to return a result in compute().
The fork/join framework RecursiveAction has void return type.
In what scenario(s) would we want to use RecursiveAction?


How do we keep track of information if it’s not passed via return type?
7
Q
Q1: In what scenario(s) would we want to use RecursiveAction?








Q2: How do we keep track of information if it’s not passed via return type?

RecursiveTask<T> vs. RecursiveAction
Sometimes, it’s not necessary to return a result in compute().
The fork/join framework RecursiveAction has void return type.
In what scenario(s) would we want to use RecursiveAction?


How do we keep track of information if it’s not passed via return type?
8
A

Parallel Map
Parallelize vector addition. Describe your algorithm in English first before writing code.
9
Q
def add(a1, a2, result):
    assert len(a1) == len(a2) == len(result)
    for i in range(len(a1)):
        result[i] = a1[i] + a2[i]
Q1: Parallelize vector addition. Describe your algorithm in English first before writing code.

Parallel Map
Parallelize vector addition. Describe your algorithm in English first before writing code.
10
A

Parallel Map
11
A