Docsity

Dense Matrix - Parallel Computing - Lecture Slides, Slides of Parallel Computing and Programming

Partial preview of the text

Download Dense Matrix - Parallel Computing - Lecture Slides and more Slides Parallel Computing and Programming in PDF only on Docsity!

Algorithms for Dense

Matrices, Vectors, and Arrays

2

Topics for Today

  • Matrix-vector multiplication —1D and 2D partitionings
  • Matrix-matrix multiplication —2D partitioning and Cannon’s algorithm
  • Exotic distributions for dense arrays —generalized multipartitioning for line sweep computations

4

Matrix-Vector Multiplication

  • Problem: — multiply a dense n x n matrix A with an n x 1 vector x — yield the n x 1 result vector y
  • Serial algorithm requires^ n 2

multiplications and additions

5 Review: Matrix-Vector Multiplication: 1D Partitioning

First, consider simple case: p = n

  • p^ x^ p^ matrix is partitioned among^ p^ processors — each processor stores a complete row of the matrix
  • p^ x 1 vector^ x^ is also partitioned — each process owns one element of x

7 Matrix-Vector Multiplication: 1D Partitioning

Analysis for p = n

  • All-to-all broadcast:^ Θ(p)^ =^ Θ(n)^ time —each process must receive vector y, which is of length n
  • Computation of y[i]:^ Θ(p)=^ Θ(n)^ time —each process performs dot product of length n
  • Overall algorithm:^ Θ(p)=^ Θ(n)^ time —both communication and computation are Θ(n)
  • Total parallel work =^ Θ(p 2

) = Θ(n

2

  • Algorithm is cost optimal: sequential work W =^ Θ(p 2

) = Θ(n

2

—recall that cost optimality means that^ pTp =^ Θ(W)

  • total parallel work is asymptotically equivalent to sequential work

8

Consider p < n

  • Initially, each process stores —n/p complete rows of the matrix A —n/p elements of the vector x
  • Distribute all of vector to each process —all-to-all broadcast among p processes, messages of size n/p
  • On each processor: n/p local dot products on rows of length n
  • Parallel run time
  • Cost-optimal execution for^ p = O(n) Matrix-Vector Multiplication: 1D Partitioning (for large p on hypercube)

10 Matrix-Vector Multiplication: 2D Partitioning

Computation sketch

  • Align the vector with the matrix —first communication step - align the vector x along the main diagonal of the matrix —second communication step - copy vector element from each diagonal process to all processes in its corresponding column use n independent broadcasts one among all processors in each column
  • Compute the result vector —perform all-to-one reduction along the rows

11 Matrix-Vector Multiplication: 2-D Partitioning For the one-element-per-process case, p = n 2 if matrix size is n x n

13

Matrix-Vector Multiplication: 2D Partitioning

Consider when p < n

2

  • Each process owns an^ block of the matrix
  • Vector initial distribution —blocks of elements —in the last process-column only
  • Message for alignment, broadcast, and reduction all size
  • Computation —product of an submatrix with a vector of length

14

Matrix-Vector Multiplication: 2D Partitioning

  • The first alignment step takes time
  • The broadcast and reductions take time
  • Local matrix-vector products take time
  • Total time is

16

Matrix-Matrix Multiplication

Consider two n x n matrices A and B partitioned into p blocks

  • Ai,j and^ Bi,j (0^ ≤^ i, j^ <^ ) of size^ each
  • Process^ Pi,j — initially stores^ Ai,j and^ Bi,j —computes block^ Ci,j of the result matrix.
  • Computing submatrix^ Ci,j —requires all submatrices^ Ai,k and^ Bk,j for 0^ ≤^ k^ <

17

Matrix Multiplication

Consider data needed for output matrix block shown in purple

× =

19 Matrix-Matrix Multiplication: Cannon's Algorithm

Memory efficient algorithm idea

  • Approach —schedule the computations of the processes of each row — any given time, each process is using different blocks
  • Systematically rotate blocks among the processes —rotate after every submatrix multiplication —each process get fresh blocks in each rotation

20 Matrix-Matrix Multiplication: Cannon's Algorithm

Aim: align the blocks of A and B so that

each process multiplies its local submatrices

  • Initial alignment —shift all submatrices^ Ai,j to the left (with wraparound) by^ i^ steps —shift all submatrices^ Bi,j up (with wraparound) by^ j^ steps
  • Perform local block multiplication
  • Rotate —move each block of A one step left (with wraparound) —move each block of B moves one step up (with wraparound)
  • Perform next block multiplication
  • Add to partial result
  • Repeat until all^ blocks have been multiplied.