Speed up Matlab Xin Sheng Zhou
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
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
Code 1
Preallocating arrays
Preallocating arrays Looking for larger memory Moving data
Preallocating arrays Looking for larger memory Moving data
Preallocating arrays
Preallocating a non-double matrix A = int8(zeros(100)); A = zeros(100, 'int8');
Changing a variable’s data type or dimension Another case: assigning real and complex numbers
Using appropriate logical operators Large if…else… block, consider switch…case…
Code 2
Vectorizing loops
Profiling Measure where a program spends time
Case study Low density parity check decoder Block decoding Block length: 10 6 Generate parity check matrix Decoding
Resource used for block length 10^6 and 10^7
LDPC Code H*c T =0 For code ½, block length 10 6 H: Matrix (0.5*10 6 ,10 6 ) C: Row vector, size 10 6 Encoding: c=s*G s: row vector, size 0.5*10 6 G: matrix, (0.5*10 6 ,10 6 ) Get G from H is not easy Instead, generate parity check bits p=A -1 Bs 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 > 10 5 , saving is also slow Since H is sparse, sparse matrix format should be used. However, A -1 is not sparse
Other ways… Parallel computing Multi-threaded GPU Multi-machine MEX file Matlab Compiler
Summary Preallocating arrays Vectorizing loops Profiling
Read more …
Recommend
More recommend