© 2015 the mathworks, inc.matlab 개발환경및프로그래밍 기법 application engineer...

29
1 © 2015 The MathWorks, Inc.

Upload: others

Post on 18-Jan-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

1© 2015 The MathWorks, Inc.

Page 2: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

2© 2015 The MathWorks, Inc.

생산성증대를위한MATLAB 개발환경및프로그래밍기법

Application Engineer

엄준상대리

Page 3: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

4

Agenda

Large Data set and efficient

and parallel programming

Improving code quality and performance

Parallel Computing on Desktop

GPU Computing

Page 4: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

5

Example:

Large Data set and efficient programming

Evaluate function at grid points

Reevaluate function

over larger blocks

Compare the results

Evaluate code performance

Page 5: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

6

Summary of Example

Used built-in timing functions

>> tic

>> toc

Used Code Analyzer to find

suboptimal code

Preallocated arrays

Vectorized code

Page 6: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

7

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 4

4

7

4

7

4

7

12

X(3) = 12X(2) = 7

Page 7: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

8

Benefit of Preallocation

>> x = zeros(3,1)

>> x(1) = 4

>> x(2) = 7

>> x(3) = 12

0x0000

0x0008

0x0010

0x0018

0x0020

0x0028

0x0030

0x0038

0

0

0

0x0000

0x0008

0x0010

0x0018

0x0020

0x0028

0x0030

0x0038

0

0

0

0x0000

0x0008

0x0010

0x0018

0x0020

0x0028

0x0030

0x0038

0x0000

0x0008

0x0010

0x0018

0x0020

0x0028

0x0030

0x0038

4

0

0

4

7

0

4

7

12

Page 8: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

9

Data Storage of MATLAB Arrays

>> x = magic(3)

x =

8 1 6

3 5 7

4 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

8

3

4

1

5

9

6

7

2

Page 9: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

10

Code Quality

Writing “better” code

– Less error-prone

– Human readable code

– Performance tuning

Robustness

– Validate, guard inputs/outputs

– Handle errors, exceptions

Page 10: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

11

Improving Code Quality in MATLAB

Analyzing code

Checking McCabe complexity

Debugging

Input and error handling

Page 11: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

12

MATLAB Code Analyzer

Optimize your code and

avoid syntax errors

Automatically check

code in Editor

Run on multiple files

in folder and

generate a report

Page 12: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

13

Check McCabe Complexity

McCabe complexity (checkcode -cyc)

– Quantitative measure of the complexity of a program

Can lower the complexity by dividing a

function into smaller, simpler functions

Good rule of thumb is to aim for

complexity around 10 or lower

Lower complexity Easier to understand, modify

Higher complexity More likely to contain errors

1 10

Simple program, low risk

Moderate risk

10 20

High risk

20 50

Untestable program, very high risk

50+

Page 13: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

14

Debugging with MATLAB

Diagnose problems

– Graphical user interface in Editor

– Command line interface

Set standard, conditional,

or error breakpoints

Step through a file and

examine variable values

Page 14: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

15

Measuring Code Performance

tic and toc

– For timing for smaller portions of code and scripts

– Measures performance using a stopwatch timer

timeit

– For timing a function

– Measures the function multiple times

and computes the median

Profiler

– For identifying specific performance

bottlenecks in code

– Measures relative execution time

Page 15: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

16

How does MATLAB store data?Container overhead*

d Header (112)

Data

d = [1 2] dcell ={[1 2]}

dcell Header (112)

Data

Cell Header (112)

dstruct.d = [1 2]

dstruct Header (112)

Data

Element Header (112)

Fieldname (64)

* Using values for 64-bit MATLAB

Page 16: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

17

Sparse Matrices

Require less memory and are faster

When to use sparse?

– < 1/2 dense on 64-bit (double precision)

– < 2/3 dense on 32-bit (double precision)

Functions that support sparse matrices

>> help sparfun

Blog Post: Creating Sparse Finite Element Matriceshttp://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-elem

ent-matrices-in-matlab/

Page 17: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

18

Reading in Part of a Dataset from Files

MAT file

– Load and save part of a variable using the matfile

– http://blogs.mathworks.com/loren/2011/10/14/new-mat-file-funct

ionality-in-r2011b/

ASCII file

– Selectively choose rows and columns using textscan

– Pointer keeps track of location in file

Binary file

– Read and write directly to/from file using memmapfile

– Maps address space to file

Page 18: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

19

Batch processing…

Load the entire file and process it all at once

Stream processing

Load a frame and process it before moving on to the next frame

Source

Batch

Processing

Algorithm

Memory

MATLAB Memory

Stream

Source

Stream

Processing

Page 19: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

20

System Objects

A class of MATLAB objects that

support streaming workflows

Simplifies data access for streaming applications

– Manages flow of data from files or network

– Handles data indexing and buffering

Contain algorithms to work with streaming data

– Manages algorithm state

– Available for Signal Processing, Communications,

Video Processing, and Phased Array Applications

Available from DSP System Toolbox

Communications System Toolbox

Computer Vision System Toolbox

Phased Array System Toolbox

Image Acquisition Toolbox

Page 20: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

22

Parallel Computing with MATLAB

User’s Desktop

Parallel Computing Toolbox

MATLAB Workers

MATLAB Distributed

Computing Server

Compute Cluster

Page 21: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

23

Example: Parameter Sweep of ODEs

Solve a 2nd order ODE

Simulate with different

values for b and k

Record peak value for each run

Plot results

0,...2,1,...2,1

5

xkxbxm

0 5 10 15 20 25-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

1.2

Time (s)

Dis

pla

cem

ent

(x)

m = 5, b = 2, k = 2

m = 5, b = 5, k = 5

0

2

4

6 12

34

5

0.5

1

1.5

2

2.5

Stiffness (k)Damping (b)

Pe

ak D

isp

lace

me

nt (x

)

Page 22: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

24

Summary of Example

Mixed task-parallel and serial

code in the same function

Ran loops on a pool of

MATLAB resources

Used Code Analyzer to helpin converting existing for-loop

into parfor-loop

0 5 10 15 20 25-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

1.2

Time (s)

Dis

pla

cem

ent

(x)

m = 5, b = 2, k = 2

m = 5, b = 5, k = 5

0

2

4

6 12

34

5

0.5

1

1.5

2

2.5

Stiffness (k)Damping (b)

Pe

ak D

isp

lace

me

nt (x

)

Page 23: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

25

Parallel Computing enables you to …

Larger Compute Pool Larger Memory Pool

11 26 41

12 27 42

13 28 43

14 29 44

15 30 45

16 31 46

17 32 47

17 33 48

19 34 49

20 35 50

21 36 51

22 37 52

Speed up Computations Work with Large Data

Page 24: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

26

Core 1

Core 3 Core 4

Core 2

Cache

Gaining Performance with More Hardware

Using More Cores (CPUs) Using GPUs

Device Memory

Page 25: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

27

Overloaded MATLAB Functions

A = magic(1000);

G = gpuArray(A); %Push to GPU memory

b = rand(1000,1,'gpuArray'); %Create on GPU

F = fft(G);

x = G\b;

z = gather(x); %Bring back into MATLAB

Full list of built-in functions that support GPUArray

Parallel Computing → GPU Computing

→ Establish Arrays on a GPU

→ Run Built-In Functions on a GPU

Page 26: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

28

Using arrayfun on GPU

gain = 1.5;

offset = -0.1;

x = rand(1000,1,'gpuArray'); %Create on GPU

x = arrayfun(@myGPUfun, x)%Execute on GPU

function c = myGPUfun(x, gain, offset)

c = (x .* gain) + offset;

end

Full list of functions for use with arrayfun on GPU

Parallel Computing → GPU Computing

→ Run Element-wise MATLAB Code on GPU

Page 27: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

35

Key Takeaway

Consider performance benefit of vector and

matrix operations in MATLAB

Leverage parallel computing tools

to take advantage of additional

computing resources

Consider Code Quality and Performance

Page 28: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

36

Additional Resources

Documentation

– Source Control Integration

– Techniques for Improving Performance

– Unit Testing Framework

– Toolbox Distribution and Documentation Tools

Webinars

– Programming with MATLAB

– Speeding up MATLAB Applications

– Managing and Sharing MATLAB Code

MATLAB Central

– Open exchange for the MATLAB and

Simulink user community

Page 29: © 2015 The MathWorks, Inc.MATLAB 개발환경및프로그래밍 기법 Application Engineer 엄준상대리 4 Agenda Large Data set and efficient and parallel programming Improving

37© 2015 The MathWorks, Inc.

Questions?