Multithreaded Matrix Multiplication

Pseudo-code of Matrix-Multiply
Matrix − Multiply(C, A, B, n) // The result of A × B in C with n a power of 2 for simplicity
1. if (n == 1)
2. C [1, 1] = A [1, 1] + B [1, 1]
3. else
4. allocate a temporary matrix T [1...n, 1...n]
5. partition A, B, C, T into n/2 *n/2 submatrices
6. spawn Matrix − Multiply (C11, A11,     B11, n/2)
7. spawn Matrix − Multiply (C12, A11,     B12, n/2)
8. spawn Matrix − Multiply (C21, A21,     B11, n/2)
9. spawn Matrix − Multiply (C22, A21,     B12, n/2)
10. spawn Matrix − Multiply (T11, A12,   B21, n/2)
11. spawn Matrix − Multiply (T12, A12,   B21, n/2)
12. spawn Matrix − Multiply (T21, A22,   B21, n/2)
13. spawn Matrix − Multiply (T22, A22,   B22, n/2)
14. sync
15.Matrix − Add(C, T, n)


Matrix Add Code
Matrix − Add(C, T, n)
// Add matrices C and T in-place to produce C = C + T
1. if (n == 1)
2. C [1, 1] = C [1, 1] + T [1, 1]
3. else
4. Partition C and T into
sub-matrices n/2 * n/2
5. spawn Matrix − Add (C11, T11, n/2)
6. spawn Matrix − Add (C12, T12, n/2)
7. spawn Matrix − Add (C21, T21, n/2)
8. spawn Matrix − Add (C22, T22, n/2)
9. sync

Comments

Popular posts from this blog

Fibonacci Heap

Tracable and Non Tracable problems

Amortized analysis- Splay Tree