Professional Documents
Culture Documents
MATLAB HandsOn PDF
MATLAB HandsOn PDF
1
The MathWorks at a Glance
Headquarters:
Natick, Massachusetts US
US:
California, Michigan,
Washington DC, Texas
Europe:
UK, France, Germany,
Switzerland, Italy, Spain,
the Netherlands, Sweden
Asia-Pacific:
China, Korea, Australia
Worldwide training
and consulting
Earths topography on an equidistant cylindrical projection,
Distributors in 25 countries created with MATLAB and Mapping Toolbox.
2
MATLAB Central
Visit www.mathworks.com/matlabcentral 3
Visit www.mathworks.com/academia 4
1,200+ Books in 27 Languages
Controls
Signal Processing
Image Processing
Biosciences
Communications
Mechanical Engineering
Aerospace Engineering
Electrical Engineering
Physical sciences
Finance
Mathematics
Visit www.mathworks.com/books 5
Training Services
Training Options
Public training
Offered throughout the world
Schedule and course information
at www.mathworks.com/training Example course topics
Onsite training Introductory and intermediate
Bring training to your site, with training on MATLAB, Simulink,
course customization available Stateflow, and Real-Time
Workshop
Web-based training
Specialized courses in control
Instructor-led e-learning
design, signal processing, parallel
Train at work or at home, with computing, code generation,
flexible dates and times communications, financial analysis,
and other areas
Visit www.mathworks.com/training 6
Further information
Stay for questions
Workshop:
Parallel Computing with MATLAB
Outline
Worker Worker
Worker
TOOLBOXES Worker
Worker
Worker
BLOCKSETS Worker
Worker
10
MATLAB Workers
12
Task-parallel Applications
Using the parfor constructs
Using jobs and tasks
Data-parallel Applications
Using distributed arrays
Using the spmd construct
13
Task-parallel Applications
14
Optimization Toolbox
Global Optimization Toolbox
Statistics Toolbox Worker
Worker
Communications Toolbox
.
16
17
Even if you have not exceeded the number of labs, you can only
open one matlabpool at a time
18
19
20
Displacement (x)
0.6
0.4
5 0.2
m = 5, b = 2, k = 2
m x b x k x 0 0
-0.2
m = 5, b = 5, k = 5
-0.4
1, 2 ,... 1, 2 ,... 0 5 10
Time (s)
15 20 25
0.5
0
5
2 4
4 3
Damping (b) 2
6 1 Stiffness (k)
\task_parallel\paramSweepScript.m 21
Worker
a(i) = i; Worker
a = zeros(10, 1) a(i) = i;
parfor i = 1:10
a(i) = i;
end Worker
Worker
a a(i) = i;
a(i) = i;
23
http://blogs.mathworks.com/loren/2009/10/02/using-
parfor-loops-getting-up-and-running/
24
Work Worker
TOOLBOXES Scheduler
Worker
Worker
Result
BLOCKSETS Worker
25
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
26
Using Configurations
Managing configurations
Typically created by Sys Admins
Label configurations based on the version of MATLAB
E.g. linux_r2009a
27
Validating Configurations
Validate configurations
Ensure new configuration is properly working
Helpful when debugging failed jobs
28
Find resource
Create job
Create task(s)
Submit job
Get results
Destroy job
Rather than using a shell script to submit a job to a cluster, well write
our jobscript in MATLAB.
task_parallel\basic_jobscript.m 29
task_parallel\jobscript_ode.m 30
task_parallel\ode_return.m 31
32
33
task_parallel\jobscript_tasks.m 34
task_parallel\tasks_return.m 35
37
Unclassified Variables
task_parallel\valid_indexing_error.m 38
Classification Description
Loop Serves as a loop index for arrays
Sliced An array whose segments are operated on by different
iterations of the loop
Broadcast A variable defined before the loop whose value is used
inside the loop, but never assigned inside the loop
Reduction Accumulates a value across iterations of the loop,
regardless of iteration order
Temporary Variable created inside the loop, but unlike sliced or
reduction variables, not available outside the loop
39
Loop variable
Temporary variable
Reduction variable
Sliced output variable Sliced input variable
Broadcast variable
40
task_parallel\what_is_it_parfor.m 41
Results
a: ones(1:10) (broadcast)
b: undefined (temp)
c: undefined (temp)
d: 1:10 (sliced)
e: 55 (reduction)
f: 5 (temp)
g: 20 (reduction)
h: 10 (temp)
idx: undefined (loop)
42
Within the list of indices for a sliced variable, one of these indices is of the form i, i+k, i-k, k+i,
or k-i, where i is the loop variable and k is a constant or a simple (non-indexed) variable; and
every other index is a constant, a simple variable, colon, or end.
task_parallel\valid_indexing_error.m 43
Create a temporary variable, b to store the row vector. Use the looping index, i, to index
the columns and the colon to assign the row vector to the temporary variable between the
for loops.
task_parallel\valid_indexing_fix1.m 44
Use cell arrays. The restrictions on indexing only apply to the top-level indexing (i.e.
indexing into the cell array). Indexing into contents of the cell arrays is allowed.
task_parallel\valid_indexing_fix2.m 45
task_parallel\simParforEx1.m 46
47
48
Data-parallel Applications
49
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
data_parallel\distributed_example.m 50
spmd
Block of code executed on workers
Worker specific commands
Explicit communication between workers
Mixture of parallel and serial code
51
spmd
% single program across workers
end
data_parallel\spmd_example.m 52
Worker
x 1 Worker
x = 1 y = x + 1 x 1
y = x + 1
spmd
y = x + 1
end Worker
Worker
y x 1
x 1
y = x + 1 y = x + 1
Composite Arrays
54
2
>> x = Composite(4)
0x0000
2
TOOLBOXES 3
0x0008
3
4 5
>> x{1} = 2 BLOCKSETS 0x0010
0x0008
>> x{4} = tsobject() 0x0008 0x0008
tsobject @sin
0x0010
0x0010 0x0010
0x0018
0xFFFF
spmd
56
MPI-Based Functions in
Parallel Computing Toolbox
Use when a high degree of control over parallel algorithm is required
Automatic bookkeeping
Setup: communication, ranks, etc.
Error detection: deadlocks and miscommunications
Pluggable
Use any MPI implementation that is binary-compatible with MPICH2
data_parallel\mpi_example.m 5
SPMD END
Flow control from serial to parallel TOOLBOXES
Fine Grained BLOCKSETS
More control over distributed arrays
Composite Arrays
Generic data container across cluster
Accessible from client
b
2 l ( a b) l 2 crossing needles
P(l , a, b)
ab total needles l
(xo,yo)
data_parallel\jobscript_Pi.m 5
batch
matlabpool job
parallel job
Recommendations
61