M.-T. Deloddere Credential Evaluation Bureau Biomedical Doctoral School 22-10-2008.
Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare...
-
Upload
lesley-houston -
Category
Documents
-
view
216 -
download
1
Transcript of Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare...
![Page 1: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/1.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/2.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/3.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/4.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/5.jpg)
Compare:
![Page 6: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/6.jpg)
Compare:
![Page 7: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/7.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/8.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/9.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/10.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/11.jpg)
Primary Function
![Page 12: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/12.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/13.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/14.jpg)
Nested Function
![Page 15: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/15.jpg)
Nested Function
![Page 16: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/16.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/17.jpg)
Overloaded Functions
![Page 18: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/18.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/19.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/20.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/21.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/22.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/23.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/24.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/25.jpg)
![Page 26: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/26.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/27.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/28.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/29.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/30.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/31.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/32.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/33.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/34.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/35.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/36.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/37.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/38.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/39.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/40.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/41.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/42.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/43.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/44.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/45.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/46.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/47.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/48.jpg)
![Page 49: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/49.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/50.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/51.jpg)
Easy example
![Page 52: Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/52.jpg)
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.](https://reader038.fdocuments.net/reader038/viewer/2022110209/56649e245503460f94b125af/html5/thumbnails/53.jpg)
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/ ....