Docsity

Dense Linear Algebra - Scientific Computing - Slides | CMSC 828E, Study notes of Computer Science

Partial preview of the text

Download Dense Linear Algebra - Scientific Computing - Slides | CMSC 828E and more Study notes Computer Science in PDF only on Docsity!

Scientific Computing

Some slides from

James Lambers

, Stanford

Dense Linear Algebra

• Scaling and sums• Transpose• Rank-one updates• Rotations• Matrix vector products• Matrix Matrix products• BLAS

Mathematical Libraries

-^

BLAS – Basic Linear Algebra Subprograms^ –

Three levels:

-^

Level 1: Vector operations

-^

Level 2: Matrix-Vector operations

-^

Level 3: Matrix-Matrix operations

–^

Originally written in F77, the standard has evolved to containF77,F95,C,C++ libraries, each with their own version of the callingprogram. The dcopy routine is used to copy two vectors and is givenby: f_dcopy for fortran users and c_dcopy for C users.

-^

BLAS 1 was originally designed for vector processors. BLAS 2 and 3were designed for cache-based computers

-^

LINPACK (Linear Algebra Package)^ –

Linear equation and linear least squares solvers

-^

Based on level 1 routines in BLAS

Mathematical Libraries, cont’d

-^

EISPACK (Eigenvalue Software Package)^ –

Computes eigenvalues and eigenvectors

-^

LAPACK (Linear Algebra Package)^ –

Originally designed to extend LINPACK and EISPACK on shared memoryvector and parallel processors

-^

Also implements highly-tuned system-dependend BLAS libraries, usingBLAS levels 2 and 3 wherever possible

-^

SCALAPACK (Scalable Linear Algebra Package)^ –

Designed to extend LAPACK routines to work on distributed memoryparallel machines

-^

Uses BLACS (Basic Linear Algebra Communications Subprograms) forinterprocessor communication

-^

BLAS,LINPACK,EISPACK,LAPACK,SCALAPACK can be downloadedfrom http://www.netlib.org

Self-tuning Libraries

-^

PHIPAC (Portable High-performance ANSI C Linear AlgebraSubprograms)^ –

Run a performance tuner which stores information about particular machineto optimize BLAS routines

-^

Performance tuner can take days because it creates system-specificversions of all BLAS routines

-^

http://www.icsi.berkeley.edu/~bilmes/phipac/

-^

ATLAS (Automatically Tuned Linear Algebra Software)^ –

System-specific information is placed into the design of one specificsubroutine which can be called by all BLAS routines.

-^

http://www.netlib.org/atlas/

-^

FFTW (Fastest Fourier Transform in the West)^ –

System-specific information created at runtime using a "planner", whichstores information used by "executor" functions during program execution

-^

http://www.fftw.org

Commercial Libraries

-^

Hardware vendors sell highly-tuned versions of BLAS and LAPACKfor their hardware^ –

Intel Math Kernel Library (MKL)

-^

http://www.intel.com/software/products/mkl/

–^

HP MLIB (Mathematical Software Library)

-^

IBM ESSL (Engineering and Scientific Subroutine Library)

-^

SGI SCSL

–^

SUN Performance Library

-^

Other libraries developed independently of BLAS, specifically forrelevant hardware.^ –

Visual Numerics' IMSL (Mathematical and Statistical Library)

-^

http://www.vni.com

–^

Numerical Algorithms Groups' Numerical Libraries

-^

http://www.nag.co.uk

BLAS, cont’d

Prefix

Fortran type

S

Real

D

Double precision

C

Complex

Z^

Double precisioncomplex

Type code

Matrix type

GE

General full

SY

Symmetric

HE

Hermitian

TR

Triangular

GB

General banded

SB

Symmetric banded

CME212 – Introduction to Large-Scale Computing in Engineering

High Performance Computing and Programming

Scalar-Vector accumulation

• daxpy=Double precision y=a*x+y

for(i=0;i<N;i++)

y[i]+=a*x[i];

• daxpby=Double precision

y=alphax+betay

for(i=0;i<N;i++)

y[i]=alphax[i]+betay[i];

• Compiler will unroll these loops

Dot Product: BLAS ddot

-^

Effect of loop unrolling with N=1024 (8 kb), in MFlops

Two-way loop unrolling: for(i=0;i<nend;i+=2)

ddot1+=x[i]y[i];ddot2+=x[i+1]y[i+1]; }

No unrolling: for(i=0;i<N;i++)

ddot+=x[i]*y[i];

  • Loop unrolling reducesoverhead and allows forpipelining• Too much unrolling leads toregister spilling

gcc –O0: 0: 235.902: 538.41 (2.28x)4: 941.68 (3.99x)6: 1024.97 (4.34x)8: 1071.04 (4.54x)10: 1053.29 (4.46x)12: 1063.94 (4.51x)14: 1061.28 (4.50x)

gcc –O2: 0: 223.602: 553.34 (2.47x)4: 1092.23 (4.88x)6: 1449.38 (6.48x)8: 1548.78 (6.93x)10: 1594.80 (7.13x)12: 1572.16 (7.03x)14: 1529.57 (6.84x)

BLAS Level 2 routines

-^

Level 2 routines consist of 2 loops and include matrixcopy, matrix transpose, and matrix-vector routines.

-^

Matrix copy: dge_copy^ Performs well because of unit stride on inner-most loop:^ for(i=0;i<N;i++)

for(j=0;j<N;j++)

b[i][j]=a[i][j];

The transpose case is not as straightforward: for(i=0;i<N;i++)

for(j=0;j<N;j++)

b[i][j]=a[j][i];

gcc –O2 (1024 X 1024 int)Copy: 707.31 Mb/sTranspose: 54.59 Mb/s

Transpose with 1-d blocking

1-d

blocks

(a

and

b

ints):

int

en

bsize*(n/bsize);

for(jj=0;jj<en;jj+=bsize)

for(i=0;i<n;i++)

for(j=jj;j<jj+bsize;jj++)

b[i][j]=a[j][i];

-^

After i=0 inner loop (assuming 32-byte cache blocks):^ – 8*bsize elements of a in L1 cache (a[0:bsize-1][0:7])

-^

Next 7 loops in i will hit a[][] data

B

A

0 700 600 500 400 300 200 100

1

10

100

1000

bsize

Mb/s

Effect of Blocking on Transpose

Performance (1024 x 1024 int)

2-d blocks

1-d blocks

Note:32 x 32 ints=4096 bytes45 x 45 ints=8100 bytesL1 = 16 kb

16 kb

Peak throughput occurs when bsize=45(when two 45x45 matrices fit into L1 cache!)

In-place Transpose: dge_trans

-^

Previous examples involved a copy and transpose^ –

This is termed

out-of-place matrix transpose

-^

Consider the

in-place matrix transpose

(method 1):

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

temp

a[i][j];

a[i][j]

a[j][i];

a[j][i]

temp;

Exchange

Move along a column and swap elements.suffers from possible cache thrashing when n is a power of 2 since a[in+j]and a[jn+i] can map to same cache location