Docsity

Numerical Linear Algebra: Using BLAS and LAPACK for Efficient Matrix Computations, Study notes of Convex Optimization

Partial preview of the text

Download Numerical Linear Algebra: Using BLAS and LAPACK for Efficient Matrix Computations and more Study notes Convex Optimization in PDF only on Docsity!

Numerical Linear Algebra Software (based on slides written by Michael Grant) • BLAS, ATLAS • LAPACK • sparse matrices^ Prof. S. Boyd, EE364b, Stanford University

Numerical linear algebra in optimization most memory usage^ and^

computation time

in optimization methods is

spent on numerical linear algebra,

e.g.,

-^ constructing sets of linear equations (

e.g., Newton or KKT systems)

-^ matrix-matrix products, matrix-vector products,... • and solving them –^ factoring, forward and backward substitution,.. .... so knowing about numerical linear algebra is a good thing Prof. S. Boyd, EE364b, Stanford University

How to write numerical linear algebra software

DON’T!

whenever possible, rely on

existing, mature

software libraries

-^ you can focus on the higher-level algorithm •^ your code will be more portable, less buggy, and will runfaster—sometimes

much^ faster Prof. S. Boyd, EE364b, Stanford University

Netlib

the grandfather of

all^ numerical linear algebra web sites^ http://www.netlib.org

-^ maintained by University of Tennessee, Oak Ridge National Laboratory,and colleagues worldwide •^ most of the code is public domain or freely licensed •^ much written in FORTRAN 77 (gasp!) Prof. S. Boyd, EE364b, Stanford University

BLAS operations

Level 1^ addition/scaling

αx,^

αx^ +^ y dot products, norms

T^ xy,^ ‖ x‖,^ ‖x‖^21

Level 2^ matrix/vector products

αAx^ +^ βy,

T^ αAx^ +^ βy

rank 1 updates

A^ +

T^ αxy,^ A^ +^ Tαxx

rank 2 updates

A^ +

T^ T αxy+^ αyx triangular solves

− αT 1 −T^ x,^ αT^ x Level 3^ matrix/matrix products

αAB^ +^ βC

T^ , αAB+^ βCT T^ T^ αAB + βC, αAB+ βC

rank-k^ updates

αAA

T^ +^ βC,^ αA

T^ A^ +^ βC

rank-^2 k^ updates

T αA T^ B^ +^ αBA^ +

βC

triangular solves

− αT 1 −T^ C,^ αT^ C

Prof. S. Boyd, EE364b, Stanford University

Level 1 BLAS naming convention

BLAS routines have a Fortran-inspired naming convention:

cblas_^

X^ XXXX prefix data type^ operation data types:^ s^ single precision real

d^ double precision real c^ single precision complex

z^ double precision complex operations:^ axpy^

y^ ←^ αx^ +^ y^

Tdot r ← x y

nrm2^ r^ ← ‖

√T^ x‖= xx^2 asum^ r^ ← ‖

∑x‖= |x| 1 i^ i^

example:^ cblas_ddot

double precision real dot product Prof. S. Boyd, EE364b, Stanford University

Using BLAS efficiently

always choose a higher-level BLAS routine over multiple calls to alower-level BLAS routine^ A^ ←

k∑T A +^ xyii^ i=

m×n, A ∈ R m, x∈ R, yi i n∈ R

two choices:^ k

separate calls to the Level 2 routine

cblas_dger A^ ←^ A^ +^ xy^1

T,^...^ A^ ← 1

T A + xykk

or a single call to the Level 3 routine

cblas_dgemm A^ ←^ A^ +^ XY

T^ ,^ X^ = [x^1 · · ·^ x]^ ,^ Y^ = [k

y· · ·^ y]^1 k

the Level 3 choice will perform much better Prof. S. Boyd, EE364b, Stanford University

Is BLAS necessary?

why use BLAS when writing your own routines is so easy?^ A^ ←^

T^ A + XY ,^

m×nA ∈ R, X m×p ∈ R, Y^ ∈ n×p R A←^ A+ij^ ij^

p∑^ XYikjk k= void matmultadd( int m, int n, int p, double* A,

const double* X, const double* Y ) { int i, j, k;for ( i = 0 ; i < m ; ++i )for ( j = 0 ; j < n ; ++j )for ( k = 0 ; k < p ; ++k )A[ i + j * n ] += X[ i + k * p ] * Y[ j + k * p ];} Prof. S. Boyd, EE364b, Stanford University

Improving performance through blocking blocking is used to improve the performance of matrix/vector andmatrix/matrix multiplications, Cholesky factorizations, etc.^ [^ T^ A^ +^ XY^ ←

  • ] AA 11 12 + AA
  • [ ][ X^11 + X
    • ]TTY Y - A← A+^11
      • TXY , A
        • ← A+ X
      • TY , - A← A+^21 - TXY , A - ← A+ X - TY

Linear Algebra PACKage (LAPACK) LAPACK contains subroutines for solving linear systems and performingcommon matrix decompositions and factorizations • first release: February 1992; latest version (3.0): May 2000 • supercedes predecessors EISPACK and LINPACK • supports same data types (single/double precision, real/complex) andmatrix structure types (symmetric, banded,... ) as BLAS • uses BLAS for internal computations • routines divided into three categories:

auxiliary^ routines,

computational

routines, and^ driver

routines Prof. S. Boyd, EE364b, Stanford University

LAPACK driver routines

driver routines call a sequence of computational routines to solve standardlinear algebra problems, such as^ •^ linear equations:

AX^ =^ B

-^ linear least squares: minimize

‖b^ −^ Ax‖x 2

-^ linear least-norm:

minimize

‖y‖y (^2) subject to d^ =^ By

-^ generalized linear least squares problems:minimize

‖c^ −^ Ax‖x (^2) subject to Bx^ =^ d

minimize‖yy^

‖^2

subject to^ d^

=^ Ax^ +^ By

Prof. S. Boyd, EE364b, Stanford University

LAPACK example

solve KKT system

[^ TH^ A^ A^0

] [^ ]^ [^ xa^ =yb

]

n x ∈ R,^ v^ ∈^ R mT^ ,^ H^ =^ H≻

0 ,^ m < n option 1^ : driver routine

dsysv^ uses computational routine

dsytrf^ to

compute permuted

T^ LDLfactorization [^ H^ A A^0

] T^ →^ P LDLP

T

and performs remaining computations to compute solution

[^ ]xT^ =^ P^ Ly

−^1 −^1 −T^ DLP

[^ ]a b

Prof. S. Boyd, EE364b, Stanford University

What about other languages?

BLAS and LAPACK routines can be called from C, C++, Java, Python,... an alternative is to use a “native” library, such as^ •^ C++: Boost uBlas, Matrix Template Library^ •^ Python: NumPy/SciPy, CVXOPT^ •^ Java: JAMA Prof. S. Boyd, EE364b, Stanford University

Sparse matrices^0 5 10 15 20

0 5 30 35 40 10 15 20 25 30 35 40 nz = 505 m×n^ • A ∈ R is^ sparse^ if it has “enough zeros that it pays to takeadvantage of them” (J. Wilkinson)

-^ usually this means

n, number of elements known to be nonzero, isnz small:^ n≪^ nz^

mn Prof. S. Boyd, EE364b, Stanford University