fork/join framework。そしてlambdaへ。

23
Fork/Join Framework。 Java in the Box そして Lambda へ。 櫻庭 祐一

Upload: skrb

Post on 05-Dec-2014

6.684 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Fork/Join Framework。そしてLambdaへ。

Fork/Join Framework。

Java in the Box

そして Lambda へ。

櫻庭 祐一

Page 2: Fork/Join Framework。そしてLambdaへ。

Java には信頼性の高い

Java: The Good Parts

10章より引用

並行プログラムを構築するのに使える

構成要素がある

Page 3: Fork/Join Framework。そしてLambdaへ。

1994 Java 1.0α ThreadRunnable

2004 J2SE 5.0 JSR 166Concurrency Utilities

Page 4: Fork/Join Framework。そしてLambdaへ。

1994 Java 1.0α ThreadRunnable

2004 J2SE 5.0 JSR 166Concurrency Utilities

Concurrent

Parallel

Page 5: Fork/Join Framework。そしてLambdaへ。

トランジスタ数

周波数 消費電力

Page 6: Fork/Join Framework。そしてLambdaへ。

トランジスタ数

周波数 消費電力

Page 7: Fork/Join Framework。そしてLambdaへ。

2004 UltraSPARC IV

2006 Core2 Extream

2009 Nehalem-EX

2010 Opteron 6100

2010 SPARC T3

Intel SCC

Page 8: Fork/Join Framework。そしてLambdaへ。

SingleCore 時代の Software

MultiCore 時代の Software

Page 9: Fork/Join Framework。そしてLambdaへ。

JSR166 Executor

Core 1

Core 2

Core 3

Core 4

同期

Page 10: Fork/Join Framework。そしてLambdaへ。

JSR166y Fork/Join Framework

Core 1

Core 2

Core 3

Core 4

同期

Page 11: Fork/Join Framework。そしてLambdaへ。

分割統治法

Page 12: Fork/Join Framework。そしてLambdaへ。

分割統治法

フィボナッチ数列 F = 0, F = 1F = F + F

0 1n n-1 n-2

public int compute(int n) { if (n <= 1) { return n; } return compute(n-1) + compute(n-2); }

Page 13: Fork/Join Framework。そしてLambdaへ。

分割統治法

フィボナッチ数列 F = 0, F = 1F = F + F

0 1n n-1 n-2

class FibonacciTask extends RecursiveTask<Integer> { private final int n; FibonacciTask(int n) { this.n = n; } protected Integer compute() { if (n <= 1) { return n; } FibonacciTask f1 = new FibonacciTask(n - 1); f1.fork(); FibonacciTask f2 = new FibonacciTask(n - 2); return f2.compute() + f1.join(); }}

Page 14: Fork/Join Framework。そしてLambdaへ。

Work Stealing

Worker1

Worker2

両端キュー DequeTask

ForkComp

ForkComp

ForkComp

ForkComp

ソート 検索数値計算行列操作枝狩り et al.

Page 15: Fork/Join Framework。そしてLambdaへ。

内部イテレータ

List<Integer> numbers = ...; for (int i = 0; i < numbers.size(); i++) { numbers.set(i, numbers.get(i) * 2);}

Java

def numbers = ... numbers.collect { it * 2 }

Groovy

独立並行処理可

Page 16: Fork/Join Framework。そしてLambdaへ。

extra166y ParallelArray

Integer[] numbers = ...;ForkJoinPool pool = new ForkJoinPool();

ParallelArray<Integer> array = ParallelArray.createFromCopy(numbers, pool); Ops.Op<Integer, Integer> doubler = new Ops.Op<>() { @Override public Integer op(Integer x) { return x * 2; }}; array.withMapping(doubler);

Page 17: Fork/Join Framework。そしてLambdaへ。

extra166y ParallelArray

Integer[] numbers = ...;ForkJoinPool pool = new ForkJoinPool();

ParallelArray<Integer> array = ParallelArray.createFromCopy(numbers, pool); Ops.Op<Integer, Integer> doubler = new Ops.Op<>() { @Override public Integer op(Integer x) { return x * 2; }}; array.withMapping(doubler);

Page 18: Fork/Join Framework。そしてLambdaへ。

extra166y ParallelArray

Integer[] numbers = ...;ForkJoinPool pool = new ForkJoinPool();

ParallelArray<Integer> array = ParallelArray.createFromCopy(numbers, pool); Ops.Op<Integer, Integer> doubler = #{Integer num -> num * 2}; array.withMapping(doubler); ラムダ式

Project Lambda

Page 19: Fork/Join Framework。そしてLambdaへ。

extra166y ParallelArray

Integer[] numbers = ...;ForkJoinPool pool = new ForkJoinPool();

ParallelArray<Integer> array = ParallelArray.createFromCopy(numbers, pool); Ops.Op<Integer, Integer> doubler = #{Integer num -> num * 2}; array.withMapping(doubler); ラムダ式

Project Lambda

Page 20: Fork/Join Framework。そしてLambdaへ。

extra166y ParallelArray

Integer[] numbers = ...;ForkJoinPool pool = new ForkJoinPool();

ParallelArray<Integer> array = ParallelArray.createFromCopy(numbers, pool); array.withMapping(#{Integer num -> num * 2});

Page 21: Fork/Join Framework。そしてLambdaへ。

Thread/ExecutorMulticore Era

JSR 166yFork/Join FrameworkJSR 166yFork/Join Framework

Work Stealing

内部イテレータextra166y

もっと簡単に!JSR 335Project LambdaJSR 335Project Lambda

Page 22: Fork/Join Framework。そしてLambdaへ。

Tips

時間のかかる処理はしない

キャッシュを考慮する

参考 ITpro Java SE 7 徹底理解

No.2 細粒度の並行処理 - Fork/Join Framework

No.3 Fork/Join Framework から Project Lambda へ

Page 23: Fork/Join Framework。そしてLambdaへ。

Fork/Join Framework。

Java in the Box

そして Lambda へ。

櫻庭 祐一