Post on 14-Jul-2015
transcript
Solving Poisson Equation using Conjugate Gradient Method
and its implementation
Jongsu Kim
Theoretical
From the Basics, Ax=b
Linear Systems
๐ด๐ฅ = ๐
Goal of this presentation
What have you learned?
โข Direct Methodโข Gauss Eliminationโข Thomas Algorithm (TDMA) (for tridiagonal matrix only)
โข Iterative Methodโข Jacobi methodโข SOR methodโข Conjugate Gradient Methodโข Red Black Jacobi Method
Iterative Method
Start with decomposition
๐ด = ๐ท โ ๐ธ โ ๐น
Jacobi Method
๐ฅ๐+1 = ๐ทโ1 ๐ธ + ๐น ๐ฅ๐ + ๐ทโ1๐
Gauss-Seidel Method
๐ฅ๐+1 = (๐ท โ ๐ธ)โ1๐น๐ฅ๐ + ๐ท โ ๐ธ โ1๐
๐ด๐ฅ = ๐
Backward Gauss-Seidel Iteration
๐ท โ ๐น ๐ฅ๐+1 = ๐ธ๐ฅ๐ + ๐
(๐ = 1,โฆ , ๐ โ 1, ๐)
(๐ = ๐, ๐ โ 1โฆ , )
Splitting of A matrix
Previous method has a common form
๐ด = ๐ท โ ๐ธ โ ๐น
๐ฅ๐+1 = ๐ทโ1 ๐ธ + ๐น ๐ฅ๐ + ๐ทโ1๐
๐ฅ๐+1 = (๐ท โ ๐ธ)โ1๐น๐ฅ๐ + ๐ท โ ๐ธ โ1๐
๐ด๐ฅ = ๐
๐ท โ ๐น ๐ฅ๐+1 = ๐ธ๐ฅ๐ + ๐
๐ด๐๐+๐ = ๐ต๐๐ + ๐ = ๐ดโ ๐จ ๐๐ + ๐
๐จ = ๐ดโ๐ต
Introducing SOR (Successive Over Relaxation) method
๐๐จ = ๐ซ โ๐๐ฌ โ (๐๐ญ + ๐ โ๐ ๐ซ)
๐ซ โ๐๐ฌ ๐๐+๐ = ๐๐ญ + ๐ โ๐ ๐ซ ๐๐ +๐๐
SOR to SSOR
Gauss Seidel method
๐ฅ๐+1 = (๐ท โ ๐ธ)โ1๐น๐ฅ๐ + ๐ท โ ๐ธ โ1๐
SOR (Successive Over Relaxation) method
๐ท โ ๐๐ธ ๐ฅ๐+1 = ๐๐น + 1 โ ๐ ๐ท ๐ฅ๐ + ๐๐
(๐ท โ ๐ธ)๐ฅ๐+1= ๐น๐ฅ๐ + ๐
๐ท โ ๐น ๐ฅ๐+1 = ๐ธ๐ฅ๐ + ๐
Backward Gauss Seidel method
Backward SOR method
๐ท โ ๐๐น ๐ฅ๐+1 = ๐๐ธ + 1 โ ๐ ๐ท ๐ฅ๐ + ๐๐
SSOR method
SSOR (Symmetric Successive Over Relaxation) method
SOR step followed by backward SOR step for symmetric matrix
๐ท โ ๐๐ธ ๐ฅ๐+1/2 = ๐๐น + 1 โ ๐ ๐ท ๐ฅ๐ + ๐๐
๐ท โ ๐๐น ๐ฅ๐+1 = ๐๐ธ + 1 โ ๐ ๐ท ๐ฅ๐+1/2 + ๐๐
๐ฅ๐+1 = ๐ฎ๐๐ฅ๐ + ๐๐
๐ฎ๐ = ๐ท โ ๐๐น โ1 ๐๐ธ + 1 โ ๐ ๐ท ร ๐ท โ ๐๐ธ โ1 ๐๐น + 1 โ ๐ ๐ท
๐๐ = ๐ ๐ท โ ๐๐น โ1 ๐ผ + ๐๐ธ + 1 โ ๐ ๐ท ๐ท โ ๐๐ธ โ1 ๐
Observing that
๐๐ธ + 1 โ ๐ ๐ท ๐ท โ ๐๐ธ โ1 = โ ๐ท โ ๐๐ธ + 2 โ ๐ ๐ท ๐ท โ ๐๐ธ โ1
= โ๐ผ + 2 โ ๐ ๐ท ๐ท โ ๐๐ธ โ1
๐๐ = ๐ 2 โ ๐ ๐ท โ ๐๐น โ1๐ท ๐ท โ ๐๐ธ โ1๐
Used as preconditioner (explain later)
Preconditioned System
๐๐+๐ = ๐ฎ๐๐๐ + ๐๐
๐บ๐บ๐ ๐ด = ๐ผ โ (๐ท โ ๐ธ)โ1๐ด๐บ๐ฝ๐ด ๐ด = ๐ผ โ ๐ทโ1๐ด,
๐๐+๐ = ๐ดโ๐๐ต๐๐ +๐ดโ๐๐
We have two forms for iterative method
Ex)
๐บ = ๐โ1๐ = ๐โ1 ๐ โ ๐ด = ๐ผ โ๐โ1๐ด ๐ = ๐โ1๐
๐ผ โ ๐บ ๐ฅ = ๐
Another viewโฆ
[๐ผ โ (๐ผ โ ๐โ1๐ด)]๐ฅ = ๐
๐โ1๐ด๐ฅ = ๐
๐ดโ๐๐จ๐ = ๐ดโ๐๐ Preconditioner ๐
Preconditioned System
๐ดโ๐๐จ๐ = ๐ดโ๐๐ With Preconditioner ๐
๐๐บ๐ = ๐ท โ ๐ธGauss-Seidel
๐๐๐๐๐ =1
๐ 2 โ ๐๐ท โ ๐๐ธ ๐ทโ1(๐ท โ ๐๐น)SSOR
๐๐ฝ๐ด = ๐ทJacobi
๐๐ฝ๐ด =1
๐(๐ท โ ๐๐ธ)SOR
It may not be โSPARSEโ due to inverse (๐โ1 )
How to compute this?
๐ค = ๐โ1๐ด๐ฃ ๐ = ๐ด๐ฃ and ๐๐ค = ๐
๐ด๐ฃ might be expensive. Much better?
๐ค = ๐โ1๐ด๐ฃ = ๐โ1 ๐ โ๐ ๐ฃ = ๐ผ โ๐โ1๐ ๐ฃ
๐ = ๐๐ฃ
๐ค = ๐โ1๐
๐ค โ ๐ฃ โ๐ค
N may be sparser than A and less expensive than ๐ด๐ฃ
Minimization Problem
Forget about ๐ด๐ฅ = ๐ temporarily, but thinking about some quadratic function ๐
Function Matrix
๐(x) =1
2๐ด๐ฅ2 โ ๐๐ฅ + ๐ ๐ ๐ฅ =
1
2๐ฅ๐๐ด๐ฅ โ ๐๐๐ฅ + ๐
๐โฒ x = ๐ด๐ฅ โ b ๐โฒ x =1
2๐ด๐๐ฅ +
1
2A๐ฅ โ b
If Matrix ๐ด is symmetric, ๐ด๐ = ๐ด, then
๐โฒ ๐ = ๐จ๐ โ ๐
Setting the gradient to zero, we get the linear system we wish to solve.
Our original GOAL!!
(a) Quadratic form for a positive definite matrix
(b) Quadratic form for a negative definite matrix
(c) Singular (and positive-indefinite) matrix; A line that runs through bottom of the valley is the set of solutions
(d) For an indefinite matrix. Saddle point.
For a Symmetric and Positive Definite Matrix, minimizing
๐ ๐ฅ =1
2๐ฅ๐๐ด๐ฅ โ ๐๐๐ฅ + ๐
Reduced to our solution
Minimization Problem
Steep Descent Method
Choose direction in which ๐ decrease most quickly, which is the direction opposite ๐โฒ(๐ฅ ๐ )
๐(๐) = ๐ โ ๐ด๐ฅ(๐)
โ๐โฒ ๐ฅ ๐ = ๐(๐) = ๐ โ ๐ด๐ฅ(๐)
๐ฅ(1) = ๐ฅ(0) + ๐ผ๐(0)
To Find ๐ผ, set ๐
๐๐ผ๐ ๐ฅ 1 = 0
๐
๐๐ผ๐ ๐ฅ 1 = ๐โฒ ๐ฅ 1
๐ ๐
๐๐ผ๐ฅ(1) = ๐โฒ ๐ฅ 1
๐๐(0)
๐โฒ ๐ฅ ๐+1๐
and ๐(๐) are orthogonal!
โ๐โฒ ๐ฅ ๐+1 = ๐(๐+1)
๐โฒ ๐ฅ ๐+1๐๐(๐) = 0
๐ ๐+1๐ ๐(๐) = 0
๐ถ =๐ ๐๐ป ๐ ๐
๐(๐)๐ป ๐จ๐(๐)
Conjugate Gradient Method
Steep Descent Method not always converge well
Worst case of steep descent method
โข Solid lines : worst convergence lineโข Dashed line : steps toward convergence
Why it doesnโt directly go along line for fast convergence? โ related to eigen value problem
Introducing Conjugate Gradient method
Conjugate Gradient Method
What is the meaning of conjugate?โข Definition : A binomial formed by negating the second term of binomialโข ๐ฅ + ๐ฆ โ conjugate โ ๐ฅ โ ๐ฆ
Then, what is the meaning of conjugate gradient?โข Steep descent method often finds itself taking steps in the same directionโข Wouldnโt it better if we got it right the every step?โข Here is a step
โข error ๐(๐) = ๐ฅ(๐) โ ๐ฅ, residual ๐(๐) = ๐ โ ๐ด๐ฅ(๐), ๐(๐) a set of orthogonal search
directionโข for each step, we choose a point ๐ฅ(๐+1) = ๐ฅ(๐) + ๐ผ(๐)๐(๐)โข To find ๐ผ, ๐(๐+1) should be orthogonal to ๐(๐). (๐ ๐+1 = ๐ ๐ + ๐ผ ๐ ๐ ๐ )
๐(๐)๐ ๐(๐+1) = 0
๐(๐)๐ (๐ ๐ +๐ผ(๐)๐(๐)) = 0
๐ผ(๐) = โ๐ ๐๐ ๐ ๐
๐(๐)๐ ๐(๐)
We donโt know anything about ๐(๐), because if we know ๐(๐), it means we know the answer.
Conjugate Gradient Method
Instead of orthogonal, introduce ๐ด-orthogonal
๐ (๐)๐ป ๐จ๐ (๐) = ๐, if ๐(๐) and ๐(๐) are ๐ด-orthogonal, or conjugate
๐(๐+๐) is ๐จ-orthogonal to ๐ (๐), and this condition is equivalent to finding the minimum
point along the search direction ๐(๐) , as in steep descent method
๐
๐๐ผ๐ ๐ฅ ๐+1 = 0
๐ผ minimize ๐ when directional derivative is equal to zero
๐โฒ ๐ฅ ๐+1๐ ๐
๐๐ผ๐ฅ ๐+1 = 0
โ๐ ๐+1๐ ๐(๐) = 0
Chain rule
๐โฒ ๐ฅ(๐+1) = ๐ด๐ฅ(๐+1) โ ๐
๐(๐) = ๐ โ ๐ด๐ฅ(๐)๐ฅ(๐+1) = ๐ฅ(๐) + ๐ผ(๐)๐(๐)
๐(๐)๐ ๐ด๐(๐+1) = 0 ๐ฅ(๐+1)
๐ ๐ด๐๐(๐) โ ๐๐๐ ๐ = 0
๐ฅ(๐+1)๐ ๐ด๐๐(๐) โ ๐ฅ๐๐ด๐๐ ๐ = 0
๐ ๐+1๐ ๐ด๐๐(๐) = 0 Transpose again
How it can be same as orthogonality used in steep descent method?
๐(๐+1) = ๐ฅ(๐+1) โ ๐ฅ
๐ถ(๐) = โ๐ ๐๐ป ๐ ๐
๐ (๐)๐ป ๐จ๐ (๐)
Conjugate Gradient Method
๐(๐)๐ ๐ด๐(๐+1) = 0
๐ฅ(๐+1) = ๐ฅ(๐) + ๐ผ๐(๐)๐(๐+1) = (๐ฅ(๐) + ๐ผ๐(๐)) โ ๐ฅ
๐(๐)๐ ๐ด๐(๐+1) = ๐ ๐
๐ ๐ด((๐ฅ ๐ + ๐ผ๐ ๐ ) โ ๐ฅ)
๐ ๐๐ ๐ด๐ฅ(๐) + ๐ผ๐ ๐
๐ ๐ด๐(๐) โ ๐ ๐๐ ๐ด๐ฅ = 0
๐ ๐๐ ๐ด๐ฅ ๐ โ ๐ = โ๐ผ๐ ๐
๐ ๐ด๐(๐)
How to find ๐(๐)?
Gram-Schmidt Process
๐(๐) = ๐ข(๐) + ฮฃk=0๐โ1๐ฝ๐๐๐(๐)
Find set of ๐ด-orthogonal vector
๐ฝ๐๐ = โ๐ข๐๐๐ด๐ ๐
๐(๐)๐ ๐ด๐(๐)
For set of independent vectors ๐ข๐
due to ๐ ๐๐ ๐ด๐(๐) = 0
๐ > ๐
Conjugate Gradient Method
Overall Algorithm
Initialization๐ = 0
๐ = ๐ โ ๐ด๐ฅ๐ = ๐
๐ฟ๐๐๐ค = ๐๐๐๐ฟ0 = ๐ฟ๐๐๐ค๐ = 1.0๐ โ 6
Iteration checkWhile i<imax && ๐ฟ๐๐๐ค > ๐2๐ฟ0
Inside loop๐ = ๐ด๐
๐ผ =๐ฟ๐๐๐ค๐๐๐
๐ฅ = ๐ฅ + ๐ผ๐If ๐ is divisible by 50
๐ = ๐ โ ๐ด๐ฅelse
๐ = ๐ โ ๐ผ๐endif
๐ฟ๐๐๐ = ๐ฟ๐๐๐ค๐ฟ๐๐๐ค = ๐๐๐
๐ฝ =๐ฟ๐๐๐ค๐ฟ๐๐๐
๐ = ๐ + ๐ฝ๐๐ = ๐ + 1
Preconditioner Again
๐ดโ๐๐จ๐ = ๐ดโ๐๐ With Preconditioner ๐
๐๐บ๐ = ๐ท โ ๐ธGauss-Seidel
๐๐๐๐๐ =1
๐ 2 โ ๐๐ท โ ๐๐ธ ๐ทโ1(๐ท โ ๐๐น)SSOR
๐๐ฝ๐ด = ๐ทJacobi
๐๐ฝ๐ด =1
๐(๐ท โ ๐๐ธ)SOR
Incomplete LU Decomposition ๐ด = ๐ฟ๐ โ ๐ ๐ : residual error
Incomplete Cholesky Decomposition ๐ด = ๐ฟ๐ฟ๐ โ ๐
If A is SPD (Symmetric Positive Definite), above two decomposition are same
To make sparse system, used incomplete Factorization
Implementation
Implementation Issue
โข For 3D case, Matrix ๐ด would be huge. (for (128 ร 128 ร 128) grid, ๐ด matrix has 128 ร 128 ร 128 ร 128 ร 128 ร 128 = 32๐๐ต, (for 2D it takes only 2GB)
โข However, there are almost 0 in ๐ด matrix for poisson equation. โ Sparse Matrix!
How to represent Sparse Matrix?
โข Simplest thing. Store nonzero value and row, column index. (Coordinate Format, COO)
Too many duplication
Sparse Matrix Format
Compressed Sparse Row (CSR)
โข Store only non-zero valuesโข Available three or four arrays
โข Not easy to construct the algorithm such as ILU or IC preconditioner
Use MKL (Intel Math Kernel Library)
MKL?
โข a library of optimized math routines for science, engineering, and financial applications. Core math functions include BLAS, LAPACK, ScaLAPACK, sparse solvers, fast Fourier transforms, and vector math. The routines in MKL are hand-optimized specifically for Intel processors.
โข For my problem, I usually use BLAS, fast Fourier transforms (for poissonequation solver with Neumann, periodic, dirichlet BC)
BLAS?
โข a specified set of low-level subroutines that perform common linear algebra operations, widely used. Even in MATLAB!
โข Usually used in vector or matrix multiplication, dot product like operations.โข Level 1 : vector โ vector operationโข Level 2 : matrix โ vector operationโข Level 3 : matrix โ matrix operationโข Parallelized internally by Intel. Just turn on the option.โข Reference manual : https://software.intel.com/en-us/mkl_11.1_ref
How to use Library
For MKL
โข For compile (when creating .c files in your makefile) โข -i8 -openmp -I$(MKLROOT)/include
โข For link (when creating executable files using โo option)โข -L$(MKLROOT)/lib/intel64 -lmkl_core -lmkl_intel_thread
-lpthread โlm โข https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor
Library Linking Process
โข Compileโข -I option indicate where is
header file (.h file), specifying include path
โข Linkingโข -L option indicate where is
library file (.lib, .dll, .a, .so), specifying linking path
โข -l option indicate library name
Reference
โข Shewchuk, Jonathan Richard. "An introduction to the conjugate gradient method without the agonizing pain." (1994).
โข Deepak Chandan, โUsing Sparse Matrix and Solver Routines from Intel MKLโ, Scinet User Group Meeting, (2013)
โข Saad, Yousef. Iterative methods for sparse linear systems. Siam, 2003.โข Akhunov, R. R., et al. "Optimization of the ILU(0) factorization algorithm with
the use of compressed sparse row format." Zapiski Nauchnykh Seminarov POMI405 (2012): 40-53.