+ All Categories
Home > Documents > Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies...

Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies...

Date post: 12-Jan-2016
Category:
Upload: imogene-brown
View: 213 times
Download: 0 times
Share this document with a friend
61
Multithreaded Programming in Cilk LECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence Laboratory Massachusetts Institute of Technology
Transcript
Page 1: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

Multithreaded Programming in

CilkLECTURE 2

Multithreaded Programming in

CilkLECTURE 2

Charles E. LeisersonSupercomputing Technologies Research Group

Computer Science and Artificial Intelligence LaboratoryMassachusetts Institute of Technology

Page 2: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 2© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Minicourse Outline●LECTURE 1

Basic Cilk programming: Cilk keywords, performance measures, scheduling.

●LECTURE 2Analysis of Cilk algorithms: matrix multiplication, sorting, tableau construction.

●LABORATORYProgramming matrix multiplication in Cilk

— Dr. Bradley C. Kuszmaul●LECTURE 3

Advanced Cilk programming: inlets, abort, speculation, data synchronization, & more.

Page 3: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 3© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

LECTURE 2

• Matrix Multiplication

• Tableau Construction

• Recurrences (Review)

• Conclusion

• Merge Sort

Page 4: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 4© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

The Master MethodThe Master Method for solving recurrences applies to recurrences of the form

T(n) = a T(n/b) + f (n) ,

where a ¸ 1, b > 1, and f is asymptotically positive.

IDEA: Compare nlogba with f (n) .IDEA: Compare nlogba with f (n) .

*The unstated base case is T(n) = (1) for sufficiently small n.

*

Page 5: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 5© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Master Method — CASE 1

nlogba À f (n)nlogba À f (n)

Specifically, f (n) = O(nlogba – ) for some constant > 0.Solution: T(n) = (nlogba) .

T(n) = a T(n/b) + f (n)T(n) = a T(n/b) + f (n)

Page 6: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 6© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Master Method — CASE 2

Specifically, f (n) = (nlogba lgkn) for some constant k ¸ 0.Solution: T(n) = (nlogba lgk+1n) .

nlogba ¼ f (n)nlogba ¼ f (n)

T(n) = a T(n/b) + f (n)T(n) = a T(n/b) + f (n)

Page 7: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 7© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Master Method — CASE 3

Specifically, f (n) = (nlogba + ) for some constant > 0 and f (n) satisfies the regularity condition that a f (n/b) · c f (n) for some constant c < 1.Solution: T(n) = (f (n)) .

nlogba ¿ f (n)nlogba ¿ f (n)

T(n) = a T(n/b) + f (n)T(n) = a T(n/b) + f (n)

Page 8: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 8© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Master Method Summary

CASE 1: f (n) = O(nlogba – ), constant > 0 T(n) = (nlogba) .

CASE 2: f (n) = (nlogba lgkn), constant k 0 T(n) = (nlogba lgk+1n) .

CASE 3: f (n) = (nlogba + ), constant > 0, and regularity condition

T(n) = ( f (n)) .

T(n) = a T(n/b) + f (n)T(n) = a T(n/b) + f (n)

Page 9: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 9© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Master Method Quiz

• T(n) = 4 T(n/2) + nnlogba = n2 À n ) CASE 1: T(n) = (n2).

• T(n) = 4 T(n/2) + n2

nlogba = n2 = n2 lg0n ) CASE 2: T(n) = (n2lg n).

• T(n) = 4 T(n/2) + n3

nlogba = n2 ¿ n3 ) CASE 3: T(n) = (n3).

• T(n) = 4 T(n/2) + n2/ lg nMaster method does not apply!

Page 10: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 10© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

LECTURE 2

• Matrix Multiplication

• Tableau Construction

• Recurrences (Review)

• Conclusion

• Merge Sort

Page 11: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 11© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Square-Matrix Multiplicationc11 c12 c1n

c21 c22 c2n

cn1 cn2 cnn

a11 a12 a1n

a21 a22 a2n

an1 an2 ann

b11 b12 b1n

b21 b22 b2n

bn1 bn2 bnn

= £

C A B

cij=

k = 1

n

aik bkj

Assume for simplicity that n = 2k.

Page 12: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 12© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Recursive Matrix Multiplication

8 multiplications of (n/2) £ (n/2) matrices.1 addition of n £ n matrices.

Divide and conquer —

C11 C12

C21 C22

= £A11 A12

A21 A22

B11 B12

B21 B22

= +A11B11 A11B12

A21B11 A21B12

A12B21 A12B22

A22B21 A22B22

Page 13: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 13© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

Matrix Multiply in Pseudo-Cilk

C = A¢ BAbsence of type

declarations.Absence of type

declarations.

Page 14: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 14© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

C = A¢ B Coarsen base cases for efficiency.Coarsen base cases for efficiency.

Matrix Multiply in Pseudo-Cilk

Page 15: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 15© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

C = A¢ B

Submatrices are produced by pointer calculation, not copying of elements.

Submatrices are produced by pointer calculation, not copying of elements.

Also need a row-size argument for array indexing.

Also need a row-size argument for array indexing.

Matrix Multiply in Pseudo-Cilk

Page 16: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 16© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(C22,A21,B12,n/2); spawn Mult(C21,A21,B11,n/2); spawn Mult(T11,A12,B21,n/2); spawn Mult(T12,A12,B22,n/2); spawn Mult(T22,A22,B22,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

C = A¢ B

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}C = C + T

Matrix Multiply in Pseudo-Cilk

Page 17: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 17© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

A1(n) = ?4 A1(n/2) + (1)

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

Work of Matrix Addition

Work:

nlogba = nlog24 = n2 À (1) .

— CASE 1= (n2)

Page 18: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 18© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

cilk void Add(*C, *T, n) { h base case & partition matrices i spawn Add(C11,T11,n/2); spawn Add(C12,T12,n/2); spawn Add(C21,T21,n/2); spawn Add(C22,T22,n/2); sync; return;}

A1(n) = ?

Span of Matrix Addition

A1(n/2) + (1)Span:

nlogba = nlog21 = 1 ) f (n) = (nlogba lg0n) .

— CASE 2= (lg n)

maximum

Page 19: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 19© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

M1(n) = ?

Work of Matrix Multiplication

8 M1(n/2) +A1(n) + (1)Work:

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

8

nlogba = nlog28 = n3 À (n2) .

= 8 M1(n/2) + (n2)= (n3) — CASE 1

Page 20: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 20© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { float *T = Cilk_alloca(n*n*sizeof(float)); h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

M1(n) = ?M1(n/2) + A1(n) + (1)

Span of Matrix Multiplication

Span:

nlogba = nlog21 = 1 ) f (n) = (nlogba lg1n) .

= M1(n/2) + (lg n)= (lg2 n) — CASE 2

8

Page 21: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 21© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallelism of Matrix Multiply

M1(n) = (n3)Work:

M1(n) = (lg2n)Span:

Parallelism: M1(n)M1(n)

= (n3/lg2n)

For 1000 £ 1000 matrices, parallelism ¼ (103)3/102 = 107.

Page 22: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 22© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void Mult(*C, *A, *B, n) { h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

cilk void Mult(*C, *A, *B, n) { h base case & partition matrices i spawn Mult(C11,A11,B11,n/2); spawn Mult(C12,A11,B12,n/2); spawn Mult(T21,A22,B21,n/2); sync; spawn Add(C,T,n); sync; return;}

Stack Temporariesfloat *T = Cilk_alloca(n*n*sizeof(float));

In hierarchical-memory machines (especially chip multiprocessors), memory accesses are so expensive that

minimizing storage often yields higher performance.

IDEA: Trade off parallelism for less storage.IDEA: Trade off parallelism for less storage.

Page 23: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 23© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

No-Temp Matrix Multiplicationcilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

Saves space, but at what expense?

Page 24: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 24© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

= (n3)

Work of No-Temp Multiply

M1(n) = ?8 M1(n/2) + (1)Work:— CASE 1

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

Page 25: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 25© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

cilk void MultA(*C, *A, *B, n) { // C = C + A * B h base case & partition matrices i spawn MultA(C11,A11,B11,n/2); spawn MultA(C12,A11,B12,n/2); spawn MultA(C22,A21,B12,n/2); spawn MultA(C21,A21,B11,n/2); sync; spawn MultA(C21,A22,B21,n/2); spawn MultA(C22,A22,B22,n/2); spawn MultA(C12,A12,B22,n/2); spawn MultA(C11,A12,B21,n/2); sync; return;}

= (n)M1(n) = ?

Span of No-Temp Multiply

Span:— CASE 1

2 M1(n/2) + (1)

maximum

maximum

Page 26: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 26© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallelism of No-Temp Multiply

M1(n) = (n3)Work:

M1(n) = (n)Span:

Parallelism: M1(n)M1(n)

= (n2)

For 1000 £ 1000 matrices, parallelism ¼ (103)3/103 = 106.

Faster in practice!

Page 27: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 27© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Testing SynchronizationCilk language feature: A programmer can check whether a Cilk procedure is “synched” (without actually performing a sync) by testing the pseudovariable SYNCHED:•SYNCHED = 0 ) some spawned children

might not have returned.•SYNCHED = 1 ) all spawned children have

definitely returned.

Page 28: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 28© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Best of Both Worldscilk void Mult1(*C, *A, *B, n) {// multiply & store h base case & partition matrices i spawn Mult1(C11,A11,B11,n/2); // multiply & store spawn Mult1(C12,A11,B12,n/2); spawn Mult1(C22,A21,B12,n/2); spawn Mult1(C21,A21,B11,n/2); if (SYNCHED) { spawn MultA1(C11,A12,B21,n/2); // multiply & add spawn MultA1(C12,A12,B22,n/2); spawn MultA1(C22,A22,B22,n/2); spawn MultA1(C21,A22,B21,n/2); } else { float *T = Cilk_alloca(n*n*sizeof(float)); spawn Mult1(T11,A12,B21,n/2); // multiply & store spawn Mult1(T12,A12,B22,n/2); spawn Mult1(T22,A22,B22,n/2); spawn Mult1(T21,A22,B21,n/2); sync; spawn Add(C,T,n); // C = C + T } sync; return;}

cilk void Mult1(*C, *A, *B, n) {// multiply & store h base case & partition matrices i spawn Mult1(C11,A11,B11,n/2); // multiply & store spawn Mult1(C12,A11,B12,n/2); spawn Mult1(C22,A21,B12,n/2); spawn Mult1(C21,A21,B11,n/2); if (SYNCHED) { spawn MultA1(C11,A12,B21,n/2); // multiply & add spawn MultA1(C12,A12,B22,n/2); spawn MultA1(C22,A22,B22,n/2); spawn MultA1(C21,A22,B21,n/2); } else { float *T = Cilk_alloca(n*n*sizeof(float)); spawn Mult1(T11,A12,B21,n/2); // multiply & store spawn Mult1(T12,A12,B22,n/2); spawn Mult1(T22,A22,B22,n/2); spawn Mult1(T21,A22,B21,n/2); sync; spawn Add(C,T,n); // C = C + T } sync; return;}

This code is just as parallel as the original, but it only uses more space if runtime parallelism actually exists.

This code is just as parallel as the original, but it only uses more space if runtime parallelism actually exists.

Page 29: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 29© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Ordinary Matrix Multiplication

cij=

k = 1

n

aik bkj

IDEA: Spawn n2 inner products in parallel. Compute each inner product in parallel.

Work: (n3)Span: (lg n)Parallelism: (n3/lg n)

BUT, this algorithm exhibits poor locality and does not exploit the cache hierarchy of modern microprocessors, especially CMP’s.

Page 30: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 30© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

LECTURE 2

• Matrix Multiplication

• Tableau Construction

• Recurrences (Review)

• Conclusion

• Merge Sort

Page 31: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 31© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

3 12 19 46

4 14 21 23

193

4

12

14 21 23

46

Merging Two Sorted Arraysvoid Merge(int *C, int *A, int *B, int na, int nb) { while (na>0 && nb>0) { if (*A <= *B) { *C++ = *A++; na--; } else { *C++ = *B++; nb--; } } while (na>0) { *C++ = *A++; na--; } while (nb>0) { *C++ = *B++; nb--; }}

void Merge(int *C, int *A, int *B, int na, int nb) { while (na>0 && nb>0) { if (*A <= *B) { *C++ = *A++; na--; } else { *C++ = *B++; nb--; } } while (na>0) { *C++ = *A++; na--; } while (nb>0) { *C++ = *B++; nb--; }}

Time to merge n elements = ?Time to merge n elements = ? (n).

Page 32: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 32© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

Merge Sort

144619 3 12 33 4 21

4 3319 46 143 12 21

46 333 12 19 4 14 21

46143 4 12 19 21 33

merge

merge

merge

Page 33: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 33© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

= (n lg n)T1(n) = ?2 T1(n/2) + (n)

Work of Merge Sort

Work:— CASE 2

nlogba = nlog22 = n ) f (n) = (nlogba lg0n) .

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

Page 34: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 34© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

T1(n) = ?T1(n/2) + (n)

Span of Merge Sort

Span:— CASE 3= (n)

nlogba = nlog21 = 1 ¿ (n) .

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn MergeSort(C, A, n/2); spawn MergeSort(C+n/2, A+n/2, n-n/2); sync; Merge(B, C, C+n/2, n/2, n-n/2); } }

Page 35: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 35© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallelism of Merge Sort

T1(n) = (n lg n)Work:

T1(n) = (n)Span:

Parallelism: T1(n)T1(n)

= (lg n)

We need to parallelize the merge!We need to parallelize the merge!

Page 36: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 36© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

B

A0 na

0 nbna ¸ nb

Parallel Merge

· A[na/2] ¸ A[na/2]

Binary search

j j+1

Recursivemerge

Recursivemerge

na/2

· A[na/2] ¸ A[na/2]

KEY IDEA: If the total number of elements to be merged in the two arrays is n = na + nb, the total number of elements in the larger of the two recursive merges is at most ? (3/4) n .

Page 37: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 37© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallel Mergecilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) { spawn P_Merge(C, B, A, nb, na); } else if (na==1) { if (nb == 0) { C[0] = A[0]; } else { C[0] = (A[0]<B[0]) ? A[0] : B[0]; /* minimum */ C[1] = (A[0]<B[0]) ? B[0] : A[0]; /* maximum */ } } else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

cilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) { spawn P_Merge(C, B, A, nb, na); } else if (na==1) { if (nb == 0) { C[0] = A[0]; } else { C[0] = (A[0]<B[0]) ? A[0] : B[0]; /* minimum */ C[1] = (A[0]<B[0]) ? B[0] : A[0]; /* maximum */ } } else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

Coarsen base cases for efficiency.

Page 38: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 38© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

T1(n) = ?T1(3n/4) + (lg n)

Span of P_Merge

Span:— CASE 2= (lg2n)

cilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) {

} else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

cilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) {

} else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

nlogba = nlog4/31 = 1 ) f (n) = (nlogba lg1n) .

Page 39: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 39© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

T1(n) = ?T1(n) + T1((1–)n) + (lg n),where 1/4 · · 3/4 .

Work of P_Merge

Work:

cilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) {

} else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

cilk void P_Merge(int *C, int *A, int *B, int na, int nb) { if (na < nb) {

} else { int ma = na/2; int mb = BinarySearch(A[ma], B, nb); spawn P_Merge(C, A, B, ma, mb); spawn P_Merge(C+ma+mb, A+ma, B+mb, na-ma, nb-mb); sync; }}

CLAIM: T1(n) = (n) .

Page 40: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 40© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Analysis of Work Recurrence

Substitution method: Inductive hypothesis is T1(k) · c1k – c2lg k, where c1,c2 > 0. Prove that the relation holds, and solve for c1 and c2.

T1(n) = T1(n) + T1((1–)n) + (lg n), where 1/4 · · 3/4 .

T1(n) = T1(n) + T1((1–)n) + (lg n)

· c1(n) – c2lg(n) + c1((1–)n) – c2lg((1–)n) + (lg n)

Page 41: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 41© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Analysis of Work Recurrence

T1(n) = T1(n) + T1((1–)n) + (lg n)

· c1(n) – c2lg(n) + c1(1–)n – c2lg((1–)n) + (lg n)

T1(n) = T1(n) + T1((1–)n) + (lg n), where 1/4 · · 3/4 .

Page 42: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 42© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

T1(n) = T1(n) + T1((1–)n) + (lg n)

· c1(n) – c2lg(n) + c1(1–)n – c2lg((1–)n) + (lg n)

Analysis of Work Recurrence

· c1n – c2lg(n) – c2lg((1–)n) + (lg n)· c1n – c2 ( lg((1–)) + 2 lg n ) + (lg n)· c1n – c2 lg n

– (c2(lg n + lg((1–))) – (lg n))· c1n – c2 lg n

by choosing c1 and c2 large enough.

T1(n) = T1(n) + T1((1–)n) + (lg n), where 1/4 · · 3/4 .

Page 43: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 43© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallelism of P_Merge

T1(n) = (n)Work:

T1(n) = (lg2n)Span:

Parallelism: T1(n)T1(n)

= (n/lg2n)

Page 44: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 44© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

Parallel Merge Sort

Page 45: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 45© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

T1(n) = 2 T1(n/2) + (n)

Work of Parallel Merge Sort

Work:— CASE 2= (n lg n)

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

Page 46: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 46© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Span of Parallel Merge Sort

T1(n) = ?T1(n/2) + (lg2n)Span:— CASE 2= (lg3n)

nlogba = nlog21 = 1 ) f (n) = (nlogba lg2n) .

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

cilk void P_MergeSort(int *B, int *A, int n) { if (n==1) { B[0] = A[0]; } else { int *C; C = (int*) Cilk_alloca(n*sizeof(int)); spawn P_MergeSort(C, A, n/2); spawn P_MergeSort(C+n/2, A+n/2, n-n/2); sync;

spawn P_Merge(B, C, C+n/2, n/2, n-n/2); } }

Page 47: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 47© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Parallelism of Merge Sort

T1(n) = (n lg n)Work:

T1(n) = (lg3n)Span:

Parallelism: T1(n)T1(n)

= (n/lg2n)

Page 48: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 48© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

LECTURE 2

• Matrix Multiplication

• Tableau Construction

• Recurrences (Review)

• Conclusion

• Merge Sort

Page 49: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 49© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Tableau Construction

A[i, j] = f ( A[i, j–1], A[i–1, j], A[i–1, j–1] ).Problem: Fill in an n £ n tableau A, where

Dynamic programming• Longest common

subsequence• Edit distance• Time warping

0000 0101 0202 0303 0404 0505 0606 0707

1010 1111 1212 1313 1414 1515 1616 1717

2020 2121 2222 2323 2424 2525 2626 2727

3030 3131 3232 3333 3434 3535 3636 3737

4040 4141 4242 4343 4444 4545 4646 4747

5050 5151 5252 5353 5454 5555 5656 5757

6060 6161 6262 6363 6464 6565 6666 6767

7070 7171 7272 7373 7474 7575 7676 7777Work: (n2).

Page 50: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 50© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

n

n

spawn I;sync;spawn II;spawn III;sync;spawn IV;sync;

II IIII

IIIIII IVIV

Cilk code

Recursive Construction

Page 51: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 51© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

n

n

Work: T1(n) = ?4T1(n/2) + (1)

spawn I;sync;spawn II;spawn III;sync;spawn IV;sync;

II IIII

IIIIII IVIV

Cilk code

Recursive Construction

= (n2) — CASE 1

Page 52: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 52© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Span: T1(n) = ?

n

n

spawn I;sync;spawn II;spawn III;sync;spawn IV;sync;

II IIII

IIIIII IVIV

Cilk code

Recursive Construction

3T1(n/2) + (1)= (nlg3) — CASE 1

Page 53: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 53© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Analysis of Tableau Construction

Work: T1(n) = (n2)

Span: T1(n) = (nlg3)¼ (n1.58)

Parallelism: T1(n)T1(n)

¼ (n0.42)

Page 54: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 54© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

n spawn I;sync;spawn II;spawn III;sync;spawn IV;spawn V;spawn VIsync;spawn VII;spawn VIII;sync;spawn IX;sync;

A More-Parallel Construction

II IIII

IIIIII

IVIV

VV

VIVI

VIIVII

VIIIVIII IXIX

n

Page 55: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 55© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

n spawn I;sync;spawn II;spawn III;sync;spawn IV;spawn V;spawn VIsync;spawn VII;spawn VIII;sync;spawn IX;sync;

A More-Parallel Construction

II IIII

IIIIII

IVIV

VV

VIVI

VIIVII

VIIIVIII IXIX

n

Work: T1(n) = ?9T1(n/3) + (1)= (n2) — CASE 1

Page 56: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 56© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

n spawn I;sync;spawn II;spawn III;sync;spawn IV;spawn V;spawn VIsync;spawn VII;spawn VIII;sync;spawn IX;sync;

A More-Parallel Construction

II IIII

IIIIII

IVIV

VV

VIVI

VIIVII

VIIIVIII IXIX

n

Span: T1(n) = ?5T1(n/3) + (1)= (nlog35) — CASE 1

Page 57: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 57© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Analysis of Revised Construction

Work: T1(n) = (n2)

Span: T1(n) = (nlog35)¼ (n1.46)

Parallelism: T1(n)T1(n)

¼ (n0.54)

More parallel by a factor of(n0.54)/(n0.42) = (n0.12) .

Page 58: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 58© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Puzzle

• You may only use basic Cilk control constructs (spawn, sync) for synchronization.

• No locks, synchronizing through memory, etc.

What is the largest parallelism that can be obtained for the tableau-

construction problem using Cilk?

What is the largest parallelism that can be obtained for the tableau-

construction problem using Cilk?

Page 59: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 59© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

LECTURE 2

• Matrix Multiplication

• Tableau Construction

• Recurrences (Review)

• Conclusion

• Merge Sort

Page 60: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 60© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Key Ideas• Cilk is simple: cilk, spawn, sync, SYNCHED

• Recurrences, recurrences, recurrences, …• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span• Work & span

Page 61: Multithreaded Programming in Cilk L ECTURE 2 Charles E. Leiserson Supercomputing Technologies Research Group Computer Science and Artificial Intelligence.

July 14, 2006 61© 2006 by Charles E. Leiserson Multithreaded Programming in Cilk —LECTURE 2

Minicourse Outline●LECTURE 1

Basic Cilk programming: Cilk keywords, performance measures, scheduling.

●LECTURE 2Analysis of Cilk algorithms: matrix multiplication, sorting, tableau construction.

●LABORATORYProgramming matrix multiplication in Cilk

— Dr. Bradley C. Kuszmaul●LECTURE 3

Advanced Cilk programming: inlets, abort, speculation, data synchronization, & more.


Recommended