2012/1/6
多核心處理器的崛起…
Chip Multiprocessors: 一個 IC 裡面有多個處理核心 , 每一個都 可以單獨運算 , 並存取共同的記 憶體
Computer clusters: 用網路連接起 來的許多電腦
Supercomputers: 客製化的架構及 處理單元之間的通訊網路
便宜
貴
What if a problem is NP-hard?
說不定 n 不會很大 ? 說不定 constant 很小 ?
n 可能出現的範圍內 , 執行時間是合理的嗎 ?
我的想法 :
n
Multithreaded
Algorithm on a parallel platform
Regular (Serial) algorithm
Running time
Reasonable running time
可處理的 n 的範圍變大了 !
Memory
Processo r
Processo r
Processo r
Processo r
Memory Memory Memory Memory
Distributed-Memory Multiprocessing
Process Thread
Thread
Thread Thread
Memory used by the
process (shared by all threads)
Process Process
Process Process
Memory exec.
Memory
exec. exec. Memory
Memory exec.
Inter Process Communications
OS Scheduler
Processor 1
Processor 2
Processor n
……
The variables are shared among all threads.
Threads are assigned to processors by the scheduler for executions.
Each processes have their own memory (for storing variables)
Processes are assigned to processors by the scheduler for executions.
Concurrency platforms
Static threading: 通常一個 program 的 thread 數目是固定的 .
困難之處 : 每個 thread 要分到數量差不多的分 量 , 才能使執行時間縮到最短 動態分配 困難
需要一個 scheduler 來分配資源 , 協調並提供給 thread 運用
因此 , 創造出 concurrency platform, 使得 pro grammer 不需要自己撰寫 scheduler.
可能是一個 programming library 或是一整套的 程式語言 / 語法 + compiler + library
Dynamic Multithreading
Concurrency platform 的一個種類
通常有兩個 feature:
Nested parallelism: 有點像 fork. “Spawn” 出 一支新的 subroutine, 使得原本的程式可以繼續執 行不用等待 .
Parallel loops: for 迴圈的不同 iteration 可以同 時執行
關鍵概念 : Programmer 只管邏輯上怎麼平行化一 個程式 , 而不需要管細節如 scheduling, 資源 分配等等 (platform 自己會處理 )
這個 model 的好處
平行化後的程式只是原本程式的簡單延伸 . 只 需要加上一些關鍵字 : parallel, spawn, syn c.
易於使用理論分析
許多 divide-and-conquer 的演算法可以很容易 地用 nested parallelism 來改成 multithread ed 的演算法
許多現存的 concurrency platform 使用此 mod el
Computing Fibonacci numbers
FIB(n) if n<=1
return n else
x=FIB(n-1) y=FIB(n-2) return x+y
�
( �−1 )
�
( �−2 ) Θ(1)
�
( � ) =� ( �−1 ) + � ( �−2 ) + Θ(1)
執行時間分析
歸納法證明 :
假設是 constants, for m<n
Computing Fibonacci numbers
FIB(n) if n<=1
return n else
x=FIB(n-1) y=FIB(n-2) return x+y
這兩個不相干 , 可以分別執 行 !
Computing Fibonacci numbers
P-FIB(n) if n<=1
return n else
x=spawn P-FIB(n-1) y=P-FIB(n-2)
sync
return x+y
新產生出來的 subroutine 叫 child
原本的 subroutine 叫 parent
Child 執行 spawn 後面的 Parent 繼續執行下面的 , 不等call
child
等所有 child 執行完回來才能繼 續這時才能安心使用 spawned subroutine 的 return value
一些細節
“Spawn” 不表示”一定要平行地執行”
( 也就是有時候可能由同一個 processor 依序 執行 )
由 scheduler 去看是否需要 / 適合
一個 function return 的時候 , 也隱含了會有 sync 關鍵字
( 等待所有 child subroutine 執行完回來 )
Dependencies
between instruction strands
if n<=1 return n else
x=spawn P-FIB(n-1) y=P-FIB(n-2)
sync
return x+y
如果有 uv 的路徑 : u and v are in series.
如果沒有 uv 的路徑 : u and v are in parallel.
Continuation edge
Spawn
edge Call edge
Return edge Final Strand
Ideal Parallel Computer
A set of processors and a sequentially co nsistent shared memory
Sequential consistency: 雖然實際上可能很 多 processor 會同時讀寫記憶體 , 但是產生的 結果和一次只執行一個 processor 的讀寫指令 產生的結果一樣 .
每個 Processor 的 computing power 一樣
Scheduling 產生的 overhead 可忽略
Performance measures
Work: 在一個 processor 上執行整個計算所需 的時間
假設每個 strand 執行的時間差不多 , 則 work
=computation dag 的 vertex 數目
Span: 在 computation dag 中最長的路徑所需 的執行時間
假設每個 strand 執行的時間差不多 , 則 span
=computation dag 的最長路徑上 vertex 數目
實際執行時間跟有幾個 processor 有關
假設有 P 個 processor
: P 個 processor 的執行時間
: 1 個 processor 的執行時間 =work
: 無限多個 processor 的執行時間 =span
Lower bounds
Work law:
P 個 processor 做時間後總工作量最多為 . 全 部工作量為 .
Span law:
無限多個 processor 的機器可以模擬 P 個 proc essor 的機器 . 因此 P 個 processor 的執行時 間一定永遠比較大或一樣大 .
Speedup
Speedup=
Linear speedup: When
Perfect linear speedup: When
Parallelism
Parallelism=
Parallelism 可用下面三種方法解釋 :
1. 平均可和在 critical path 上 (computation da g 的最長路徑上 ) 每一個步驟平行處理的工作量
2. Parallelism 是可能得到的 max speedup ( 使用 任何數量的 processor)
3. 對 Perfect linear speedup 的可能性做限制 : 只要 processor 數量比 parallelism 大 , 就不 可能達到 perfect linear speedup
說明 : 假設 , 則 . 使用超過 parallelism 個 p rocessor 意義不大 . ( 離 perfect speedup 越 遠 )
Back to P-FIB
P-FIB(4) 的 case:
Work=17
Span=8
Parallelism=17/8=2.125
所以最多 speedup 大概就是雙倍左右 ( 用再多 processor 也沒有 用 !)
試試看 P-FIB(n) 當 n 比較大的時候 !
Parallel Slackness=
(Parallelism 比 P 超過多少 )
Slackness < 1 表示沒希望 perfect linear speedup 了 .
當 Slackness>1 時 , 每個 processor 分配到的工作量是是否能達 成 perfect linear speedup 的關鍵 scheduler 可以幫忙 !
Scheduling
Scheduler 的工作 : 把 strand 指定給 processo r 執行 .
On-line: scheduler 事先並不知道什麼時候 st rand 會 spawn, 或者 spawn 出來的什麼時候會 完成 .
Centralized scheduler: 單一的 scheduler 知道整體狀況並作 scheduling ( 比較容易分 析 )
Distributed scheduler: 每個 thread 互相溝 通合作 , 找出最好的 scheduling 方法
Greedy scheduler
每一個 time step, 把越多 strand 分給越多 processor 執行越好
如果某個 time step 的時候 , 至少有 P 個 strand 是準 備好被執行的 , 則稱為 complete step
反之則稱為 incomplete step
Lower bounds: 最好的狀況也需要這些時間 :
work law:
span law:
Greedy scheduling 的 upper bound 為這兩個 lower bo und 的和