Você está na página 1de 45

Efficient programming in MATLAB

2011 MathWorks, Inc.

Agenda
900 - 1015

Functions in MATLAB What are the options?


-

Function Quiz!

Memory Considerations
-

Container overhead

Lazy copying (copy-on-write)

1015 - 1045
1045 - 1200

Parallel Computing with MATLAB


-

Task parallelism and Data parallelism


Parallel for-loops and Distributed arrays

Symbolic Computing with MATLAB


-

Symbolic computations at the MATLAB command line


Introduction to the MuPAD notebook interface

Why use functions?

Automate calculations that are


performed often
Improve code compactness
Improve code readability
Restrict scoping of variables to save memory space
Minimize risk of errors due to variable name clashes

Function Quiz

Lets see how much you know about functions in


MATLAB

This quiz covers


Different types of MATLAB functions
When to use what type of function

This quiz does not cover


MEX functions, private functions,

Function Quiz Review

Primary functions

Subfunctions

Nested functions

Function handles

Anonymous functions
5

Summary and Advice


Anonymous Functions


Create simple functions without creating m-files:


fh = @(x,y) a*sin(x)*cos(y)

Useful for fun-funs (function functions)


Optimization

>> getXLSData
= @(worksheet)
Solving differential
equationsxlsread('records.xls', worksheet);
>> myData = getXLSData('Sheet1');

Numerical integration
Plotting

Array functions (cellfun, structfun, )





Convenient for simple callbacks


Embed data in function calls, to help reduce copy-paste
coding
6

Summary and Advice


Nested Functions
Embed one function within another, with shared, persistent
workspace:
function main
function nest
end
end




Convenient for callbacks (shared workspace)


Encapsulate functionality and data (export as function
handles)

MATLAB Function Types: Additional


Resources


MATLAB Digest
Dynamic Function Creation with Anonymous and Nested Functions
http://www.mathworks.com/company/newsletters/digest/2005/sept/dynfuncti
ons.html

Examples
Anonymous functions
http://www.mathworks.com/products/matlab/demos.html?file=/products/dem
os/shipping/matlab/anondemo.html

Nested Functions
http://www.mathworks.com/products/matlab/demos.html?file=/products/dem
os/shipping/matlab/nesteddemo.html

The Art of MATLAB Blog


Look at Category: Function Handles
http://blogs.mathworks.com/loren/

Agenda
900 - 1015

Functions in MATLAB What are the options?


-

Function Quiz!

Memory Considerations
-

Container overhead

Lazy copying (copy-on-write)

1015 - 1045
1045 - 1200

Parallel Computing with MATLAB


-

Task parallelism and Data parallelism


Parallel for-loops and Distributed arrays

Symbolic Computing with MATLAB


-

Symbolic computations at the MATLAB command line


Introduction to the MuPAD notebook interface

Memory Used for Different Array Types


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

% Double array
% Cell array
% Struct array

whos

10

Container Overhead
Numeric Array

Cell Array

Structure

Variable header: 60 bytes


Element header: 60 bytes
Field name: 64 bytes
Data: 16 bytes

2nd Element header: 60 bytes


Field name: 64 bytes
Data: 16 bytes

Total Used:

76 + 16 = 92

136 + 76 = 212

200 + 140 = 340

Total Reported:

16 + 16 = 32

76 + 76 = 152

140 + 140 = 280


11

MATLAB and Memory

Lazy copying (copy-on-write):


s.A = rand(3000,3000);
s.B = rand(3000,3000);
sNew = s;
s.A(1,1) = 2;

12

MATLAB and Memory


Different ways of storing an image
im1{1} = redPlane;
% each plane is m x n
im1{2} = greenPlane;
im1{3} = bluePlane;
versus

% each 1x3
im2{1,1} = [redPlane(1) greenPlane(1) bluePlane(1)];
im2{2,1} = [redPlane(2) greenPlane(2) bluePlane(2)];
...
im2{m,n} = [redPlane(m*n) greenPlane(m*n) bluePlane(m*n)];

13

MATLAB and Memory

im1copy

im1

Change
one pixel

>> im1copy==0;im1; % Red


>> im1copy{1}(1)
>> im1copy{2}(1) = 0;
% Green
>> im1copy{3}(1) = 0;
% Blue

14

MATLAB and Memory

im1copy

im1

Change
one pixel

im2

[
[
[
[

]
]
]
]

im2copy == im2;
>>>>
im2copy{1}
[0 0 0];
[
[
[
[

]
]
]
]

[
[
[
[

]
]
]
]

im2copy

15

Summary of Memory Usage in MATLAB




When is data copied?


Lazy copy: only when necessary (copy on write)
Never, if operation can be performed in-place

How does MATLAB store data?


Every array has overhead
Structures and cell arrays are containers which can hold
multiple arrays

16

In-place Optimizations
When does MATLAB do calculations in-place?

function inplaceOptimization
x = randn(3e7,1);
y = myfunc(x);
% No.
x = myfuncInPlace(x); % Yes!
x = myfunc(x);
% No.
function y = myfunc(x)
y = sin(2*x.^2+3*x+4);
function x = myfuncInPlace(x)
x = sin(2*x.^2+3*x+4);
17

MATLAB and Memory: Additional Resources

Recorded Webinar:
Handling Large Data Sets Efficiently in MATLAB
mathworks.com -> Recorded Webinars

Memory Management Guide (Tech Note 1106)


>> doc memory
Search mathworks.com for 1106

18

Agenda
900 - 1015

Functions in MATLAB What are the options?


-

Function Quiz!

Memory Considerations
-

Container overhead

Lazy copying (copy-on-write)

1015 - 1045
1045 - 1200

Parallel Computing with MATLAB


-

Task parallelism and Data parallelism


Parallel for-loops and Distributed arrays

Symbolic Computing with MATLAB


-

Symbolic computations at the MATLAB command line


Introduction to the MuPAD notebook interface

19

Agenda
900 - 1015

Functions in MATLAB What are the options?


-

Function Quiz!

Memory Considerations
-

Container overhead

Lazy copying (copy-on-write)

1015 - 1045
1045 - 1200

Parallel Computing with MATLAB


-

Task parallelism and Data parallelism


Parallel for-loops and Distributed arrays

Symbolic Computing with MATLAB


-

Symbolic computations at the MATLAB command line


Introduction to the MuPAD notebook interface

20

Processes

Distributing Tasks (Task Parallel)

Time

Time
21

Large Data Sets (Data Parallel)

11 26 41

11 26 41

12 27 42

12 27 42

13 28 43

13 28 43

14 29 44

14 29 44

15 30 45

15 30 45

16 31 46

16 31 46

17 32 47

17 32 47

17 33 48

17 33 48

19 34 49

19 34 49

20 35 50

20 35 50

21 36 51

21 36 51

22 37 52

22 37 52

22

Parallel Computing with MATLAB

Computer Cluster
MATLAB Distributed Computing Server

CPU
Worker

CPU
Worker

TOOLBOXES

Parallel
Computing
Toolbox

Scheduler

CPU
Worker

BLOCKSETS
CPU
Worker

23

Parallel Computing with MATLAB


No code changes

 Toolbox Support:
Optimization Toolbox
Statistics Toolbox
SystemTest
Global Optimization Toolbox
Simulink Design Optimization
Bioinformatics Toolbox
Model-Based Calibration Toolbox

Trivial changes

 parfor
 Distributed Arrays, spmd
 GPUArrays

Extensive changes

 MPI programming in MATLAB


24

Parallel Computing with MATLAB

Worker

Worker

Worker

TOOLBOXES

Worker

Worker
Worker

BLOCKSETS

Worker

Worker

>> matlabpool open


Pool of MATLAB Workers
25

The Mechanics of parfor Loops

1
2

23 3 4 4 55 6 6

8 8 9 910 10

Worker

myInput = 0.1:0.1:1;
myOutput = zeros(10, 1);
parfor k = 1:10
x = myInput(k);
y = myFunction(x);
myOutput(k) = y;
end
myOutput

x = myInput(k);
y = myFunction(x);
myOutput(k) = y;

Worker
x = myInput(k);
y = myFunction(x);
myOutput(k) = y;

Worker
x = myInput(k);
y = myFunction(x);
myOutput(k) = y;

Worker
x = myInput(k);
y = myFunction(x);
myOutput(k) = y;

Pool of MATLAB Workers


26

Example: Parameter Sweep of ODEs

Solve a 2nd order ODE

}5
m &x& + b{ x& + k{ x = 0
1, 2 ,...


m
b

1, 2 ,...

Initial conditions: x(0) = 0,

x ' (0 ) = 1

1.2
1

Displacement (x)

0.8

Simulate with different


values for b and k

0.6
0.4

m = 5, b = 2, k = 2

0.2

Record peak value for


each run
Plot results

Peak Displacement (x)

0
2.5

m = 5, b = 5, k = 5

-0.2
2

-0.4
1.5

10

15

20

25

Time (s)

1
0.5
0

Damping (b)

4
3

2
6

Stiffness (k)

27

Parallel for Loops


parfor i = 1 : n
% do something with i
end





Mix task parallel and serial code in the same function


Run loops on a pool of MATLAB resources
Iterations must be order-independent
M-Lint analysis helps to identify if existing for loops
can be changed to parfor

28

Client-side Distributed Arrays

11 26 41
12 27 42
13 28 43
14 29 44
15 30 45
16 31 46

TOOLBOXES

17 32 47
17 33 48

BLOCKSETS

19 34 49
20 35 50
21 36 51
22 37 52

Remotely Manipulate Array


from Desktop

Distributed Array
Lives on the Cluster
29

Distributed Array

Worker
Worker

Column 1:3 of y

Column 4:6 of y

y = distributed(rand(10));
Worker
Column 7:8 of y

Worker
Column 9:10 of y

Pool of MATLAB Workers


30

Demo:
- Handle large data sets

31

MATLAB Functions on Distributed Arrays ~ 150


supported functions

32

Interactive to Scheduled


Interactive
Great for prototyping
Immediate access to MATLAB workers

Scheduled
Offloads work to other MATLAB workers (local or on a cluster)
Access to more computing resources for improved performance
Frees up local MATLAB session

>> batch('myScript')
33

Run Eight Local Workers with a Parallel


Computing Toolbox License
 Easy to experiment with explicit
Parallel
Computing
Toolbox

parallelism on multicore
machines
 Rapidly develop parallel

applications on local computer


 Take full advantage of desktop

power
 No separate computer cluster

required
34

Scale Up to Cluster Configuration with No


Code Changes

Computer cluster
Parallel
Computing
Toolbox

MATLAB Distributed Computing Server

CPU
Worker

CPU
Worker

Scheduler

CPU
Worker

CPU
Worker

35

What is a Graphics Processing Unit (GPU)

Originally for graphics acceleration, now also


used for scientific calculations

Massively parallel array of integer and


floating point processors
Typically hundreds of processors per card
GPU cores complement CPU cores

Dedicated high-speed memory

* Parallel Computing Toolbox requires NVIDIA GPUs with Compute Capability 1.3 or greater, including
NVIDIA Tesla 10-series and 20-series products. See http://www.nvidia.com/object/cuda_gpus.html
for a complete listing
36

Summary of Options for Targeting GPUs

Ease of Use

2) Execute custom functions on elements of


the GPU array

Greater Control

1) Use GPU array interface with MATLAB


built-in functions

3) Invoke your CUDA kernels directly from


MATLAB

37

Performance: A\b with Double Precision

38

Agenda
900 - 1015

Functions in MATLAB What are the options?


-

Function Quiz!

Memory Considerations
-

Container overhead

Lazy copying (copy-on-write)

1015 - 1045
1045 - 1200

Parallel Computing with MATLAB


-

Task parallelism and Data parallelism


Parallel for-loops and Distributed arrays

Symbolic Computing with MATLAB


-

Symbolic computations at the MATLAB command line


Introduction to the MuPAD notebook interface

39

Overview of Symbolic Computing

Governing equation:

mx' ' (t ) + bx' (t ) + kx(t ) = 0


Initial conditions:

x(0) = 0

x' (0) = 1

Numeric:

Symbolic:

Approximate solution in vector form

Exact solution in form of analytical expression

x(t)

0.1

0.099

0.2

0.193

0.3

0.283

0.4

0.364

0.5

0.437

x (t ) =
e

t b b 2 4 km

2m

b 2 4km

t b + b 2 4 km

2m

b 2 4km

Benefits
1. Insight into how parameters affect solution
2. Often more efficient than numeric implementation
40

Working with Symbolic Math Toolbox


From notebook interface:

From MATLAB:

Sharing

Perform symbolic computations using


familiar MATLAB syntax

Conveniently manage & document symbolic


computations
 Math notation, embedded text & graphics
Access complete MuPAD language
 15+ libraries of symbolic math functions
41

Symbolic Math Toolbox Libraries


 Calculus






Differentiation
Integrals (definite, indefinite)
Jacobian
Taylor series
Limits

 Solving Equations



Algebraic Equations
Ordinary Differential
Equations

 Integral and ZTransforms






Fourier transform
Laplace transform
Z-transforms

 Linear Algebra



Operations
Eigenvalues

 Special Functions



Bernoulli, Bessel, Beta,


Fresnel sine/cosine integral,
Gamma

 Variable Precision Arithmetic


 Plotting



2-D
3-D contour, surface, mesh,
movies

 Simplification



Expansion of polynomials
Substitution
42

Key Takeaways
1. Integrated numerics and graphics


Integration with MATLAB platform

2. Convenient interface for managing &


documenting model derivation


Notebook interface

3. Deep and broad set of mathematical areas




15+ libraries of symbolic math functions

4. Ability to extend built-in capabilities




MuPAD language for developing custom


symbolic functions and libraries

43

44

Thank You for Attending




Products
www.mathworks.com

Upcoming events (seminars, webinars,


trade shows, conferences)
www.mathworks.com/events

Training
www.mathworks.com/training

45

Você também pode gostar