Finish Multithreaded algorithms




CS255

Chris Pollett

Feb 16, 2015

Outline

Introduction

Race Conditions

Preventing Race Conditions

Another Race Example

Quiz

Which of the following is true?

  1. The serialization of a multithreaded algorithm is the algorithm that results from deleting all occurrences of spawn, sync, and parallel keywords.
  2. The work law says `T_P ge T_(infty)`.
  3. We showed in class that it is impossible to simulate parallel for loops using spawn and sync.

Chess Program Socrates

Multithreaded Matrix Multiplication

Multithreaded Matrix Multiplication - Take 2

Divide-and-Conquer Matrix Multiply with Parallelism

P-MATRIX-MULTIPLY-RECURSIVE(C, A, B)
1 n = A.rows
2 if n == 1
3     c[1][1] = a[1][1] * b[1][1]
4 else let T be a new n x n matrix
5     partition A, B , C , and T into n/2 x n/2 submatrices 
          A_(11), A_(12), A_(21), A_(22); B_(11), B_(12), B_(21), B_(22);
          C_(11), C_(12), C_(21), C_(22); and T_(11), T_(12), T_(21), T_(22) respectively
6     spawn P-MATRIX-MULTIPLY-RECURSIVE(C_(11), A_(11), B_(11))
7     spawn P-MATRIX-MULTIPLY-RECURSIVE(C_(12), A_(11), B_(12))
8     spawn P-MATRIX-MULTIPLY-RECURSIVE(C_(21), A_(21), B_(11))
9     spawn P-MATRIX-MULTIPLY-RECURSIVE(C_(22), A_(21), B_(12))
10    spawn P-MATRIX-MULTIPLY-RECURSIVE(T_(11), A_(12), B_(21))
11    spawn P-MATRIX-MULTIPLY-RECURSIVE(T_(12), A_(12), B_(22))
12    spawn P-MATRIX-MULTIPLY-RECURSIVE(T_(21), A_(22), B_(21))
13    P-MATRIX-MULTIPLY-RECURSIVE(T_(22), A_(22), B_(22))
14    sync
15    parallel for i = 1 to n
16        parallel for j = 1 to n
17            c[i][j] = c[i][j] + t[i][j]

Analysis of P-MATRIX-MULTIPLY-RECURSIVE(C, A, B)