5. GHCのスレッドモデル
とても軽い
a `par` b スパーク
(sparks)
軽い
Haskellスレッド
forkIO a
(OSスレッドの
100x以上軽い)
CPU数と
./a.out 同数程度の
+RTS -N 重いOSスレッド
(worker
threads)
CPU #0 CPU #1 CPU #2 CPU #3
6. 並列ランタイムの仕組み
Haskell HEC #2 HEC #3 HEC #4
Execution
Context #1
(HEC)
● An ownership field ● An ownership field ... ...
● An message queue ● An message queue
● A run queue ...
● An allocation area
● GC remembered
set
● A spark pool
● A worker pool
CPU #0 CPU #1 CPU #2 CPU #3
7. 並列ランタイムの仕組み
Haskell
Execution
Haskell Execution Context (HEC)
Context #1
(HEC) CPUごとに一つ作られる
● An ownership field +RTS -Nで指定する数だけ作られる
● An message queue
● A run queue
worker threadがHaskell threadを実
● An allocation area 行するのに必要なデータを持っている
● GC remembered
set “Capability”とか”virtual
A spark pool
processor”とも呼ばれる
●
● A worker pool
CPU #0
8. 並列ランタイムの仕組み
Haskell
Haskellスレッドの実体はThread State Object
Execution
Context #1 (TSO)というヒープに割り当てられたデータ構造
(HEC)
15 words + sizeof stack + αで小さい
● An ownership field
run queueに入っているのをHECが順次round-
● An message queue
● A run queue
robinで実行していく
● An allocation area parで作られるsparkはspark poolに入る
● GC remembered
set システムの負荷が高くないときに、GCを生き延
A spark pool
びた一部のsparkのみspark threadが作られ並列
●
A worker pool
評価される
●
つまり必ず並列評価されるわけではない
CPU #0
9. 並列ランタイムの仕組み
Haskell
Execution HECのライフサイクル
Context #1
(HEC) message queueのメッセージを処理
● An ownership field run queueのスレッドを走らせる
● An message queue
● A run queue spark poolにsparkが入っていれば
● An allocation area
spark threadを起動し実行
● GC remembered
set
HEC内でなくglobalな、black hole
● A spark pool
● A worker pool poolをpollingして、いずれかのスレッ
ドが実行できるようになったら実行
CPU #0
17. GHCのスレッドモデル
とても軽い
a `par` b スパーク
(sparks)
軽い
Haskellスレッド
forkIO a
(OSスレッドの
100x以上軽い)
CPU数と
./a.out 同数程度の
+RTS -N 重いOSスレッド
(worker
threads)
CPU #0 CPU #1 CPU #2 CPU #3
5
18. 並列ランタイムの仕組み
Haskell HEC #2 HEC #3 HEC #4
Execution
Context #1
(HEC)
● An ownership field ● An ownership field ... ...
● An message queue ● An message queue
● A run queue ...
● An allocation area
● GC remembered
set
● A spark pool
● A worker pool
CPU #0 CPU #1 CPU #2 CPU #3
6
19. 並列ランタイムの仕組み
Haskell
Execution
Haskell Execution Context (HEC)
Context #1
(HEC) CPUごとに一つ作られる
● An ownership field +RTS -Nで指定する数だけ作られる
An message queue
worker threadがHaskell threadを実
●
● A run queue
● An allocation area 行するのに必要なデータを持っている
● GC remembered
set “Capability”とか”virtual
A spark pool
processor”とも呼ばれる
●
● A worker pool
CPU #0
7
- ownership fieldは、どのworker threadがcapabilityを
持っているか(実行中か)
- message queueは、他のHECからの要求を受け取るた
めのキュー。たとえば”スレッドTを起こして!”という
ような具合
- run queueは実行準備ができているスレッドのキュー
- allocation areaは、HEC固有のアロケーション領域。
ヒープは一つを共有するけど、固有のもあるらしい。
- GC remembered setsは?
- spark poolはa `par` bとするときのaのサンクが入る
- worker poolはスペアのworker threadとforeign call用
のプール
20. 並列ランタイムの仕組み
Haskell
Execution
Haskellスレッドの実体はThread State Object
Context #1 (TSO)というヒープに割り当てられたデータ構造
(HEC)
15 words + sizeof stack + αで小さい
● An ownership field
run queueに入っているのをHECが順次round-
● An message queue
● A run queue
robinで実行していく
● An allocation area parで作られるsparkはspark poolに入る
● GC remembered
set システムの負荷が高くないときに、GCを生き延
A spark pool
びた一部のsparkのみspark threadが作られ並列
●
A worker pool
評価される
●
つまり必ず並列評価されるわけではない
CPU #0
8
21. 並列ランタイムの仕組み
Haskell
Execution HECのライフサイクル
Context #1
(HEC) message queueのメッセージを処理
● An ownership field run queueのスレッドを走らせる
● An message queue
● A run queue spark poolにsparkが入っていれば
● An allocation area
spark threadを起動し実行
● GC remembered
set
HEC内でなくglobalな、black hole
● A spark pool
● A worker pool poolをpollingして、いずれかのスレッ
ドが実行できるようになったら実行
CPU #0
9