Speed up Matlab - University of Waterloo

25
Speed up Matlab Xin Sheng Zhou

Transcript of Speed up Matlab - University of Waterloo

Page 1: Speed up Matlab - University of Waterloo

Speed up Matlab

Xin Sheng Zhou

Page 2: Speed up Matlab - University of Waterloo

Matlab

MATrix LABoratory Started in the late 1970s

Matrix manipulation

Plotting of data

Implementation of algorithms

Creation of user interface

Interfacing with other languages, such as C and Fortran

Slower, but reducing programming effort

Page 3: Speed up Matlab - University of Waterloo

Java

Java based Appeared in 1995

Run on Java Virtual Machine

Compile once, run everywhere

Inherited slower than C/C++

Currently (February 2012), microbenchmarks show Java 7 is approximately 1.5 times slower than C

Page 4: Speed up Matlab - University of Waterloo

Code 1

Page 5: Speed up Matlab - University of Waterloo

Preallocating arrays

Page 6: Speed up Matlab - University of Waterloo

Preallocating arrays

Looking for larger memory

Moving data

Page 7: Speed up Matlab - University of Waterloo

Preallocating arrays

Looking for larger memory

Moving data

Page 8: Speed up Matlab - University of Waterloo

Preallocating arrays

Page 9: Speed up Matlab - University of Waterloo

Preallocating a non-double matrix

A = int8(zeros(100));

A = zeros(100, 'int8');

Page 10: Speed up Matlab - University of Waterloo

Changing a variable’s data type or

dimension

Another case: assigning real and complex

numbers

Page 11: Speed up Matlab - University of Waterloo

Using appropriate logical operators

Large if…else… block, consider

switch…case…

Page 12: Speed up Matlab - University of Waterloo

Code 2

Page 13: Speed up Matlab - University of Waterloo

Vectorizing loops

Page 14: Speed up Matlab - University of Waterloo

Profiling

Measure where a program spends time

Page 15: Speed up Matlab - University of Waterloo
Page 16: Speed up Matlab - University of Waterloo
Page 17: Speed up Matlab - University of Waterloo
Page 18: Speed up Matlab - University of Waterloo

Case study

Low density parity check decoder

Block decoding

Block length: 106

Generate parity check matrix

Decoding

Page 19: Speed up Matlab - University of Waterloo

Resource used for block length

10^6 and 10^7

Page 20: Speed up Matlab - University of Waterloo

LDPC Code

H*cT=0

For code ½, block length 106

H: Matrix (0.5*106,106)

C: Row vector, size 106

Encoding: c=s*G s: row vector, size 0.5*106

G: matrix, (0.5*106,106)

Get G from H is not easy

Instead, generate parity check bits p=A-1Bs H=[A B], c=[p s]

Matlab’s matrix multiplication and inversion cannot be used since it is based on double value and encoding is based on GF(2)

Solution: Custom matrix inversion and multiplication on GF(2)

Inversion is slow, and A-1 is required to be saved

When block length > 105, saving is also slow

Since H is sparse, sparse matrix format should be used. However, A-1 is not sparse

Page 21: Speed up Matlab - University of Waterloo
Page 22: Speed up Matlab - University of Waterloo
Page 23: Speed up Matlab - University of Waterloo

Other ways…

Parallel computing

Multi-threaded

GPU

Multi-machine

MEX file

Matlab Compiler

Page 24: Speed up Matlab - University of Waterloo

Summary

Preallocating arrays

Vectorizing loops

Profiling

Page 25: Speed up Matlab - University of Waterloo

Read more …