• 沒有找到結果。

Multi-threaded Algorithm 1

N/A
N/A
Protected

Academic year: 2022

Share "Multi-threaded Algorithm 1"

Copied!
25
0
0

加載中.... (立即查看全文)

全文

(1)

2012/1/6

(2)

多核心處理器的崛起…

(3)

Chip Multiprocessors: 一個 IC 裡面有多個處理核心 , 每一個都 可以單獨運算 , 並存取共同的記 憶體

Computer clusters: 用網路連接起 來的許多電腦

Supercomputers: 客製化的架構及 處理單元之間的通訊網路

便宜

(4)

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 的範圍變大了 !

(5)

Memory

Processo r

Processo r

Processo r

Processo r

Memory Memory Memory Memory

Distributed-Memory Multiprocessing

(6)

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.

(7)

Concurrency platforms

Static threading: 通常一個 program 的 thread 數目是固定的 .

困難之處 : 每個 thread 要分到數量差不多的分 量 , 才能使執行時間縮到最短 動態分配 困難

需要一個 scheduler 來分配資源 , 協調並提供給 thread 運用

因此 , 創造出 concurrency platform, 使得 pro grammer 不需要自己撰寫 scheduler.

可能是一個 programming library 或是一整套的 程式語言 / 語法 + compiler + library

(8)

Dynamic Multithreading

 Concurrency platform 的一個種類

 通常有兩個 feature:

Nested parallelism: 有點像 fork. “Spawn” 出 一支新的 subroutine, 使得原本的程式可以繼續執 行不用等待 .

Parallel loops: for 迴圈的不同 iteration 可以同 時執行

 關鍵概念 : Programmer 只管邏輯上怎麼平行化一 個程式 , 而不需要管細節如 scheduling, 資源 分配等等 (platform 自己會處理 )

(9)

這個 model 的好處

 平行化後的程式只是原本程式的簡單延伸 . 只 需要加上一些關鍵字 : parallel, spawn, syn c.

 易於使用理論分析

 許多 divide-and-conquer 的演算法可以很容易 地用 nested parallelism 來改成 multithread ed 的演算法

 許多現存的 concurrency platform 使用此 mod el

(10)

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)

(11)

執行時間分析

 歸納法證明 :

 假設是 constants, for m<n

 

(12)

Computing Fibonacci numbers

FIB(n) if n<=1

return n else

x=FIB(n-1) y=FIB(n-2) return x+y

這兩個不相干 , 可以分別執 行 !

(13)

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

(14)

一些細節

 “Spawn” 不表示”一定要平行地執行”

 ( 也就是有時候可能由同一個 processor 依序 執行 )

 由 scheduler 去看是否需要 / 適合

 一個 function return 的時候 , 也隱含了會有 sync 關鍵字

 ( 等待所有 child subroutine 執行完回來 )

(15)

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

如果有 uv 的路徑 : u and v are in series.

如果沒有 uv 的路徑 : u and v are in parallel.

(16)

Continuation edge

Spawn

edge Call edge

Return edge Final Strand

(17)

Ideal Parallel Computer

 A set of processors and a sequentially co nsistent shared memory

 Sequential consistency: 雖然實際上可能很 多 processor 會同時讀寫記憶體 , 但是產生的 結果和一次只執行一個 processor 的讀寫指令 產生的結果一樣 .

 每個 Processor 的 computing power 一樣

 Scheduling 產生的 overhead 可忽略

(18)

Performance measures

 Work: 在一個 processor 上執行整個計算所需 的時間

 假設每個 strand 執行的時間差不多 , 則 work

=computation dag 的 vertex 數目

 Span: 在 computation dag 中最長的路徑所需 的執行時間

 假設每個 strand 執行的時間差不多 , 則 span

=computation dag 的最長路徑上 vertex 數目

(19)

 實際執行時間跟有幾個 processor 有關

 假設有 P 個 processor

 : P 個 processor 的執行時間

 : 1 個 processor 的執行時間 =work

 : 無限多個 processor 的執行時間 =span

 

(20)

Lower bounds

 Work law:

 P 個 processor 做時間後總工作量最多為 . 全 部工作量為 .

 Span law:

 無限多個 processor 的機器可以模擬 P 個 proc essor 的機器 . 因此 P 個 processor 的執行時 間一定永遠比較大或一樣大 .

 

(21)

Speedup

 Speedup=

 Linear speedup: When

 Perfect linear speedup: When

 

(22)

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 越 遠 )

 

(23)

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 可以幫忙 !

 

(24)

Scheduling

 Scheduler 的工作 : 把 strand 指定給 processo r 執行 .

 On-line: scheduler 事先並不知道什麼時候 st rand 會 spawn, 或者 spawn 出來的什麼時候會 完成 .

 Centralized scheduler: 單一的 scheduler 知道整體狀況並作 scheduling ( 比較容易分 析 )

 Distributed scheduler: 每個 thread 互相溝 通合作 , 找出最好的 scheduling 方法

(25)

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 的和

 

參考文獻

相關文件

上課時,你總是自 動的舉手想回答老師 的發問問題,上課態 度認真又專心,真是 個好孩子!皆下來的

 基准制的选用原则是:优先选用基孔制 ;只有在下列条件 下才使用基轴制 :①直接使用有一定公差等级而不再进行机

另有很多學者,並不使⽤Axiology這個字,⽽以Philosophy of Value ,Theory of

有關 PHP 的敘述何者有誤?①可在 Apache、MS IIS 等 Web 伺服 器執行的 Script②只能在 Linux 或 Unix 作業系統上執行,無法於 Windows 或 Mac

若我們能知道有 k 個 row 的矩陣一 定能利用 elementary row operations 化為 echelon form 這個事實且利用這個事實證得有 k + 1 個 row 的矩陣一定能利用 elementary row

Cause: A software bug known existed in General Electric Energy's Unix-based XA/21 energy management

在這次的實作遊戲中,我們必須要先對所使用到的硬體 和軟體有其基本的認識,這樣我們才能充分利用我們所擁有 的條件,進一步達成目標。首先 DE2-70 繼承了 Altera 一系 列的開發軟體,如

 Corollary: Let be the running time of a multithreaded computation produced by a g reedy scheduler on an ideal parallel comp uter with P processors, and let and be the work