matlab for engineers centre for doctoral training in ...gari/teaching/matlabforengineers...matlab...

38
Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department of Engineering Science University of Oxford Supported by the RCUK Digital Economy Programme grant number EP/G036861/1

Upload: others

Post on 02-Apr-2020

10 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Matlab for Engineers

Alistair Johnson

31st May 2012

Centre for Doctoral Training in Healthcare InnovationInstitute of Biomedical EngineeringDepartment of Engineering Science

University of Oxford

Supported by the RCUK Digital Economy Programme grant number EP/G036861/1

Page 2: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Overview

Parallel computingLicensing○Licensing○Simple parallelisation - parfor details○Parallelization exercise○Parallel crashes, profiling, tips, and a remote demo

1.GPU computing2.MEX files

○LIBSVM example

Page 3: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallel Computing

LicensingParallel Computing Toolbox (PCT)

PCT ExerciseGraphical Processing Units

Page 4: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

LicensingUsing MATLAB in the IBME (or on campus/after VPN) uses

the network license

Page 5: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

LicensingMATLAB licenses are released:

After closing MATLABAfter a period of idle time elapses

Page 6: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Licensing

Try to release your licenses!As of 09/28/2011, we are restricted to 20 university wide

./lmutil lmstat -a -c /opt/MATLAB/R2010b/licenses/network.licMore detail in speaker notes.

Page 7: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

LicensingTry to release your licenses!Here is an example of how this can be accomplished:

Page 8: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallel Computing

Some tasks lend themselves nicely to parallelizationThe main tradeoff to consider:

Computation Overhead vs Computation Time

Note that MATLAB already has implicit parallelization!

Page 9: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallel Computing Toolbox (PCT)

You will likely work with parfor loops as opposed to for loopsCalculations performed are identical, but how the

calculations are performed changesThe MATLAB client starts a pool of workersA general parallel script may look like this:

Page 10: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

parfor

parfor requires all iterations be independent of order and each other

Variable assignment command line output and graphical displays (i.e. figures) will not display from workers

parfor rules:Don't use clear or evalDon't load/save data in a parfor loopDon't use break, return, global, or persistentUse feval for function handles

Page 11: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

parfor variables

Try to make sure any large arrays are sliced, as this will reduce overhead, i.e.:

All of its first level indices are identical throughout the loopExactly one of the first level indices is the loop variable

Page 12: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

parfor indexingExample workaround for ensuring a variable is indexed properly

________________________________________

Page 13: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

PCT crashes

A PCT crash will give you the line, but not the workspace

This can make standard debugging annoyingUsually best to remove "par" from parfor and debug normally

Page 14: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Always open/close the matlabpool

Open:

Close:

onCleanup:

Page 15: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallelizing the peak detector

● We'll get a better idea of parallelization by using it in conjunction with the peak detector

● Open PCT_importfile.m and PCT_importfilecsv.m Ensure data.zip is extracted, and the data folder is in your

path

Page 16: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Example

Page 17: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

PCT_peak_detector_for

Page 18: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

PCT_peak_detector_parfor

Page 19: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

PCT_peak_detector_parfor_saveSaves results!

Page 20: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallelizing Peak Detector

PCT_peak_detector.mRuns the peak detector on 8 separate data

measurements concatenated togetherDoesn't keep the output (not interested in it for demo

purposes)

Page 21: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

SPMD example

Page 22: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

SPMD example

Loading data, performed with spmd...All three variants (for, parfor, and spmd) are compared in PCT_comparison.m

Try it out (note: it assumes you have at least 4 workers available for use, change the defaults if necessary)

Page 23: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

PCT_spmd_profiler.m

Page 24: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Parallelize EVERYTHING

parfor - simple, effective, and a good way to get started

spmd - complicated, but useful, allows for communication between cores

batch/task - multiple independent programs, very advanced

Page 25: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Further reading

As always, the MATLAB help is invaluable

Note that there are also demos available! At the MATLAB prompt:

demo 'toolbox' 'parallel computing'

Page 26: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Graphics Processing Unit (GPU)

GPUs can also be used for parallel processing

GPUs have much more cores (sometimes 512) but less processing power per core

IBME machines have:Two NVIDIA Quadro NVS 295 (8 cores each)Intel Xeon E5520 (4 cores, 8 threads)

Page 27: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

GPU speed-up

Page 28: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

GPU speed-up

Page 29: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

GPU Technical Requirements

GPU computing requires:MATLAB 2010b or higherNVIDIA CUDA 1.3 or higher

My 1st year CDT machine had

MATLAB 2009NVIDIA CUDA 1.1

Page 30: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Your computers

● MATLAB 2011 .. ish● NVIDIA Quadro 600● CUDA 2.1 compatible● Will work for GPU

computing! cpu_x = rand(1,100000000)*10*pi;gpu_x = gpuArray(cpu_x);gpu_y = sin(gpu_x);cpu_y = gather(gpu_y) A nice 3 part introduction for MATLAB:http://www.walkingrandomly.com/?p=3730

Page 31: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

OSC

Oxford Supercomputing recently changed their funding scheme You now get "free" access to their CPU/GPU

clusters (paid by engineering department), ~25,000 hours Requires knowledge of Linux terminal, but they

have training courses

Page 32: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Matlab ExecutablesMEXBasics

Simple exampleLIBSVM example

Page 33: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

MATLAB Executable (MEX)

Allow MATLAB to run external libraries as if they were .m functions

Use if...There exists Fortran, C, or C++ code you want to use in

MATLABYou'd like to speed up computation

Page 34: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

MEX basics

source MEX C, C++, or Fortran source code

binary MEX Subroutine executed by MATLAB

mex build script

Builds binary from source

Let's assume you don't want to code in C, and simply have pre-coded source mex files you want to compile 3 files needed for using MEX

Page 35: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

MEX Requirements

You will need a compiler (3rd party software) For Windows 64-bit:

Microsoft Visual C++ 2010 ExpressMicrosoft Windows SDK 7.1

For Linux 64-bit: GNU gcc/g++ 4.3 or higher

For OS X 64-bit: Apple XCode 4.0 (Snow Leopard) or 4.1 (Lion)

Page 36: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Select Compiler

Make sure your compiler is using the right language (C, C++, or Fortran)

We'll be using C

mex -setupCheck your compiler's info:

cc = mex.getCompilerConfigurations

Page 37: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

Easy example

Page 38: Matlab for Engineers Centre for Doctoral Training in ...gari/teaching/MatlabForEngineers...Matlab for Engineers Alistair Johnson 31st May 2012 Centre for Doctoral Training in Healthcare

LIBSVM Practical Example

LIBSVM is a C library for SVM classification/regressionWe are going to compile it in MATLABThey have already created a make.m file, so run it!