+ All Categories
Home > Documents > How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or...

How to speed up MATLAB - MathWorks · 17 Acceleration using MEX (MATLAB Executable) Call C or...

Date post: 11-Jun-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
18
1 © 2015 The MathWorks, Inc. How to speed up MATLAB Isaac Noh Application Engineer
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

18

Questions?


Recommended