Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Matlab for Engineers Alistair Johnson 5th October 2011 Centre for Doctoral Training in Healthcare Innovation Institute of Biomedical Engineering Department."— Presentation transcript:

1 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

2 Overview 1.Functions vs scripts (AJ) [1.30pm-2.30pm] o First - commenting your code o Writing a function o Checking syntax of arguments o Function Handles o Help files 2.Parallel computing (AJ) [2.30pm - 3.15pm] o Simple parallelisation o Dividing across your processors o Recovering from crashes - indexing your jobs to pick up where you left off o Worked examples using an optimization process o how to execute remotely - sending tasks to a cluster

3 Commenting your code

4 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.

5 Compare:

6

7 Commenting new files TODO: 1.... add syntax to the header comment 2.... add examples to the header comment 3.... describe the inputs 4.... describe the outputs 5.... copyright and licensing 6.... 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!

8 Function Basics Function Types and Scope Function Input/Output Help files

9 Function Types and Scope Primary Function Subfunction Nested Function Overloaded Function Anonymous Function Private Function

10 Primary Function Basic function type Requires first line to be function definition line

11 Primary Function

12 Subfunction Functions embedded within a primary function Appear after primary function's body

13 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

14 Nested Function

15

16 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.m o ~/home/alistair/@int32/calc_average.m

17 Overloaded Functions

18 Anonymous Function Defined in-line fhandle = @(arg1,arg2) expression Useful for quick function handles to pass to other functions o @(x) x.^2 o y=cellfun(@(x) x.^2, X);

19 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:

20 Private Function Example Execution:

21 Function Input/Output nargin, nargout o Give the number of input/output arguments o Useful for argument checking

22 Variable Function Input varargin All inputs combined into a cell array

23 Variable Function Output varargout

24 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

25

26 Help files MATLAB Help is, put simply, amazing

27 Help files Be sure to check the user's guide!

28 Parallel Computing Parallel Computing Toolbox Licensing Graphical Processing Units Practical Exercise

29 Parallel Computing Some tasks lend themselves nicely to parallelization The trade off o Computation Overhead vs Computation Time Note that MATLAB already has parallelization!

30 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:

31 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 eval o Don't load/save data in a parfor loop o Don't use break, return, global, or persistent o Use feval for function handles

32 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

33 parfor indexing Example workaround for ensuring a variable is indexed properly

34 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

35 Miscellaneous PCT tips Open: Close: onCleanup:

36 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

37 Parallelizing Peak Detector Parallel Profiler

38 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)

39 GPU speed-up

40

41 GPU Technical Requirements GPU computing requires: o MATLAB 2010b or higher o NVIDIA CUDA 1.3 or higher Your IBME machines have o MATLAB 2009 o NVIDIA CUDA 1.1 ?Ask your supervisor for an upgrade!

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

43 Licensing MATLAB licenses are released: o After closing MATLAB o After a period of idle time elapses

44 Licensing Try to release your licenses! As of Sept 2011, we are restricted to 20 university wide lmstat:

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

46 Matlab Executables MEX Basics Simple example LIBSVM example Troubleshooting

47 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 MATLAB o You'd like to speed up computation

48

49 MEX Requirements You will need a compiler (3rd party software) For Windows 64-bit: o Microsoft Visual C++ 2010 Express o 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)

50 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

51 Easy example

52 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!

53 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 details o www.ibme.ox.ac.uk/....


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

Similar presentations


Ads by Google