Docsity

Parallel Matrix Multiply - Lecture Slides | N 1, Exams of Health sciences

Toggle sidebar
bg1
2/26/2002 CS377 - Spring 2002 1
Parallel Matrix Multiply
Parallel Prefix Algorithms
Initial Condition: A vector which holds the number of items in
each set in a sequence of sets.
Result:A vector with the number of items preceding each set in
the sequence of sets.
Parallel Matrix Multiply
Initial Condition: Two conforming arrays holding the elements
of the matrices.
Result: An array holding the result of matrix multiplication.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Parallel Matrix Multiply - Lecture Slides | N 1 and more Exams Health sciences in PDF only on Docsity!

CS377 - Spring 2002

Parallel Matrix Multiply

Parallel Prefix Algorithms

Initial Condition: A vector which holds the number of items in

each set in a sequence of sets.

Result:A vector with the number of items preceding each set in

the sequence of sets.

Parallel Matrix Multiply

Initial Condition: Two conforming arrays holding the elementsof the matrices. Result: An array holding the result of matrix multiplication.

CS377 - Spring 2002

Parallel Matrix Multiply

Parallel Prefix Algorithm^ Sequential Algorithm^ For i = 2, step 1 until N

v[i] = v[i-1] + v[i];

2/26/

CS377 - Spring 2002

Parallel Matrix Multiply

Sequential Program for Even/Odd Algorithm

level := 2; while level <= N

begin for i := level step level until N

V[i] = V[i] + V[i-level/2];

level := level * 2; end

If level = N then level := level/2; while level > 1

begin for i = level + level/2 step level until N

V[i] = V[i] + V[i-level/2];

level := level/2; end;

2/26/

CS377 - Spring 2002

Parallel Matrix Multiply Parallel Algorithm for Odd/Even Prefix

Private level, j, i; Shared N, NP, V[1:N]; level := 2; while level <= N

begin for i := level (j-i)level step levelNP until N**

V[i] = V[i] + V[i-level/2];

barrier level := level * 2; end

If level = N then level := level/2; while level > 1

begin for i = level + level/2 +(j-i)level step levelNP until N**

V[i] = V[i] + V[i-level/2];

barrier level := level/2; end;

CS377 - Spring 2002

Parallel Matrix Multiply

private i, j, k; shared A[N,N], B[N,N], C[N,N], N; For j := 0 step 1 until N-2 fork DOCOL; j:= N-1; For i := 0 step 1 until N-1 begin

C[i,j] := 0; for k := 0 step until N-1 C[i,j] = := C[i,j] + A[i,k]* B[k,j];

end

CS377 - Spring 2002

Parallel Matrix Multiply

More Parallel Matrix Multiply Algorithms

Scalar Formulation

n

a(i, j) =

b(i, k) * c(k, j) , for i = 1 - - - n and j = 1 - - - n Σ k=

Three Indices - Six Permutations

i j k j i k k i j k j i i k jj k i

CS377 - Spring 2002

Parallel Matrix Multiply

k i j AND k j i

Compute all first terms of the inner products; (2)

Compute all second terms of the innerproducts and accumulate results with the firstterms; (3)

Continue cobegin i : <1 : n>

cobegin j : <1 : n>

temp (i, j) := 0for k := 1 to n do

temp (i, j) := temp (i, j) + b(i, k) x c(k, j) enddo

a(i, j) := temp(i, j)

coend coend

CS377 - Spring 2002

Parallel Matrix Multiply

i k j AND j k i

1. Use n

2 processes.

2. Use broadcast communication.Let matrix_of_b(i,j) = b(i,k) be a broadcast operatorwhere each element of matrix_of_b(i,j) receives value ofb(i,k). cobegin i : <1 : n>

cobegin j : <1 : n>

temp (i, j) := 0for k := 1 to n do

matrix_of_b(i, j) := b(i, k)matrix_of_c(i, j) := c(k, j)temp (i, j) := temp(i, j) +

matrix_of_b(i, j) x matrix_of_c(i, j)

enddoa(i, j) := temp(i, j) coend coend

CS377 - Spring 2002

Parallel Matrix Multiply

Matrix Multiply on an array processor with end-around nearest

neighbor connections. (Cannon’s Algorithm)

S R M 1... N

Memory foreach cell

SHIFT PLANE^ REGISTER PLANE^ MEMORY PLANE^ Memory planesConnected toS, R, M planes

CS377 - Spring 2002

Parallel Matrix Multiply

Rotation Algorithm

for k := 1 to n do

cobegin i : <1 : n>

cobegin j : <1 : n>

if i > k then b(i, j) := b(i, j Rot 1)if j > k then c(i, j) := c(i Rot 1, j) coend coend enddo

Rot is a column (row) operationwhich shifts columns up (orrows to right)

CS377 - Spring 2002

Parallel Matrix Multiply What have we done?

Pre-skewed a

,^

pre-skewed b

By rows

by cols

Rotate jth row

rotate jth column

By j-1 positions

by j-1 positions

To left

to top Then

  1. elementwise multiply2) rotate a left by rows

Rotate b top by columns

  1. elementwise multiply and sum4) repeat 2 and 3

2/26/

CS377 - Spring 2002

Parallel Matrix Multiply Block Matrix Multiply

General Case

A = (N x N)

,^

B = (N x N) N = 2k

A =

B =

A^

A

A^

A

B^

B

B^

B

N = pk C^

=^

Σ^

A^

B

p

i^

j

)^

ij

A^

= B

= (k x k)

ij^

ij

C^

C

C = A x B =

C

C

A(

B^

+ A

B

A^

B^

+ A

B

)^

A^

B^

+ A

B

B^

+ A

B

=^

(^ A

CS377 - Spring 2002

Parallel Matrix Multiply

DECOMPOSITIONINTO A

, B i^

j

A^

, B A^

, B

11 1112

21

A^

, B A^

, B

12 1112

22

A^

, B A^

, B

11 2122

21

A^

, B A^

, B

12 2222

22

C^

= A^

B + A^

B (^1112)

(^21) 11

11

C^

= A^

B 1211 + A B

12 12

22

SYNCH

CS377 - Spring 2002

Parallel Matrix Multiply

C^

= A^

B

A^

B 11 12

21 11

11

C^

= C+ A^

B 11

11 11

11

C^

= C+ A^

B 11

11 12

21

C^11 MutualExclusion

SYNCH

C^

= A^

B

A^

B 11 12

21 11

11

T^11

= A^

B 1

11

T^12

= A^

B 2

21

C^

= T^

(^111) T 2