Race Conditions, Chess, Matrix Multiplication




CS255

Chris Pollett

Feb 14, 2018

Outline

Introduction

Race Conditions

Preventing Race Conditions

Another Race Example

In-Class Exercise

Give a specific matrix `a[i][j]`, vector `x[j]` and situation such that the code of the last slide has a race condition.

Come up with a condition on the matrix `a[i][j]` such that if `a[i][j]` satisfies this condition, then the code of the previous slide does not involve a race condition.

Post your solution to the Feb 14 In-Class Exercise Thread.

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)