Академический Документы
Профессиональный Документы
Культура Документы
Brian Goetz
Sr. Staff Engineer, Sun Microsystems
brian.goetz@sun.com
2
Copyright © 2008 Brian Goetz
Hardware trends
3
Copyright © 2008 Brian Goetz
Hardware trends
4
Copyright © 2008 Brian Goetz
Hardware trends
7
Copyright © 2008 Brian Goetz
Finding finer-grained parallelism
8
Copyright © 2008 Brian Goetz
Running example: select-max
10
Copyright © 2008 Brian Goetz
First attempt: Executor+Future
11
Copyright © 2008 Brian Goetz
Parallelization technique: divide-and-conquer
12
Copyright © 2008 Brian Goetz
Divide-and-conquer
13
Copyright © 2008 Brian Goetz
Fork-join parallelism
14
Copyright © 2008 Brian Goetz
Solving select-max with fork-join
The RecursiveAction class in the fork-join framework is ideal
for representing divide-and-conqure solutions
class MaxSolver extends RecursiveAction {
private final MaxProblem problem;
int result;
protected void compute() {
if (problem.size < THRESHOLD)
result = problem.solveSequentially();
else {
int m = problem.size / 2;
MaxSolver left, right;
left = new MaxSolver(problem.subproblem(0, m));
right = new MaxSolver(problem.subproblem(m,
problem.size));
forkJoin(left, right);
result = Math.max(left.result, right.result);
}
}
}
ForkJoinExecutor pool = new ForkJoinPool(nThreads);
MaxSolver solver = new MaxSolver(problem);
pool.invoke(solver);
15
Copyright © 2008 Brian Goetz
Fork-join example
17
Copyright © 2008 Brian Goetz
Performance considerations
18
Copyright © 2008 Brian Goetz
Performance considerations
19
Copyright © 2008 Brian Goetz
Under the hood
20
Copyright © 2008 Brian Goetz
Work stealing
21
Copyright © 2008 Brian Goetz
Work stealing
25
Copyright © 2008 Brian Goetz
Taking it up a level
27
Copyright © 2008 Brian Goetz
ParallelArray
29
Copyright © 2008 Brian Goetz
ParallelArray
30
Copyright © 2008 Brian Goetz
ParallelArray example: mean and variance
31
Copyright © 2008 Brian Goetz
ParallelArray
32
Copyright © 2008 Brian Goetz
Combining multiple ParallelArrays
33
Copyright © 2008 Brian Goetz
Connection with closures
34
Copyright © 2008 Brian Goetz
From Concurrent to Parallel
Library-based parallelism in JDK 7
Brian Goetz
Sr. Staff Engineer, Sun Microsystems
brian.goetz@sun.com