How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or...
Transcript of How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or...
![Page 1: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/1.jpg)
1© 2015 The MathWorks, Inc.
How to speed up MATLAB
Isaac NohApplication Engineer
![Page 2: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/2.jpg)
2
Agenda
Two coding tips to speed up MATLAB code
Leveraging multiple cores with MATLAB
MATLAB to C:– Generating C code from MATLAB– Integrate C functions in MATLAB
![Page 3: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/3.jpg)
3
Two Coding Tips for Speeding Up MATLAB
Pre-Allocation of Memory
Vectorization
![Page 4: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/4.jpg)
4
Pre-Allocation of Memory
for id = 1:10000a(id) = rand();
end
vs.
a = nan(1, 10000);for id = 1:10000
a(id) = rand();end
![Page 5: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/5.jpg)
5
Effect of Not Preallocating Memory
>> x = 4>> x(2) = 7>> x(3) = 12
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
4 447
47
47
12
X(3) = 12X(2) = 7
![Page 6: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/6.jpg)
6
Benefit of Preallocation
>> x = zeros(3,1)>> x(1) = 4>> x(2) = 7>> x(3) = 12
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
000
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
000
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
400
470
47
12
![Page 7: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/7.jpg)
7
Data Storage of MATLAB Arrays
>> x = magic(3)x =
8 1 63 5 74 9 2
0x0000
0x0008
0x0010
0x0018
0x0020
0x0028
0x0030
0x0038
0x0040
0x0048
0x0050
0x0058
0x0060
0x0068
See the June 2007 article in “The MathWorks News and Notes”: http://www.mathworks.com/company/newsletters/news_notes/june07/patterns.html
834159672
![Page 8: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/8.jpg)
8
Two Coding Tips for Speeding Up MATLAB
Pre-Allocation of Memory
Vectorization
![Page 9: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/9.jpg)
9
Vectorization
MATLAB is a matrix (vector)-based language supports vectorized operations (faster in general)
Process in column blocks, rather than row blocks
b = a.^a;
for id1 = 1:size(a,1)for id2 = 1:size(a,2)
b(id1, id2) = a(id1, id2)^a(id1, id2);end
end
![Page 10: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/10.jpg)
10
Agenda
Some coding techniques to speed up MATLAB code
Leveraging Parallel Computing with MATLAB
MATLAB to C:– Generating C code from MATLAB– Integrate C functions in MATLAB
![Page 11: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/11.jpg)
11
Going Beyond Serial MATLAB Applications
MATLAB Desktop (Client)
Worker
WorkerWorker
Worker
Worker
Worker
![Page 12: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/12.jpg)
12
Performance Gain with More Hardware
Using GPUs
Device Memory
GPU cores
Device Memory
Core 1
Core 3 Core 4
Core 2
Cache
Using More Cores (CPUs)
![Page 13: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/13.jpg)
13
Time
Independent Tasks or Iterations
Ideal problem for parallel computing No dependencies or communications between tasks Examples: parameter sweeps, Monte Carlo simulations
Time
![Page 14: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/14.jpg)
14
Agenda
Some coding techniques to speed up MATLAB code
Leveraging multiple cores with MATLAB
MATLAB to C:– Generating C code from MATLAB– Integrate C functions in MATLAB
![Page 15: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/15.jpg)
15
Why Engineers Translate MATLAB to C Today
Implement C code on processors or hand off to software engineers
Integrate MATLAB algorithms with existing C environment using source code and static/dynamic libraries
Prototype MATLAB algorithms on desktops as standalone executables
Accelerate user-written MATLAB algorithms
.exe
.lib
.dll
.c
MEX
![Page 16: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/16.jpg)
16
MEX
.lib
.dll
.exe
.c
With MATLAB Coder, design engineers can:
• Maintain one design in MATLAB • Design faster and get to C quickly• Test more systematically and frequently • Spend more time improving algorithms in MATLAB
With MATLAB Coder, design engineers can:
• Maintain one design in MATLAB • Design faster and get to C quickly• Test more systematically and frequently • Spend more time improving algorithms in MATLAB
Automatic Translation of MATLAB to C
itera
te
![Page 17: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/17.jpg)
17
Acceleration using MEX (MATLAB Executable)
Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API– Auto-generate C-based MEX files from
MATLAB code using MATLAB Coder
Speed-up factor will vary– May see speedup for state-based for-loops– May not see a speedup when MATLAB code is
Using multithreaded computations Using optimized libraries (BLAS, FFTW, etc.)
c = myFcn(a,b)
myFcn.cvoid mexFunction(
int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{/* more C code ... */
}
![Page 18: How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or Fortran code directly from MATLAB – Integrate existing code using MEX API – Auto-generate](https://reader033.fdocuments.us/reader033/viewer/2022060213/5f0528d97e708231d411917c/html5/thumbnails/18.jpg)
18
Questions?