Parallel Computing Workshop Part I NN
Parallel Computing Workshop Part I NN
Jonathan Murray
Application Engineer
December 2023
2
Agenda
3
Agenda
4
Why use parallel computing?
5
Why use parallel computing with MATLAB?
Core 3 Core 4
7
Before going parallel, optimize your code for the best performance
▪ Use the Profiler to find the code that runs slowest and
determine possible performance improvements
8
Before going parallel, optimize your code for the best performance
Create a new variable rather than assigning data of a different type to an existing variable
Avoid printing too much data on the screen, reuse existing graphics handles
Techniques to improve performance 11
MATLAB has built-in multithreading
Multi-core CPU
MATLAB
MATLAB multicore 12
Run parallel code by utilizing multiple CPU cores
parallel pool
13
Download Instructions
▪ https://tinyurl.com/ParallelComputingWorkshop
– Click on Add to my Files
– Click Copy Folder
– https://www.mathworks.com/licensecenter/classroom/4265400/
– Click Access MATLAB Online (maybe prompted to sign-in again)
– Click Open MATLAB Online
– In Current Folder, double click on ParallelComputingWorkshop-2.0
14
Setup: Step 1 – Copy materials via MATLAB Drive
Click Add to my Files and then click Copy Folder.
For use on your MATLAB Desktop, click Download Shared Folder instead.
https://tinyurl.com/ParallelComputingWorkshop 15
Setup: Step 2 – Launch MATLAB Online
https://www.mathworks.com/licensecenter/classroom/4265400/ 16
Hands-On Exercise: Starting a parallel pool
parpool_intro 17
Scaling MATLAB applications and Simulink simulations
Greater Control
Ease of Use
18
Automatic parallel support in toolboxes
19
Scaling MATLAB applications and Simulink simulations
Greater Control
Ease of Use
20
Explicit parallelism using parfor (parallel for-loop)
▪ Run iterations in parallel
▪ Examples: parameter sweeps, Monte Carlo simulations
MATLAB
Workers
Time
Time
21
Explicit parallelism using parfor
a = zeros(5, 1); a = zeros(5, 1);
b = pi; b = pi;
for i = 1:5 parfor i = 1:5
a(i) = i + b; a(i) = i + b;
end end
disp(a) disp(a)
MATLAB
Workers
22
Hands-On Exercise: Writing our first parfor
parfor_getting_started 23
DataQueue: Execute code as parfor iterations complete
function a = parforWaitbar
D = parallel.pool.DataQueue;
▪ Send data or messages from parallel h = waitbar(0, 'Please wait ...');
workers back to the MATLAB client afterEach(D, @nUpdateWaitbar)
N = 200;
p = 1;
▪ Retrieve intermediate values and track
computation progress parfor i = 1:N
a(i) = max(abs(eig(rand(400))));
send(D, i)
end
function nUpdateWaitbar(~)
waitbar(p/N, h)
p = p + 1;
end
end
24
Hands-On Exercise: Sending data with a dataqueue
dataqueue_getting_started 25
Execute functions in parallel asynchronously using parfeval
fetchNext
Outputs
MATLAB
Workers
parfeval_plotter 28
Use Code Analyzer to fix problems when converting for-loops to
parfor-loops
parfor-loop iterations have no guaranteed order, and one loop iteration cannot depend on a
previous iteration; therefore, you may need to rewrite your code to use parfor
29
Common problems when rewriting a for-loop as a parfor-loop
30
Hands-On Exercise: Rewrite for-loops into parfor-loops
parfor_conversions 31
Hands-On Exercise: Refactoring for-loops
parfor_refactoring 32
Optimizing parfor-loops
* Parallel overhead: time required for communication, coordination, and data transfer from client to workers and back 34
Run multiple simulations in parallel with parsim
Workers
Time Time
parallel_Simulink 36
Scaling MATLAB applications and Simulink simulations
Greater Control
Ease of Use
37
Leverage NVIDIA GPUs without learning CUDA
MATLAB client
or worker
GPU cores
Device Memory
38
Leverage your GPU to accelerate your MATLAB code
▪ Ideal Problems
– massively parallel and/or
vectorized operations
– computationally intensive
▪ 1000+ GPU-supported
functions
gpus_getting_started 40
Parallel computing on your desktop, clusters, and clouds
GPU
GPU
Multi-core CPU
Multi-core CPU
MATLAB MATLAB Parallel Server
Parallel Computing Toolbox
42
Interactive parallel computing
Leverage cluster resources in MATLAB
MATLAB
Parallel Computing Toolbox
myscript.m
a = zeros(5, 1);
b = pi;
parfor i = 1:5
a(i) = i + b;
end
43
Run a parallel pool from specified profile
mathworks.com/help/parallel-computing/choose-between-thread-based-and-process-based-environments.html 44
batch simplifies offloading computations
Submit MATLAB jobs to the cluster
worker
MATLAB
Parallel Computing Toolbox
pool
45
Hands-On Exercise: Use batch to offload serial and parallel
computations
batch_getting_started 46
batch simplifies offloading simulations
Submit Simulink jobs to the cluster
job = batchsim(in,'Pool',3);
parsim
worker
MATLAB
Parallel Computing Toolbox pool
47
Big Data Workflows
ACCESS DATA
48
tall arrays
▪ Data type designed for data that doesn’t fit into memory
▪ Lots of observations (hence “tall”)
▪ Looks like a normal MATLAB array
– Supports numeric types, tables, datetimes, strings, etc.
– Supports several hundred functions for basic math, stats, indexing, etc.
– Statistics and Machine Learning Toolbox support
(clustering, classification, etc.)
tall_getting_started 50
distributed arrays
MATLAB
11 26 41 15 30 45 20 35 50
Parallel Computing Toolbox 21 36 51
12 27 42 16 31 46
13 28 43 17 32 47 22 37 52
distributed_getting_started 52
tall arrays vs. distributed arrays
▪ tall arrays are useful for out-of-memory datasets with a “tall” shape
– Can be used on a desktop, cluster, or with Spark/Hadoop
– Low-level alternatives are MapReduce and MATLAB API for Spark
▪ distributed arrays are useful for in-memory datasets on a cluster
– Can be any shape (“tall”, “wide”, or both)
– Low-level alternative is SPMD + gop (Global operation across all workers)
53
Further Resources
▪ MATLAB Documentation
– MATLAB → Software Development Tools → Performance and Memory
– Parallel Computing Toolbox
54
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other
product or brand names may be trademarks or registered trademarks of their respective holders. © 2023 The MathWorks, Inc.
© 2023 The MathWorks, Inc.
55