Docsity

Numerical Algorithms: Matrix Multiplication and Gaussian Elimination - Prof. Jingke Li, Study notes of Computer Science

Toggle sidebar
bg1
Numerical Algorithms
Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 1 / 30
Basic Numerical Algorithms
•Dense-Matrix Algorithms:
•Matrix-vector multiplication
•Matrix-matrix multiplication
•Gaussian elimination
•Sparse-Matrix Algorithms:
•Jacobi relaxation
•Gaussian-Seidel
•Multi-grid method
Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 2 / 30
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Numerical Algorithms: Matrix Multiplication and Gaussian Elimination - Prof. Jingke Li and more Study notes Computer Science in PDF only on Docsity!

Numerical Algorithms

Jingke Li

Portland State University

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 1 / 30

Basic Numerical Algorithms

  • Dense-Matrix Algorithms:
    • Matrix-vector multiplication
    • Matrix-matrix multiplication
    • Gaussian elimination
  • (^) Sparse-Matrix Algorithms:
    • Jacobi relaxation
    • Gaussian-Seidel
    • Multi-grid method

Matrix-Vector Multiplication

Compute: y = Ax

A is an n × n matrix, x, y are n × 1 vectors

Sequential Algorithm:

procedure MAT_VECT(A,x,y) begin for i:=0 to n-1 do y[i] := 0; for j:=0 to n-1 do y[i] := y[i] + A[i,j] * x[j]; endfor endfor end MAT_VECT

Sequential Complexity: T = O(n^2 )

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 3 / 30

Row-wise 1D Partitioning

Parallel Complexity: Tp ≈ n

2 p +^ ts^ log^ p^ +^ tw^ n

Shared-Memory MM Algorithms

  • (^) Easy to partition the workload
  • (^) No synchronization needed, hence very efficient
  • Can achieve almost linear speedup

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 7 / 30

Message-Passing MM Algorithms

  • Distribution Choices:
    • 1D:

A B C

• 2D:

A B C

Message-Passing MM Algorithms

  • (^) Communication Choices:
    • Broadcast — The needed data is broadcast to all destinations once for all. (A single step, but needs a lot of buffer storage.)
    • Multiple Shifts — The needed data is shifted towards its destination one step at a time. (Only local communication and very little buffer space, but takes multiple steps for data to reach destination.)

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 9 / 30

A Simple 2D Algorithm

  • Processors are arranged in a logical

p ×

p 2D topology.

  • Each processor gets a block of √^ n p ×^ √n p block of^ A,^ B,^ &C^.
  • Perform all-to-all broadcasts within processor rows for A’s blocks.
  • Take

p iterations of the following steps:

  1. Multiply A’s block with B’s block to form C ’s block.
  2. Shift B’s blocks within processor columns.

A 00 A 11 A 22 A 33

A 00 A 11 A 22 A 33

A 00 A 00 A 00 A 11 A 11 A 11 A 22 A 22 A 22 A 33 A 33 A 33

B 11

B 01 B 02 B 03 B 10 B 12 B 13 B 20 B 21 B 22 B 23 B 30 B 31 B 32 B 33

B 00 B 11

B 01 B 02 B 03

B 10 B 12 B 13 B 20 B 21 B 22 B 23 B 30 B 31 B 32 B 33 B 00

(a)

(c)

A 00 B 00 (b)

A 00 A 00 A 00

A 22

A 11

A 33

A 11 A 11 A 11 A 22 A 22 A 22 A 33 A 33 A 33

B 01 B 02 B 03 B 10 B 20 B 30

B 11 B 12 B 13 B 21 B 22 B 23 B 31 B 32 B 33

Complexity: Tp ≈ n

3 p +^ ts^ log^ p^ + 2tw √n^2 p

Cannon’s Algorithm (cont.)

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 13 / 30

A 3D Algorithm

For multiplying two n × n matrices:

  • (^) n^3 processes are used; they are arranged in a n × n × n logical grid.
  • (^) Each process is assigned a single scale multiplication — the multiplication A[i, k] × B[k, j] is assigned to process Pi ,j,k (0 ≤ i, j, k < n).
  • (^) An addition step is performed after the multiplications.

Complexity: Tp ≈ n 3 p +^ ts^ log^ p^ +^ tw^

n^2 p^2 /^3 log p

Gaussian Elimination

  • A well-known algorithm for solving a linear system Ax = b.
  • It reduces Ax = b to an upper triangular system Tx = c,
  • then uses back substitution to solve for x.

The operations are:

  • Interchange any two rows (this simply reorders the n equations).
  • Replace any row by a linear combination of itself and another row.

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 15 / 30

Gaussian Elimination Example

2.0 x1 + 1.0 x2 + 3.0 x3 + 4.0 x4 = 29. 5.0 x1 + 2.0 x2 + 0.0 x3 + 1.0 x4 = 13. 1.0 x1 + 0.0 x2 + 0.0 x3 + 1.0 x4 = 5. 3.0 x1 + 1.0 x2 + 1.0 x3 + 0.0 x4 = 8.

=> 2.0 x1 + 1.0 x2 + 3.0 x3 + 4.0 x4 = 29.

  • 0.5 x2 - 7.5 x3 - 9.0 x4 = -59. 6.0 x3 + 8.0 x4 = 50. -2.3 x4 = -9.

=> x1 = 1, x2 =2, x3 = 3, x4 = 4

GE with Partial-Pivoting

The Naive algorithm runs into trouble when a pivot element is zero. (In fact, it runs into trouble whenever a pivot element is “small.”) Partial-Pivoting — Search the column below the diagonal and find the largest element in absolute magnitude; perform row interchange to bring this element to the diagonal. An example:

Initial: => Pivoting: 2.0 1.0 3.0 4.0 29.0 5.0 2.0 0.0 1.0 13. 1.0 0.0 0.0 1.0 5.0 1.0 0.0 0.0 1.0 5. 3.0 1.0 1.0 0.0 8.0 3.0 1.0 1.0 0.0 8. 5.0 2.0 0.0 1.0 13.0 2.0 1.0 3.0 4.0 29.

=> Elimination: 5.0 2.0 0.0 1.0 13. 0.0 -0.4 0.0 0.8 2. 0.0 -0.2 1.0 -0.6 0. 0.0 0.2 3.0 3.6 23.

Continue for other columns ...

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 19 / 30

Parallel GE with 1-D Partitioning

Parallel GE with 1-D Partitioning

  • (^) Assign one row to each process.
  • (^) Execute the outer loop sequentially.
  • (^) At iteration k+1, process Pk either broadcast or shift its row to processes Pk+1,... , Pn− 1.
  • Each process performs local computation.

Complexity:

Tp =

n(n − 1) + ts n log n +

tw n(n − 1) log n

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 21 / 30

Improvements

Problems with the Simple Algorithm — The partition is fine-grained, and towards the end, the active region of the matrix is shrinking towards lower right corner; implies that processes fall idle.

Solutions: block and cyclic partitions.

Block and Cyclic 2-D Partitions

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 25 / 30

Solving Large, Sparse Linear Systems

Laplace Equations:

φi ,j =

(φi +1,j + φi − 1 ,j + φi ,j+1 + φi ,j− 1 ) 0 < i, j < n

This is a linear system, and can be made into Ax = b form — Let I = i · n + j, then the equations become:

φI +n + φI −n + φI +1 + φI − 1 − 4 φI = 0 0 < I < n^2

In matrix form:

-4 1 0 0 ... 1 0 0 ... 1 -4 1 0 ... 0 1 0 ... 0 1 -4 1 ... 0 0 1 ... 0 0 1 - ... ... ... ... ... ... ... ... ... ... ... 1 0 0 .. ... ... ... ... 0 1 0 .. ... ... ... ... ... ... ... ... ... ... ...

The matrix is very large, yet sparse.

Better Approach — Directly solve the system on the original domain.

Jacobi Relaxation Algorithm

  • (^) Cycle through the mesh points, compute a new value for each point using the average of the four-neighboring “old” values.
  • (^) Once all the new values are found, replace old values with new ones.
  • Repeat until the difference between old and new is small enough.

Parallel Jacobi Algorithm: (Assume an n × n mesh and p processors.)

  • Distribute the mesh in both dimensions; each processor contains n √ p

×

n √ p

of the mesh.

  • Communications are only required on the “surface” of the mesh region in each processor. (Exactly how thick the surface is is stencil-dependent.)

Problems —

  • Needs buffers to store old mesh point values.
  • The convergence speed can be slow.

Jingke Li (Portland State University) CS 415/515 Numerical Algorithms 27 / 30

Parallel Jacobi Algorithm (SISD)

begin ... // initialize boundary and inner point values do diff = 0 for i = 1 to n do for j = 1 to n do newx[i][j] = (x[i-1][j] + x[i][j-1]

  • x[i+1][j] +x [i][j+1]) / 4 endfor endfor for i = 1 to n do for j = 1 to n do diff = max(diff, |newx[i][j] - x[i][j]|) x[i][j] = newx[i][j] endfor endfor while (diff > epsilon) end