Download presentation
Presentation is loading. Please wait.
1
Multi-threaded Algorithm 3
Michael Tsai 2014/1/2
2
Multithreaded matrix multiplication
P-SQUARE-MATRIX-MULTIPLY(A,B) n=A.rows let C be a new n x n matrix parallel for i=1 to n parallel for j=1 to n ๐ ๐๐ =0 for k=1 to n ๐ ๐๐ = ๐ ๐๐ + ๐ ๐๐ โ ๐ ๐๐ return C ๐ 1 (๐)=ฮ ๐ 3 ๐ โ ๐ =ฮ log n +ฮ log ๐ +ฮ ๐ =ฮ(๐) ๐ 1 ๐ ๐ โ ๐ = ฮ ๐ 3 ฮ ๐ =ฮ ๐ 2
3
Divide-and-conquer Multithreaded Algorithm for Matrix Multiplication (ๅๅ้ท)
๐ด= ๐ด 11 ๐ด 12 ๐ด 21 ๐ด 22 ๐ต= ๐ต 11 ๐ต 12 ๐ต 21 ๐ต 22 ๐ถ= ๐ถ 11 ๐ถ 12 ๐ถ 21 ๐ถ 22 = ๐ด 11 ๐ด 12 ๐ด 21 ๐ด ๐ต 11 ๐ต 12 ๐ต 21 ๐ต = ๐ด 11 ๐ต 11 ๐ด 12 ๐ต 12 ๐ด 21 ๐ต 11 ๐ด 22 ๐ต ๐ด 12 ๐ต ๐ด 12 ๐ต 22 ๐ด 22 ๐ต ๐ด 22 ๐ต 22 C T
4
๐ 1 ๐ ๐ โ ๐ = ฮ ๐ 3 ฮ log 2 ๐ =ฮ ๐ 3 log 2 ๐
P-MATRIX-MULTIPLY-RECURSIVE(C,A,B) n=A.rows if n==1 ๐ 11 = ๐ 11 ๐ 11 else let T be a new n x n matrix partition A,B,C, and T into n/2 x n/2 submatrices ( ๐ด ๐๐ , ๐ต ๐๐ , ๐ถ ๐๐ , ๐ ๐๐ , ๐,๐={1,2}) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ถ 11 , ๐ด 11 , ๐ต 11 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ถ 12 , ๐ด 11 , ๐ต 12 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ถ 21 , ๐ด 21 , ๐ต 11 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ถ 22 , ๐ด 21 , ๐ต 12 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ 11 , ๐ด 12 , ๐ต 21 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ 12 , ๐ด 12 , ๐ต 22 ) spawn P-MATRIX-MULTIPLY-RECURSIVE( ๐ 21 , ๐ด 22 , ๐ต 21 ) P-MATRIX-MULTIPLY-RECURSIVE( ๐ 22 , ๐ด 22 , ๐ต 22 ) sync parallel for i=1 to n parallel for j=1 to n ๐ ๐๐ = ๐ ๐๐ + ๐ก ๐๐ ๐ 1 ๐ =8 ๐ 1 ๐ 2 +ฮ ๐ 2 =ฮ ๐ 3 ๐ 1 ๐ ๐ โ ๐ = ฮ ๐ 3 ฮ log 2 ๐ =ฮ ๐ 3 log 2 ๐ ๐ โ ๐ = ๐ โ ๐ 2 +ฮ log ๐ +ฮ log ๐ =ฮ log 2 ๐
5
How about Strassenโs method?
Reading assignment: p Parallelism: ฮ( ๐ log log 2 ๐ ), slightly less than the original recursive version!
6
Multithreaded Merge Sort
A: Array to be sorted p and r: Start and end index of the range to be sorted Merge-Sortโ(A,p,r) if p<r q= (๐+๐)/2 spawn MERGE-SORTโ(A,p,q) MERGE-SORTโ(A,q+1,r) sync MERGE(A,p,q,r) Merge() is the bottleneck! ฮ(๐) ๐ ๐ 1 โฒ ๐ =2๐ ๐ 1 โฒ ๐ 2 +ฮ ๐ =ฮ ๐ log ๐ Parallelism= ฮ( log ๐ ) ๐ ๐ โ โฒ ๐ =๐ ๐ โ โฒ ๐ 2 +ฮ ๐ =ฮ ๐
7
ๆMergeไน็จDivide & Conquerไพ่งฃ!
(ๆนไพฟไบค็ตฆไธๅ็threadๅปๅ) 2. ๆพๅบ ๐ 2 ~ ๐ 2 ไธญ ๐ 2 ้ๅไฝ็ฝฎไฝฟๅพ ๐ 2 ~ ๐ 2 โ1้ฝ< x, ๐ 2 ~ ๐ 2 ้ฝโฅ x 1. ๆๅบ ๐ 1 ~ ๐ 1 ็ไธญไฝๆธx 3. Copy x ๅฐๆฐๅฐๆน(ๆ นๆxๅ ๐ 2 ็ไฝ็ฝฎ) Base case: ๐ 1 ~ ๐ 1 ๅ ๐ 2 ~ ๐ 2 ้ฝๆฏ็ฉบ็ 4. ้ๅ่บซๅปmerge ๐ 1 ~ ๐ 1 โ1ๅ ๐ 2 ~ ๐ 2 โ1, ๅ ๐ 1 +1~ ๐ 1 ๅ ๐ 2 ~ ๐ 2 ๅ
ฉๅๅๆฎต
8
Multithreaded Merge P-MERGE(T, ๐ 1 , ๐ 1 , ๐ 2 , ๐ 2 ,A, ๐ 3 ) ๐ 1 = ๐ 1 โ ๐ 1 +1 ๐ 2 = ๐ 2 โ ๐ 2 +1 if ๐ 1 < ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 if ๐ 1 ==0 return else ๐ 1 = ( ๐ 1 + ๐ 1 )/2 ๐ 2 =BINARY-SEARCH(T[ ๐ 1 ],T, ๐ 2 , ๐ 2 ) ๐ 3 = ๐ 3 + ๐ 1 โ ๐ 1 +( ๐ 2 โ ๐ 2 ) A[ ๐ 3 ]=T[ ๐ 1 ] spawn P-MERGE(T, ๐ 1 , ๐ 1 โ1, ๐ 2 , ๐ 2 โ1,A, ๐ 3 ) P-MERGE(T, ๐ 1 +1, ๐ 1 , ๐ 2 , ๐ 2 ,A, ๐ 3 +1) sync T: Array to be merged A: Array to save the merged result ๐ 1 , ๐ 1 , ๐ 2 , ๐ 2 : Start and end index of the range to be merged ๐ 3 : The index of the median in A
9
๐ 1 โฅ ๐ 2 , ๐= ๐ 1 + ๐ 2 ๐ 2 = 2 ๐ 2 2 โค ๐ 1 + ๐ 2 2 = ๐ 2 ๆ็ณ็็ๆณไธ, xๆฏๆๆ ๐ 2 ~ ๐ 2 ็้ฝๅคง ่ฆmerge ๐ ๐ 2 ๅๅ
็ด ๐ ๐ 2 โค ๐ ๐ ๐ 2 2 = ๐ 1 + ๐ ๐ 2 2 โค ๐ 2 + ๐ 4 = 3๐ 4
10
Multithreaded Merge P-MERGE(T, ๐ 1 , ๐ 1 , ๐ 2 , ๐ 2 ,A, ๐ 3 ) ๐ 1 = ๐ 1 โ ๐ 1 +1 ๐ 2 = ๐ 2 โ ๐ 2 +1 if ๐ 1 < ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 exchange ๐ 1 ๐ค๐๐กโ ๐ 2 if ๐ 1 ==0 return else ๐ 1 = ( ๐ 1 + ๐ 1 )/2 ๐ 2 =BINARY-SEARCH(T[ ๐ 1 ],T, ๐ 2 , ๐ 2 ) ๐ 3 = ๐ 3 + ๐ 1 โ ๐ 1 +( ๐ 2 โ ๐ 2 ) A[ ๐ 3 ]=T[ ๐ 1 ] spawn P-MERGE(T, ๐ 1 , ๐ 1 โ1, ๐ 2 , ๐ 2 โ1,A, ๐ 3 ) P-MERGE(T, ๐ 1 +1, ๐ 1 , ๐ 2 , ๐ 2 ,A, ๐ 3 +1) sync ๐ ๐ โ ๐ =๐ ๐ โ 3๐ 4 +ฮ log ๐ =ฮ log 2 ๐ ๐ ๐ 1 ๐ =ฮฉ ๐ , ๅ ็บ่ณๅฐ่ฆcopy n elements ๐ ๐ 1 ๐ =O ๐ , ่ฆ่ชฒๆฌp.802 โ๐ ๐ 1 ๐ =ฮ ๐ ฮ log ๐
11
P-MERGE-SORT(A,p,r,B,s) n=r-p+1 if n==1 B[s]=A[p] else let T[1
P-MERGE-SORT(A,p,r,B,s) n=r-p+1 if n==1 B[s]=A[p] else let T[1..n] be a new array ๐= ๐+๐ 2 ๐ โฒ =๐โ๐+1 spawn P-MERGE-SORT(A,p,q,T,1) P-MERGE-SORT(A,q+1,r,T,qโ+1) sync P-MERGE(T,1,qโ,qโ+1,n,B,s) A: Array to be merged p,r: Index of the range to be sorted B: Array to save the result ๐๐ ๐ 1 ๐ =2๐๐ ๐ 1 ๐ 2 +๐ ๐ 1 ๐ =2๐๐ ๐ 1 ๐ 2 +ฮ ๐ =ฮ ๐ log ๐ ๐๐ ๐ โ ๐ =๐๐ ๐ โ ๐ 2 +๐ ๐ โ ๐ =๐๐ ๐ โ ๐ 2 +ฮ log 2 ๐ =ฮ log 3 ๐ Parallelism = ฮ ๐ log ๐ log 3 ๐ =ฮ ๐ log 2 ๐ Much better now!
12
ๆ้บผ่ชๅทฑๅญธๆผ็ฎๆณ ้ๅธธ้่ฆ ้ๅ ่ชฒๆฒ่พฆๆณๆๆๆโ้่ฆโ็ๆผ็ฎๆณ้ฝๆๅฎ ไฝๆฏๅธๆ้็จไธญไฝ ๅทฒ็ถๅปบ็ซไบ่ชๅทฑๅญธ็ฟๆผ็ฎๆณ็่ฝๅ
ๆบๅ็ ็ฉถๆ่่ฉฆ็ๆๅๅฏ่ฝๆ้่ฆ
13
ๆ็็ถ้ฉ ๆไน่ท่ๅคงๅฎถไธ่ตทๅญธ็ฟ ่ณๆ็ตๆง ่ ๆผ็ฎๆณ ไธไบๅฅฝๆนๆณ(่ชไปฅ็บ)่ทๅคงๅฎถๅไบซ: ็ซๅ, ็จ็ฐกๅฎ็ไพๅญๅ่งฃๆญฅ้ฉ
ๅ
ไบ่งฃๆฆๅฟต(ๅคงๆนๅ), ไธๆฅ่็่ค้็ๆธๅญธ (ๆๅธธ่ชช: ๅฆๆไป้บผ้ฝๆฒๅผๆ, ๅ
ๅผๆ้ๅ) ไธๆฌกไบ่งฃไธๅฐ้จๅๅฐฑๅฅฝ (ๆจก็ตๅๅญธ็ฟ), ไธๆฅ่ๅผๆๆๆ็้จๅ ็ธไฟก่ชๅทฑไธๅฎๅฏไปฅๅผๆ (้ๅธธ้่ฆ) ไธ็ฅ้่ชๅทฑๅฐๅบๆไบๆฒ? ็จไธไบๅคๆช็ไพๅญไพ่ฉฆ่ฉฆ (boundary case) ็ทด็ฟ่ชๅทฑ็่ชฒๆฌ (้ๅธธ้่ฆ)
14
ๆๆซ่ๅ
งๅฎน&ๅๅผ Closed book, 2 x A4 cheat sheets (double-sided) ไฝๅญธๆๆ็ธพ26%
ๅ
ๅซๆดๅญธๆไธ่ชฒๅ
งๅฎน, ไฝไปฅๆไธญ่้ๅพ็ๅ
งๅฎน็บไธป 180 minutes ้กๅ่ๆไธญ่้กไผผ (ๆฏ้+้ธๆ+่งฃ้) Extra office hour ??
Similar presentations
© 2024 slidesplayer.com Inc.
All rights reserved.