53
Matlab for Engineers Alistair Johnson 5th October 2011 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

Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Embed Size (px)

Citation preview

Page 1: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Matlab for Engineers 

Alistair Johnson 

5th October 2011

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

University of OxfordSupported by the RCUK Digital Economy Programme grant number EP/G036861/1 

Page 2: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Overview

1. Functions vs scripts (AJ) [1.30pm-2.30pm]o First - commenting your codeo Writing a functiono Checking syntax of argumentso Function Handleso Help files

2. Parallel computing (AJ) [2.30pm - 3.15pm] o Simple parallelisationo Dividing across your processorso Recovering from crashes - indexing your jobs to pick up where you left offo Worked examples using an optimization processo how to execute remotely - sending tasks to a cluster

Page 3: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Commenting your code

 

Page 4: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Commenting

• Commenting is vital for not only others to understand your code (i.e. supervisor), but for yourself later

    “Commenting your code is like cleaning your bathroom—you never want to do it, but it really does create a more pleasant experience for

you and your guests.”

• Commenting properly will never lead to this situation - you should comment your code as you write it, not after.

Page 5: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Compare:

Page 6: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Compare:

Page 7: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Commenting new files

TODO:1.... add syntax to the header comment2.... add examples to the header comment3.... describe the inputs4.... describe the outputs5.... copyright and licensing6.... don't forget SI units where needed!7.… set up source code control to auto-comment version:

* SVN revision information:  * @version $Revision: 82 $:  * @author  $Author: smoot $:  * @date    $Date: 2010-07-10 08:36:02 +0200 (Sat, 10 Jul 2010) $

Provided function: newfun.m

(generates the structure of a new function for you!)Try it out now!

Page 8: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Function Basics

Function Types and ScopeFunction Input/Output

Help files

Page 9: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Function Types and Scope

• Primary Function• Subfunction• Nested Function• Overloaded Function• Anonymous Function• Private Function

Page 10: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Primary Function

• Basic function type• Requires first line to be function definition line

Page 11: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Primary Function

Page 12: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Subfunction

• Functions embedded within a primary function• Appear after primary function's body

Page 13: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Nested Function

• Function within another function

• Inherits the workspace of the parent function

• Cannot be used inside program control statements (e.g., if, switch, try...)

• Can nest functions within functions

Page 14: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Nested Function

Page 15: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Nested Function

Page 16: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Overloaded Functions

• When two functions must have different functionalities for different types of inputs

• Each function must go in a class path

• Example:o ~/home/alistair/@double/calc_average.mo ~/home/alistair/@int32/calc_average.m

Page 17: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Overloaded Functions

Page 18: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Anonymous Function

• Defined in-line• fhandle = @(arg1,arg2) expression• Useful for quick function handles to pass to other

functionso @(x) x.^2o y=cellfun(@(x) x.^2, X);

Page 19: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Private Function

• Located in a sub-folder named private• Identical to primary function• Only visible to functions in the parent folder• Sub-folder private should NOT be in the

MATLAB path

• Example:

Page 20: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Private Function

Example Execution:

Page 21: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Function Input/Output

• nargin, nargouto Give the number of input/output argumentso Useful for argument checking

Page 22: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Variable Function Inputvarargin• All inputs combined into a cell array

Page 23: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Variable Function Output

• varargout 

Page 24: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Input parser

• MATLAB has an inputParser class which is useful for parsing input arguments... as you might have guessed.

• photoPrint.m is an example MATLAB function which uses the main features of the input parser

• photoPrint.m is a faux function which prints a photo given the filename, file type, and some dimension/quality variables

Page 25: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department
Page 26: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Help files

• MATLAB Help is, put simply, amazing

Page 27: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Help files

• Be sure to check the user's guide!

Page 28: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Parallel Computing

Parallel Computing ToolboxLicensing

Graphical Processing UnitsPractical Exercise

Page 29: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Parallel Computing

• Some tasks lend themselves nicely to parallelization

• The trade offo Computation Overhead vs Computation Time

• Note that MATLAB already has parallelization!

Page 30: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Parallel Computing Toolbox (PCT)

• You will likely work with parfor loops as opposed to for loops• Simple to use, but they require proper indexing• Maximum 8 threads• A general parallel script may look like this:

Page 31: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

parfor

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

• Assume all parfor run independent of each other• parfor rules:

o Don't use clear or evalo Don't load/save data in a parfor loopo Don't use break, return, global, or persistento Use feval for function handles

Page 32: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

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 loop• Exactly one of the first level indices is the loop variable

Page 33: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

parfor indexingExample workaround for ensuring a variable is indexed properly

Page 34: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

PCT crashes

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

• This can make standard debugging annoying• Usually best to remove "par" from parfor and debug

normally

Page 35: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Miscellaneous PCT tips

Open:

Close:

onCleanup:

Page 36: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Practice!

parfor - simple, but a good way to get started

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

task - multiple independent programs, too advanced

Page 37: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Parallelizing Peak Detector

Parallel Profiler

Page 38: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

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:o Two NVIDIA Quadro NVS 295 (8 cores each)o Intel Xeon E5520 (4 cores, 8 threads)

Page 39: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

GPU speed-up

Page 40: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

GPU speed-up

Page 41: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

GPU Technical Requirements

• GPU computing requires:o MATLAB 2010b or highero NVIDIA CUDA 1.3 or higher

• Your IBME machines haveo MATLAB 2009o NVIDIA CUDA 1.1

?Ask your supervisor for an upgrade!

Page 42: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Licensing

• Using MATLAB in the IBME (or on campus/after VPN) uses the network license

Page 43: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Licensing

• MATLAB licenses are released:o After closing MATLABo After a period of idle time elapses

Page 44: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Licensing

• Try to release your licenses!• As of Sept 2011, we are restricted to 20 university wide

 lmstat:

Page 45: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Licensing

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

Page 46: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Matlab ExecutablesMEXBasics

Simple exampleLIBSVM exampleTroubleshooting

Page 47: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

MATLAB Executable (MEX)

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

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

MATLABo You'd like to speed up computation

Page 48: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department
Page 49: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

MEX Requirements

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

o Microsoft Visual C++ 2010 Expresso Microsoft Windows SDK 7.1

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

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

Page 50: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Select Compiler

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

• We'll be using C•  mex -setup

• Check your compiler's info:• cc = mex.getCompilerConfigurations

Page 51: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

Easy example

Page 52: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

LIBSVM Practical Example

• LIBSVM is a C library for SVM classification/regression• We are going to compile it in MATLAB• They have already created a make.m file, so run it!

Page 53: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department

P.S... Source code control & backups!!!

• You should use subversion control for your code!• Linux

o Should already be installed• Windows

o You will need to take extra steps to install GCC, but it's worth it

• Check the IBME wiki for detailso  www.ibme.ox.ac.uk/ ....