Upload
johan-camilo
View
216
Download
0
Embed Size (px)
Citation preview
7/23/2019 Matrices and Libraries
1/51
Mathematical Libraries for LinearAlgebra
William Oquendo, [email protected]
Credits: Computational Physics - Landau and Paez, Deitel,
cplusplus tutorial, GSL, Gnu free software fundation, eigen c++,armadillo, python
Thursday, November 14, 13
mailto:[email protected]:[email protected]7/23/2019 Matrices and Libraries
2/51
Matrices: What experts say
Landau y Paez
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
3/51
Matrices: Typical examples
Ax = b
ANNxN1 = bN1Ax =b
Solve a linear systemEigenvalue problems
C = AB
Matrix Multiplication
A = LU
Matrix Decomposition
C = Ax+By
Linear Combinations...
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
4/51
Matrices: Practical aspects
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
5/51
Matrices: Practical aspects
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
6/51
Matrices: Practical aspects
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
7/51
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 thedeterminant, 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 than LU, 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 squaresproblems, http://en.wikipedia.org/wiki/QR_decomposition
...
Thursday, November 14, 13
http://en.wikipedia.org/wiki/QR_decompositionhttp://en.wikipedia.org/wiki/QR_decompositionhttp://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/QR_decompositionhttp://en.wikipedia.org/wiki/QR_decompositionhttp://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/LU_decompositionhttp://en.wikipedia.org/wiki/Gauss-Jordan_eliminationhttp://en.wikipedia.org/wiki/Gauss-Jordan_elimination7/23/2019 Matrices and Libraries
8/51
Matrix problems: best method forsolving .... Use scientific libraries!!!!
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
9/51
Why scientific libraries? Routines for common operations, like vector and matrix
operations, or Fourier transforms, implemented to take mostadvantages from cpu.
Can be optimized for each cpu.
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.
...
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
10/51
Linear Algebra: Main numericallibraries
BLAS: Basic Linear Algebra Subroutines. Standard API for lowlevel matrix operations, like matrix multiplication.
LaPACK: Linear Algebra Package based on BLAS. Optimized.Real or Complex.
Fast(est) Fourier Transform (in The West) FFTW.
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
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
11/51
BLAS
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
12/51
GSL
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
13/51
What about GSL and BLAS?
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
14/51
How to use the GSL?
Please red the tutorial, or man gsl, or info gsl.
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
15/51
How to use the GSL? (once installed)
linking to gsl and gslcblas
linking to gsl and blas and atlas
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
16/51
How to use the GSL?
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
17/51
How to use the GSL?Check!
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
18/51
How to use the GSL?
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
19/51
Matrix solution example
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
20/51
Matrix solution example
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
21/51
Example of GSL usage
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
22/51
Example of GSL usage
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
23/51
Examples of GSL usage
introGSL.cpp
gslMatrixBasic.cpp
gslVectorBasic.cpp
blas.c
blasV2.c
gsllinalgLU.cpp
gsllinalgQRls.cpp
gsllinalgCholeski.cpp
eigen_nonsymmMOD.c
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
24/51
Matrices: Exercises
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
25/51
Matrices: Exercises
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
26/51
Eigen C++ LinAlg library
http://eigen.tuxfamily.org/index.php?title=Main_Page
Thursday, November 14, 13
http://eigen.tuxfamily.org/index.php?title=Main_Pagehttp://eigen.tuxfamily.org/index.php?title=Main_Page7/23/2019 Matrices and Libraries
27/51
Eigen C++ LinAlg library
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
28/51
How to use Eigen? headers only!
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
29/51
Fully templated
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
30/51
Several headers for several areas
Thursday, November 14, 13
Q i k f (1)
7/23/2019 Matrices and Libraries
31/51
Quickref (1)
Thursday, November 14, 13
Q i k f (2)
7/23/2019 Matrices and Libraries
32/51
Quickref (2)
Thursday, November 14, 13
Li Al b S S l i
7/23/2019 Matrices and Libraries
33/51
Linear Algebra: System Solution
Thursday, November 14, 13
Some Linear Algebra Solving
7/23/2019 Matrices and Libraries
34/51
Some Linear Algebra SolvingMethods
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
35/51
Eigen Solvers
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
36/51
Inverse and others
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
37/51
Armadillo C++ LinAlg library
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
38/51
Armadillo should be compiled
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
39/51
Matrix type
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
40/51
Linear Systems
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
41/51
Eigen decompositions (1)
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
42/51
Eigen decompositions (2)
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
43/51
Python scipy.linalg library
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
44/51
Python scipy.linalg library
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
45/51
Python scipy.linalg library
Thursday, November 14, 13
7/23/2019 Matrices and Libraries
46/51
Python example 1
import
Thursday, November 14, 13
P h l II
7/23/2019 Matrices and Libraries
47/51
Python example II
Thursday, November 14, 13
E P f C
7/23/2019 Matrices and Libraries
48/51
Exercise : Performance Comparison
The goal is to compare the performance of each library for solvingrandomly-generated matrices as a function of the size, testing also theactual algorithm used to solve the system. Optionally, the eigen valuedecomposition can also be useful.
Thursday, November 14, 13
Ti i i i C/C
7/23/2019 Matrices and Libraries
49/51
Timing routines in C/C++
gnu timecommand
std::chronosor clock
Thursday, November 14, 13
Ti i i i P h
7/23/2019 Matrices and Libraries
50/51
Timing routines in Python
gnu timecommand
timeit
Thursday, November 14, 13
E i /H k
7/23/2019 Matrices and Libraries
51/51
Exercise/Homework
For a linear system solved by QR and LU, compute the performance ofeigen, gsl+cblas, armadillo, and python, as function of the matrix size.
Final result: Several figures, one per method (QR and LU and simplesolve). 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).
Initialize randomly the matrices at each time step using seed=0 and
srand48 or srand if needed explicitly.
Print only the average after 20 tries to improve statistics.
Before starting the benchmarks, make some test to check if thesolutions are the solutions.