Reduction 平行歸約與最小化分歧 練習題 (Practice - Reduction Fundamentals and the Simple Kernel)


Question 1 - Reduction 的定義與 identity value [recall]

情境:什麼是 reduction?要能對一個 binary operator 定義 reduction,數學上必須具備什麼條件?請寫出 sumproductminmax 四種 reduction 的 identity value。

Question 2 - Associative 與 Commutative [recall]

情境:把 sequential reduction 改寫成 reduction tree,operator 需要哪個性質?若要進一步「重排 operand 位置」(如 §10.4 讓 active thread 相鄰) 又需要哪個性質?

Question 3 - Reduction tree 的工作量與步數 [recall]

情境:對 N 個元素的 reduction tree,總共要做幾次運算?需要幾個 time steps?這與 sequential 版本相比是否更省「工作量」?

Question 4 - Peak 與 Average Parallelism [application]

情境:N = 1024。請計算這個 reduction tree 的 (a) peak parallelism、(b) average parallelism、(c) 相對 sequential 的理想 speedup。

Question 5 - Simple Kernel 的 Thread 映射與 owner-computes [recall]

情境:在 Fig. 10.6 的 simple sum reduction kernel 中,thread k 擁有哪個 input 位置?stride 如何變化?__syncthreads() 放在迴圈內的作用是什麼?

Question 6 - 為何只能單一 Block / 2048 元素上限 [recall]

情境:前幾版 reduction kernel 都只 launch 一個 block,最多只能處理 2048 個元素。為什麼有這個限制?

Question 7 - Convergent Kernel 的三行關鍵改動 [recall]

情境:從 simple kernel (Fig. 10.6) 改成 convergent kernel (Fig. 10.9) 只改了三處,請列出。這三處同時解決了哪兩個瓶頸?

Question 8 - 為何 if 還在,divergence 卻變少 [analysis]

情境:convergent kernel 仍然有 if-statement,且每次迭代的 active thread 數量與 simple kernel 完全相同。那為什麼 control divergence 會大幅下降、efficiency 從 35% 升到 66%?

Question 9 - Memory Coalescing:Strided vs Coalesced [analysis]

情境:比較 simple kernel 與 convergent kernel 的 global memory 存取模式。為什麼 simple kernel 是非 coalesced?對 N=256 各觸發幾次 global memory requests?為何此比值在 N 越大時越懸殊?

Question 10 - Shared-Memory Kernel 的 Global 存取量 [recall]

情境:Fig. 10.11 用 shared memory 後,對 N 個元素的 reduction 只需多少次 global memory accesses?coalesced 後又是多少 requests?__syncthreads() 為何要移到迴圈開頭?

Question 11 - 練習:Simple Kernel 第五次迭代的 divergence warp 數 [application]

情境 (Exercise 1):simple kernel (Fig. 10.6),N = 1024、warp size = 32。第 5 次迭代時,block 中有幾個 warp 會發生 control divergence?

Question 12 - 練習:Convergent Kernel 第五次迭代的 divergence warp 數 [application]

情境 (Exercise 2):convergent kernel (Fig. 10.9),N = 1024、warp size = 32。第 5 次迭代時有幾個 warp 會 divergence?與上一題對比說明。

Question 13 - 階層式分段多 Block Reduction 與 atomicAdd [recall]

情境:要對數百萬元素做 reduction,如何擴展到多個 block?segment 大小是多少?各 block 的 partial sum 如何合併?使用前 host 端必須先做什麼?

Question 14 - Thread Coarsening 的做法 [recall]

情境:thread coarsening 在 reduction 中具體怎麼做?coarsen factor 為 C 時每個 thread 序列加幾個元素?這個序列階段為什麼不需要 __syncthreads()?它砍掉了哪三種 overhead?

Question 15 - Coarsening 的權衡與 factor 上限 [analysis]

情境:既然硬體在資源不足時本來就會把多餘的 block 序列化執行,為什麼我們還要「自己」用 thread coarsening 序列化?以 Fig. 10.16 (factor=2) 的步數說明,並說明 coarsening factor 過大會有什麼後果。