34
Multithreaded algorithms 9/28/20

Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Multithreaded algorithms9/28/20

Page 2: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Administrivia

• HW 2 due Monday night

• For Wednesday, finish reading section 27.1

Page 3: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Big Picture

• Essentially every computer is now multicore• Means it can run multiple parts of a program at the same time

• Threads• Main abstraction for shared memory computing

Page 4: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Recall: Naïve Fibonacci implementation

Fib(n)if(n <= 1)

return nelse

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

Page 5: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Parallel version

P_Fib(n)if(n <= 1)

return nelse

x = spawn P_Fib(n-1)y = P_Fib(n-2)syncreturn x + y

Allows child procedure to run in parallel with its parent

Causes parent to wait for all children to complete

Page 6: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Parallel version

P_Fib(n)if(n <= 1)

return nelse

x = spawn P_Fib(n-1)y = P_Fib(n-2)syncreturn x + y

Can represent runtime behavior using a DAG (directed acyclic graph)

Vertices are strands (instructions w/o spawn or sync)

Edges show precedence constraints

Page 7: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Parallel version

P_Fib(n)if(n <= 1)

return nelse

x = spawn P_Fib(n-1)y = P_Fib(n-2)syncreturn x + y

Can represent runtime behavior using a DAG (directed acyclic graph)

Vertices are strands (instructions w/o spawn or sync)

Edges show precedence constraints

Metrics:Work T1 = time on one processorSpan T∞ = length of longest path

Page 8: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Which of the following is a lower bound on Tp, the time on p processors?

A. T1 + p

B. T1 - p

C. p T1

D. T1 / p

E. Not exactly one of the above

Page 9: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Which of the following is a lower bound on Tp, the time on p processors?

A. T1 + p

B. T1 - p

C. p T1

D. T1 / p (called the work law)

E. Not exactly one of the above

Page 10: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Which of the following is a lower bound on Tp, the time on p processors?

A. T∞

B. T∞ - p

C. pT∞

D. T∞ / p

E. Not exactly one of the above

Page 11: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Which of the following is a lower bound on Tp, the time on p processors?

A. T∞ (called the span law)

B. T∞ - p

C. pT∞

D. T∞ / p

E. Not exactly one of the above

Page 12: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Page 13: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Corollary: The running time of any such scheduler is within a factor of 2 of optimal

Page 14: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Corollary: The running time of any such scheduler is within a factor of 2 of optimal

Another metric: parallelism = T1/ T∞(intuitively, the number of processors we can use)

Page 15: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 16: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 17: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 18: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 19: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 20: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 21: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 22: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time

Tp ⩽ T1/p + T∞

Proof: Charge each time step to one of the terms.Charge complete steps (when all processors are busy) to T1/p.Suppose to contrary that every processor is busy > ⌊T1/p⌋ time steps.Then these steps do work ≥ p(⌊T1/p⌋ + 1) = p ⌊T1/p⌋ + p

= T1 – (T1 mod p) + p> T1

Charge incomplete steps (at least one processor idle) to T∞.If a processor is idle, it must schedule every strand w/o incomplete prerequisites. Every path must start with one of these, so this shortens every critical path, which can happen at most T∞ times.

Page 23: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Combining subcomputations

A BA

B

Page 24: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Combining subcomputations

Work: T1(A) + T1(B) Work: T1(A) + T1(B)

A BA

B

Page 25: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Combining subcomputations

Work: T1(A) + T1(B)

Span: T∞(A) + T ∞(B)

Work: T1(A) + T1(B)

Span: max{ T∞(A), T ∞(B) }

A BA

B

Page 26: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the span of the following code?for(int i=0; i < n; i++)

spawn A[i] = B[i];syncA. ⍬(1)B. ⍬(log n)C. ⍬(n1/2)D. ⍬(n)E. None of the above

Page 27: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the span of the following code?for(int i=0; i < n; i++)

spawn A[i] = B[i];syncA. ⍬(1)B. ⍬(log n)C. ⍬(n1/2)D. ⍬(n)E. None of the above

Page 28: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

Alternate idea of a for loop

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

Page 29: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What recurrence gives the work of this?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

(n = e – s + 1) A. T1(n) = T1(n/2) + 1B. T1(n) = T1(n/2) + nC. T1(n) = 2T1(n/2) + 1D. T1(n) = 2T1(n/2) + nE. None of the above

Page 30: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What recurrence gives the work of this?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

(n = e – s + 1) A. T1(n) = T1(n/2) + 1B. T1(n) = T1(n/2) + nC. T1(n) = 2T1(n/2) + 1D. T1(n) = 2T1(n/2) + nE. None of the above

Page 31: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the work of this?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

A. ⍬(1)B. ⍬(log n)C. ⍬(n)D. ⍬(n log n)E. None of the above

Page 32: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the work of this?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

A. ⍬(1)B. ⍬(log n)C. ⍬(n)D. ⍬(n log n)E. None of the above

Page 33: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the span?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

A. ⍬(1)B. ⍬(log n)C. ⍬(n0.5)D. ⍬(n)E. None of the above

Page 34: Multithreaded - Knox Collegecourses.knox.edu/cs205/notes/Multithreaded.pdf · Th: With p processors, any scheduler that is never voluntarily idle completes a computation in time T

What is the span?

void do_it(int s, int e) {if(s == e)

A[s] = B[s]else {

spawn do_it(s, (s+e)/2)do_it((s+e)/2+1, e)sync

}}…do_it(1,n)

A. ⍬(1)B. ⍬(log n)C. ⍬(n0.5)D. ⍬(n)E. None of the above