Sunteți pe pagina 1din 51

Mathematical Libraries for Linear

Algebra

William Oquendo, woquendo@gmail.com


Credits: Computational Physics - Landau and Paez, Deitel,
cplusplus tutorial, GSL, Gnu free software fundation, eigen c++,
armadillo, python

Simulation of Physical Systems Group


Departament of Physics
National University of Colombia

Cr 30 # 45-03, Ed 404, of 348


Bogot (Colombia)
Phone: (571) 3165000, ext 13031
Fax: (571) 3165135
www.ssf.unal.edu.co
ssf@unal.edu.co

Thursday, November 14, 13

ssf+un

Matrices: What experts say

Landau y Paez

Thursday, November 14, 13

Matrices: Typical examples


Solve a linear system

Ax = b
AN N xN 1 = bN 1

Matrix Multiplication
C = AB

Linear Combinations
C = Ax + By
Thursday, November 14, 13

Eigenvalue problems
Ax = b

Matrix Decomposition
A = LU

...

Matrices: Practical aspects

Thursday, November 14, 13

Matrices: Practical aspects

Thursday, November 14, 13

Matrices: Practical aspects

Thursday, November 14, 13

Solving typical linear problems

Gauss-Jordan elimination: Well suited for finding inverses, not very


efficient, http://en.wikipedia.org/wiki/Gauss-Jordan_elimination

Gauss elimination: Very efficient, can also find the rank of a matrix,
and compute the inverse of a square matrix, http://en.wikipedia.org/wiki/
Gaussian_elimination

LU Decomposition: Write the original matrix as a product of an


Lower and an Upper triangular matrices, useful for computing the
determinant, http://en.wikipedia.org/wiki/LU_decomposition

Cholesky decomposition: Valid for symmetric definite matrices,


t w i c e e f fi c i e n t t h a n L U , h t t p : / / e n . w i k i p e d i a . o r g / w i k i /
Cholesky_decomposition

QR decomposition: Orthogonal and rigth triangular, linear squares


problems, http://en.wikipedia.org/wiki/QR_decomposition

...

Thursday, November 14, 13

Matrix problems: best method for


solving .... Use scientific libraries!!!!

Thursday, November 14, 13

Why scientific libraries?

Routines for common operations, like vector and matrix


operations, or Fourier transforms, implemented to take most
advantages from cpu.

Can be optimized for each cpu.

Thursday, November 14, 13

Coding is easier since API is stable.


Increase portability (Dont use non-standard libraries!)
Better to help compiler to perform optimization.
Developer time is saved. Efficiency increased.
Can implement multithreaded operations out of the box.
Cache friendly.
...

Linear Algebra: Main numerical


libraries

BLAS: Basic Linear Algebra Subroutines. Standard API for low


level matrix operations, like matrix multiplication.

LaPACK: Linear Algebra Package based on BLAS. Optimized.


Real or Complex.

Fast(est) Fourier Transform (in The West) FFTW.

Thursday, November 14, 13

MKL: BLAS LAPACK routines optimized for Intel machines.


AMD Math Core Library - ACML : For AMD machines.
ATLAS: Automatically tuned BLAS and LAPACK
GSL, eigen, armadillo, ... : More general libraries

BLAS

Thursday, November 14, 13

GSL

Thursday, November 14, 13

What about GSL and BLAS?

Thursday, November 14, 13

How to use the GSL?

Thursday, November 14, 13

Please red the tutorial, or man gsl, or info gsl.

How to use the GSL? (once installed)

linking to gsl and gslcblas


linking to gsl and blas and atlas

Thursday, November 14, 13

How to use the GSL?

Thursday, November 14, 13

Check!

Thursday, November 14, 13

How to use the GSL?

How to use the GSL?

Thursday, November 14, 13

Matrix solution example

Thursday, November 14, 13

Matrix solution example

Thursday, November 14, 13

Example of GSL usage

Thursday, November 14, 13

Example of GSL usage

Thursday, November 14, 13

Examples of GSL usage

Thursday, November 14, 13

introGSL.cpp
gslMatrixBasic.cpp
gslVectorBasic.cpp
blas.c
blasV2.c
gsllinalgLU.cpp
gsllinalgQRls.cpp
gsllinalgCholeski.cpp
eigen_nonsymmMOD.c

Matrices: Exercises

Thursday, November 14, 13

Matrices: Exercises

Thursday, November 14, 13

Eigen C++ LinAlg library


http://eigen.tuxfamily.org/index.php?title=Main_Page

Thursday, November 14, 13

Eigen C++ LinAlg library

Thursday, November 14, 13

How to use Eigen? headers only!

Thursday, November 14, 13

Fully templated

Thursday, November 14, 13

Several headers for several areas

Thursday, November 14, 13

Quickref (1)

Thursday, November 14, 13

Quickref (2)

Thursday, November 14, 13

Linear Algebra: System Solution

Thursday, November 14, 13

Some Linear Algebra Solving


Methods

Thursday, November 14, 13

Eigen Solvers

Thursday, November 14, 13

Inverse and others

Thursday, November 14, 13

Armadillo C++ LinAlg library

Thursday, November 14, 13

Armadillo should be compiled

Thursday, November 14, 13

Matrix type

Thursday, November 14, 13

Linear Systems

Thursday, November 14, 13

Eigen decompositions (1)

Thursday, November 14, 13

Eigen decompositions (2)

Thursday, November 14, 13

Python scipy.linalg library

Thursday, November 14, 13

Python scipy.linalg library

Thursday, November 14, 13

Python scipy.linalg library

Thursday, November 14, 13

Python example 1

import

Thursday, November 14, 13

Python example II

Thursday, November 14, 13

Exercise : Performance Comparison

The goal is to compare the performance of each library for solving


randomly-generated matrices as a function of the size, testing also the
actual algorithm used to solve the system. Optionally, the eigen value
decomposition can also be useful.

Thursday, November 14, 13

Timing routines in C/C++


gnu time
command

std::chronos
or clock

Thursday, November 14, 13

Timing routines in Python


gnu time
command

timeit

Thursday, November 14, 13

Exercise/Homework

For a linear system solved by QR and LU, compute the performance of


eigen, gsl+cblas, armadillo, and python, as function of the matrix size.

Final result: Several figures, one per method (QR and LU and simple
solve). Each figure showing user-time versus matrix size per library/
method (several curves per figure).

Details:

Change matrix size from 2x2 up to 1024x1024 (or higher).

Print only the average after 20 tries to improve statistics.

Initialize randomly the matrices at each time step using seed=0 and
srand48 or srand if needed explicitly.

Before starting the benchmarks, make some test to check if the


solutions are the solutions.

Thursday, November 14, 13

S-ar putea să vă placă și