• 沒有找到結果。

Mutual exclusion problem 在 multiprocessing system 中是一個很基本的問題,對於某 些不可分割的資源,必須確保在任何瞬間最多只有一個 process 被允許存取該資源;一 台印表機無法同時給多個 processes 同時使用,所以印表機需要在 mutual exclusion 的條 件下才能正常使用。概念上,mutual exclusion problem 就是在設計一個 concurrent algorithm,其中有一段稱為 critical region(或 critical section,CS for short)的 code 保證 任何瞬間最多只有一個 process 能夠進入 CS 執行。Mutual exclusion problems 的定義包 含二項:

(1) safety property:

任何時刻至多一個 process 可以執行 CS。

(2) progress property:

如果在某時刻 t 無任何 process 在 CS,而且已有 process 在 trying region

(如:Figure 1),則存在某一個 process 在 t 之後某時刻可進 CS。

trying region

critical region (CS)

exit region remainder region

Figure 1: Life cycle of mutual exclusion algorithm

Mutual exclusion algorithm 的 code 可區分為四個 region:trying region、critical region

(CS)、exit region、remainder region[9],在進入 CS 之前必須透過 trying region 中所設

計的 contention protocol 來決定哪 process 可以進入 CS,在 contention protocol 中所有的 processes 都必須讓其它 processes 發現自己是 contender,CS 之後緊接著 exit region,在 exit region 中必須讓所有其它 processes 發現到自己已經離開 CS,在完成 exit region 之 後,process 就回到 remainder region,如 Figure 1。

當 algorithm 執行時,若已有 process 進入 CS,其它在 trying region 的 processes 就 必須 waiting,常見的作法有主動地 spinning on shared variables 或是被動地 sleeping 讓 OS 的 scheduler 來負責喚醒,但是在某些沒有 scheduler 的系統架構下,只能用 spinning 的方式,例如作業系統本身,而 spinning on shared variables 會傷害系統效能,processes 必須一直將 shared memory 中的資料讀取到 register 做條件判斷,在 memory bandwidth 有限的狀況下,shared memory contention 將造成系統的負擔。

Local spin 是一個解決 shared memory contention 的方法之一,在 distributed shared memory(DSM)的架構下,讓需要 busy waiting 的 process 只需讀取 local shared memory 中的 data,以減少 remote memory access 的次數,提升系統的效能。DSM 系統下的每個 process 擁有屬於自己的 local shared memory,可供自己作快速的 local access,其它 processes 也可用 remote access 方式讀寫此塊 memory,但速度慢許多。MCS[10]在 1991 年提出符合 local spin 概念的 mutual exclusion algorithms,並且在 2006 年獲得了 Edsger W.

Dijkstra Prize,但它使用了 compare-and-swap 指令,依賴功能較強的硬體才能實現;之 後國內亦有 local spin 相關研究,如 Tsay[12]與 Chen and Huang[3]。在一般 atomic read/write model 之下 local spin algorithms 難度較高,除了上述 Tsay[12]兼顧 algorithms derivation 正規方法之外,尚有 Yang and Anderson[13]以 tree 的方式將在 contention 之下 trying region 中的 time complexity 從O(n)降到O(log n),並引用 Lamport’s fast mutual exclusion algorithm[7]的精神,在 contention-free 的狀況下只需O(1)即可進入 CS,但是 如果在 fast path 遇到其它 process 的 contention,則 worst case 仍然是O(n);之後 Anderson and Kim[1]將 fast path 中遭遇 contention 的 time complexity 又降到O(log n)。

本文提出一個 generic approach,一般以 atomic read/write registers 為 model 的 mutual exclusion algorithms,均可透過此 generic approach 加上 local spin 的機制以便在 DSM 系 統上執行,之後以 Dijkstra’s[4]、Knuth’s[6]、Eisenburg-McGuire’s[5]、Lamport’s bakery[8]

和 Burns’[2] mutual exclusion algorithms 為例,實際演練 generic approach,得到大幅降低 shared memory contention 之初步具 local spin 的 algorithm;之後又分析這些 algorithm 運 作細節,大幅更改 program structure,更進一步嘗試減少在 exit region 中使用 remote write 叫醒其它 process 的次數到 1 次,這種只釋放特定的一個 local-spinning process 的動作稱 之為“focused release",有了 focused release 後,我們嘗試找到“fast track",讓被釋 放的 process 可省去檢查其它 processes 狀態所產生的一連串 remote memory access 動作 而直接進入 CS,所得 algorithm 仍然維持原本 safety property 和 progress property 的正確 性。本文所提 local spin algorithms 之 worst case time complexity 雖不如 Anderson and

Kim[1],但如果只討論系統在 heavy loading 時的表現,則因經常有許多 processes 在 trying region 等待,有利於本文 algorithms 之執行效率,其中以 Eisenburg-McGuire’s[5]

為基礎得到的最佳版本 EM4而言,進出 CS 一次所需 remote memory access 平均次數降 低到很小 constant 值。

相關文件