1© 2015 The MathWorks, Inc.
How to speed up MATLAB
Isaac NohApplication Engineer
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
3
Two Coding Tips for Speeding Up MATLAB
Pre-Allocation of Memory
Vectorization
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
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
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
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
8
Two Coding Tips for Speeding Up MATLAB
Pre-Allocation of Memory
Vectorization
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
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
11
Going Beyond Serial MATLAB Applications
MATLAB Desktop (Client)
Worker
WorkerWorker
Worker
Worker
Worker
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)
13
Time
Independent Tasks or Iterations
Ideal problem for parallel computing No dependencies or communications between tasks Examples: parameter sweeps, Monte Carlo simulations
Time
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
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
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
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 ... */
}
18
Questions?