Speed up Matlab - University of Waterloo
Transcript of Speed up Matlab - University of Waterloo
![Page 1: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/1.jpg)
Speed up Matlab
Xin Sheng Zhou
![Page 2: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/2.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/4.jpg)
Code 1
![Page 5: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/5.jpg)
Preallocating arrays
![Page 6: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/6.jpg)
Preallocating arrays
Looking for larger memory
Moving data
![Page 7: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/7.jpg)
Preallocating arrays
Looking for larger memory
Moving data
![Page 8: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/8.jpg)
Preallocating arrays
![Page 9: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/9.jpg)
Preallocating a non-double matrix
A = int8(zeros(100));
A = zeros(100, 'int8');
![Page 10: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/10.jpg)
Changing a variable’s data type or
dimension
Another case: assigning real and complex
numbers
![Page 11: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/11.jpg)
Using appropriate logical operators
Large if…else… block, consider
switch…case…
![Page 12: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/12.jpg)
Code 2
![Page 13: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/13.jpg)
Vectorizing loops
![Page 14: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/14.jpg)
Profiling
Measure where a program spends time
![Page 15: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/15.jpg)
![Page 16: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/16.jpg)
![Page 17: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/17.jpg)
![Page 18: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/18.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/19.jpg)
Resource used for block length
10^6 and 10^7
![Page 20: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/20.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/21.jpg)
![Page 22: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/22.jpg)
![Page 23: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/23.jpg)
Other ways…
Parallel computing
Multi-threaded
GPU
Multi-machine
MEX file
Matlab Compiler
![Page 24: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/24.jpg)
Summary
Preallocating arrays
Vectorizing loops
Profiling
![Page 25: Speed up Matlab - University of Waterloo](https://reader031.fdocuments.us/reader031/viewer/2022012512/618b11d0f9b93724035090b8/html5/thumbnails/25.jpg)
Read more …