parallel computing with matlab · implicit multithreaded matlab toolbox support: optimization...

45
© 2008 The MathWorks, Inc. ® ® Parallel Computing with MATLAB ® Elwin Chan

Upload: others

Post on 19-Jan-2021

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

©20

08 T

he M

athW

orks

, Inc

.

® ®

Parallel Computing with MATLAB®

Elwin Chan

Page 2: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

2

® ®

Solving Big Technical Problems

Large data set

Difficulties

Long running

Computationally intensive

Wait

Load data onto multiple machines that work together in parallel

Solutions

Run similar tasks on independent processors in parallel

Reduce sizeof problem

You could…

Page 3: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

3

® ®

Parallel Computing

Difficulties Solution

Work interactively in parallel

Jobs run in scheduled mode

Hard to debug

Cannot access intermediate answers

Hard to diagnose bottlenecks in algorithm

Page 4: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

4

® ®

Parallel Computing with MATLAB

TOOLBOXES

BLOCKSETS

Pool of MATLAB Workers

Page 5: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

5

® ®

Implicit Multithreaded MATLABToolbox Support:

Optimization Toolbox™Genetic Algorithm and Direct Search Toolbox™SystemTest™

parfor

job and tasks

Task Parallel Data Parallel

Parallel Computing with MATLABNo code changes

Trivial changes

Extensive changes

distributed

MATLAB and MPI

Page 6: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

6

® ®

Agenda

Speed up algorithms without code changes

Develop parallel code interactively

Task parallel applications for faster processing

Data parallel applications for handling large data sets

Schedule your programs to run

Tips on developing parallel code

Page 7: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

7

® ®

Implicit Multithreaded MATLABToolbox Support:

Optimization Toolbox Genetic Algorithm and Direct Search Toolbox SystemTest

parfor

job and tasks

Task Parallel Data Parallel

Parallel Computing with MATLABNo code changes

Trivial changes

Extensive changes

distributed

MATLAB and MPI

Page 8: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

8

® ®

Demo: Speed Up Mathematical Operations

Page 9: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

9

® ®

Demo: Speed Up for Implicit Multithreaded Computations

Linear Algebra Element-wise

No change required for user code

Enables multithreading for key mathematical routines

Linear algebra operationsElement-wise operations

Page 10: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

10

® ®

Implicit Multithreaded Computation

Linear algebra operationsUses multithreaded Basic Linear Algebra Subroutines (BLAS)BLAS are vendor specificOptimized for specific processor

Element-wise operationsJust-in-time acceleration (JIT) generates on-the-fly multithreaded code

Page 11: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

11

® ®

Implicit Multithreaded MATLABToolbox Support:

Optimization Toolbox Genetic Algorithm and Direct Search Toolbox SystemTest

parfor

job and tasks

Task Parallel Data Parallel

Parallel Computing with MATLABNo code changes

Trivial changes

Extensive changes

distributed

MATLAB and MPI

Page 12: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

12

® ®

Demo: Support in Optimization Toolbox

Page 13: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

13

® ®

Parallel Support in Optimization Toolbox

Functions: fmincon

finds a constrained minimum of a function of several variablesfminimax

finds a minimax solution of a function of several variablesfgoalattain

solves the multiobjective goal attainment optimization problem

Functions can take finite differences in parallel in order to speed the estimation of gradients

Page 14: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

14

® ®

SystemTest Supports Parallel Computing for MATLAB and Simulink Applications Distribute MATLAB and Simulink models for execution on a computer cluster or a multiprocessor system

Run multiple simulations faster

Use a checkbox to distribute – no additional code required

Use homogeneous or heterogeneous platforms

Page 15: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

15

® ®

Agenda

Speed up algorithms without code changes

Develop parallel code interactively

Task parallel applications for faster processing

Data parallel applications for handling large data sets

Schedule your programs to run

Page 16: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

16

® ®

Implicit Multithreaded MATLABToolbox Support:

Optimization Toolbox Genetic Algorithm and Direct Search Toolbox SystemTest

parfor

job and tasks

Task Parallel Data Parallel

Parallel Computing with MATLABNo code changes

Trivial changes

Extensive changes

distributed

MATLAB and MPI

Page 17: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

17

® ®

Distributing Tasks (Task Parallel)

Time Time

Pro

cess

es

Page 18: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

18

® ®

Demo: Monte Carlo Simulation of Coin Tossing

11

Number of Heads Out of 20

7 12 15 7 9 9 7 8 1211

10 Simulations of Flipping 20 Coins at a Time

Page 19: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

19

® ®

Demo: Monte Carlo Simulation of Coin Tossing

11

Number of Heads Out of 20

7 12 15 9 9 13 7 8 1211

10 Simulations of Flipping 20 Coins at a Time

↑-- ↑ → ↑ ↑ ↑↓

Change in Number of Heads

↓ ↓

Page 20: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

20

® ®

Parallel for-Loops

parfor i = 1 : n% do something with i

end

Mix task-parallel and serial code in the same functionRun loops on a pool of MATLAB resourcesIterations must be order-independentM-Lint analysis helps in converting existing for-loops into to parfor-loops

Page 21: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

21

® ®

Agenda

Speed up algorithms without code changes

Develop parallel code interactively

Task parallel applications for faster processing

Data parallel applications for handling large data sets

Schedule your programs to run

Tips on developing parallel code

Page 22: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

22

® ®

Implicit Multithreaded MATLABToolbox Support:

Optimization Toolbox Genetic Algorithm and Direct Search Toolbox SystemTest

parfor

job and tasks

Task Parallel Data Parallel

Parallel Computing with MATLABNo code changes

Trivial changes

Extensive changes

distributed

MATLAB and MPI

Page 23: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

23

® ®

Large Data Sets (Data Parallel)

1111 2626 4141

1212 2727 4242

1313 2828 4343

1414 2929 4444

1515 3030 4545

1616 3131 4646

1717 3232 4747

1717 3333 4848

1919 3434 4949

2020 3535 5050

2121 3636 5151

2222 3737 5252

1111 2626 4141

1212 2727 4242

1313 2828 4343

1414 2929 4444

1515 3030 4545

1616 3131 4646

1717 3232 4747

1717 3333 4848

1919 3434 4949

2020 3535 5050

2121 3636 5151

2222 3737 5252

Page 24: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

24

® ®

Demo: Interactive Face Recognition

Do we recognize this person?

Compare this image against a database.

Images in database are represented using six principal eigenfaces (component images).

Image set must be handled in one bite.

Page 25: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

25

® ®

Dataset of FacesSingle snapshot used to build eigenfaces

Data set also contains same individuals pulling different expressions –used to test recognition algorithm

40 individuals in 10 poses in this dataset …etc

[ Face Data provided courtesy of AT&T Laboratories Cambridge ]

Page 26: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

26

® ®

Face Recognition AlgorithmSample faces processed into eigenface components

Compute Mean Face

Facial Signatures

M

Subtract

M

ComputeEigenvectors(Eigenfaces)

M

Select 6PrincipalEigenfaces

∑=

=6

1iiieaFace

Page 27: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

27

® ®

Face Recognition AlgorithmSample faces processed into eigenface components

Mean Face

Facial Signatures

Select 6PrincipalEigenfaces

Identificationrequires only this Reduced Dataset!

Page 28: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

28

® ®

Face Recognition AlgorithmTest image broken into eigenface components and compared with existing database

Mean Face

6 PrincipalEigenfaces Facial Signatures

Subtract

Test Image

Find Closest Match (LMS)

indexReconstruct

Test

ReconstructMatch

FitMetric

Page 29: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

29

® ®

Distributed Arrays and Parallel Algorithms

Distributed arraysStore segments of data across participating workersCreate from any built-in class in MATLAB

Examples: doubles, sparse, logicals, cell arrays, and arrays of structs

Parallel algorithms for distributed arraysMatrix manipulation operations

Examples: indexing, data type conversion, and transposeParallel linear algebra functions, such as svd and luData distribution

Automatic, specify your own, or change at any time

Page 30: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

30

® ®

MPI-Based Functions in Parallel Computing Toolbox™Use when a high degree of control over parallel algorithm is required

High-level abstractions of MPI functionslabSendReceive, labBroadcast, and othersSend, receive, and broadcast any data type in MATLAB

Automatic bookkeepingSetup: communication, ranks, etc.Error detection: deadlocks and miscommunications

Pluggable Use any MPI implementation that is binary-compatible with MPICH2

Page 31: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

31

® ®

Agenda

Speed up algorithms without code changes

Develop parallel code interactively

Task parallel applications for faster processing

Data parallel applications for handling large data sets

Schedule your programs to run

Tips on developing parallel code

Page 32: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

32

® ®

Compute ClusterCompute Cluster

CPU

CPU

CPU

CPU

MATLAB® Distributed Computing Server™MATLAB® Distributed Computing Server™

Scheduler

Result

Result

Result

Result

Client MachineClient Machine

Task

Task

Task

Task

Worker

Worker

Worker

Worker

Distributed Applications

ParallelComputing

ToolboxTOOLBOXES

BLOCKSETS

Result

Job

Page 33: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

33

® ®

Demo: Scheduled Monte Carlo Coin

>> createJob(…) >> createTask(…)

Page 34: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

34

® ®

Demo: Scheduled Monte Carlo Coinusing parfor

>> createMatlabPoolJob

Page 35: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

35

® ®

Demo: Scheduled Face Recognition

>> createParallelJob

Page 36: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

36

® ®

Options for Scheduling Jobs

Task Parallel Data Parallel

>> createParallelJob

>> createMatlabPoolJobor>> batch

>> createJob(…) >> createTask(…)

Page 37: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

37

® ®

Dependencies

job – FileDependenciesFiles are copied from client to each worker machineZip compressedUncompressed and added to the MATLAB pathConvenient for .m files, but can be slow for large data files

job – PathDependenciesShared directories are added to the MATLAB pathMixing of Windows® and UNIX® paths allowedReduces the amount of data transfer from client to cluster

Page 38: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

38

® ®

Configurations

Save environment-specific parameters for your cluster

BenefitsEnter cluster information only onceModify configurations without changing MATLAB codeApply multiple configurations when running within same session

Page 39: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

39

® ®

Run Four Local Workers with a Parallel Computing Toolbox License

Easily experiment with explicit parallelism on multicore machines

Rapidly develop parallel applications on local computer

Take full advantage of desktop power

Separate computer cluster not required

Parallel Computing

Toolbox

Page 40: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

40

® ®

Scale Up to Cluster Configuration with No Code Changes

Parallel Computing

Toolbox

Computer ClusterComputer Cluster

MATLAB Distributed Computing ServerMATLAB Distributed Computing Server

Scheduler

CPU

CPU

CPU

CPU

Worker

Worker

Worker

Worker

Page 41: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

41

® ®

Agenda

Speed up algorithms without code changes

Develop parallel code interactively

Task parallel applications for faster processing

Data parallel applications for handling large data sets

Schedule your programs to run

Tips on developing parallel code

Page 42: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

42

® ®

Development and Debugging ProcessDevelop serial code normally

on local machine

Task or Data Parallel?

parfor / Jobs and Tasks pmode / Distributed Arrays

Run on N cluster nodes

Task Parallel Data Parallel

Run on local workers

Page 43: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

43

® ®

Parallel Profiler

Profiles the execution time for a function Similar to the MATLAB profilerIncludes information about the communication between labs

Time spent in communicationAmount of data passed between labs

BenefitsIdentify the bottlenecks in your parallel algorithmUnderstand which operations require communication

Page 44: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

44

® ®

Factors to Consider for Speeding Up Your Code

Share code and data with workers efficiently using FileDependencies or PathDependencies

There is always an overhead to distributionDon’t make a task too smallCombine small repetitive function calls into one larger one

Use the M-lint and parallel profiler (mpiprofile) to identify slow code

Minimize I/O

Page 45: Parallel Computing with MATLAB · Implicit Multithreaded MATLAB Toolbox Support: Optimization Toolbox™ Genetic Algorithm and Direct Search Toolbox™ SystemTest™ parfor job and

45

® ®

Summary

Speed up algorithms without code changes

Develop parallel code interactively

Task-parallel applications for faster processing

Data-parallel applications for handling large data sets

Schedule your programs to run

Tips on developing parallel code